icon.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package storage // import "miniflux.app/v2/internal/storage"
  4. import (
  5. "database/sql"
  6. "fmt"
  7. "strings"
  8. "miniflux.app/v2/internal/crypto"
  9. "miniflux.app/v2/internal/model"
  10. )
  11. // HasFeedIcon reports whether the specified feed already has an associated icon record.
  12. func (s *Storage) HasFeedIcon(feedID int64) bool {
  13. var result bool
  14. query := `SELECT true FROM feed_icons WHERE feed_id=$1 LIMIT 1`
  15. s.db.QueryRow(query, feedID).Scan(&result)
  16. return result
  17. }
  18. // IconByID fetches a single icon by its internal identifier, returning nil when it is not found.
  19. func (s *Storage) IconByID(iconID int64) (*model.Icon, error) {
  20. var icon model.Icon
  21. query := `
  22. SELECT
  23. id,
  24. hash,
  25. mime_type,
  26. content,
  27. external_id
  28. FROM icons
  29. WHERE id=$1`
  30. err := s.db.QueryRow(query, iconID).Scan(&icon.ID, &icon.Hash, &icon.MimeType, &icon.Content, &icon.ExternalID)
  31. switch {
  32. case err == sql.ErrNoRows:
  33. return nil, nil
  34. case err != nil:
  35. return nil, fmt.Errorf("store: cannot load icon id=%d: %w", iconID, err)
  36. default:
  37. return &icon, nil
  38. }
  39. }
  40. // IconByExternalID fetches an icon using its external identifier, returning nil when no match exists.
  41. func (s *Storage) IconByExternalID(externalIconID string) (*model.Icon, error) {
  42. var icon model.Icon
  43. query := `
  44. SELECT
  45. id,
  46. hash,
  47. mime_type,
  48. content,
  49. external_id
  50. FROM icons
  51. WHERE external_id=$1
  52. `
  53. err := s.db.QueryRow(query, externalIconID).Scan(&icon.ID, &icon.Hash, &icon.MimeType, &icon.Content, &icon.ExternalID)
  54. switch {
  55. case err == sql.ErrNoRows:
  56. return nil, nil
  57. case err != nil:
  58. return nil, fmt.Errorf("store: cannot load icon external_id=%s: %w", externalIconID, err)
  59. default:
  60. return &icon, nil
  61. }
  62. }
  63. // IconByFeedID returns the icon linked to the given feed for the specified user, or nil if none is set.
  64. func (s *Storage) IconByFeedID(userID, feedID int64) (*model.Icon, error) {
  65. query := `
  66. SELECT
  67. icons.id,
  68. icons.hash,
  69. icons.mime_type,
  70. icons.content,
  71. icons.external_id
  72. FROM icons
  73. LEFT JOIN feed_icons ON feed_icons.icon_id=icons.id
  74. LEFT JOIN feeds ON feeds.id=feed_icons.feed_id
  75. WHERE
  76. feeds.user_id=$1 AND feeds.id=$2
  77. LIMIT 1
  78. `
  79. var icon model.Icon
  80. err := s.db.QueryRow(query, userID, feedID).Scan(&icon.ID, &icon.Hash, &icon.MimeType, &icon.Content, &icon.ExternalID)
  81. switch {
  82. case err == sql.ErrNoRows:
  83. return nil, nil
  84. case err != nil:
  85. return nil, fmt.Errorf("store: cannot load icon for feed_id=%d user_id=%d: %w", feedID, userID, err)
  86. default:
  87. return &icon, nil
  88. }
  89. }
  90. // StoreFeedIcon creates or reuses an icon by hash and associates it with the given feed atomically.
  91. func (s *Storage) StoreFeedIcon(feedID int64, icon *model.Icon) error {
  92. tx, err := s.db.Begin()
  93. if err != nil {
  94. return fmt.Errorf(`store: unable to start transaction: %v`, err)
  95. }
  96. if err := tx.QueryRow(`SELECT id FROM icons WHERE hash=$1`, icon.Hash).Scan(&icon.ID); err == sql.ErrNoRows {
  97. query := `
  98. INSERT INTO icons
  99. (hash, mime_type, content, external_id)
  100. VALUES
  101. ($1, $2, $3, $4)
  102. RETURNING
  103. id
  104. `
  105. err := tx.QueryRow(
  106. query,
  107. icon.Hash,
  108. normalizeMimeType(icon.MimeType),
  109. icon.Content,
  110. crypto.GenerateRandomStringHex(20),
  111. ).Scan(&icon.ID)
  112. if err != nil {
  113. tx.Rollback()
  114. return fmt.Errorf(`store: unable to create icon: %v`, err)
  115. }
  116. } else if err != nil {
  117. tx.Rollback()
  118. return fmt.Errorf(`store: unable to fetch icon by hash %q: %v`, icon.Hash, err)
  119. }
  120. if _, err := tx.Exec(`DELETE FROM feed_icons WHERE feed_id=$1`, feedID); err != nil {
  121. tx.Rollback()
  122. return fmt.Errorf(`store: unable to delete feed icon: %v`, err)
  123. }
  124. if _, err := tx.Exec(`INSERT INTO feed_icons (feed_id, icon_id) VALUES ($1, $2)`, feedID, icon.ID); err != nil {
  125. tx.Rollback()
  126. return fmt.Errorf(`store: unable to associate feed and icon: %v`, err)
  127. }
  128. if err := tx.Commit(); err != nil {
  129. return fmt.Errorf(`store: unable to commit transaction: %v`, err)
  130. }
  131. return nil
  132. }
  133. // Icons lists all icons currently associated with any feed owned by the given user.
  134. func (s *Storage) Icons(userID int64) (model.Icons, error) {
  135. query := `
  136. SELECT
  137. icons.id,
  138. icons.hash,
  139. icons.mime_type,
  140. icons.content,
  141. icons.external_id
  142. FROM icons
  143. LEFT JOIN feed_icons ON feed_icons.icon_id=icons.id
  144. LEFT JOIN feeds ON feeds.id=feed_icons.feed_id
  145. WHERE
  146. feeds.user_id=$1
  147. `
  148. rows, err := s.db.Query(query, userID)
  149. if err != nil {
  150. return nil, fmt.Errorf(`store: unable to fetch icons: %v`, err)
  151. }
  152. defer rows.Close()
  153. var icons model.Icons
  154. for rows.Next() {
  155. var icon model.Icon
  156. err := rows.Scan(&icon.ID, &icon.Hash, &icon.MimeType, &icon.Content, &icon.ExternalID)
  157. if err != nil {
  158. return nil, fmt.Errorf(`store: unable to fetch icons row: %v`, err)
  159. }
  160. icons = append(icons, &icon)
  161. }
  162. return icons, nil
  163. }
  164. func normalizeMimeType(mimeType string) string {
  165. mimeType = strings.ToLower(mimeType)
  166. switch mimeType {
  167. case "image/png", "image/jpeg", "image/jpg", "image/webp", "image/svg+xml", "image/x-icon", "image/gif":
  168. return mimeType
  169. default:
  170. return "image/x-icon"
  171. }
  172. }