miniflux/internal/storage/icon.go

160 lines
3.8 KiB
Go
Raw Normal View History

// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
2017-11-20 06:10:04 +01:00
package storage // import "miniflux.app/v2/internal/storage"
2017-11-20 06:10:04 +01:00
import (
"database/sql"
"fmt"
"strings"
"miniflux.app/v2/internal/model"
2017-11-20 06:10:04 +01:00
)
2017-11-28 06:30:04 +01:00
// HasIcon checks if the given feed has an icon.
2017-11-20 06:10:04 +01:00
func (s *Storage) HasIcon(feedID int64) bool {
2019-10-30 06:48:07 +01:00
var result bool
query := `SELECT true FROM feed_icons WHERE feed_id=$1`
2017-11-20 06:10:04 +01:00
s.db.QueryRow(query, feedID).Scan(&result)
2019-10-30 06:48:07 +01:00
return result
2017-11-20 06:10:04 +01:00
}
2017-11-28 06:30:04 +01:00
// IconByID returns an icon by the ID.
func (s *Storage) IconByID(iconID int64) (*model.Icon, error) {
2017-11-20 06:10:04 +01:00
var icon model.Icon
query := `SELECT id, hash, mime_type, content FROM icons WHERE id=$1`
err := s.db.QueryRow(query, iconID).Scan(&icon.ID, &icon.Hash, &icon.MimeType, &icon.Content)
if err == sql.ErrNoRows {
return nil, nil
} else if err != nil {
return nil, fmt.Errorf("store: unable to fetch icon by hash: %v", err)
2017-11-20 06:10:04 +01:00
}
return &icon, nil
}
// IconByFeedID returns a feed icon.
func (s *Storage) IconByFeedID(userID, feedID int64) (*model.Icon, error) {
query := `
SELECT
2019-10-30 06:48:07 +01:00
icons.id,
icons.hash,
icons.mime_type,
icons.content
FROM icons
LEFT JOIN feed_icons ON feed_icons.icon_id=icons.id
LEFT JOIN feeds ON feeds.id=feed_icons.feed_id
2019-10-30 06:48:07 +01:00
WHERE
feeds.user_id=$1 AND feeds.id=$2
LIMIT 1
`
var icon model.Icon
err := s.db.QueryRow(query, userID, feedID).Scan(&icon.ID, &icon.Hash, &icon.MimeType, &icon.Content)
if err != nil {
2019-10-30 06:48:07 +01:00
return nil, fmt.Errorf(`store: unable to fetch icon: %v`, err)
}
return &icon, nil
}
2017-11-28 06:30:04 +01:00
// IconByHash returns an icon by the hash (checksum).
func (s *Storage) IconByHash(icon *model.Icon) error {
2017-11-20 06:10:04 +01:00
err := s.db.QueryRow(`SELECT id FROM icons WHERE hash=$1`, icon.Hash).Scan(&icon.ID)
if err == sql.ErrNoRows {
return nil
} else if err != nil {
2019-10-30 06:48:07 +01:00
return fmt.Errorf(`store: unable to fetch icon by hash: %v`, err)
2017-11-20 06:10:04 +01:00
}
return nil
}
2017-11-28 06:30:04 +01:00
// CreateIcon creates a new icon.
2017-11-20 06:10:04 +01:00
func (s *Storage) CreateIcon(icon *model.Icon) error {
query := `
INSERT INTO icons
2019-10-30 06:48:07 +01:00
(hash, mime_type, content)
2017-11-20 06:10:04 +01:00
VALUES
2019-10-30 06:48:07 +01:00
($1, $2, $3)
RETURNING
id
2017-11-20 06:10:04 +01:00
`
err := s.db.QueryRow(
query,
icon.Hash,
normalizeMimeType(icon.MimeType),
icon.Content,
).Scan(&icon.ID)
if err != nil {
2019-10-30 06:48:07 +01:00
return fmt.Errorf(`store: unable to create icon: %v`, err)
2017-11-20 06:10:04 +01:00
}
return nil
}
2017-11-28 06:30:04 +01:00
// CreateFeedIcon creates an icon and associate the icon to the given feed.
func (s *Storage) CreateFeedIcon(feedID int64, icon *model.Icon) error {
2017-11-28 06:30:04 +01:00
err := s.IconByHash(icon)
2017-11-20 06:10:04 +01:00
if err != nil {
return err
}
if icon.ID == 0 {
err := s.CreateIcon(icon)
if err != nil {
return err
}
}
_, err = s.db.Exec(`INSERT INTO feed_icons (feed_id, icon_id) VALUES ($1, $2)`, feedID, icon.ID)
2017-11-20 06:10:04 +01:00
if err != nil {
2019-10-30 06:48:07 +01:00
return fmt.Errorf(`store: unable to create feed icon: %v`, err)
2017-11-20 06:10:04 +01:00
}
return nil
}
2021-05-13 15:38:32 +02:00
// Icons returns all icons that belongs to a user.
2017-12-04 02:44:27 +01:00
func (s *Storage) Icons(userID int64) (model.Icons, error) {
query := `
SELECT
2019-10-30 06:48:07 +01:00
icons.id,
icons.hash,
icons.mime_type,
icons.content
2017-12-04 02:44:27 +01:00
FROM icons
LEFT JOIN feed_icons ON feed_icons.icon_id=icons.id
LEFT JOIN feeds ON feeds.id=feed_icons.feed_id
2019-10-30 06:48:07 +01:00
WHERE
feeds.user_id=$1
2017-12-04 02:44:27 +01:00
`
rows, err := s.db.Query(query, userID)
if err != nil {
2019-10-30 06:48:07 +01:00
return nil, fmt.Errorf(`store: unable to fetch icons: %v`, err)
2017-12-04 02:44:27 +01:00
}
defer rows.Close()
var icons model.Icons
for rows.Next() {
var icon model.Icon
err := rows.Scan(&icon.ID, &icon.Hash, &icon.MimeType, &icon.Content)
if err != nil {
2019-10-30 06:48:07 +01:00
return nil, fmt.Errorf(`store: unable to fetch icons row: %v`, err)
2017-12-04 02:44:27 +01:00
}
icons = append(icons, &icon)
}
return icons, nil
}
2017-11-20 06:10:04 +01:00
func normalizeMimeType(mimeType string) string {
mimeType = strings.ToLower(mimeType)
switch mimeType {
case "image/png", "image/jpeg", "image/jpg", "image/webp", "image/svg+xml", "image/x-icon", "image/gif":
return mimeType
default:
return "image/x-icon"
}
}