icon.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 checks if the given feed has an icon.
  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 returns an icon by the ID.
  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. if err == sql.ErrNoRows {
  32. return nil, nil
  33. } else if err != nil {
  34. return nil, fmt.Errorf("store: unable to fetch icon #%d: %w", iconID, err)
  35. }
  36. return &icon, nil
  37. }
  38. // IconByExternalID returns an icon by the External Icon ID.
  39. func (s *Storage) IconByExternalID(externalIconID string) (*model.Icon, error) {
  40. var icon model.Icon
  41. query := `
  42. SELECT
  43. id,
  44. hash,
  45. mime_type,
  46. content,
  47. external_id
  48. FROM icons
  49. WHERE external_id=$1
  50. `
  51. err := s.db.QueryRow(query, externalIconID).Scan(&icon.ID, &icon.Hash, &icon.MimeType, &icon.Content, &icon.ExternalID)
  52. if err == sql.ErrNoRows {
  53. return nil, nil
  54. } else if err != nil {
  55. return nil, fmt.Errorf("store: unable to fetch icon %s: %w", externalIconID, err)
  56. }
  57. return &icon, nil
  58. }
  59. // IconByFeedID returns a feed icon.
  60. func (s *Storage) IconByFeedID(userID, feedID int64) (*model.Icon, error) {
  61. query := `
  62. SELECT
  63. icons.id,
  64. icons.hash,
  65. icons.mime_type,
  66. icons.content,
  67. icons.external_id
  68. FROM icons
  69. LEFT JOIN feed_icons ON feed_icons.icon_id=icons.id
  70. LEFT JOIN feeds ON feeds.id=feed_icons.feed_id
  71. WHERE
  72. feeds.user_id=$1 AND feeds.id=$2
  73. LIMIT 1
  74. `
  75. var icon model.Icon
  76. err := s.db.QueryRow(query, userID, feedID).Scan(&icon.ID, &icon.Hash, &icon.MimeType, &icon.Content, &icon.ExternalID)
  77. if err != nil {
  78. return nil, fmt.Errorf(`store: unable to fetch icon: %v`, err)
  79. }
  80. return &icon, nil
  81. }
  82. // StoreFeedIcon creates or updates a feed icon.
  83. func (s *Storage) StoreFeedIcon(feedID int64, icon *model.Icon) error {
  84. tx, err := s.db.Begin()
  85. if err != nil {
  86. return fmt.Errorf(`store: unable to start transaction: %v`, err)
  87. }
  88. if err := tx.QueryRow(`SELECT id FROM icons WHERE hash=$1`, icon.Hash).Scan(&icon.ID); err == sql.ErrNoRows {
  89. query := `
  90. INSERT INTO icons
  91. (hash, mime_type, content, external_id)
  92. VALUES
  93. ($1, $2, $3, $4)
  94. RETURNING
  95. id
  96. `
  97. err := tx.QueryRow(
  98. query,
  99. icon.Hash,
  100. normalizeMimeType(icon.MimeType),
  101. icon.Content,
  102. crypto.GenerateRandomStringHex(20),
  103. ).Scan(&icon.ID)
  104. if err != nil {
  105. tx.Rollback()
  106. return fmt.Errorf(`store: unable to create icon: %v`, err)
  107. }
  108. } else if err != nil {
  109. tx.Rollback()
  110. return fmt.Errorf(`store: unable to fetch icon by hash %q: %v`, icon.Hash, err)
  111. }
  112. if _, err := tx.Exec(`DELETE FROM feed_icons WHERE feed_id=$1`, feedID); err != nil {
  113. tx.Rollback()
  114. return fmt.Errorf(`store: unable to delete feed icon: %v`, err)
  115. }
  116. if _, err := tx.Exec(`INSERT INTO feed_icons (feed_id, icon_id) VALUES ($1, $2)`, feedID, icon.ID); err != nil {
  117. tx.Rollback()
  118. return fmt.Errorf(`store: unable to associate feed and icon: %v`, err)
  119. }
  120. if err := tx.Commit(); err != nil {
  121. return fmt.Errorf(`store: unable to commit transaction: %v`, err)
  122. }
  123. return nil
  124. }
  125. // Icons returns all icons that belongs to a user.
  126. func (s *Storage) Icons(userID int64) (model.Icons, error) {
  127. query := `
  128. SELECT
  129. icons.id,
  130. icons.hash,
  131. icons.mime_type,
  132. icons.content,
  133. icons.external_id
  134. FROM icons
  135. LEFT JOIN feed_icons ON feed_icons.icon_id=icons.id
  136. LEFT JOIN feeds ON feeds.id=feed_icons.feed_id
  137. WHERE
  138. feeds.user_id=$1
  139. `
  140. rows, err := s.db.Query(query, userID)
  141. if err != nil {
  142. return nil, fmt.Errorf(`store: unable to fetch icons: %v`, err)
  143. }
  144. defer rows.Close()
  145. var icons model.Icons
  146. for rows.Next() {
  147. var icon model.Icon
  148. err := rows.Scan(&icon.ID, &icon.Hash, &icon.MimeType, &icon.Content, &icon.ExternalID)
  149. if err != nil {
  150. return nil, fmt.Errorf(`store: unable to fetch icons row: %v`, err)
  151. }
  152. icons = append(icons, &icon)
  153. }
  154. return icons, nil
  155. }
  156. func normalizeMimeType(mimeType string) string {
  157. mimeType = strings.ToLower(mimeType)
  158. switch mimeType {
  159. case "image/png", "image/jpeg", "image/jpg", "image/webp", "image/svg+xml", "image/x-icon", "image/gif":
  160. return mimeType
  161. default:
  162. return "image/x-icon"
  163. }
  164. }