icon.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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
  5. import (
  6. "database/sql"
  7. "fmt"
  8. "strings"
  9. "time"
  10. "github.com/miniflux/miniflux2/helper"
  11. "github.com/miniflux/miniflux2/model"
  12. )
  13. func (s *Storage) HasIcon(feedID int64) bool {
  14. var result int
  15. query := `SELECT count(*) as c FROM feed_icons WHERE feed_id=$1`
  16. s.db.QueryRow(query, feedID).Scan(&result)
  17. return result == 1
  18. }
  19. func (s *Storage) GetIconByID(iconID int64) (*model.Icon, error) {
  20. defer helper.ExecutionTime(time.Now(), "[Storage:GetIconByID]")
  21. var icon model.Icon
  22. query := `SELECT id, hash, mime_type, content FROM icons WHERE id=$1`
  23. err := s.db.QueryRow(query, iconID).Scan(&icon.ID, &icon.Hash, &icon.MimeType, &icon.Content)
  24. if err == sql.ErrNoRows {
  25. return nil, nil
  26. } else if err != nil {
  27. return nil, fmt.Errorf("Unable to fetch icon by hash: %v", err)
  28. }
  29. return &icon, nil
  30. }
  31. func (s *Storage) GetIconByHash(icon *model.Icon) error {
  32. defer helper.ExecutionTime(time.Now(), "[Storage:GetIconByHash]")
  33. err := s.db.QueryRow(`SELECT id FROM icons WHERE hash=$1`, icon.Hash).Scan(&icon.ID)
  34. if err == sql.ErrNoRows {
  35. return nil
  36. } else if err != nil {
  37. return fmt.Errorf("Unable to fetch icon by hash: %v", err)
  38. }
  39. return nil
  40. }
  41. func (s *Storage) CreateIcon(icon *model.Icon) error {
  42. defer helper.ExecutionTime(time.Now(), "[Storage:CreateIcon]")
  43. query := `
  44. INSERT INTO icons
  45. (hash, mime_type, content)
  46. VALUES
  47. ($1, $2, $3)
  48. RETURNING id
  49. `
  50. err := s.db.QueryRow(
  51. query,
  52. icon.Hash,
  53. normalizeMimeType(icon.MimeType),
  54. icon.Content,
  55. ).Scan(&icon.ID)
  56. if err != nil {
  57. return fmt.Errorf("Unable to create icon: %v", err)
  58. }
  59. return nil
  60. }
  61. func (s *Storage) CreateFeedIcon(feed *model.Feed, icon *model.Icon) error {
  62. defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Storage:CreateFeedIcon] feedID=%d", feed.ID))
  63. err := s.GetIconByHash(icon)
  64. if err != nil {
  65. return err
  66. }
  67. if icon.ID == 0 {
  68. err := s.CreateIcon(icon)
  69. if err != nil {
  70. return err
  71. }
  72. }
  73. _, err = s.db.Exec(`INSERT INTO feed_icons (feed_id, icon_id) VALUES ($1, $2)`, feed.ID, icon.ID)
  74. if err != nil {
  75. return fmt.Errorf("unable to create feed icon: %v", err)
  76. }
  77. return nil
  78. }
  79. func normalizeMimeType(mimeType string) string {
  80. mimeType = strings.ToLower(mimeType)
  81. switch mimeType {
  82. case "image/png", "image/jpeg", "image/jpg", "image/webp", "image/svg+xml", "image/x-icon", "image/gif":
  83. return mimeType
  84. default:
  85. return "image/x-icon"
  86. }
  87. }