sonda/internal/images/service.go
tgiachi 89826fcd07 feat(federation): shared libraries and the signed read API for friends
A friend's server can now read what an admin chose to share.

- A library has a shared flag, off by default (migration 0024), set with
  PATCH /libraries/{id}. Changing it never waits for a scan.
- Requests between friends are signed with each server's Ed25519 key: the
  method, the path and query from /api/v1/peer on, a date, a nonce and the
  hash of the empty body. A date more than two minutes off, a nonce used
  before, an unknown or removed key and a bad signature all get the same
  401. A friend still waiting for approval gets 403, and while federation is
  off the routes are missing.
- Routes under /api/v1/peer: info, artists, albums, tracks, covers and
  streaming with Range. The catalogue, the counts and the format come from
  the shared libraries only, and the file served is the one in a shared
  library. No path, library id, listen, taste, user or provider data is part
  of an answer.
- A friend never makes this server build a cover, read an audio file or ask
  a provider: only covers already built from the audio files are served.
- Per-friend limits, set by the admin: parallel streams (default 3) and KiB
  a second (default 4096, 0 for none), shared by all the streams of a friend.
- A log of the last requests friends made (route, status, bytes; never the
  query), kept for 7 days, in GET /federation/requests.
- POST /federation/friends/{id}/check lets the side that pasted an invite
  find out that the other admin approved; a signed call from a friend this
  server is waiting for makes it active too.
- Admin: a Shared with friends switch on each library, which asks first, and
  limits, recent requests and a Check button in the Federation section, in
  English and Italian.
- OpenAPI paths and schemas, contract test, two real servers checked end to
  end with tagged audio files.
2026-11-13 15:04:07 +01:00

767 lines
27 KiB
Go

package images
import (
"context"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"io"
"io/fs"
"os"
"path"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"sync"
"time"
"go.senan.xyz/taglib"
"golang.org/x/sync/singleflight"
)
// Kinds of owner a picture belongs to.
const (
kindAlbum = "album"
kindArtist = "artist"
folderAlbumRemote = "album-remote" // the covers from the archive
)
// What is stored about a picture, and where it came from.
const (
StatusOK = "ok" // a cover was found and is stored
StatusNone = "none" // the files were searched and have none
StatusStale = "stale" // a file of the album changed: look again
SourceEmbedded = "embedded" // the picture inside an audio file
SourceFolder = "folder" // a cover.jpg, folder.png ... next to the audio
SourceCommons = "commons" // a free picture of an artist from Wikimedia Commons
SourceCoverArt = "coverartarchive" // the cover of an album from the Cover Art Archive
)
// Tuning.
const (
// MaxFilesTried is how many files of an album are asked for an embedded picture:
// they all carry the same one, and reading a file costs.
MaxFilesTried = 3
// MaxParallelBuilds is how many covers are built at the same time. Building one
// decodes a picture that can take 200 MB, and a client showing a page of albums
// asks for dozens at once.
MaxParallelBuilds = 2
// CoverTTL is how long a cover that was found is believed before the files are
// looked at again: a picture replaced in a folder changes no audio file.
CoverTTL = 7 * 24 * time.Hour
// tempMaxAge is how old a leftover temporary file must be to be removed.
tempMaxAge = time.Hour
// ArtistTTL is how long a picture of an artist is believed; NoImageTTL how long "there is
// no free picture". Pictures of artists change rarely and every check costs three requests.
ArtistTTL = 30 * 24 * time.Hour
NoImageTTL = 7 * 24 * time.Hour
// NoCoverTTL is how long "no cover" is believed. Adding a picture to a folder
// changes no audio file, so the answer has to be checked again now and then.
NoCoverTTL = 24 * time.Hour
buildTimeout = 2 * time.Minute
)
// Errors returned by the service.
var (
ErrAlbumNotFound = errors.New("album not found")
ErrNoCover = errors.New("this album has no cover")
ErrInvalidSize = errors.New("invalid size")
// ErrFilesUnreadable means none of the files and folders of the album could be looked
// at (an unmounted disk, a permission): that says nothing about whether there is a picture,
// so it is not remembered and nothing already built is touched.
ErrFilesUnreadable = errors.New("the files of the album could not be read")
// Answers of an ArtistSource.
ErrNoImage = errors.New("there is no free picture of this artist")
ErrProviderDisabled = errors.New("the provider is switched off")
ErrNoID = errors.New("the artist has no MusicBrainz id")
// ErrBadPicture is wrapped together with ErrNoImage when what the provider sent could not
// be used (a refused download, bytes that are not a picture): there is no picture to show,
// but that is not an answer about the artist, so it never replaces a picture that was good.
ErrBadPicture = errors.New("the picture could not be used")
ErrArtistNotFound = errors.New("artist not found")
ErrSourceUnavailable = errors.New("the provider could not be reached")
)
// Size is one of the two stored sizes.
type Size string
// The sizes.
const (
Small Size = "small" // 300 pixels at most
Large Size = "large" // 1000 pixels at most
)
// File is where an audio file lives: the directory of its library and the path
// inside it, as the scanner stored them.
type File struct {
Root string
RelPath string // slash-separated
}
// Source finds the playable files of an album, in play order. It returns
// ErrAlbumNotFound for an album that does not exist or has nothing to play.
type Source interface {
AlbumFiles(ctx context.Context, albumID int64) ([]File, error)
}
// Credit says whose picture it is and under which licence, as it has to be shown next
// to it. Pictures taken from the user's own files have none.
type Credit struct {
Author string // plain text; may be empty
License string // like "CC BY-SA 4.0"
LicenseURL string // may be empty
PageURL string // the page of the file, where the details are
}
// Found is a free picture of an artist, as the source got it, with its credit.
type Found struct {
Data []byte
Credit Credit
}
// ArtistSource finds a free picture of an artist. It returns ErrNoImage when there is
// none, ErrProviderDisabled when the provider is off, ErrNoID when the artist has no
// MusicBrainz id, ErrArtistNotFound for an unknown artist and ErrSourceUnavailable (or
// any other error) when it could not get an answer.
type ArtistSource interface {
ArtistImage(ctx context.Context, artistID int64) (Found, error)
}
// AlbumRemote finds the cover of an album outside the user's files, when they have none. It
// answers like an ArtistSource: ErrNoImage, ErrNoID (the album has no MusicBrainz id),
// ErrProviderDisabled, ErrAlbumNotFound, and ErrSourceUnavailable (or any other error) for a
// failure.
type AlbumRemote interface {
AlbumImage(ctx context.Context, albumID int64) (Found, error)
}
// Record is what is known about the picture of one owner.
type Record struct {
Status string // StatusOK or StatusNone
Source string // SourceEmbedded, SourceFolder or SourceCommons; empty for StatusNone
Credit Credit // empty for what comes from the user's own files
ETag string // identifies the stored picture
// Gen counts the changes of the owner's files or data. A build saves its result only
// if the count is still the one it started from, so that it cannot bring back an
// answer that a change made out of date.
Gen int64
Width int // of the original
Height int
CheckedAt time.Time
}
// Store keeps the records. The files themselves are in the data folder.
type Store interface {
Get(ctx context.Context, kind string, ownerID int64) (Record, bool, error)
// Save keeps the record unless the owner's Gen is no longer r.Gen, and says
// whether it was kept.
Save(ctx context.Context, kind string, ownerID int64, r Record) (bool, error)
}
// Cover is a stored picture ready to be served. The caller must close File.
type Cover struct {
File *os.File
ModTime time.Time
ETag string
Credit Credit // empty for a cover from the user's own files
}
// Service builds covers from the user's own files, on the first request, and keeps
// them in the data folder.
type Service struct {
dir string
store Store
src Source
embedded func(path string) ([]byte, error)
now func() time.Time
flight singleflight.Group
slots chan struct{} // one per cover build allowed at the same time
artistSl chan struct{} // the same for artist pictures, which wait for the network
artists ArtistSource
remote AlbumRemote
buildTimeout time.Duration // how long one build may take
// purges counts the times the provider's pictures were taken back (BeginPurge). A
// build that started before one must not keep what it found: its files and its record
// would bring back what the admin just removed. Keeping a result takes the read side, so
// a purge waits for the results being kept and the next ones are refused.
purgeMu sync.RWMutex
purges uint64
tidied sync.Map // the folders whose old temporary files were already removed
}
// NewService builds a Service that keeps its files under dir.
func NewService(dir string, store Store, src Source) *Service {
return &Service{dir: dir, store: store, src: src, embedded: taglib.ReadImage, now: time.Now,
slots: make(chan struct{}, MaxParallelBuilds), artistSl: make(chan struct{}, MaxParallelBuilds),
buildTimeout: buildTimeout}
}
// SetAlbumRemote tells where the cover of an album comes from when the user's own files
// have none. Without one, such an album has no cover.
func (s *Service) SetAlbumRemote(r AlbumRemote) { s.remote = r }
// SetArtistSource tells where pictures of artists come from. Without one, they are
// reported as ErrProviderDisabled.
func (s *Service) SetArtistSource(src ArtistSource) { s.artists = src }
// AlbumCover returns the cover of an album in the given size, building it first when
// it was never built or its record was cleared (a changed file does that), and
// searching again for an album that had none after NoCoverTTL.
func (s *Service) AlbumCover(ctx context.Context, albumID int64, size Size) (*Cover, error) {
if size != Small && size != Large {
return nil, fmt.Errorf("%w: %q", ErrInvalidSize, string(size))
}
if albumID <= 0 {
return nil, ErrAlbumNotFound
}
if err := ctx.Err(); err != nil {
return nil, err
}
rec, ok, err := s.store.Get(ctx, kindAlbum, albumID)
if err != nil {
return nil, fmt.Errorf("read picture record: %w", err)
}
if ok && rec.Status == StatusNone && s.now().Sub(rec.CheckedAt) < NoCoverTTL {
return nil, ErrNoCover
}
if ok && rec.Status == StatusOK && s.now().Sub(rec.CheckedAt) < CoverTTL {
if c, err := s.open(kindAlbum, albumID, size, rec); err == nil {
return c, nil
} // the stored file is gone: build it again
}
built, err := s.buildShared(ctx, albumID)
if err != nil {
// An older cover (from the provider) is better than none when it cannot be asked now.
if errors.Is(err, ErrSourceUnavailable) && ok && rec.Status == StatusOK {
if c, oerr := s.open(kindAlbum, albumID, size, rec); oerr == nil {
return c, nil
}
}
return nil, err
}
if built.Status == StatusNone {
return nil, ErrNoCover
}
return s.open(kindAlbum, albumID, size, built)
}
// StoredAlbumCover is AlbumCover for a friend: it returns a cover that was already built from the audio files, and
// nothing else. It builds nothing, reads no audio file and asks nobody, so a friend cannot make this server do
// work or contact a provider. A cover that came from the Cover Art Archive is not given either: it has no licence
// to share. Anything else is ErrNoCover.
func (s *Service) StoredAlbumCover(ctx context.Context, albumID int64, size Size) (*Cover, error) {
if size != Small && size != Large {
return nil, fmt.Errorf("%w: %q", ErrInvalidSize, string(size))
}
if albumID <= 0 {
return nil, ErrAlbumNotFound
}
rec, ok, err := s.store.Get(ctx, kindAlbum, albumID)
if err != nil {
return nil, fmt.Errorf("read picture record: %w", err)
}
if !ok || rec.Status != StatusOK || rec.Source == SourceCoverArt {
return nil, ErrNoCover
}
c, err := s.open(kindAlbum, albumID, size, rec)
if err != nil {
return nil, ErrNoCover
}
return c, nil
}
// StoredOnly is a Service that answers AlbumCover as StoredAlbumCover does, for the routes friends use.
type StoredOnly struct{ *Service }
// AlbumCover returns a cover that was already built, or ErrNoCover.
func (s StoredOnly) AlbumCover(ctx context.Context, albumID int64, size Size) (*Cover, error) {
return s.StoredAlbumCover(ctx, albumID, size)
}
// buildShared builds the cover of an album, one build per album however many ask at once. It
// runs on its own context, so the first one to leave does not fail the others; each caller
// waits only as long as its own context allows.
func (s *Service) buildShared(ctx context.Context, albumID int64) (Record, error) {
done := s.flight.DoChan(strconv.FormatInt(albumID, 10), func() (v any, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("building a cover panicked: %v", r)
}
}()
return s.build(albumID)
})
select {
case <-ctx.Done():
return Record{}, ctx.Err()
case r := <-done:
if r.Err != nil {
return Record{}, r.Err
}
return r.Val.(Record), nil
}
}
// EnsureAlbumCover makes sure the cover of an album is built, and says where it came from
// (SourceEmbedded, SourceFolder or SourceCoverArt), or "" when the album has none. A stored
// cover that is still good is left alone; otherwise, or with refresh, the album is searched
// again, and a failure is returned as one: unlike an HTTP request, which is better served an
// older cover, the job that builds every cover must see that the provider is down. It is
// what that job calls.
func (s *Service) EnsureAlbumCover(ctx context.Context, albumID int64, refresh bool) (string, error) {
if albumID <= 0 {
return "", ErrAlbumNotFound
}
if !refresh {
rec, ok, err := s.store.Get(ctx, kindAlbum, albumID)
if err != nil {
return "", fmt.Errorf("read picture record: %w", err)
}
if ok && rec.Status == StatusNone && s.now().Sub(rec.CheckedAt) < NoCoverTTL {
return "", nil
}
if ok && rec.Status == StatusOK && s.now().Sub(rec.CheckedAt) < CoverTTL {
if _, err := os.Stat(s.filePath(folderOf(kindAlbum, rec.Source), albumID, Small)); err == nil {
return rec.Source, nil
}
}
}
rec, err := s.buildShared(ctx, albumID)
if err != nil {
return "", err
}
if rec.Status == StatusNone {
return "", nil
}
return rec.Source, nil
}
// folderOf is the folder (under images/) where the files of a picture are kept. The covers
// that came from the archive have a folder of their own, so that taking the provider's data
// back removes exactly them whatever became of their records.
func folderOf(kind, source string) string {
if kind == kindAlbum && source == SourceCoverArt {
return folderAlbumRemote
}
return kind
}
func (s *Service) filePath(folder string, id int64, size Size) string {
return filepath.Join(s.dir, "images", folder, strconv.FormatInt(id, 10)+"-"+string(size)+".jpg")
}
func (s *Service) open(kind string, id int64, size Size, rec Record) (*Cover, error) {
f, err := os.Open(s.filePath(folderOf(kind, rec.Source), id, size))
if err != nil {
return nil, err
}
info, err := f.Stat()
if err != nil || info.Size() == 0 { // empty: a write that did not finish before a crash
_ = f.Close()
return nil, errors.Join(err, os.ErrNotExist)
}
etag := rec.ETag
if size == Small {
etag += "s"
}
return &Cover{File: f, ModTime: info.ModTime(), ETag: etag, Credit: rec.Credit}, nil
}
// build looks for a picture, stores it and records the outcome. If a file of the
// album changed, or the pictures of a provider were taken back, while it was working,
// what it found may be out of date: it starts again.
func (s *Service) build(albumID int64) (Record, error) {
ctx, cancel := context.WithTimeout(context.Background(), s.buildTimeout)
defer cancel()
var rec Record
purged := false
for attempt := 0; attempt < 3; attempt++ {
cur, _, err := s.store.Get(ctx, kindAlbum, albumID)
if err != nil {
return Record{}, fmt.Errorf("read picture record: %w", err)
}
files, err := s.src.AlbumFiles(ctx, albumID)
if err != nil {
return Record{}, err
}
epoch := s.purgeCount()
var (
pic *Processed
off bool
)
rec, pic, off, err = s.search(ctx, albumID, files)
if err != nil {
return Record{}, err
}
rec.Gen = cur.Gen
// The provider was switched off (its data was not taken back): a cover it gave stays,
// instead of being replaced by "no cover" and deleted. Its date is renewed so that
// the files are not searched again at every request.
keep := off && pic == nil && cur.Status == StatusOK && cur.Source == SourceCoverArt
if keep {
rec = cur
rec.CheckedAt = s.now()
}
saved, err := s.commit(ctx, kindAlbum, albumID, rec, pic, epoch, keep)
if errors.Is(err, errPurged) {
purged = true
continue
}
if err != nil {
return Record{}, err
}
if saved {
return rec, nil
}
}
if purged {
return Record{}, ErrSourceUnavailable // its files were never written: there is nothing to serve
}
return rec, nil // it kept changing; this one is served and the record stays stale
}
// search finds a picture: in the user's own files first, at most MaxParallelBuilds at a
// time, and, only when they have none, from the provider of covers if there is one. The
// picture is not stored yet (see commit); nil means there is none.
func (s *Service) search(ctx context.Context, albumID int64, files []File) (rec Record, pic *Processed, providerOff bool, err error) {
none := Record{Status: StatusNone, CheckedAt: s.now()}
p, source, ok, err := s.searchFiles(ctx, files)
if err != nil {
return Record{}, nil, false, err
}
if ok {
return s.record(p, source, Credit{}, none.CheckedAt), &p, false, nil
}
if s.remote == nil {
return none, nil, false, nil
}
rp, credit, ok, off, err := s.searchRemote(ctx, albumID)
if err != nil {
return Record{}, nil, false, err
}
if !ok {
return none, nil, off, nil
}
return s.record(rp, SourceCoverArt, credit, none.CheckedAt), &rp, false, nil
}
func (s *Service) searchFiles(ctx context.Context, files []File) (Processed, string, bool, error) {
select {
case s.slots <- struct{}{}:
defer func() { <-s.slots }()
case <-ctx.Done():
return Processed{}, "", false, ctx.Err()
}
p, source, ok, readable := s.find(files)
if !ok && !readable {
return Processed{}, "", false, ErrFilesUnreadable
}
return p, source, ok, nil
}
// searchRemote asks the provider of covers. "There is none" (no picture, no id, the
// provider is off, a picture that is not one) is an answer; a failure is returned.
func (s *Service) searchRemote(ctx context.Context, albumID int64) (p Processed, credit Credit, ok, providerOff bool, err error) {
// This context is nobody's: when it ends here, the build ran out of time waiting for the
// provider, which is an outage of the provider and not an internal error.
outage := func(err error) error {
if ctx.Err() != nil && (errors.Is(err, context.DeadlineExceeded) || errors.Is(err, context.Canceled)) {
return fmt.Errorf("%w: %w", ErrSourceUnavailable, err)
}
return err
}
select {
case s.artistSl <- struct{}{}: // the slots of the work that waits for the network
defer func() { <-s.artistSl }()
case <-ctx.Done():
return Processed{}, Credit{}, false, false, outage(ctx.Err())
}
found, err := s.remote.AlbumImage(ctx, albumID)
switch {
case errors.Is(err, ErrProviderDisabled):
return Processed{}, Credit{}, false, true, nil
case errors.Is(err, ErrNoImage), errors.Is(err, ErrNoID):
return Processed{}, Credit{}, false, false, nil
case err != nil:
return Processed{}, Credit{}, false, false, outage(err)
}
p, err = ProcessLimited(found.Data, MaxArtistPixels)
if err != nil {
return Processed{}, Credit{}, false, false, nil // not a usable picture: there is no cover to show
}
return p, found.Credit, true, false, nil
}
// record describes a picture that was found.
func (s *Service) record(p Processed, source string, credit Credit, at time.Time) Record {
h := sha256.New()
h.Write(p.Large)
// The credit is part of the identity: the same picture under another author or licence
// must not be taken for the same one by a client that kept the old credit.
h.Write([]byte("\x00" + credit.Author + "\x00" + credit.License + "\x00" + credit.LicenseURL + "\x00" + credit.PageURL))
return Record{
Status: StatusOK, Source: source, Credit: credit, ETag: hex.EncodeToString(h.Sum(nil)[:8]),
Width: p.Width, Height: p.Height, CheckedAt: at,
}
}
// commit keeps what was found: the files and the record, unless the pictures of a
// provider were taken back since the build began (errPurged) or a change moved the
// record's generation (saved is false). Both happen under the read side of the purge lock,
// so a purge waits for it and the next ones are refused.
func (s *Service) commit(ctx context.Context, kind string, id int64, rec Record, pic *Processed, epoch uint64, keepFiles bool) (bool, error) {
s.purgeMu.RLock()
defer s.purgeMu.RUnlock()
if s.purges != epoch {
return false, errPurged
}
folder := folderOf(kind, rec.Source)
if pic != nil {
if err := s.write(folder, id, *pic); err != nil {
return false, err
}
}
saved, err := s.store.Save(ctx, kind, id, rec)
if err != nil {
return false, fmt.Errorf("save picture record: %w", err)
}
if saved && !keepFiles {
// What the record no longer describes must not stay on disk: the files of the other
// folder when the cover moved between the user's files and the archive, and all of
// them when there is no cover any more.
for _, f := range foldersOf(kind) {
if pic == nil || f != folder {
s.removeFiles(f, id)
}
}
}
return saved, nil
}
// foldersOf lists the folders a kind of picture can be kept in.
func foldersOf(kind string) []string {
if kind == kindAlbum {
return []string{kindAlbum, folderAlbumRemote}
}
return []string{kind}
}
// removeFiles deletes both sizes of a picture, if they are there.
func (s *Service) removeFiles(folder string, id int64) {
for _, size := range []Size{Large, Small} {
_ = os.Remove(s.filePath(folder, id, size))
}
}
// find returns the first usable picture: embedded in one of the first files, then an
// image in the folder of one of them. readable says whether anything of the library could
// be looked at: when none of the files and none of the folders could, "no picture" would be
// a lie (an unmounted disk, a permission), and the caller must not remember it.
func (s *Service) find(files []File) (p Processed, source string, ok, readable bool) {
if len(files) > MaxFilesTried {
files = files[:MaxFilesTried]
}
for _, f := range files {
data, found, seen := s.embeddedPicture(f)
readable = readable || seen
if found {
if p, err := Process(data); err == nil {
return p, SourceEmbedded, true, true
}
}
}
seen := map[string]bool{}
var dirs []File // each folder once, then the folders above disc folders
for _, f := range files {
dir := path.Dir(f.RelPath)
if !seen[f.Root+"\x00"+dir] {
seen[f.Root+"\x00"+dir] = true
dirs = append(dirs, File{Root: f.Root, RelPath: dir})
}
}
for _, d := range append([]File(nil), dirs...) {
if parent := path.Dir(d.RelPath); discFolder.MatchString(path.Base(d.RelPath)) && parent != "." && !seen[d.Root+"\x00"+parent] {
seen[d.Root+"\x00"+parent] = true
dirs = append(dirs, File{Root: d.Root, RelPath: parent})
}
}
for _, d := range dirs {
names, seen := folderPictureNames(d.Root, d.RelPath)
readable = readable || seen
for _, name := range names {
data, got := readFolderPicture(d.Root, d.RelPath, name)
if !got {
continue
}
if p, err := Process(data); err == nil {
return p, SourceFolder, true, true
}
}
}
return Processed{}, "", false, readable
}
// discFolder matches the name of a folder that holds one disc of an album: "CD1",
// "Disc 2", "disk 03". The picture of such an album is usually in the folder above.
var discFolder = regexp.MustCompile(`(?i)^(cd|dis[ck])[ _.-]*\d+$`)
// embeddedPicture reads the picture inside an audio file, after checking through
// os.Root that the file is a regular file inside its library.
func (s *Service) embeddedPicture(f File) (data []byte, found, readable bool) {
root, err := os.OpenRoot(filepath.Clean(f.Root))
if err != nil {
return nil, false, false
}
defer root.Close()
info, err := root.Stat(f.RelPath)
if err != nil {
// A link that leaves the library is refused on purpose: the library itself was read.
return nil, false, escapesLibrary(err)
}
if !info.Mode().IsRegular() {
return nil, false, true
}
data, err = s.embedded(filepath.Join(filepath.Clean(f.Root), filepath.FromSlash(f.RelPath)))
if err != nil || len(data) == 0 {
return nil, false, true
}
return data, true, true
}
// escapesLibrary says whether an error is os.Root refusing a link that leads outside the
// library. That is a decision of ours, not a disk that cannot be read, and os.Root has no
// exported error for it.
func escapesLibrary(err error) bool {
return strings.Contains(err.Error(), "path escapes from parent")
}
// folderNames are the names of picture files that mean "the cover", best first.
var folderNames = []string{"cover", "folder", "front", "album", "albumart"}
// folderPictureNames returns the names of the cover-like picture files of a folder of
// the library, best name first. Only regular files inside the library are listed (a
// link is not followed), and none above MaxSourceBytes.
func folderPictureNames(rootDir, dir string) (names []string, readable bool) {
root, err := os.OpenRoot(filepath.Clean(rootDir))
if err != nil {
return nil, false
}
defer root.Close()
entries, err := fs.ReadDir(root.FS(), dir)
if err != nil {
return nil, escapesLibrary(err)
}
type pick struct {
rank int
name string
}
var picks []pick
for _, e := range entries {
if !e.Type().IsRegular() {
continue
}
base := strings.ToLower(e.Name())
ext := path.Ext(base)
if ext != ".jpg" && ext != ".jpeg" && ext != ".png" {
continue
}
for rank, n := range folderNames {
if strings.TrimSuffix(base, ext) == n {
if info, err := e.Info(); err == nil && info.Size() <= MaxSourceBytes {
picks = append(picks, pick{rank, e.Name()})
}
break
}
}
}
sort.Slice(picks, func(i, j int) bool {
if picks[i].rank != picks[j].rank {
return picks[i].rank < picks[j].rank
}
return picks[i].name < picks[j].name
})
out := make([]string, len(picks))
for i, p := range picks {
out[i] = p.name
}
return out, true
}
// readFolderPicture reads one of the files folderPictureNames returned, through
// os.Root, without waiting on a pipe, and not more than MaxSourceBytes.
func readFolderPicture(rootDir, dir, name string) ([]byte, bool) {
root, err := os.OpenRoot(filepath.Clean(rootDir))
if err != nil {
return nil, false
}
defer root.Close()
f, err := root.OpenFile(path.Join(dir, name), os.O_RDONLY|nonBlock, 0)
if err != nil {
return nil, false
}
defer f.Close()
if info, err := f.Stat(); err != nil || !info.Mode().IsRegular() {
return nil, false // what was opened is not what the listing showed
}
data, err := io.ReadAll(io.LimitReader(f, MaxSourceBytes+1))
if err != nil || len(data) > MaxSourceBytes {
return nil, false
}
return data, true
}
// write stores both sizes. Each goes to a temporary file first and is renamed, so a
// reader never sees half a picture.
func (s *Service) write(folder string, id int64, p Processed) error {
dir := filepath.Dir(s.filePath(folder, id, Large))
if err := os.MkdirAll(dir, 0o700); err != nil {
return fmt.Errorf("create the data folder: %w", err)
}
if _, done := s.tidied.LoadOrStore(dir, true); !done {
removeOldTemps(dir, time.Now()) // file times are the disk's, not the clock of the service
}
for size, data := range map[Size][]byte{Large: p.Large, Small: p.Small} {
tmp, err := os.CreateTemp(dir, "tmp-*")
if err != nil {
return fmt.Errorf("create a picture file: %w", err)
}
_, werr := tmp.Write(data)
serr := tmp.Sync() // on disk before it gets its name: a crash must not leave a short file under it
cerr := tmp.Close()
if werr != nil || serr != nil || cerr != nil {
_ = os.Remove(tmp.Name())
return fmt.Errorf("write a picture file: %w", errors.Join(werr, serr, cerr))
}
if err := os.Rename(tmp.Name(), s.filePath(folder, id, size)); err != nil {
_ = os.Remove(tmp.Name())
return fmt.Errorf("store a picture file: %w", err)
}
}
return nil
}
// removeOldTemps removes the temporary files that a crash left behind. A recent one may
// belong to a build that is running, so it stays.
func removeOldTemps(dir string, now time.Time) {
entries, err := os.ReadDir(dir)
if err != nil {
return
}
for _, e := range entries {
if !strings.HasPrefix(e.Name(), "tmp-") {
continue
}
if info, err := e.Info(); err == nil && now.Sub(info.ModTime()) > tempMaxAge {
_ = os.Remove(filepath.Join(dir, e.Name()))
}
}
}