Add User Agent to image download reqs (#1222)

This commit is contained in:
bnkai
2021-03-23 23:12:11 +02:00
committed by GitHub
parent 73a8bad1bc
commit 68d4a4fe42
4 changed files with 55 additions and 0 deletions

View File

@@ -2,6 +2,7 @@ package utils
import (
"crypto/md5"
"crypto/tls"
"encoding/base64"
"fmt"
"io/ioutil"
@@ -33,6 +34,10 @@ func ProcessImageInput(imageInput string) ([]byte, error) {
// ReadImageFromURL returns image data from a URL
func ReadImageFromURL(url string) ([]byte, error) {
client := &http.Client{
Transport: &http.Transport{ // ignore insecure certificates
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
Timeout: imageGetTimeout,
}
@@ -47,6 +52,7 @@ func ReadImageFromURL(url string) ([]byte, error) {
if req.URL.Scheme != "" {
req.Header.Set("Referer", req.URL.Scheme+"://"+req.Host+"/")
}
req.Header.Set("User-Agent", GetUserAgent())
resp, err := client.Do(req)
@@ -54,6 +60,10 @@ func ReadImageFromURL(url string) ([]byte, error) {
return nil, err
}
if resp.StatusCode >= 400 {
return nil, fmt.Errorf("http error %d", resp.StatusCode)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)