Phase 1: core bot
Link in, card, download, send, in private chats and groups. - cmd/core with an embedded download worker - Telegram front end on telego v1.13.0 (Bot API 10.4): link card with thumbnail and format buttons, one live status message, /dl /audio /thumb /info /queue /cancel, group support with requester-only buttons - Database backed queue: paid and free lanes, leases with heartbeats, one automatic retry, cancel, queue position and wait estimate - yt-dlp and ffprobe behind a Runner interface with classified, redacted errors mapped to friendly messages - SQLite via modernc with goose migrations, portable schema - Local file storage behind a Storage interface - go-i18n with an English catalog, no literal text in handlers - Free tier limits enforced before queueing and again on the real file - Bot token and webhook secret are stripped from all log output - Tests: URL parsing, job lifecycle, worker, yt-dlp wrapper, and end to end flows against a fake Bot API server; opt-in test with the real yt-dlp Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,167 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Local stores files in a directory on disk.
|
||||
type Local struct {
|
||||
root string
|
||||
}
|
||||
|
||||
var _ Storage = (*Local)(nil)
|
||||
|
||||
// NewLocal creates the directory if needed and returns the backend.
|
||||
func NewLocal(root string) (*Local, error) {
|
||||
abs, err := filepath.Abs(root)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := os.MkdirAll(abs, 0o750); err != nil {
|
||||
return nil, fmt.Errorf("storage: create %s: %w", abs, err)
|
||||
}
|
||||
return &Local{root: abs}, nil
|
||||
}
|
||||
|
||||
// path maps a key to a file path and refuses anything that escapes the root.
|
||||
func (l *Local) path(key string) (string, error) {
|
||||
if key == "" || strings.HasPrefix(key, "/") || strings.Contains(key, "\\") || strings.ContainsRune(key, 0) {
|
||||
return "", fmt.Errorf("storage: bad key %q", key)
|
||||
}
|
||||
for _, part := range strings.Split(key, "/") {
|
||||
if part == "" || part == "." || part == ".." {
|
||||
return "", fmt.Errorf("storage: bad key %q", key)
|
||||
}
|
||||
}
|
||||
return filepath.Join(l.root, filepath.FromSlash(key)), nil
|
||||
}
|
||||
|
||||
// Put implements Storage. The file is written to a temporary name and
|
||||
// renamed, so readers never see a partial file.
|
||||
func (l *Local) Put(ctx context.Context, key string, r io.Reader, _ int64) error {
|
||||
p, err := l.path(key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Dir(p), 0o750); err != nil {
|
||||
return err
|
||||
}
|
||||
tmp, err := os.CreateTemp(filepath.Dir(p), ".put-*")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tmpName := tmp.Name()
|
||||
defer os.Remove(tmpName)
|
||||
if _, err := io.Copy(tmp, &ctxReader{ctx: ctx, r: r}); err != nil {
|
||||
tmp.Close()
|
||||
return err
|
||||
}
|
||||
if err := tmp.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
return os.Rename(tmpName, p)
|
||||
}
|
||||
|
||||
// Open implements Storage.
|
||||
func (l *Local) Open(_ context.Context, key string) (io.ReadCloser, int64, error) {
|
||||
p, err := l.path(key)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
f, err := os.Open(p)
|
||||
if err != nil {
|
||||
if errors.Is(err, fs.ErrNotExist) {
|
||||
return nil, 0, ErrNotFound
|
||||
}
|
||||
return nil, 0, err
|
||||
}
|
||||
st, err := f.Stat()
|
||||
if err != nil {
|
||||
f.Close()
|
||||
return nil, 0, err
|
||||
}
|
||||
return f, st.Size(), nil
|
||||
}
|
||||
|
||||
// Stat implements Storage.
|
||||
func (l *Local) Stat(_ context.Context, key string) (Object, error) {
|
||||
p, err := l.path(key)
|
||||
if err != nil {
|
||||
return Object{}, err
|
||||
}
|
||||
st, err := os.Stat(p)
|
||||
if err != nil {
|
||||
if errors.Is(err, fs.ErrNotExist) {
|
||||
return Object{}, ErrNotFound
|
||||
}
|
||||
return Object{}, err
|
||||
}
|
||||
return Object{Key: key, Size: st.Size(), ModTime: st.ModTime()}, nil
|
||||
}
|
||||
|
||||
// Delete implements Storage. Empty parent directories are removed too.
|
||||
func (l *Local) Delete(_ context.Context, key string) error {
|
||||
p, err := l.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 != l.root && strings.HasPrefix(dir, l.root); dir = filepath.Dir(dir) {
|
||||
if os.Remove(dir) != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// List implements Storage.
|
||||
func (l *Local) List(ctx context.Context, prefix string, fn func(Object) error) error {
|
||||
return filepath.WalkDir(l.root, func(p string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
if errors.Is(err, fs.ErrNotExist) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
if ctx.Err() != nil {
|
||||
return ctx.Err()
|
||||
}
|
||||
if d.IsDir() || strings.HasPrefix(d.Name(), ".put-") {
|
||||
return nil
|
||||
}
|
||||
rel, err := filepath.Rel(l.root, p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
key := filepath.ToSlash(rel)
|
||||
if !strings.HasPrefix(key, prefix) {
|
||||
return nil
|
||||
}
|
||||
fi, err := d.Info()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return fn(Object{Key: key, Size: fi.Size(), ModTime: fi.ModTime()})
|
||||
})
|
||||
}
|
||||
|
||||
type ctxReader struct {
|
||||
ctx context.Context
|
||||
r io.Reader
|
||||
}
|
||||
|
||||
func (c *ctxReader) Read(p []byte) (int, error) {
|
||||
if err := c.ctx.Err(); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return c.r.Read(p)
|
||||
}
|
||||
Reference in New Issue
Block a user