Skip to content

Commit

Permalink
More reliable favicon retrieval.
Browse files Browse the repository at this point in the history
Previously, the application looked for favicons in `/favicon.ico`.
But favicons can be anywhere on a website and the location can be
defined in HTML and JSON manifests. Websites can also have multiple
favicons.

As a result, when a new site was added to stash-box which did not have a
`/favicon.ico`, the site was created without a proper favicon. Additionally,
since stash-box didn't check the filetype of the data returned by the
`/favicon.ico` GET request, it stored various 404 and other redirected
html pages.

A non-trivial amount of logic is required to discover all favicon
locations, check filetypes, and sort them. It therefore makes sense to
rely on a third-party package for this job. This patch uses
`go.deanishe.net/favicon`.
  • Loading branch information
aghoulcoder committed Jun 3, 2023
1 parent c42ceea commit b495b27
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions pkg/image/favicon.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/gofrs/uuid"
"github.com/stashapp/stash-box/pkg/manager/config"
"github.com/stashapp/stash-box/pkg/models"
"go.deanishe.net/favicon"
)

var iconCache = map[uuid.UUID][]byte{}
Expand Down Expand Up @@ -70,9 +71,14 @@ func downloadIcon(ctx context.Context, iconPath string, siteURL string) {
if err != nil {
return
}
u.Path = path.Join(u.Path, "favicon.ico")

req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
icons, err := favicon.Find(u.Scheme + "://" + u.Host)
if err != nil {
return
}

// Icons are sorted widest first. We currently get the first one (icons[0]).
req, err := http.NewRequestWithContext(ctx, http.MethodGet, icons[0].URL, nil)
if err != nil {
return
}
Expand Down

0 comments on commit b495b27

Please sign in to comment.