finder.go 3.6 KB

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