From e5b4639cabe08ee9cad3cfa2526bea3976f7389a Mon Sep 17 00:00:00 2001 From: Lucas BEE Date: Tue, 7 Apr 2020 17:45:39 +0200 Subject: [PATCH] Add check of HTTP status when downloading --- backend/ratings/ratings.go | 5 +++++ backend/web/download.go | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/backend/ratings/ratings.go b/backend/ratings/ratings.go index 3be0df0..cea2362 100644 --- a/backend/ratings/ratings.go +++ b/backend/ratings/ratings.go @@ -4,6 +4,7 @@ import ( "bufio" "bytes" "compress/gzip" + "fmt" "io/ioutil" "net/http" "strconv" @@ -34,6 +35,10 @@ func Refresh(env *web.Env) error { } defer resp.Body.Close() + if resp.StatusCode != http.StatusOK { + return fmt.Errorf("got HTTP error %s", resp.Status) + } + // Read all the file (~5MB) in memory // We do that because the ~1 000 000 upserts take too long, and the IMDB // server will cut our connection after ~2h diff --git a/backend/web/download.go b/backend/web/download.go index 1e906e1..ee38cae 100644 --- a/backend/web/download.go +++ b/backend/web/download.go @@ -1,6 +1,7 @@ package web import ( + "fmt" "image" "image/jpeg" "net/http" @@ -26,6 +27,10 @@ var Download = func(srcURL, dest string, scale bool) error { } defer resp.Body.Close() + if resp.StatusCode != http.StatusOK { + return fmt.Errorf("got HTTP error %s", resp.Status) + } + image, _, err := image.Decode(resp.Body) if err != nil { return err