summaryrefslogtreecommitdiffstats
path: root/vendor/go.mau.fi/whatsmeow/download.go
diff options
context:
space:
mode:
authorWim <wim@42.be>2023-08-05 20:43:19 +0200
committerGitHub <noreply@github.com>2023-08-05 20:43:19 +0200
commit56e7bd01ca09ad52b0c4f48f146a20a4f1b78696 (patch)
treeb1355645342667209263cbd355dc0b4254f1e8fe /vendor/go.mau.fi/whatsmeow/download.go
parent9459495484d6e06a3d46de64fccd8d06f7ccc72c (diff)
downloadmatterbridge-msglm-56e7bd01ca09ad52b0c4f48f146a20a4f1b78696.tar.gz
matterbridge-msglm-56e7bd01ca09ad52b0c4f48f146a20a4f1b78696.tar.bz2
matterbridge-msglm-56e7bd01ca09ad52b0c4f48f146a20a4f1b78696.zip
Update dependencies and remove old matterclient lib (#2067)HEADmaster
Diffstat (limited to 'vendor/go.mau.fi/whatsmeow/download.go')
-rw-r--r--vendor/go.mau.fi/whatsmeow/download.go25
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