icon.go 3.7 KB

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