diff options
Diffstat (limited to 'internal/reader/icon/finder.go')
-rw-r--r-- | internal/reader/icon/finder.go | 85 |
1 files changed, 52 insertions, 33 deletions
diff --git a/internal/reader/icon/finder.go b/internal/reader/icon/finder.go index bb412615..afc032e3 100644 --- a/internal/reader/icon/finder.go +++ b/internal/reader/icon/finder.go @@ -21,48 +21,73 @@ import ( ) // FindIcon try to find the website's icon. -func FindIcon(websiteURL, iconURL, userAgent string, fetchViaProxy, allowSelfSignedCertificates bool) (*model.Icon, error) { - if iconURL == "" { - rootURL := urllib.RootURL(websiteURL) - logger.Debug("[FindIcon] Trying to find an icon: rootURL=%q websiteURL=%q userAgent=%q", rootURL, websiteURL, userAgent) +func FindIcon(websiteURL, feedIconURL, userAgent string, fetchViaProxy, allowSelfSignedCertificates bool) (icon *model.Icon, err error) { + if feedIconURL == "" { + feedIconURL, err = fetchHTMLDocumentAndFindIconURL(websiteURL, userAgent, fetchViaProxy, allowSelfSignedCertificates) + if err != nil { + return nil, err + } + } - clt := client.NewClientWithConfig(rootURL, config.Opts) - clt.WithUserAgent(userAgent) - clt.AllowSelfSignedCertificates = allowSelfSignedCertificates + if strings.HasPrefix(feedIconURL, "data:") { + return parseImageDataURL(feedIconURL) + } - if fetchViaProxy { - clt.WithProxy() - } + feedIconURL, err = generateIconURL(websiteURL, feedIconURL) + if err != nil { + return nil, err + } - response, err := clt.Get() - if err != nil { - return nil, fmt.Errorf("icon: unable to download website index page: %v", err) - } + if icon, err = downloadIcon(feedIconURL, userAgent, fetchViaProxy, allowSelfSignedCertificates); err != nil { + return nil, err + } - if response.HasServerFailure() { - return nil, fmt.Errorf("icon: unable to download website index page: status=%d", response.StatusCode) - } + return icon, nil +} + +func generateIconURL(websiteURL, feedIconURL string) (iconURL string, err error) { + feedIconURL = strings.TrimSpace(feedIconURL) - iconURL, err = parseDocument(rootURL, response.Body) + if feedIconURL == "" { + iconURL, err = urllib.JoinBaseURLAndPath(urllib.RootURL(websiteURL), "favicon.ico") if err != nil { - return nil, err + return "", fmt.Errorf(`icon: unable to join base URL and path: %w`, err) + } + } else { + iconURL, err = urllib.AbsoluteURL(websiteURL, feedIconURL) + if err != nil { + return "", fmt.Errorf(`icon: unable to convert icon URL to absolute URL: %w`, err) } } - if strings.HasPrefix(iconURL, "data:") { - return parseImageDataURL(iconURL) + return iconURL, nil +} + +func fetchHTMLDocumentAndFindIconURL(websiteURL, userAgent string, fetchViaProxy, allowSelfSignedCertificates bool) (string, error) { + rootURL := urllib.RootURL(websiteURL) + logger.Debug("[FindIcon] Find icon from HTML webpage: %s", rootURL) + + clt := client.NewClientWithConfig(rootURL, config.Opts) + clt.WithUserAgent(userAgent) + clt.AllowSelfSignedCertificates = allowSelfSignedCertificates + + if fetchViaProxy { + clt.WithProxy() } - logger.Debug("[FindIcon] Fetching icon => %s", iconURL) - icon, err := downloadIcon(iconURL, userAgent, fetchViaProxy, allowSelfSignedCertificates) + response, err := clt.Get() if err != nil { - return nil, err + return "", fmt.Errorf("icon: unable to download website index page: %v", err) } - return icon, nil + if response.HasServerFailure() { + return "", fmt.Errorf("icon: unable to download website index page: status=%d", response.StatusCode) + } + + return findIconURLFromHTMLDocument(response.Body) } -func parseDocument(websiteURL string, data io.Reader) (string, error) { +func findIconURLFromHTMLDocument(body io.Reader) (string, error) { queries := []string{ "link[rel='shortcut icon']", "link[rel='Shortcut Icon']", @@ -70,7 +95,7 @@ func parseDocument(websiteURL string, data io.Reader) (string, error) { "link[rel='icon']", } - doc, err := goquery.NewDocumentFromReader(data) + doc, err := goquery.NewDocumentFromReader(body) if err != nil { return "", fmt.Errorf("icon: unable to read document: %v", err) } @@ -88,12 +113,6 @@ func parseDocument(websiteURL string, data io.Reader) (string, error) { } } - if iconURL == "" { - iconURL = urllib.RootURL(websiteURL) + "favicon.ico" - } else { - iconURL, _ = urllib.AbsoluteURL(websiteURL, iconURL) - } - return iconURL, nil } |