internal/storage keeps files that must outlast the app's disk (exports, uploads) behind one interface: a folder (<data>/files) or an S3-compatible bucket (minio-go, path-style, for Garage). Names that would reach outside the store are refused. An S3 store is checked at start; supermarket config checks the storage and Postgres for real. The same tests run on a folder and on Garage (SUPERMARKET_TEST_S3). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
105 lines
3.4 KiB
Go
105 lines
3.4 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
|
|
}
|