feat: impl files endpoints

This commit is contained in:
2025-12-02 22:08:50 +00:00
parent e8a558d652
commit ca9dfd90b2
13 changed files with 372 additions and 61 deletions

View File

@@ -3,6 +3,7 @@ package blob
import (
"context"
"io"
"log/slog"
"os"
"path/filepath"
@@ -23,6 +24,14 @@ func NewFSStore(config FSStoreConfig) *FSStore {
return &FSStore{config: config}
}
func (s *FSStore) SupportsDirectUpload() bool {
return false
}
func (s *FSStore) SupportsDirectDownload() bool {
return false
}
func (s *FSStore) Initialize(ctx context.Context) error {
return os.MkdirAll(s.config.Root, 0755)
}
@@ -30,6 +39,8 @@ func (s *FSStore) Initialize(ctx context.Context) error {
func (s *FSStore) Put(ctx context.Context, key Key, reader io.Reader) error {
path := filepath.Join(s.config.Root, string(key))
slog.Info("fs store: putting file", "path", path)
err := os.MkdirAll(filepath.Dir(path), 0755)
if err != nil {
return err
@@ -55,6 +66,9 @@ func (s *FSStore) Put(ctx context.Context, key Key, reader io.Reader) error {
func (s *FSStore) Read(ctx context.Context, key Key) (io.ReadCloser, error) {
path := filepath.Join(s.config.Root, string(key))
slog.Info("fs store: reading file", "path", path)
f, err := os.Open(path)
if err != nil {
if os.IsNotExist(err) {
@@ -68,6 +82,8 @@ func (s *FSStore) Read(ctx context.Context, key Key) (io.ReadCloser, error) {
func (s *FSStore) ReadRange(ctx context.Context, key Key, offset, length int64) (io.ReadCloser, error) {
path := filepath.Join(s.config.Root, string(key))
slog.Info("fs store: reading range", "path", path, "offset", offset, "length", length)
f, err := os.Open(path)
if err != nil {
if os.IsNotExist(err) {
@@ -86,6 +102,9 @@ func (s *FSStore) ReadRange(ctx context.Context, key Key, offset, length int64)
func (s *FSStore) ReadSize(ctx context.Context, key Key) (int64, error) {
path := filepath.Join(s.config.Root, string(key))
slog.Info("fs store: reading size", "path", path)
fi, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
@@ -97,7 +116,9 @@ func (s *FSStore) ReadSize(ctx context.Context, key Key) (int64, error) {
}
func (s *FSStore) Delete(ctx context.Context, key Key) error {
err := os.Remove(filepath.Join(s.config.Root, string(key)))
path := filepath.Join(s.config.Root, string(key))
slog.Info("fs store: deleting file", "path", path)
err := os.Remove(path)
// no op if file does not exist
// swallow error if file does not exist
if err != nil && !os.IsNotExist(err) {
@@ -145,10 +166,10 @@ func (s *FSStore) Move(ctx context.Context, srcKey, dstKey Key) error {
return nil
}
func (s *FSStore) SupportsDirectUpload() bool {
return false
}
func (s *FSStore) GenerateUploadURL(ctx context.Context, key Key, opts UploadURLOptions) (string, error) {
return "", nil
}
func (s *FSStore) GenerateDownloadURL(ctx context.Context, key Key, opts DownloadURLOptions) (string, error) {
return "", nil
}

View File

@@ -10,11 +10,21 @@ type UploadURLOptions struct {
Duration time.Duration
}
type DownloadURLOptions struct {
Duration time.Duration
}
type UpdateOptions struct {
ContentType string
}
type Store interface {
// SupportsDirectUpload returns true if the store allows files to be uploaded directly to the blob store.
SupportsDirectUpload() bool
// SupportsDirectDownload returns true if the store allows files to be downloaded directly from the blob store.
SupportsDirectDownload() bool
Initialize(ctx context.Context) error
Put(ctx context.Context, key Key, reader io.Reader) error
Update(ctx context.Context, key Key, opts UpdateOptions) error
@@ -25,9 +35,9 @@ type Store interface {
ReadRange(ctx context.Context, key Key, offset, length int64) (io.ReadCloser, error)
ReadSize(ctx context.Context, key Key) (int64, error)
// SupportsDirectUpload returns true if the store allows files to be uploaded directly to the blob store.
SupportsDirectUpload() bool
// GenerateUploadURL generates a URL that can be used to upload a file directly to the blob store. If unsupported, returns an empty string with no error.
GenerateUploadURL(ctx context.Context, key Key, opts UploadURLOptions) (string, error)
// GenerateDownloadURL generates a URL that can be used to download a file directly from the blob store. If unsupported, returns an empty string with no error.
GenerateDownloadURL(ctx context.Context, key Key, opts DownloadURLOptions) (string, error)
}