icon.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. // HasFeedIcon checks if the given feed has an icon.
  11. func (s *Storage) HasFeedIcon(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. // StoreFeedIcon creates or updates a feed icon.
  52. func (s *Storage) StoreFeedIcon(feedID int64, icon *model.Icon) error {
  53. tx, err := s.db.Begin()
  54. if err != nil {
  55. return fmt.Errorf(`store: unable to start transaction: %v`, err)
  56. }
  57. if err := tx.QueryRow(`SELECT id FROM icons WHERE hash=$1`, icon.Hash).Scan(&icon.ID); err == sql.ErrNoRows {
  58. query := `
  59. INSERT INTO icons
  60. (hash, mime_type, content)
  61. VALUES
  62. ($1, $2, $3)
  63. RETURNING
  64. id
  65. `
  66. err := tx.QueryRow(
  67. query,
  68. icon.Hash,
  69. normalizeMimeType(icon.MimeType),
  70. icon.Content,
  71. ).Scan(&icon.ID)
  72. if err != nil {
  73. tx.Rollback()
  74. return fmt.Errorf(`store: unable to create icon: %v`, err)
  75. }
  76. } else if err != nil {
  77. tx.Rollback()
  78. return fmt.Errorf(`store: unable to fetch icon by hash %q: %v`, icon.Hash, err)
  79. }
  80. if _, err := tx.Exec(`DELETE FROM feed_icons WHERE feed_id=$1`, feedID); err != nil {
  81. tx.Rollback()
  82. return fmt.Errorf(`store: unable to delete feed icon: %v`, err)
  83. }
  84. if _, err := tx.Exec(`INSERT INTO feed_icons (feed_id, icon_id) VALUES ($1, $2)`, feedID, icon.ID); err != nil {
  85. tx.Rollback()
  86. return fmt.Errorf(`store: unable to associate feed and icon: %v`, err)
  87. }
  88. if err := tx.Commit(); err != nil {
  89. return fmt.Errorf(`store: unable to commit transaction: %v`, err)
  90. }
  91. return nil
  92. }
  93. // Icons returns all icons that belongs to a user.
  94. func (s *Storage) Icons(userID int64) (model.Icons, error) {
  95. query := `
  96. SELECT
  97. icons.id,
  98. icons.hash,
  99. icons.mime_type,
  100. icons.content
  101. FROM icons
  102. LEFT JOIN feed_icons ON feed_icons.icon_id=icons.id
  103. LEFT JOIN feeds ON feeds.id=feed_icons.feed_id
  104. WHERE
  105. feeds.user_id=$1
  106. `
  107. rows, err := s.db.Query(query, userID)
  108. if err != nil {
  109. return nil, fmt.Errorf(`store: unable to fetch icons: %v`, err)
  110. }
  111. defer rows.Close()
  112. var icons model.Icons
  113. for rows.Next() {
  114. var icon model.Icon
  115. err := rows.Scan(&icon.ID, &icon.Hash, &icon.MimeType, &icon.Content)
  116. if err != nil {
  117. return nil, fmt.Errorf(`store: unable to fetch icons row: %v`, err)
  118. }
  119. icons = append(icons, &icon)
  120. }
  121. return icons, nil
  122. }
  123. func normalizeMimeType(mimeType string) string {
  124. mimeType = strings.ToLower(mimeType)
  125. switch mimeType {
  126. case "image/png", "image/jpeg", "image/jpg", "image/webp", "image/svg+xml", "image/x-icon", "image/gif":
  127. return mimeType
  128. default:
  129. return "image/x-icon"
  130. }
  131. }