11package main
2+
23import (
34 "github.com/valyala/fasthttp"
45 "log"
@@ -8,10 +9,14 @@ import (
89 "io/ioutil"
910 "strconv"
1011 "time"
11- "fmt"
12+ "fmt"
13+ "os/exec"
14+ "runtime"
15+ "flag"
1216)
1317
14- func download (url string ) bool {
18+ //从url中获取文件名
19+ func getFileName (url string ) string {
1520 fileNames := strings .Split (url , "/" )
1621 fileName := fileNames [len (fileNames ) - 1 ]
1722 if fileName == "" {
@@ -20,6 +25,12 @@ func download(url string) bool {
2025 fileName = strings .Replace (fileName , "?" , "_" , - 1 )
2126 fileName = strings .Replace (fileName , "#" , "_" , - 1 )
2227 fileName = strings .Replace (fileName , "&" , "_" , - 1 )
28+ return fileName
29+ }
30+
31+ //使用fasthttp下载文件
32+ func download (url string ) bool {
33+ fileName := getFileName (url )
2334 out , _ := os .Create ("./download/" + fileName )
2435 defer out .Close ()
2536 statusCode , body , err := fasthttp .Get (nil , url )
@@ -29,7 +40,22 @@ func download(url string) bool {
2940 return true
3041}
3142
43+ //调用wget下载文件
44+ func downloadByWget (url string ) bool {
45+ cmd := exec .Command ("wget" , "-c" , "-P" , "download" , url )
46+ err := cmd .Start ()
47+ if err != nil {
48+ log .Fatal (err )
49+ }
50+ err = cmd .Wait ()
51+ log .Printf ("Command finished with error: %v" , err )
52+ return true
53+ }
54+
3255func main () {
56+ host := flag .String ("host" , "0.0.0.0" , "host" )
57+ port := flag .String ("port" , "8081" , "port" )
58+ flag .Parse ()
3359 m := func (ctx * fasthttp.RequestCtx ) {
3460 switch {
3561 case string (ctx .Path ()) == "/" :
@@ -191,6 +217,7 @@ func main() {
191217</html>
192218` )
193219 case string (ctx .Path ()) == "/down" :
220+ //点击“下载”触发,下载远程文件
194221 args := ctx .QueryArgs ()
195222 url := string (args .Peek ("url" ))
196223 protocol := strings .Split (url , "://" )
@@ -199,23 +226,30 @@ func main() {
199226 log .Println (protocol )
200227 return
201228 }
202- go download (url )
229+ //
230+ if runtime .GOOS != "linux" {
231+ go download (url )
232+ } else {
233+ go downloadByWget (url )
234+ }
203235 ctx .WriteString ("success" )
204236 log .Println ("visiter " + ctx .RemoteIP ().String () + " use " + string (ctx .UserAgent ()) + " downloaded " + url + " in " + time .Now ().Format ("2006-01-02 15:04:05" ))
205237 case string (ctx .Path ()) == "/delete" :
238+ //删除文件
206239 args := ctx .QueryArgs ()
207240 file := string (args .Peek ("file" ))
208241 file = strings .Replace (file , "/" , "" , - 1 )
209- os .Remove ("./download/ " + file )
242+ os .Remove ("./download" + file )
210243 ctx .WriteString ("success" )
211244 case bytes .HasPrefix (ctx .Path (), []byte ("/download/" )):
212- fasthttp .FSHandler ("./download/ " , 1 )(ctx )
245+ fasthttp .FSHandler ("./download" , 1 )(ctx )
213246 case string (ctx .Path ()) == "/downlist" :
214- dirList , _ := ioutil .ReadDir ("./download/" )
247+ //生成文件列表
248+ dirList , _ := ioutil .ReadDir ("./download" )
215249 length := len (dirList )
216250 ctx .WriteString ("[" )
217251 for i := 0 ; i < length ; i ++ {
218- ctx .WriteString (`{"name":"` + dirList [i ].Name () + `","mtime":"` + dirList [i ].ModTime ().Format ("2006-01-02 15:04:05" ) + `","size":"` + strconv .FormatInt (dirList [i ].Size (), 10 ) + ` B "}` )
252+ ctx .WriteString (`{"name":"` + dirList [i ].Name () + `","mtime":"` + dirList [i ].ModTime ().Format ("2006-01-02 15:04:05" ) + `","size":"` + strconv .FormatInt (dirList [i ].Size () / 1024 / 1024 , 10 ) + ` MB "}` )
219253 if i != length - 1 {
220254 ctx .WriteString ("," )
221255 }
@@ -227,12 +261,13 @@ func main() {
227261 }
228262 if fileInfo , err := os .Stat ("./download" ); err != nil || ! fileInfo .IsDir () {
229263 os .Mkdir ("./download" , 0755 )
230- log .Println ("mkDir ./download " )
264+ log .Println ("mkDir dirctionary " )
231265 }
232- bind := fmt .Sprintf ("%s:%s" , os .Getenv ("OPENSHIFT_GO_IP" ), os .Getenv ("OPENSHIFT_GO_PORT" ))
233- // bind := fmt.Sprintf("%s:%s", "0.0.0.0", "8081")
266+ //bind := fmt.Sprintf("%s:%s", os.Getenv("OPENSHIFT_GO_IP"), os.Getenv("OPENSHIFT_GO_PORT"))
267+ bind := fmt .Sprintf ("%s:%s" , * host , * port )
268+ log .Printf ("run on %s...\n " , runtime .GOOS )
234269 log .Printf ("listening on %s...\n " , bind )
235270 if err := fasthttp .ListenAndServe (bind , m ); err != nil {
236271 log .Fatal (err )
237272 }
238- }
273+ }
0 commit comments