2023-06-19 17:42:47 -04:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2017-11-20 00:10:04 -05:00
|
|
|
|
2023-08-10 22:46:45 -04:00
|
|
|
package icon // import "miniflux.app/v2/internal/reader/icon"
|
2017-11-20 00:10:04 -05:00
|
|
|
|
|
|
|
import (
|
2017-12-22 22:01:39 -05:00
|
|
|
"encoding/base64"
|
2017-11-20 00:10:04 -05:00
|
|
|
"fmt"
|
2017-11-20 20:12:37 -05:00
|
|
|
"io"
|
2023-10-19 00:42:34 -04:00
|
|
|
"log/slog"
|
2023-08-13 22:09:01 -04:00
|
|
|
"net/url"
|
2024-02-26 19:40:34 -05:00
|
|
|
"regexp"
|
2017-12-22 22:01:39 -05:00
|
|
|
"strings"
|
2017-11-20 20:12:37 -05:00
|
|
|
|
2023-08-10 22:46:45 -04:00
|
|
|
"miniflux.app/v2/internal/config"
|
|
|
|
"miniflux.app/v2/internal/crypto"
|
|
|
|
"miniflux.app/v2/internal/model"
|
2023-12-01 19:27:18 -05:00
|
|
|
"miniflux.app/v2/internal/reader/encoding"
|
2023-10-21 22:50:29 -04:00
|
|
|
"miniflux.app/v2/internal/reader/fetcher"
|
2023-08-13 22:09:01 -04:00
|
|
|
"miniflux.app/v2/internal/urllib"
|
2017-11-20 00:10:04 -05:00
|
|
|
|
|
|
|
"github.com/PuerkitoBio/goquery"
|
|
|
|
)
|
|
|
|
|
2023-10-19 00:42:34 -04:00
|
|
|
type IconFinder struct {
|
2023-10-21 22:50:29 -04:00
|
|
|
requestBuilder *fetcher.RequestBuilder
|
|
|
|
websiteURL string
|
|
|
|
feedIconURL string
|
2023-10-19 00:42:34 -04:00
|
|
|
}
|
|
|
|
|
2023-10-21 22:50:29 -04:00
|
|
|
func NewIconFinder(requestBuilder *fetcher.RequestBuilder, websiteURL, feedIconURL string) *IconFinder {
|
2023-10-19 00:42:34 -04:00
|
|
|
return &IconFinder{
|
2023-10-21 22:50:29 -04:00
|
|
|
requestBuilder: requestBuilder,
|
|
|
|
websiteURL: websiteURL,
|
|
|
|
feedIconURL: feedIconURL,
|
2023-10-19 00:42:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *IconFinder) FindIcon() (*model.Icon, error) {
|
|
|
|
slog.Debug("Begin icon discovery process",
|
|
|
|
slog.String("website_url", f.websiteURL),
|
|
|
|
slog.String("feed_icon_url", f.feedIconURL),
|
|
|
|
)
|
|
|
|
|
|
|
|
if f.feedIconURL != "" {
|
|
|
|
if icon, err := f.FetchFeedIcon(); err != nil {
|
|
|
|
slog.Debug("Unable to download icon from feed",
|
|
|
|
slog.String("website_url", f.websiteURL),
|
|
|
|
slog.String("feed_icon_url", f.feedIconURL),
|
|
|
|
slog.Any("error", err),
|
|
|
|
)
|
|
|
|
} else if icon != nil {
|
|
|
|
return icon, nil
|
2023-09-09 17:18:39 -04:00
|
|
|
}
|
|
|
|
}
|
2022-01-08 18:09:12 -05:00
|
|
|
|
2023-10-19 00:42:34 -04:00
|
|
|
if icon, err := f.FetchIconsFromHTMLDocument(); err != nil {
|
|
|
|
slog.Debug("Unable to fetch icons from HTML document",
|
|
|
|
slog.String("website_url", f.websiteURL),
|
|
|
|
slog.Any("error", err),
|
|
|
|
)
|
|
|
|
} else if icon != nil {
|
|
|
|
return icon, nil
|
2023-09-09 17:18:39 -04:00
|
|
|
}
|
2022-01-08 18:09:12 -05:00
|
|
|
|
2023-10-19 00:42:34 -04:00
|
|
|
return f.FetchDefaultIcon()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *IconFinder) FetchDefaultIcon() (*model.Icon, error) {
|
|
|
|
slog.Debug("Fetching default icon",
|
|
|
|
slog.String("website_url", f.websiteURL),
|
|
|
|
)
|
|
|
|
|
|
|
|
iconURL, err := urllib.JoinBaseURLAndPath(urllib.RootURL(f.websiteURL), "favicon.ico")
|
2023-09-09 17:18:39 -04:00
|
|
|
if err != nil {
|
2023-10-19 00:42:34 -04:00
|
|
|
return nil, fmt.Errorf(`icon: unable to join root URL and path: %w`, err)
|
2023-09-09 17:18:39 -04:00
|
|
|
}
|
2021-02-21 16:42:49 -05:00
|
|
|
|
2023-10-19 00:42:34 -04:00
|
|
|
icon, err := f.DownloadIcon(iconURL)
|
|
|
|
if err != nil {
|
2023-09-09 17:18:39 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-11-20 00:10:04 -05:00
|
|
|
|
2023-09-09 17:18:39 -04:00
|
|
|
return icon, nil
|
|
|
|
}
|
|
|
|
|
2023-10-19 00:42:34 -04:00
|
|
|
func (f *IconFinder) FetchFeedIcon() (*model.Icon, error) {
|
|
|
|
slog.Debug("Fetching feed icon",
|
|
|
|
slog.String("website_url", f.websiteURL),
|
|
|
|
slog.String("feed_icon_url", f.feedIconURL),
|
|
|
|
)
|
2017-11-20 00:10:04 -05:00
|
|
|
|
2023-10-19 00:42:34 -04:00
|
|
|
iconURL, err := urllib.AbsoluteURL(f.websiteURL, f.feedIconURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf(`icon: unable to convert icon URL to absolute URL: %w`, err)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2023-10-19 00:42:34 -04:00
|
|
|
return f.DownloadIcon(iconURL)
|
2023-09-09 17:18:39 -04:00
|
|
|
}
|
|
|
|
|
2023-10-19 00:42:34 -04:00
|
|
|
func (f *IconFinder) FetchIconsFromHTMLDocument() (*model.Icon, error) {
|
|
|
|
slog.Debug("Searching icons from HTML document",
|
|
|
|
slog.String("website_url", f.websiteURL),
|
|
|
|
)
|
2023-09-09 17:18:39 -04:00
|
|
|
|
2023-10-21 22:50:29 -04:00
|
|
|
rootURL := urllib.RootURL(f.websiteURL)
|
|
|
|
|
|
|
|
responseHandler := fetcher.NewResponseHandler(f.requestBuilder.ExecuteRequest(rootURL))
|
|
|
|
defer responseHandler.Close()
|
|
|
|
|
|
|
|
if localizedError := responseHandler.LocalizedError(); localizedError != nil {
|
|
|
|
return nil, fmt.Errorf("icon: unable to download website index page: %w", localizedError.Error())
|
2017-12-22 22:01:39 -05:00
|
|
|
}
|
|
|
|
|
2023-12-01 19:27:18 -05:00
|
|
|
iconURLs, err := findIconURLsFromHTMLDocument(
|
|
|
|
responseHandler.Body(config.Opts.HTTPClientMaxBodySize()),
|
|
|
|
responseHandler.ContentType(),
|
|
|
|
)
|
2017-11-20 00:10:04 -05:00
|
|
|
if err != nil {
|
2023-10-19 00:42:34 -04:00
|
|
|
return nil, err
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2023-10-19 00:42:34 -04:00
|
|
|
slog.Debug("Searched icon from HTML document",
|
|
|
|
slog.String("website_url", f.websiteURL),
|
|
|
|
slog.String("icon_urls", strings.Join(iconURLs, ",")),
|
|
|
|
)
|
|
|
|
|
|
|
|
for _, iconURL := range iconURLs {
|
|
|
|
if strings.HasPrefix(iconURL, "data:") {
|
|
|
|
slog.Debug("Found icon with data URL",
|
|
|
|
slog.String("website_url", f.websiteURL),
|
|
|
|
)
|
|
|
|
return parseImageDataURL(iconURL)
|
|
|
|
}
|
|
|
|
|
|
|
|
iconURL, err = urllib.AbsoluteURL(f.websiteURL, iconURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf(`icon: unable to convert icon URL to absolute URL: %w`, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if icon, err := f.DownloadIcon(iconURL); err != nil {
|
|
|
|
slog.Debug("Unable to download icon from HTML document",
|
|
|
|
slog.String("website_url", f.websiteURL),
|
|
|
|
slog.String("icon_url", iconURL),
|
|
|
|
slog.Any("error", err),
|
|
|
|
)
|
|
|
|
} else if icon != nil {
|
|
|
|
slog.Debug("Found icon from HTML document",
|
|
|
|
slog.String("website_url", f.websiteURL),
|
|
|
|
slog.String("icon_url", iconURL),
|
|
|
|
)
|
|
|
|
return icon, nil
|
|
|
|
}
|
2023-09-09 17:18:39 -04:00
|
|
|
}
|
|
|
|
|
2023-10-19 00:42:34 -04:00
|
|
|
return nil, nil
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2023-10-19 00:42:34 -04:00
|
|
|
func (f *IconFinder) DownloadIcon(iconURL string) (*model.Icon, error) {
|
|
|
|
slog.Debug("Downloading icon",
|
|
|
|
slog.String("website_url", f.websiteURL),
|
|
|
|
slog.String("icon_url", iconURL),
|
|
|
|
)
|
|
|
|
|
2023-10-21 22:50:29 -04:00
|
|
|
responseHandler := fetcher.NewResponseHandler(f.requestBuilder.ExecuteRequest(iconURL))
|
|
|
|
defer responseHandler.Close()
|
2017-11-20 00:10:04 -05:00
|
|
|
|
2023-10-21 22:50:29 -04:00
|
|
|
if localizedError := responseHandler.LocalizedError(); localizedError != nil {
|
|
|
|
return nil, fmt.Errorf("icon: unable to download website icon: %w", localizedError.Error())
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2023-10-21 22:50:29 -04:00
|
|
|
responseBody, localizedError := responseHandler.ReadBody(config.Opts.HTTPClientMaxBodySize())
|
|
|
|
if localizedError != nil {
|
|
|
|
return nil, fmt.Errorf("icon: unable to read response body: %w", localizedError.Error())
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
icon := &model.Icon{
|
2023-10-21 22:50:29 -04:00
|
|
|
Hash: crypto.HashFromBytes(responseBody),
|
|
|
|
MimeType: responseHandler.ContentType(),
|
|
|
|
Content: responseBody,
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return icon, nil
|
|
|
|
}
|
2017-12-22 22:01:39 -05:00
|
|
|
|
2023-12-01 19:27:18 -05:00
|
|
|
func findIconURLsFromHTMLDocument(body io.Reader, contentType string) ([]string, error) {
|
2023-10-19 00:42:34 -04:00
|
|
|
queries := []string{
|
2024-02-26 19:40:34 -05:00
|
|
|
"link[rel='icon' i]",
|
|
|
|
"link[rel='shortcut icon' i]",
|
|
|
|
"link[rel='icon shortcut' i]",
|
|
|
|
"link[rel='apple-touch-icon-precomposed.png']",
|
2023-10-19 00:42:34 -04:00
|
|
|
}
|
|
|
|
|
2023-12-01 19:27:18 -05:00
|
|
|
htmlDocumentReader, err := encoding.CharsetReaderFromContentType(contentType, body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("icon: unable to create charset reader: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
doc, err := goquery.NewDocumentFromReader(htmlDocumentReader)
|
2023-10-19 00:42:34 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("icon: unable to read document: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var iconURLs []string
|
|
|
|
for _, query := range queries {
|
|
|
|
slog.Debug("Searching icon URL in HTML document", slog.String("query", query))
|
|
|
|
|
|
|
|
doc.Find(query).Each(func(i int, s *goquery.Selection) {
|
|
|
|
if href, exists := s.Attr("href"); exists {
|
2024-02-26 19:40:34 -05:00
|
|
|
if iconURL := strings.TrimSpace(href); iconURL != "" {
|
|
|
|
iconURLs = append(iconURLs, iconURL)
|
|
|
|
slog.Debug("Found icon URL in HTML document",
|
|
|
|
slog.String("query", query),
|
|
|
|
slog.String("icon_url", iconURL))
|
|
|
|
}
|
2023-10-19 00:42:34 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return iconURLs, nil
|
|
|
|
}
|
|
|
|
|
2022-03-26 01:05:19 -04:00
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs#syntax
|
2024-02-26 19:40:34 -05:00
|
|
|
// data:[<mediatype>][;encoding],<data>
|
|
|
|
// we consider <mediatype> to be mandatory, and it has to start with `image/`.
|
|
|
|
// we consider `base64`, `utf8` and the empty string to be the only valid encodings
|
2017-12-22 22:01:39 -05:00
|
|
|
func parseImageDataURL(value string) (*model.Icon, error) {
|
2024-02-26 19:40:34 -05:00
|
|
|
re := regexp.MustCompile(`^data:` +
|
|
|
|
`(?P<mediatype>image/[^;,]+)` +
|
|
|
|
`(?:;(?P<encoding>base64|utf8))?` +
|
|
|
|
`,(?P<data>.+)$`)
|
|
|
|
|
|
|
|
matches := re.FindStringSubmatch(value)
|
|
|
|
if matches == nil {
|
|
|
|
return nil, fmt.Errorf(`icon: invalid data URL %q`, value)
|
2017-12-22 22:01:39 -05:00
|
|
|
}
|
|
|
|
|
2024-02-26 19:40:34 -05:00
|
|
|
mediaType := matches[re.SubexpIndex("mediatype")]
|
|
|
|
encoding := matches[re.SubexpIndex("encoding")]
|
|
|
|
data := matches[re.SubexpIndex("data")]
|
2017-12-22 22:01:39 -05:00
|
|
|
|
2022-03-26 01:05:19 -04:00
|
|
|
var blob []byte
|
|
|
|
switch encoding {
|
|
|
|
case "base64":
|
|
|
|
var err error
|
|
|
|
blob, err = base64.StdEncoding.DecodeString(data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf(`icon: invalid data %q (%v)`, value, err)
|
|
|
|
}
|
|
|
|
case "":
|
2023-08-13 22:09:01 -04:00
|
|
|
decodedData, err := url.QueryUnescape(data)
|
2022-03-26 01:05:19 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf(`icon: unable to decode data URL %q`, value)
|
|
|
|
}
|
|
|
|
blob = []byte(decodedData)
|
2023-10-18 23:11:24 -04:00
|
|
|
case "utf8":
|
|
|
|
blob = []byte(data)
|
2017-12-22 22:01:39 -05:00
|
|
|
}
|
|
|
|
|
2024-02-26 19:40:34 -05:00
|
|
|
return &model.Icon{
|
2018-01-02 22:15:08 -05:00
|
|
|
Hash: crypto.HashFromBytes(blob),
|
2017-12-22 22:01:39 -05:00
|
|
|
Content: blob,
|
2022-03-26 01:05:19 -04:00
|
|
|
MimeType: mediaType,
|
2024-02-26 19:40:34 -05:00
|
|
|
}, nil
|
2017-12-22 22:01:39 -05:00
|
|
|
}
|