1
0
Fork 0
miniflux/reader/icon/finder.go

165 lines
3.9 KiB
Go
Raw Normal View History

2017-11-20 00:10:04 -05:00
// Copyright 2017 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
2018-08-25 00:51:50 -04:00
package icon // import "miniflux.app/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"
"io"
2017-12-22 22:01:39 -05:00
"strings"
"miniflux.app/config"
2018-08-25 00:51:50 -04:00
"miniflux.app/crypto"
"miniflux.app/http/client"
"miniflux.app/logger"
"miniflux.app/model"
"miniflux.app/url"
2017-11-20 00:10:04 -05:00
"github.com/PuerkitoBio/goquery"
)
// FindIcon try to find the website's icon.
func FindIcon(websiteURL string, fetchViaProxy, allowSelfSignedCertificates bool) (*model.Icon, error) {
2017-12-02 01:29:18 -05:00
rootURL := url.RootURL(websiteURL)
clt := client.NewClientWithConfig(rootURL, config.Opts)
clt.AllowSelfSignedCertificates = allowSelfSignedCertificates
if fetchViaProxy {
clt.WithProxy()
}
2018-04-28 13:51:07 -04:00
response, err := clt.Get()
2017-11-20 00:10:04 -05:00
if err != nil {
return nil, fmt.Errorf("unable to download website index page: %v", err)
}
if response.HasServerFailure() {
return nil, fmt.Errorf("unable to download website index page: status=%d", response.StatusCode)
}
iconURL, err := parseDocument(rootURL, response.Body)
if err != nil {
return nil, err
}
2017-12-22 22:01:39 -05:00
if strings.HasPrefix(iconURL, "data:") {
return parseImageDataURL(iconURL)
}
2017-12-15 21:55:57 -05:00
logger.Debug("[FindIcon] Fetching icon => %s", iconURL)
icon, err := downloadIcon(iconURL, fetchViaProxy, allowSelfSignedCertificates)
2017-11-20 00:10:04 -05:00
if err != nil {
return nil, err
}
return icon, nil
}
func parseDocument(websiteURL string, data io.Reader) (string, error) {
queries := []string{
"link[rel='shortcut icon']",
"link[rel='Shortcut Icon']",
"link[rel='icon shortcut']",
"link[rel='icon']",
}
doc, err := goquery.NewDocumentFromReader(data)
if err != nil {
return "", fmt.Errorf("unable to read document: %v", err)
}
var iconURL string
for _, query := range queries {
doc.Find(query).Each(func(i int, s *goquery.Selection) {
if href, exists := s.Attr("href"); exists {
2020-11-06 20:11:52 -05:00
iconURL = strings.TrimSpace(href)
2017-11-20 00:10:04 -05:00
}
})
if iconURL != "" {
break
}
}
if iconURL == "" {
2017-12-02 01:29:18 -05:00
iconURL = url.RootURL(websiteURL) + "favicon.ico"
2017-11-20 00:10:04 -05:00
} else {
2017-12-02 01:29:18 -05:00
iconURL, _ = url.AbsoluteURL(websiteURL, iconURL)
2017-11-20 00:10:04 -05:00
}
return iconURL, nil
}
func downloadIcon(iconURL string, fetchViaProxy, allowSelfSignedCertificates bool) (*model.Icon, error) {
clt := client.NewClientWithConfig(iconURL, config.Opts)
clt.AllowSelfSignedCertificates = allowSelfSignedCertificates
if fetchViaProxy {
clt.WithProxy()
}
2018-04-28 13:51:07 -04:00
response, err := clt.Get()
2017-11-20 00:10:04 -05:00
if err != nil {
return nil, fmt.Errorf("unable to download iconURL: %v", err)
}
if response.HasServerFailure() {
return nil, fmt.Errorf("unable to download icon: status=%d", response.StatusCode)
}
body, err := io.ReadAll(response.Body)
2017-11-20 00:10:04 -05:00
if err != nil {
return nil, fmt.Errorf("unable to read downloaded icon: %v", err)
}
if len(body) == 0 {
return nil, fmt.Errorf("downloaded icon is empty, iconURL=%s", iconURL)
}
icon := &model.Icon{
2018-01-02 22:15:08 -05:00
Hash: crypto.HashFromBytes(body),
2017-11-20 00:10:04 -05:00
MimeType: response.ContentType,
Content: body,
}
return icon, nil
}
2017-12-22 22:01:39 -05:00
func parseImageDataURL(value string) (*model.Icon, error) {
colon := strings.Index(value, ":")
semicolon := strings.Index(value, ";")
comma := strings.Index(value, ",")
if colon <= 0 || semicolon <= 0 || comma <= 0 {
return nil, fmt.Errorf(`icon: invalid data url "%s"`, value)
}
mimeType := value[colon+1 : semicolon]
encoding := value[semicolon+1 : comma]
data := value[comma+1:]
if encoding != "base64" {
return nil, fmt.Errorf(`icon: unsupported data url encoding "%s"`, value)
}
if !strings.HasPrefix(mimeType, "image/") {
return nil, fmt.Errorf(`icon: invalid mime type "%s"`, mimeType)
}
blob, err := base64.StdEncoding.DecodeString(data)
if err != nil {
return nil, fmt.Errorf(`icon: invalid data "%s" (%v)`, value, err)
}
if len(blob) == 0 {
return nil, fmt.Errorf(`icon: empty data "%s"`, value)
}
icon := &model.Icon{
2018-01-02 22:15:08 -05:00
Hash: crypto.HashFromBytes(blob),
2017-12-22 22:01:39 -05:00
Content: blob,
MimeType: mimeType,
}
return icon, nil
}