Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
Max committed Mar 6, 2021
2 parents 5e0cbc4 + 1fa6129 commit 3cd137d
Showing 1 changed file with 42 additions and 14 deletions.
56 changes: 42 additions & 14 deletions download.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,25 +167,53 @@ func readAndCheckImage(r io.Reader, contentLength int) (*imageData, error) {
}

func requestImage(imageURL string) (*http.Response, error) {
req, err := http.NewRequest("GET", imageURL, nil)
if err != nil {
return nil, newError(404, err.Error(), msgSourceImageIsUnreachable).SetUnexpected(conf.ReportDownloadingErrors)
}

req.Header.Set("User-Agent", conf.UserAgent)
if body, _ := l.Get(imageURL); body != nil {
fmt.Println(imageURL + " from cache!")
r := bufio.NewReader(bytes.NewReader(body.([]byte)))
res, err := http.ReadResponse(r, nil)
if err != nil {
return res, newError(404, err.Error(), msgSourceImageIsUnreachable).SetUnexpected(conf.ReportDownloadingErrors)
}

res, err := downloadClient.Do(req)
if err != nil {
return res, newError(404, err.Error(), msgSourceImageIsUnreachable).SetUnexpected(conf.ReportDownloadingErrors)
}
if res.StatusCode != 200 {
body, _ := ioutil.ReadAll(res.Body)
msg := fmt.Sprintf("Can't download image; Status: %d; %s", res.StatusCode, string(body))
return res, newError(404, msg, msgSourceImageIsUnreachable).SetUnexpected(conf.ReportDownloadingErrors)
}

return res, nil
} else {
req, err := http.NewRequest("GET", imageURL, nil)
if err != nil {
return nil, newError(404, err.Error(), msgSourceImageIsUnreachable).SetUnexpected(conf.ReportDownloadingErrors)
}

req.Header.Set("User-Agent", conf.UserAgent)

res, _ := downloadClient.Do(req)

body, err := httputil.DumpResponse(res, true)
l.Add(imageURL, body)

if err != nil {
msg := fmt.Sprintf("Can't dump response")
return res, newError(404, msg, msgSourceImageIsUnreachable).SetUnexpected(conf.ReportDownloadingErrors)
}

if err != nil {
return res, newError(404, err.Error(), msgSourceImageIsUnreachable).SetUnexpected(conf.ReportDownloadingErrors)
}

if res.StatusCode != 200 {
body, _ := ioutil.ReadAll(res.Body)
msg := fmt.Sprintf("Can't download image; Status: %d; %s", res.StatusCode, string(body))
return res, newError(404, msg, msgSourceImageIsUnreachable).SetUnexpected(conf.ReportDownloadingErrors)
}

if res.StatusCode != 200 {
body, _ := ioutil.ReadAll(res.Body)
msg := fmt.Sprintf("Can't download image; Status: %d; %s", res.StatusCode, string(body))
return res, newError(404, msg, msgSourceImageIsUnreachable).SetUnexpected(conf.ReportDownloadingErrors)
return res, nil
}

return res, nil
}

func downloadImage(ctx context.Context) (context.Context, context.CancelFunc, error) {
Expand Down

0 comments on commit 3cd137d

Please sign in to comment.