Phase 9, part 5: file storage, a folder or an S3 bucket
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>
This commit is contained in:
@@ -0,0 +1,151 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Dir keeps files in a folder, readable only by the app's user.
|
||||
type Dir struct {
|
||||
root string
|
||||
}
|
||||
|
||||
// OpenDir opens (creating) the folder at root.
|
||||
func OpenDir(root string) (*Dir, error) {
|
||||
if err := os.MkdirAll(root, 0o700); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Dir{root: root}, nil
|
||||
}
|
||||
|
||||
func (d *Dir) String() string { return d.root }
|
||||
|
||||
func (d *Dir) path(key string) (string, error) {
|
||||
if err := CheckKey(key); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return filepath.Join(d.root, filepath.FromSlash(key)), nil
|
||||
}
|
||||
|
||||
// Put writes to a temporary file beside the target and renames it, so a
|
||||
// reader never sees half a file.
|
||||
func (d *Dir) Put(ctx context.Context, key string, r io.Reader, size int64) error {
|
||||
p, err := d.path(key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Dir(p), 0o700); err != nil {
|
||||
return err
|
||||
}
|
||||
tmp, err := os.CreateTemp(filepath.Dir(p), ".put-*")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer os.Remove(tmp.Name()) // a no-op once renamed
|
||||
if _, err := io.Copy(tmp, readerCtx{ctx, r}); err != nil {
|
||||
tmp.Close()
|
||||
return err
|
||||
}
|
||||
if err := tmp.Sync(); err != nil {
|
||||
tmp.Close()
|
||||
return err
|
||||
}
|
||||
if err := tmp.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
return os.Rename(tmp.Name(), p)
|
||||
}
|
||||
|
||||
func (d *Dir) Get(ctx context.Context, key string) (io.ReadCloser, error) {
|
||||
p, err := d.path(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
f, err := os.Open(p)
|
||||
if errors.Is(err, fs.ErrNotExist) {
|
||||
return nil, ErrNotExist
|
||||
}
|
||||
return f, err
|
||||
}
|
||||
|
||||
func (d *Dir) Stat(ctx context.Context, key string) (Info, error) {
|
||||
p, err := d.path(key)
|
||||
if err != nil {
|
||||
return Info{}, err
|
||||
}
|
||||
fi, err := os.Stat(p)
|
||||
if errors.Is(err, fs.ErrNotExist) || (err == nil && fi.IsDir()) {
|
||||
return Info{}, ErrNotExist
|
||||
}
|
||||
if err != nil {
|
||||
return Info{}, err
|
||||
}
|
||||
return Info{Key: key, Size: fi.Size(), Modified: fi.ModTime()}, nil
|
||||
}
|
||||
|
||||
func (d *Dir) List(ctx context.Context, prefix string) ([]Info, error) {
|
||||
if err := checkPrefix(prefix); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var out []Info
|
||||
err := filepath.WalkDir(d.root, func(p string, e fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if e.IsDir() || strings.HasPrefix(e.Name(), ".put-") {
|
||||
return nil
|
||||
}
|
||||
rel, err := filepath.Rel(d.root, p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
key := filepath.ToSlash(rel)
|
||||
if !strings.HasPrefix(key, prefix) || strings.HasPrefix(key, ".") {
|
||||
return nil
|
||||
}
|
||||
fi, err := e.Info()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
out = append(out, Info{Key: key, Size: fi.Size(), Modified: fi.ModTime()})
|
||||
return nil
|
||||
})
|
||||
sort.Slice(out, func(i, j int) bool { return out[i].Key < out[j].Key })
|
||||
return out, err
|
||||
}
|
||||
|
||||
// Delete removes the file, and the folders it leaves empty.
|
||||
func (d *Dir) Delete(ctx context.Context, key string) error {
|
||||
p, err := d.path(key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.Remove(p); err != nil && !errors.Is(err, fs.ErrNotExist) {
|
||||
return err
|
||||
}
|
||||
for dir := filepath.Dir(p); dir != d.root && strings.HasPrefix(dir, d.root); dir = filepath.Dir(dir) {
|
||||
if os.Remove(dir) != nil { // not empty
|
||||
break
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// readerCtx stops a copy when the context ends.
|
||||
type readerCtx struct {
|
||||
ctx context.Context
|
||||
r io.Reader
|
||||
}
|
||||
|
||||
func (r readerCtx) Read(p []byte) (int, error) {
|
||||
if err := r.ctx.Err(); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return r.r.Read(p)
|
||||
}
|
||||
Reference in New Issue
Block a user