diff options
Diffstat (limited to 'vendor/github.com/Rhymen/go-whatsapp/media.go')
-rw-r--r-- | vendor/github.com/Rhymen/go-whatsapp/media.go | 33 |
1 files changed, 13 insertions, 20 deletions
diff --git a/vendor/github.com/Rhymen/go-whatsapp/media.go b/vendor/github.com/Rhymen/go-whatsapp/media.go index 0fcacd2e..f72e7922 100644 --- a/vendor/github.com/Rhymen/go-whatsapp/media.go +++ b/vendor/github.com/Rhymen/go-whatsapp/media.go @@ -10,6 +10,7 @@ import ( "fmt" "io" "io/ioutil" + "net" "net/http" "net/url" "time" @@ -71,16 +72,10 @@ func downloadMedia(url string) (file []byte, mac []byte, err error) { if err != nil { return nil, nil, err } - if resp.StatusCode != 200 { - if resp.StatusCode == 404 { - return nil, nil, ErrMediaDownloadFailedWith404 - } - if resp.StatusCode == 410 { - return nil, nil, ErrMediaDownloadFailedWith410 - } + defer resp.Body.Close() + if resp.StatusCode != http.StatusOK { return nil, nil, fmt.Errorf("download failed with status code %d", resp.StatusCode) } - defer resp.Body.Close() if resp.ContentLength <= 10 { return nil, nil, fmt.Errorf("file to short") } @@ -101,8 +96,8 @@ type MediaConn struct { Hosts []struct { Hostname string `json:"hostname"` IPs []struct { - IP4 string `json:"ip4"` - IP6 string `json:"ip6"` + IP4 net.IP `json:"ip4"` + IP6 net.IP `json:"ip6"` } `json:"ips"` } `json:"hosts"` } `json:"media_conn"` @@ -125,21 +120,17 @@ func (wac *Conn) queryMediaConn() (hostname, auth string, ttl int, err error) { return "", "", 0, fmt.Errorf("query media conn timed out") } - if resp.Status != 200 { + if resp.Status != http.StatusOK { return "", "", 0, fmt.Errorf("query media conn responded with %d", resp.Status) } - var host string for _, h := range resp.MediaConn.Hosts { if h.Hostname != "" { - host = h.Hostname - break + return h.Hostname, resp.MediaConn.Auth, resp.MediaConn.TTL, nil } } - if host == "" { - return "", "", 0, fmt.Errorf("query media conn responded with no host") - } - return host, resp.MediaConn.Auth, resp.MediaConn.TTL, nil + + return "", "", 0, fmt.Errorf("query media conn responded with no host") } var mediaTypeMap = map[MediaType]string{ @@ -202,7 +193,7 @@ func (wac *Conn) Upload(reader io.Reader, appInfo MediaType) (downloadURL string body := bytes.NewReader(append(enc, mac...)) - req, err := http.NewRequest("POST", uploadURL.String(), body) + req, err := http.NewRequest(http.MethodPost, uploadURL.String(), body) if err != nil { return "", nil, nil, nil, 0, err } @@ -222,7 +213,9 @@ func (wac *Conn) Upload(reader io.Reader, appInfo MediaType) (downloadURL string } var jsonRes map[string]string - json.NewDecoder(res.Body).Decode(&jsonRes) + if err := json.NewDecoder(res.Body).Decode(&jsonRes); err != nil { + return "", nil, nil, nil, 0, err + } return jsonRes["url"], mediaKey, fileEncSha256, fileSha256, fileLength, nil } |