Files
davidandClaude Opus 5.5 af6598271c Phase 13, part 5: each team's jobs, locks, files and sign-ins
Background jobs run for each active team on its own Server and under its
own locks, a few teams at a time and from a different team each round, so
the work is shared between copies of the app and no team holds up another.
Each team's files are under its own prefix, and an OAuth sign-in comes
back only to the team that started it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 12:26:33 -07:00

171 lines
5.0 KiB
Go

// Package storage keeps the files that must outlast the app's own disk:
// exports, uploaded files, images. The local and server editions keep
// them in a folder; the multi-tenant edition in an S3-compatible bucket
// (Garage), so every copy of the app sees the same files.
//
// Not for scratch: the sites' git clones and page previews stay on local
// disk, as a cache that can be made again.
package storage
import (
"context"
"errors"
"fmt"
"io"
"path"
"strings"
"time"
"supermarket/internal/config"
)
// ErrNotExist is returned for a file that isn't there.
var ErrNotExist = errors.New("no such file")
// Info describes a stored file.
type Info struct {
Key string // its name: slash-separated, such as exports/2026-11-25.tar.gz
Size int64
Modified time.Time
}
// Store keeps files by key.
type Store interface {
// Put stores r's contents as key, replacing any file there. size is
// the length, or -1 if unknown.
Put(ctx context.Context, key string, r io.Reader, size int64) error
// Get opens a file; ErrNotExist if there's none.
Get(ctx context.Context, key string) (io.ReadCloser, error)
// Stat describes a file; ErrNotExist if there's none.
Stat(ctx context.Context, key string) (Info, error)
// List lists the files whose keys start with prefix, by key.
List(ctx context.Context, prefix string) ([]Info, error)
// Delete removes a file; one that isn't there is not an error.
Delete(ctx context.Context, key string) error
// String says where the files are, without credentials.
String() string
}
// CheckKey refuses a key that isn't a plain, relative, slash-separated
// name: no "..", no leading slash, no empty parts, no backslashes.
func CheckKey(key string) error {
if key == "" || strings.HasPrefix(key, "/") || strings.HasSuffix(key, "/") || strings.Contains(key, "\\") ||
strings.ContainsRune(key, 0) || path.Clean(key) != key || key == "." || strings.HasPrefix(key, "../") || key == ".." {
return fmt.Errorf("storage: %q isn't a usable file name", key)
}
for _, part := range strings.Split(key, "/") {
if part == "." || part == ".." || part == "" {
return fmt.Errorf("storage: %q isn't a usable file name", key)
}
}
return nil
}
// checkPrefix allows "" (everything), or a key's leading part.
func checkPrefix(prefix string) error {
if prefix == "" {
return nil
}
return CheckKey(strings.TrimSuffix(prefix, "/") + "/x")
}
// Open opens the store the settings name: a folder, or an S3 bucket.
func Open(s config.Storage) (Store, error) {
if s.S3() {
return OpenS3(s)
}
return OpenDir(s.Dir)
}
// Check writes, reads and deletes a small file, to show the store works
// (the S3 server is reachable, and the key may write to the bucket).
func Check(ctx context.Context, st Store) error {
const key = ".supermarket-check"
want := "checked " + time.Now().UTC().Format(time.RFC3339Nano)
if err := st.Put(ctx, key, strings.NewReader(want), int64(len(want))); err != nil {
return fmt.Errorf("writing to %s: %w", st, err)
}
r, err := st.Get(ctx, key)
if err != nil {
return fmt.Errorf("reading from %s: %w", st, err)
}
got, err := io.ReadAll(r)
r.Close()
if err != nil {
return fmt.Errorf("reading from %s: %w", st, err)
}
if string(got) != want {
return fmt.Errorf("%s gave back something else than was written", st)
}
if err := st.Delete(ctx, key); err != nil {
return fmt.Errorf("deleting from %s: %w", st, err)
}
return nil
}
// Under is a store whose files are under prefix in another: each team's
// own place in the platform's store. Keys are checked before the prefix
// is added, so none reaches outside it.
func Under(s Store, prefix string) Store {
return &under{s: s, prefix: strings.Trim(prefix, "/") + "/"}
}
type under struct {
s Store
prefix string
}
func (u *under) key(k string) (string, error) {
if err := CheckKey(k); err != nil {
return "", err
}
return u.prefix + k, nil
}
func (u *under) Put(ctx context.Context, key string, r io.Reader, size int64) error {
k, err := u.key(key)
if err != nil {
return err
}
return u.s.Put(ctx, k, r, size)
}
func (u *under) Get(ctx context.Context, key string) (io.ReadCloser, error) {
k, err := u.key(key)
if err != nil {
return nil, err
}
return u.s.Get(ctx, k)
}
func (u *under) Stat(ctx context.Context, key string) (Info, error) {
k, err := u.key(key)
if err != nil {
return Info{}, err
}
i, err := u.s.Stat(ctx, k)
i.Key = strings.TrimPrefix(i.Key, u.prefix)
return i, err
}
func (u *under) List(ctx context.Context, prefix string) ([]Info, error) {
if strings.Contains(prefix, "..") || strings.HasPrefix(prefix, "/") || strings.Contains(prefix, "\\") {
return nil, fmt.Errorf("storage: %q isn't a usable prefix", prefix)
}
list, err := u.s.List(ctx, u.prefix+prefix)
for i := range list {
list[i].Key = strings.TrimPrefix(list[i].Key, u.prefix)
}
return list, err
}
func (u *under) Delete(ctx context.Context, key string) error {
k, err := u.key(key)
if err != nil {
return err
}
return u.s.Delete(ctx, k)
}
func (u *under) String() string { return u.s.String() + " under " + u.prefix }