diff options
Diffstat (limited to 'vendor/go.mau.fi/whatsmeow/download.go')
-rw-r--r-- | vendor/go.mau.fi/whatsmeow/download.go | 25 |
1 files changed, 23 insertions, 2 deletions
diff --git a/vendor/go.mau.fi/whatsmeow/download.go b/vendor/go.mau.fi/whatsmeow/download.go index e9bedd45..3e510be6 100644 --- a/vendor/go.mau.fi/whatsmeow/download.go +++ b/vendor/go.mau.fi/whatsmeow/download.go @@ -12,8 +12,10 @@ import ( "encoding/base64" "fmt" "io" + "net" "net/http" "strings" + "time" "google.golang.org/protobuf/proto" "google.golang.org/protobuf/reflect/protoreflect" @@ -233,7 +235,7 @@ func (cli *Client) DownloadMediaWithPath(directPath string, encFileHash, fileHas func (cli *Client) downloadAndDecrypt(url string, mediaKey []byte, appInfo MediaType, fileLength int, fileEncSha256, fileSha256 []byte) (data []byte, err error) { iv, cipherKey, macKey, _ := getMediaKeys(mediaKey, appInfo) var ciphertext, mac []byte - if ciphertext, mac, err = cli.downloadEncryptedMedia(url, fileEncSha256); err != nil { + if ciphertext, mac, err = cli.downloadEncryptedMediaWithRetries(url, fileEncSha256); err != nil { } else if err = validateMedia(iv, ciphertext, macKey, mac); err != nil { @@ -252,6 +254,23 @@ func getMediaKeys(mediaKey []byte, appInfo MediaType) (iv, cipherKey, macKey, re return mediaKeyExpanded[:16], mediaKeyExpanded[16:48], mediaKeyExpanded[48:80], mediaKeyExpanded[80:] } +func (cli *Client) downloadEncryptedMediaWithRetries(url string, checksum []byte) (file, mac []byte, err error) { + for retryNum := 0; retryNum < 5; retryNum++ { + file, mac, err = cli.downloadEncryptedMedia(url, checksum) + if err == nil { + return + } + netErr, ok := err.(net.Error) + if !ok { + // Not a network error, don't retry + return + } + cli.Log.Warnf("Failed to download media due to network error: %w, retrying...", netErr) + time.Sleep(time.Duration(retryNum+1) * time.Second) + } + return +} + func (cli *Client) downloadEncryptedMedia(url string, checksum []byte) (file, mac []byte, err error) { var req *http.Request req, err = http.NewRequest(http.MethodGet, url, nil) @@ -268,7 +287,9 @@ func (cli *Client) downloadEncryptedMedia(url string, checksum []byte) (file, ma } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { - if resp.StatusCode == http.StatusNotFound { + if resp.StatusCode == http.StatusForbidden { + err = ErrMediaDownloadFailedWith403 + } else if resp.StatusCode == http.StatusNotFound { err = ErrMediaDownloadFailedWith404 } else if resp.StatusCode == http.StatusGone { err = ErrMediaDownloadFailedWith410 |