1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-08-11 17:51:01 +00:00

Add "Share article" feature

A new "shareCode" field is generated for each entry, and allows
unlogged users to access the entry through the /shared endpoint.
This feature is particularly useful to share articles from miniflux
to third-party users without having them to visit the original source.

The image proxy is disabled and special cache headers are proposed in
the shared page to avoid denial of service.
This commit is contained in:
Lesterpig 2019-10-05 13:30:25 +02:00 committed by Frédéric Guillot
parent 1b86913c00
commit 41a2b7e58e
24 changed files with 243 additions and 26 deletions

View file

@ -9,6 +9,7 @@ import (
"fmt"
"time"
"miniflux.app/crypto"
"miniflux.app/logger"
"miniflux.app/model"
@ -351,3 +352,35 @@ func (s *Storage) EntryURLExists(feedID int64, entryURL string) bool {
s.db.QueryRow(query, feedID, entryURL).Scan(&result)
return result
}
// GetEntryShareCode returns the share code of the provided entry.
// It generates a new one if not already defined.
func (s *Storage) GetEntryShareCode(userID int64, entryID int64) (shareCode string, err error) {
query := `SELECT share_code FROM entries WHERE user_id=$1 AND id=$2`
err = s.db.QueryRow(query, userID, entryID).Scan(&shareCode)
if err != nil || shareCode != "" {
return
}
shareCode = crypto.GenerateRandomStringHex(16)
query = `UPDATE entries SET share_code = $1 WHERE user_id=$2 AND id=$3`
result, err := s.db.Exec(query, shareCode, userID, entryID)
if err != nil {
err = fmt.Errorf(`store: unable to set share_code for entry #%d: %v`, entryID, err)
return
}
count, err := result.RowsAffected()
if err != nil {
err = fmt.Errorf(`store: unable to set share_code for entry #%d: %v`, entryID, err)
return
}
if count == 0 {
err = errors.New(`store: nothing has been updated`)
}
return
}