finder.go 3.5 KB

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