From 5d3e115d5f5a90a7bce27d2c367c3382cfa33c2f Mon Sep 17 00:00:00 2001 From: BoppreH Date: Thu, 18 May 2017 18:03:24 -0300 Subject: [PATCH] Get google image ext from content-type header --- download.go | 15 +++++++++++++-- steamgrid.go | 3 --- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/download.go b/download.go index 2f16ad3..d18ecab 100644 --- a/download.go +++ b/download.go @@ -8,6 +8,7 @@ import ( "net/url" "path/filepath" "regexp" + "strings" ) // When all else fails, Google it. Uses the regular web interface. There are @@ -122,6 +123,17 @@ func DownloadImage(gridDir string, game *Game) (bool, error) { return false, err } + contentType := response.Header.Get("Content-Type") + urlExt := filepath.Ext(response.Request.URL.Path) + if contentType != "" { + game.ImageExt = "." + strings.Split(contentType, "/")[1] + } else if urlExt != "" { + game.ImageExt = urlExt + } else { + // Steam is forgiving on image extensions. + game.ImageExt = "jpg" + } + imageBytes, err := ioutil.ReadAll(response.Body) response.Body.Close() @@ -132,13 +144,12 @@ func DownloadImage(gridDir string, game *Game) (bool, error) { } game.CleanImageBytes = imageBytes - game.ImageExt = filepath.Ext(response.Request.URL.Path) return fromSearch, nil } - // Get game name from SteamDB as last resort. const steamDBForamt = `https://steamdb.info/app/%v` + func GetGameName(gameId string) string { response, err := tryDownload(fmt.Sprintf(steamDBForamt, gameId)) if err != nil || response == nil { diff --git a/steamgrid.go b/steamgrid.go index 988193a..edf2bc9 100644 --- a/steamgrid.go +++ b/steamgrid.go @@ -142,9 +142,6 @@ func startApplication() { if err != nil { errorAndExit(err) } - if game.ImageExt == "" { - errorAndExit(errors.New("Failed to identify image format.")) - } imagePath := filepath.Join(gridDir, game.ID+game.ImageExt) err = ioutil.WriteFile(imagePath, game.OverlayImageBytes, 0666) if err != nil {