| @@ -2,6 +2,7 @@ package main | |||
| import ( | |||
| "encoding/json" | |||
| "fmt" | |||
| "io" | |||
| "io/ioutil" | |||
| "log" | |||
| @@ -13,6 +14,7 @@ import ( | |||
| ) | |||
| func saveURL(url string) (tmpFile string, contentType string, err error) { | |||
| log.Println("saveURL: " + url) | |||
| var myClient = &http.Client{Timeout: 20 * time.Second} | |||
| r, err := myClient.Get(url) | |||
| if err != nil { | |||
| @@ -25,11 +27,12 @@ func saveURL(url string) (tmpFile string, contentType string, err error) { | |||
| contentType = r.Header.Get("Content-Type") | |||
| contentLen := r.Header.Get("Content-Length") | |||
| len, _ := strconv.Atoi(contentLen) | |||
| log.Println(r) | |||
| file, err := ioutil.TempFile(os.TempDir(), "wechat_hitxy_") | |||
| // Use io.Copy to just dump the response body to the file. This supports huge files | |||
| _, err = io.Copy(file, r.Body) | |||
| if err != nil { | |||
| log.Println(err) | |||
| return "", contentType, err | |||
| } | |||
| tmpFile = file.Name() | |||
| @@ -39,8 +42,9 @@ func saveURL(url string) (tmpFile string, contentType string, err error) { | |||
| if len < 4096 && contentType == "text/plain" { | |||
| u := readVideoURL(tmpFile) | |||
| if u != "" { | |||
| fmt.Println("saveing video url: " + u) | |||
| os.Remove(tmpFile) //remove json file | |||
| tmpFile, contentType, err = saveVideo(u) | |||
| tmpFile, contentType, err = saveURL(u) | |||
| } | |||
| } | |||
| @@ -80,11 +84,3 @@ func readVideoURL(path string) (u string) { | |||
| } | |||
| return "" | |||
| } | |||
| func saveVideo(path string) (tmpFile string, contentType string, err error) { | |||
| u := readVideoURL(path) | |||
| if u != "" { | |||
| tmpFile, contentType, err = saveURL(u) | |||
| } | |||
| return | |||
| } | |||