finder.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package icon // import "miniflux.app/reader/icon"
  5. import (
  6. "encoding/base64"
  7. "fmt"
  8. "io"
  9. "io/ioutil"
  10. "strings"
  11. "miniflux.app/config"
  12. "miniflux.app/crypto"
  13. "miniflux.app/http/client"
  14. "miniflux.app/logger"
  15. "miniflux.app/model"
  16. "miniflux.app/url"
  17. "github.com/PuerkitoBio/goquery"
  18. )
  19. // FindIcon try to find the website's icon.
  20. func FindIcon(websiteURL string, fetchViaProxy bool) (*model.Icon, error) {
  21. rootURL := url.RootURL(websiteURL)
  22. clt := client.NewClientWithConfig(rootURL, config.Opts)
  23. if fetchViaProxy {
  24. clt.WithProxy()
  25. }
  26. response, err := clt.Get()
  27. if err != nil {
  28. return nil, fmt.Errorf("unable to download website index page: %v", err)
  29. }
  30. if response.HasServerFailure() {
  31. return nil, fmt.Errorf("unable to download website index page: status=%d", response.StatusCode)
  32. }
  33. iconURL, err := parseDocument(rootURL, response.Body)
  34. if err != nil {
  35. return nil, err
  36. }
  37. if strings.HasPrefix(iconURL, "data:") {
  38. return parseImageDataURL(iconURL)
  39. }
  40. logger.Debug("[FindIcon] Fetching icon => %s", iconURL)
  41. icon, err := downloadIcon(iconURL, fetchViaProxy)
  42. if err != nil {
  43. return nil, err
  44. }
  45. return icon, nil
  46. }
  47. func parseDocument(websiteURL string, data io.Reader) (string, error) {
  48. queries := []string{
  49. "link[rel='shortcut icon']",
  50. "link[rel='Shortcut Icon']",
  51. "link[rel='icon shortcut']",
  52. "link[rel='icon']",
  53. }
  54. doc, err := goquery.NewDocumentFromReader(data)
  55. if err != nil {
  56. return "", fmt.Errorf("unable to read document: %v", err)
  57. }
  58. var iconURL string
  59. for _, query := range queries {
  60. doc.Find(query).Each(func(i int, s *goquery.Selection) {
  61. if href, exists := s.Attr("href"); exists {
  62. iconURL = strings.TrimSpace(href)
  63. }
  64. })
  65. if iconURL != "" {
  66. break
  67. }
  68. }
  69. if iconURL == "" {
  70. iconURL = url.RootURL(websiteURL) + "favicon.ico"
  71. } else {
  72. iconURL, _ = url.AbsoluteURL(websiteURL, iconURL)
  73. }
  74. return iconURL, nil
  75. }
  76. func downloadIcon(iconURL string, fetchViaProxy bool) (*model.Icon, error) {
  77. clt := client.NewClientWithConfig(iconURL, config.Opts)
  78. if fetchViaProxy {
  79. clt.WithProxy()
  80. }
  81. response, err := clt.Get()
  82. if err != nil {
  83. return nil, fmt.Errorf("unable to download iconURL: %v", err)
  84. }
  85. if response.HasServerFailure() {
  86. return nil, fmt.Errorf("unable to download icon: status=%d", response.StatusCode)
  87. }
  88. body, err := ioutil.ReadAll(response.Body)
  89. if err != nil {
  90. return nil, fmt.Errorf("unable to read downloaded icon: %v", err)
  91. }
  92. if len(body) == 0 {
  93. return nil, fmt.Errorf("downloaded icon is empty, iconURL=%s", iconURL)
  94. }
  95. icon := &model.Icon{
  96. Hash: crypto.HashFromBytes(body),
  97. MimeType: response.ContentType,
  98. Content: body,
  99. }
  100. return icon, nil
  101. }
  102. func parseImageDataURL(value string) (*model.Icon, error) {
  103. colon := strings.Index(value, ":")
  104. semicolon := strings.Index(value, ";")
  105. comma := strings.Index(value, ",")
  106. if colon <= 0 || semicolon <= 0 || comma <= 0 {
  107. return nil, fmt.Errorf(`icon: invalid data url "%s"`, value)
  108. }
  109. mimeType := value[colon+1 : semicolon]
  110. encoding := value[semicolon+1 : comma]
  111. data := value[comma+1:]
  112. if encoding != "base64" {
  113. return nil, fmt.Errorf(`icon: unsupported data url encoding "%s"`, value)
  114. }
  115. if !strings.HasPrefix(mimeType, "image/") {
  116. return nil, fmt.Errorf(`icon: invalid mime type "%s"`, mimeType)
  117. }
  118. blob, err := base64.StdEncoding.DecodeString(data)
  119. if err != nil {
  120. return nil, fmt.Errorf(`icon: invalid data "%s" (%v)`, value, err)
  121. }
  122. if len(blob) == 0 {
  123. return nil, fmt.Errorf(`icon: empty data "%s"`, value)
  124. }
  125. icon := &model.Icon{
  126. Hash: crypto.HashFromBytes(blob),
  127. Content: blob,
  128. MimeType: mimeType,
  129. }
  130. return icon, nil
  131. }