icon.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 storage // import "miniflux.app/storage"
  5. import (
  6. "database/sql"
  7. "fmt"
  8. "strings"
  9. "miniflux.app/model"
  10. )
  11. // HasIcon checks if the given feed has an icon.
  12. func (s *Storage) HasIcon(feedID int64) bool {
  13. var result bool
  14. query := `SELECT true FROM feed_icons WHERE feed_id=$1`
  15. s.db.QueryRow(query, feedID).Scan(&result)
  16. return result
  17. }
  18. // IconByID returns an icon by the ID.
  19. func (s *Storage) IconByID(iconID int64) (*model.Icon, error) {
  20. var icon model.Icon
  21. query := `SELECT id, hash, mime_type, content FROM icons WHERE id=$1`
  22. err := s.db.QueryRow(query, iconID).Scan(&icon.ID, &icon.Hash, &icon.MimeType, &icon.Content)
  23. if err == sql.ErrNoRows {
  24. return nil, nil
  25. } else if err != nil {
  26. return nil, fmt.Errorf("Unable to fetch icon by hash: %v", err)
  27. }
  28. return &icon, nil
  29. }
  30. // IconByFeedID returns a feed icon.
  31. func (s *Storage) IconByFeedID(userID, feedID int64) (*model.Icon, error) {
  32. query := `
  33. SELECT
  34. icons.id,
  35. icons.hash,
  36. icons.mime_type,
  37. icons.content
  38. FROM icons
  39. LEFT JOIN feed_icons ON feed_icons.icon_id=icons.id
  40. LEFT JOIN feeds ON feeds.id=feed_icons.feed_id
  41. WHERE
  42. feeds.user_id=$1 AND feeds.id=$2
  43. LIMIT 1
  44. `
  45. var icon model.Icon
  46. err := s.db.QueryRow(query, userID, feedID).Scan(&icon.ID, &icon.Hash, &icon.MimeType, &icon.Content)
  47. if err != nil {
  48. return nil, fmt.Errorf(`store: unable to fetch icon: %v`, err)
  49. }
  50. return &icon, nil
  51. }
  52. // IconByHash returns an icon by the hash (checksum).
  53. func (s *Storage) IconByHash(icon *model.Icon) error {
  54. err := s.db.QueryRow(`SELECT id FROM icons WHERE hash=$1`, icon.Hash).Scan(&icon.ID)
  55. if err == sql.ErrNoRows {
  56. return nil
  57. } else if err != nil {
  58. return fmt.Errorf(`store: unable to fetch icon by hash: %v`, err)
  59. }
  60. return nil
  61. }
  62. // CreateIcon creates a new icon.
  63. func (s *Storage) CreateIcon(icon *model.Icon) error {
  64. query := `
  65. INSERT INTO icons
  66. (hash, mime_type, content)
  67. VALUES
  68. ($1, $2, $3)
  69. RETURNING
  70. id
  71. `
  72. err := s.db.QueryRow(
  73. query,
  74. icon.Hash,
  75. normalizeMimeType(icon.MimeType),
  76. icon.Content,
  77. ).Scan(&icon.ID)
  78. if err != nil {
  79. return fmt.Errorf(`store: unable to create icon: %v`, err)
  80. }
  81. return nil
  82. }
  83. // CreateFeedIcon creates an icon and associate the icon to the given feed.
  84. func (s *Storage) CreateFeedIcon(feedID int64, icon *model.Icon) error {
  85. err := s.IconByHash(icon)
  86. if err != nil {
  87. return err
  88. }
  89. if icon.ID == 0 {
  90. err := s.CreateIcon(icon)
  91. if err != nil {
  92. return err
  93. }
  94. }
  95. _, err = s.db.Exec(`INSERT INTO feed_icons (feed_id, icon_id) VALUES ($1, $2)`, feedID, icon.ID)
  96. if err != nil {
  97. return fmt.Errorf(`store: unable to create feed icon: %v`, err)
  98. }
  99. return nil
  100. }
  101. // Icons returns all icons tht belongs to a user.
  102. func (s *Storage) Icons(userID int64) (model.Icons, error) {
  103. query := `
  104. SELECT
  105. icons.id,
  106. icons.hash,
  107. icons.mime_type,
  108. icons.content
  109. FROM icons
  110. LEFT JOIN feed_icons ON feed_icons.icon_id=icons.id
  111. LEFT JOIN feeds ON feeds.id=feed_icons.feed_id
  112. WHERE
  113. feeds.user_id=$1
  114. `
  115. rows, err := s.db.Query(query, userID)
  116. if err != nil {
  117. return nil, fmt.Errorf(`store: unable to fetch icons: %v`, err)
  118. }
  119. defer rows.Close()
  120. var icons model.Icons
  121. for rows.Next() {
  122. var icon model.Icon
  123. err := rows.Scan(&icon.ID, &icon.Hash, &icon.MimeType, &icon.Content)
  124. if err != nil {
  125. return nil, fmt.Errorf(`store: unable to fetch icons row: %v`, err)
  126. }
  127. icons = append(icons, &icon)
  128. }
  129. return icons, nil
  130. }
  131. func normalizeMimeType(mimeType string) string {
  132. mimeType = strings.ToLower(mimeType)
  133. switch mimeType {
  134. case "image/png", "image/jpeg", "image/jpg", "image/webp", "image/svg+xml", "image/x-icon", "image/gif":
  135. return mimeType
  136. default:
  137. return "image/x-icon"
  138. }
  139. }