2017-11-20 06:10:04 +01:00
|
|
|
// Copyright 2017 Frédéric Guillot. All rights reserved.
|
|
|
|
// Use of this source code is governed by the Apache 2.0
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2018-08-25 06:51:50 +02:00
|
|
|
package storage // import "miniflux.app/storage"
|
2017-11-20 06:10:04 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-11-22 01:08:43 +01:00
|
|
|
|
2018-08-25 06:51:50 +02:00
|
|
|
"miniflux.app/model"
|
2017-11-20 06:10:04 +01:00
|
|
|
)
|
|
|
|
|
2017-11-22 01:08:43 +01:00
|
|
|
// GetEnclosures returns all attachments for the given entry.
|
2017-11-20 06:10:04 +01:00
|
|
|
func (s *Storage) GetEnclosures(entryID int64) (model.EnclosureList, error) {
|
2019-10-30 06:48:07 +01:00
|
|
|
query := `
|
|
|
|
SELECT
|
|
|
|
id,
|
|
|
|
user_id,
|
|
|
|
entry_id,
|
|
|
|
url,
|
|
|
|
size,
|
|
|
|
mime_type
|
|
|
|
FROM
|
|
|
|
enclosures
|
|
|
|
WHERE
|
|
|
|
entry_id = $1
|
|
|
|
ORDER BY id ASC
|
|
|
|
`
|
2017-11-20 06:10:04 +01:00
|
|
|
|
|
|
|
rows, err := s.db.Query(query, entryID)
|
|
|
|
if err != nil {
|
2019-10-30 06:48:07 +01:00
|
|
|
return nil, fmt.Errorf(`store: unable to fetch enclosures: %v`, err)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
enclosures := make(model.EnclosureList, 0)
|
|
|
|
for rows.Next() {
|
|
|
|
var enclosure model.Enclosure
|
|
|
|
err := rows.Scan(
|
|
|
|
&enclosure.ID,
|
|
|
|
&enclosure.UserID,
|
|
|
|
&enclosure.EntryID,
|
|
|
|
&enclosure.URL,
|
|
|
|
&enclosure.Size,
|
|
|
|
&enclosure.MimeType,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
2019-10-30 06:48:07 +01:00
|
|
|
return nil, fmt.Errorf(`store: unable to fetch enclosure row: %v`, err)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
enclosures = append(enclosures, &enclosure)
|
|
|
|
}
|
|
|
|
|
|
|
|
return enclosures, nil
|
|
|
|
}
|
|
|
|
|
2017-11-22 01:08:43 +01:00
|
|
|
// CreateEnclosure creates a new attachment.
|
2017-11-20 06:10:04 +01:00
|
|
|
func (s *Storage) CreateEnclosure(enclosure *model.Enclosure) error {
|
2020-01-31 06:08:11 +01:00
|
|
|
if enclosure.URL == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-20 06:10:04 +01:00
|
|
|
query := `
|
|
|
|
INSERT INTO enclosures
|
2019-10-30 06:48:07 +01:00
|
|
|
(url, size, mime_type, entry_id, user_id)
|
2017-11-20 06:10:04 +01:00
|
|
|
VALUES
|
2019-10-30 06:48:07 +01:00
|
|
|
($1, $2, $3, $4, $5)
|
|
|
|
RETURNING
|
|
|
|
id
|
2017-11-20 06:10:04 +01:00
|
|
|
`
|
|
|
|
err := s.db.QueryRow(
|
|
|
|
query,
|
|
|
|
enclosure.URL,
|
|
|
|
enclosure.Size,
|
|
|
|
enclosure.MimeType,
|
|
|
|
enclosure.EntryID,
|
|
|
|
enclosure.UserID,
|
|
|
|
).Scan(&enclosure.ID)
|
|
|
|
|
|
|
|
if err != nil {
|
2019-10-30 06:48:07 +01:00
|
|
|
return fmt.Errorf(`store: unable to create enclosure %q: %v`, enclosure.URL, err)
|
2017-11-22 01:08:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsEnclosureExists checks if an attachment exists.
|
|
|
|
func (s *Storage) IsEnclosureExists(enclosure *model.Enclosure) bool {
|
|
|
|
var result int
|
|
|
|
query := `SELECT count(*) as c FROM enclosures WHERE user_id=$1 AND entry_id=$2 AND url=$3`
|
|
|
|
s.db.QueryRow(query, enclosure.UserID, enclosure.EntryID, enclosure.URL).Scan(&result)
|
|
|
|
return result >= 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateEnclosures add missing attachments while updating a feed.
|
|
|
|
func (s *Storage) UpdateEnclosures(enclosures model.EnclosureList) error {
|
|
|
|
for _, enclosure := range enclosures {
|
|
|
|
if !s.IsEnclosureExists(enclosure) {
|
|
|
|
err := s.CreateEnclosure(enclosure)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|