icon.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. "time"
  10. "miniflux.app/model"
  11. "miniflux.app/timer"
  12. )
  13. // HasIcon checks if the given feed has an icon.
  14. func (s *Storage) HasIcon(feedID int64) bool {
  15. var result int
  16. query := `SELECT count(*) as c FROM feed_icons WHERE feed_id=$1`
  17. s.db.QueryRow(query, feedID).Scan(&result)
  18. return result == 1
  19. }
  20. // IconByID returns an icon by the ID.
  21. func (s *Storage) IconByID(iconID int64) (*model.Icon, error) {
  22. defer timer.ExecutionTime(time.Now(), "[Storage:IconByID]")
  23. var icon model.Icon
  24. query := `SELECT id, hash, mime_type, content FROM icons WHERE id=$1`
  25. err := s.db.QueryRow(query, iconID).Scan(&icon.ID, &icon.Hash, &icon.MimeType, &icon.Content)
  26. if err == sql.ErrNoRows {
  27. return nil, nil
  28. } else if err != nil {
  29. return nil, fmt.Errorf("Unable to fetch icon by hash: %v", err)
  30. }
  31. return &icon, nil
  32. }
  33. // IconByFeedID returns a feed icon.
  34. func (s *Storage) IconByFeedID(userID, feedID int64) (*model.Icon, error) {
  35. defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:IconByFeedID] userID=%d, feedID=%d", userID, feedID))
  36. query := `
  37. SELECT
  38. icons.id, icons.hash, icons.mime_type, icons.content
  39. FROM icons
  40. LEFT JOIN feed_icons ON feed_icons.icon_id=icons.id
  41. LEFT JOIN feeds ON feeds.id=feed_icons.feed_id
  42. WHERE 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("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. defer timer.ExecutionTime(time.Now(), "[Storage:IconByHash]")
  55. err := s.db.QueryRow(`SELECT id FROM icons WHERE hash=$1`, icon.Hash).Scan(&icon.ID)
  56. if err == sql.ErrNoRows {
  57. return nil
  58. } else if err != nil {
  59. return fmt.Errorf("Unable to fetch icon by hash: %v", err)
  60. }
  61. return nil
  62. }
  63. // CreateIcon creates a new icon.
  64. func (s *Storage) CreateIcon(icon *model.Icon) error {
  65. defer timer.ExecutionTime(time.Now(), "[Storage:CreateIcon]")
  66. query := `
  67. INSERT INTO icons
  68. (hash, mime_type, content)
  69. VALUES
  70. ($1, $2, $3)
  71. RETURNING id
  72. `
  73. err := s.db.QueryRow(
  74. query,
  75. icon.Hash,
  76. normalizeMimeType(icon.MimeType),
  77. icon.Content,
  78. ).Scan(&icon.ID)
  79. if err != nil {
  80. return fmt.Errorf("Unable to create icon: %v", err)
  81. }
  82. return nil
  83. }
  84. // CreateFeedIcon creates an icon and associate the icon to the given feed.
  85. func (s *Storage) CreateFeedIcon(feedID int64, icon *model.Icon) error {
  86. defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:CreateFeedIcon] feedID=%d", feedID))
  87. err := s.IconByHash(icon)
  88. if err != nil {
  89. return err
  90. }
  91. if icon.ID == 0 {
  92. err := s.CreateIcon(icon)
  93. if err != nil {
  94. return err
  95. }
  96. }
  97. _, err = s.db.Exec(`INSERT INTO feed_icons (feed_id, icon_id) VALUES ($1, $2)`, feedID, icon.ID)
  98. if err != nil {
  99. return fmt.Errorf("unable to create feed icon: %v", err)
  100. }
  101. return nil
  102. }
  103. // Icons returns all icons tht belongs to a user.
  104. func (s *Storage) Icons(userID int64) (model.Icons, error) {
  105. defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:Icons] userID=%d", userID))
  106. query := `
  107. SELECT
  108. icons.id, icons.hash, icons.mime_type, 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 feeds.user_id=$1
  113. `
  114. rows, err := s.db.Query(query, userID)
  115. if err != nil {
  116. return nil, fmt.Errorf("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("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. }