icon.go 3.8 KB

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