Skip to content

Commit

Permalink
Get google image ext from content-type header
Browse files Browse the repository at this point in the history
  • Loading branch information
boppreh committed May 18, 2017
1 parent 06a94a0 commit 5d3e115
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
15 changes: 13 additions & 2 deletions download.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/url"
"path/filepath"
"regexp"
"strings"
)

// When all else fails, Google it. Uses the regular web interface. There are
Expand Down Expand Up @@ -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()

Expand All @@ -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 {
Expand Down
3 changes: 0 additions & 3 deletions steamgrid.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 5d3e115

Please sign in to comment.