parent
48f6885f44
commit
3b94217fb7
4 changed files with 106 additions and 40 deletions
|
@ -220,9 +220,9 @@ func RefreshFeed(store *storage.Storage, userID, feedID int64, forceRefresh bool
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkFeedIcon(store *storage.Storage, feedID int64, websiteURL, iconURL, userAgent string, fetchViaProxy, allowSelfSignedCertificates bool) {
|
func checkFeedIcon(store *storage.Storage, feedID int64, websiteURL, feedIconURL, userAgent string, fetchViaProxy, allowSelfSignedCertificates bool) {
|
||||||
if !store.HasIcon(feedID) {
|
if !store.HasIcon(feedID) {
|
||||||
icon, err := icon.FindIcon(websiteURL, iconURL, userAgent, fetchViaProxy, allowSelfSignedCertificates)
|
icon, err := icon.FindIcon(websiteURL, feedIconURL, userAgent, fetchViaProxy, allowSelfSignedCertificates)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Debug(`[CheckFeedIcon] %v (feedID=%d websiteURL=%s)`, err, feedID, websiteURL)
|
logger.Debug(`[CheckFeedIcon] %v (feedID=%d websiteURL=%s)`, err, feedID, websiteURL)
|
||||||
} else if icon == nil {
|
} else if icon == nil {
|
||||||
|
|
|
@ -21,10 +21,51 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// FindIcon try to find the website's icon.
|
// FindIcon try to find the website's icon.
|
||||||
func FindIcon(websiteURL, iconURL, userAgent string, fetchViaProxy, allowSelfSignedCertificates bool) (*model.Icon, error) {
|
func FindIcon(websiteURL, feedIconURL, userAgent string, fetchViaProxy, allowSelfSignedCertificates bool) (icon *model.Icon, err error) {
|
||||||
if iconURL == "" {
|
if feedIconURL == "" {
|
||||||
|
feedIconURL, err = fetchHTMLDocumentAndFindIconURL(websiteURL, userAgent, fetchViaProxy, allowSelfSignedCertificates)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.HasPrefix(feedIconURL, "data:") {
|
||||||
|
return parseImageDataURL(feedIconURL)
|
||||||
|
}
|
||||||
|
|
||||||
|
feedIconURL, err = generateIconURL(websiteURL, feedIconURL)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if icon, err = downloadIcon(feedIconURL, userAgent, fetchViaProxy, allowSelfSignedCertificates); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return icon, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func generateIconURL(websiteURL, feedIconURL string) (iconURL string, err error) {
|
||||||
|
feedIconURL = strings.TrimSpace(feedIconURL)
|
||||||
|
|
||||||
|
if feedIconURL == "" {
|
||||||
|
iconURL, err = urllib.JoinBaseURLAndPath(urllib.RootURL(websiteURL), "favicon.ico")
|
||||||
|
if err != nil {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return iconURL, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func fetchHTMLDocumentAndFindIconURL(websiteURL, userAgent string, fetchViaProxy, allowSelfSignedCertificates bool) (string, error) {
|
||||||
rootURL := urllib.RootURL(websiteURL)
|
rootURL := urllib.RootURL(websiteURL)
|
||||||
logger.Debug("[FindIcon] Trying to find an icon: rootURL=%q websiteURL=%q userAgent=%q", rootURL, websiteURL, userAgent)
|
logger.Debug("[FindIcon] Find icon from HTML webpage: %s", rootURL)
|
||||||
|
|
||||||
clt := client.NewClientWithConfig(rootURL, config.Opts)
|
clt := client.NewClientWithConfig(rootURL, config.Opts)
|
||||||
clt.WithUserAgent(userAgent)
|
clt.WithUserAgent(userAgent)
|
||||||
|
@ -36,33 +77,17 @@ func FindIcon(websiteURL, iconURL, userAgent string, fetchViaProxy, allowSelfSig
|
||||||
|
|
||||||
response, err := clt.Get()
|
response, err := clt.Get()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("icon: unable to download website index page: %v", err)
|
return "", fmt.Errorf("icon: unable to download website index page: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if response.HasServerFailure() {
|
if response.HasServerFailure() {
|
||||||
return nil, fmt.Errorf("icon: unable to download website index page: status=%d", response.StatusCode)
|
return "", fmt.Errorf("icon: unable to download website index page: status=%d", response.StatusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
iconURL, err = parseDocument(rootURL, response.Body)
|
return findIconURLFromHTMLDocument(response.Body)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if strings.HasPrefix(iconURL, "data:") {
|
func findIconURLFromHTMLDocument(body io.Reader) (string, error) {
|
||||||
return parseImageDataURL(iconURL)
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.Debug("[FindIcon] Fetching icon => %s", iconURL)
|
|
||||||
icon, err := downloadIcon(iconURL, userAgent, fetchViaProxy, allowSelfSignedCertificates)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return icon, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func parseDocument(websiteURL string, data io.Reader) (string, error) {
|
|
||||||
queries := []string{
|
queries := []string{
|
||||||
"link[rel='shortcut icon']",
|
"link[rel='shortcut icon']",
|
||||||
"link[rel='Shortcut Icon']",
|
"link[rel='Shortcut Icon']",
|
||||||
|
@ -70,7 +95,7 @@ func parseDocument(websiteURL string, data io.Reader) (string, error) {
|
||||||
"link[rel='icon']",
|
"link[rel='icon']",
|
||||||
}
|
}
|
||||||
|
|
||||||
doc, err := goquery.NewDocumentFromReader(data)
|
doc, err := goquery.NewDocumentFromReader(body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("icon: unable to read document: %v", err)
|
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
|
return iconURL, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -92,12 +92,59 @@ func TestParseDocumentWithWhitespaceIconURL(t *testing.T) {
|
||||||
/static/img/favicon.ico
|
/static/img/favicon.ico
|
||||||
">`
|
">`
|
||||||
|
|
||||||
iconURL, err := parseDocument("http://www.example.org/", strings.NewReader(html))
|
iconURL, err := findIconURLFromHTMLDocument(strings.NewReader(html))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if iconURL != "http://www.example.org/static/img/favicon.ico" {
|
if iconURL != "/static/img/favicon.ico" {
|
||||||
|
t.Errorf(`Invalid icon URL, got %q`, iconURL)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGenerateIconURL(t *testing.T) {
|
||||||
|
iconURL, err := generateIconURL("https://example.org/", "/favicon.png")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if iconURL != "https://example.org/favicon.png" {
|
||||||
|
t.Errorf(`Invalid icon URL, got %q`, iconURL)
|
||||||
|
}
|
||||||
|
|
||||||
|
iconURL, err = generateIconURL("https://example.org/", "img/favicon.png")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if iconURL != "https://example.org/img/favicon.png" {
|
||||||
|
t.Errorf(`Invalid icon URL, got %q`, iconURL)
|
||||||
|
}
|
||||||
|
|
||||||
|
iconURL, err = generateIconURL("https://example.org/", "https://example.org/img/favicon.png")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if iconURL != "https://example.org/img/favicon.png" {
|
||||||
|
t.Errorf(`Invalid icon URL, got %q`, iconURL)
|
||||||
|
}
|
||||||
|
|
||||||
|
iconURL, err = generateIconURL("https://example.org/", "//example.org/img/favicon.png")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if iconURL != "https://example.org/img/favicon.png" {
|
||||||
|
t.Errorf(`Invalid icon URL, got %q`, iconURL)
|
||||||
|
}
|
||||||
|
|
||||||
|
iconURL, err = generateIconURL("https://example.org/", " ")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if iconURL != "https://example.org/favicon.ico" {
|
||||||
t.Errorf(`Invalid icon URL, got %q`, iconURL)
|
t.Errorf(`Invalid icon URL, got %q`, iconURL)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,7 +27,7 @@ func (s *Storage) IconByID(iconID int64) (*model.Icon, error) {
|
||||||
if err == sql.ErrNoRows {
|
if err == sql.ErrNoRows {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
return nil, fmt.Errorf("Unable to fetch icon by hash: %v", err)
|
return nil, fmt.Errorf("store: unable to fetch icon by hash: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &icon, nil
|
return &icon, nil
|
||||||
|
|
Loading…
Reference in a new issue