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