first successful export

This commit is contained in:
2026-02-25 00:27:36 -08:00
parent b70ea7e877
commit 68ccc2f4fc
14 changed files with 163 additions and 207 deletions

View File

@@ -1,46 +1,62 @@
package store
import (
"context"
"crypto/rand"
"encoding/hex"
"fmt"
"time"
"github.com/redis/go-redis/v9"
"io"
"os"
"path/filepath"
)
// TTL is how long any stored file lives in Redis before automatic deletion.
const TTL = 2 * time.Hour
type Store struct {
rdb *redis.Client
dir string
}
func New(addr string) (*Store, error) {
rdb := redis.NewClient(&redis.Options{Addr: addr})
if err := rdb.Ping(context.Background()).Err(); err != nil {
return nil, fmt.Errorf("redis connect %s: %w", addr, err)
func New() (*Store, error) {
dir := filepath.Join(os.TempDir(), "aroll-uploads")
if err := os.MkdirAll(dir, 0755); err != nil {
return nil, fmt.Errorf("create upload dir: %w", err)
}
return &Store{rdb: rdb}, nil
return &Store{dir: dir}, nil
}
// Set stores binary data under key with the global TTL.
func (s *Store) Set(ctx context.Context, key string, data []byte) error {
return s.rdb.Set(ctx, key, data, TTL).Err()
// Save streams r into a new file in the store and returns its key.
func (s *Store) Save(r io.Reader, ext string) (string, error) {
key := "video-" + NewID() + ext
path := filepath.Join(s.dir, key)
f, err := os.Create(path)
if err != nil {
return "", fmt.Errorf("create file: %w", err)
}
defer f.Close()
if _, err := io.Copy(f, r); err != nil {
os.Remove(path)
return "", fmt.Errorf("write file: %w", err)
}
return key, nil
}
// Get retrieves binary data by key.
func (s *Store) Get(ctx context.Context, key string) ([]byte, error) {
return s.rdb.Get(ctx, key).Bytes()
// Path returns the absolute path for a key, if the file exists.
func (s *Store) Path(key string) (string, bool) {
path := filepath.Join(s.dir, key)
if _, err := os.Stat(path); err != nil {
return "", false
}
return path, true
}
// Delete removes a key immediately (called after download).
func (s *Store) Delete(ctx context.Context, key string) {
s.rdb.Del(ctx, key)
// Allocate returns a path for a new key without creating the file.
// Used by ExportHandler so ffmpeg can write directly to the store.
func (s *Store) Allocate(key string) string {
return filepath.Join(s.dir, key)
}
// Delete removes the file for a key.
func (s *Store) Delete(key string) {
os.Remove(filepath.Join(s.dir, key))
}
// NewID generates a random hex ID for use as a Redis key suffix.
func NewID() string {
b := make([]byte, 16)
rand.Read(b)