feat: add MinIO presign endpoints to scraper API and SvelteKit presign helper

This commit is contained in:
Admin
2026-03-02 21:35:27 +05:00
parent cb4be0848f
commit 33e2a4dc01
5 changed files with 170 additions and 0 deletions

View File

@@ -222,6 +222,16 @@ func (h *HybridStore) AudioObjectKey(slug string, n int, voice string, speed flo
return AudioObjectKey(slug, n, voice, speed)
}
// ─── Presigned URLs ───────────────────────────────────────────────────────────
func (h *HybridStore) PresignChapter(ctx context.Context, slug string, n int, expires time.Duration) (string, error) {
return h.minio.PresignChapter(ctx, slug, 0, n, expires)
}
func (h *HybridStore) PresignAudio(ctx context.Context, key string, expires time.Duration) (string, error) {
return h.minio.PresignAudio(ctx, key, expires)
}
// ─── helpers ──────────────────────────────────────────────────────────────────
func recToBookMeta(rec map[string]interface{}) scraper.BookMeta {

View File

@@ -6,6 +6,7 @@ import (
"fmt"
"io"
"strings"
"time"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
@@ -165,6 +166,29 @@ func (m *MinioClient) AudioExists(ctx context.Context, key string) bool {
return err == nil
}
// ─── Presigned URLs ───────────────────────────────────────────────────────────
// PresignChapter returns a presigned GET URL for a chapter object, valid for
// the given duration. The URL is signed with the MinIO credentials and can be
// fetched directly by the browser without authentication.
func (m *MinioClient) PresignChapter(ctx context.Context, slug string, vol, n int, expires time.Duration) (string, error) {
key := chapterKey(slug, vol, n)
u, err := m.c.PresignedGetObject(ctx, m.cfg.BucketChapters, key, expires, nil)
if err != nil {
return "", fmt.Errorf("minio: presign chapter %s: %w", key, err)
}
return u.String(), nil
}
// PresignAudio returns a presigned GET URL for an audio object.
func (m *MinioClient) PresignAudio(ctx context.Context, key string, expires time.Duration) (string, error) {
u, err := m.c.PresignedGetObject(ctx, m.cfg.BucketAudio, key, expires, nil)
if err != nil {
return "", fmt.Errorf("minio: presign audio %s: %w", key, err)
}
return u.String(), nil
}
// ─── helpers ──────────────────────────────────────────────────────────────────
// sanitiseVoice converts a voice name to a filename-safe string.

View File

@@ -121,4 +121,12 @@ type Store interface {
// AudioObjectKey returns the MinIO object key for a cached audio file.
AudioObjectKey(slug string, n int, voice string, speed float64) string
// ── Presigned URLs ─────────────────────────────────────────────────────
// PresignChapter returns a presigned GET URL for a chapter markdown object.
PresignChapter(ctx context.Context, slug string, n int, expires time.Duration) (string, error)
// PresignAudio returns a presigned GET URL for an audio object.
PresignAudio(ctx context.Context, key string, expires time.Duration) (string, error)
}