Files
Warp/server/internal/storage/sqlstore/media.go
T
tajniak81andClaude Opus 5.5 92836f8dcd
CI / Test (ubuntu-latest) (push) Canceled after 0s
CI / Test (windows-latest) (push) Canceled after 0s
CI / Storage tests on PostgreSQL (push) Canceled after 0s
CI / Complement (federation off) (push) Canceled after 0s
CI / golangci-lint (push) Canceled after 0s
CI / Smoke test the binary (push) Canceled after 0s
Phase 6: media
Implements PLAN.md Phase 6: the media repository. Clients upload files,
reserve an mxc:// URI and upload to it later, download and ask for
thumbnails, all through the authenticated media endpoints of Matrix 1.11;
other servers download this server's media through the federation media
endpoints, and media uploaded elsewhere is fetched over federation on
first use and kept.

internal/mediaapi is the component. Content goes into a BlobStore named
by its SHA-256, so the same file uploaded twice is stored once and the
hash doubles as the ETag; the metadata that maps an mxc:// URI onto a
blob is in media_repository and media_thumbnails. The local-filesystem
store writes to a temporary file and renames it into place once hashed,
so nothing ever refers to half a file. A storage driver can bring its own
store through storage.BlobProvider, which is where PocketBase's
filesystem abstraction (local or S3) plugs in with Phase 10.

Reserved uploads belong to the user who made them, take one upload,
lapse after a day, and are capped per user; a download of one waits for
its content, woken in memory the moment it arrives, for as long as
timeout_ms allows.

Thumbnails come from a fixed set of configured sizes, so a thousand
requested sizes cannot mean a thousand resizes. They are made on first
request and kept, never enlarge, honour a JPEG's EXIF orientation, and
point at the original when it already fits. The pixel count is checked
from the header before anything is decoded, at most four resizes run at
once, and concurrent requests for the same thumbnail share one.

Remote media is fetched through the signed client: the multipart/mixed
answer of Matrix 1.11, with a fallback to the legacy download endpoint
for servers that answer M_UNRECOGNIZED, and Location redirects followed
by a dialer that refuses loopback, private and link-local addresses.
Concurrent requests share one fetch, detached from the request that
started it. With federation off nothing is fetched, and what was cached
before is still served.

Downloads carry a sandboxing CSP, nosniff, inline Content-Disposition
only for the types the spec lists as safe, and private immutable caching.
The unauthenticated legacy download endpoints are off by default behind
media.legacy_unauthenticated_downloads.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-16 21:26:49 +02:00

175 lines
5.9 KiB
Go

package sqlstore
import (
"context"
"database/sql"
"errors"
"fmt"
"github.com/dariuszjaniak/warp/server/internal/storage"
)
// Media is the shared SQL implementation of storage.MediaStore.
type Media struct {
db *sql.DB
rebind func(string) string
isUnique func(error) bool
}
// NewMedia builds a media store.
func NewMedia(db *sql.DB, rebind func(string) string, isUnique func(error) bool) *Media {
if rebind == nil {
rebind = func(query string) string { return query }
}
if isUnique == nil {
isUnique = func(error) bool { return false }
}
return &Media{db: db, rebind: rebind, isUnique: isUnique}
}
func (s *Media) q(query string) string { return s.rebind(query) }
const insertMediaSQL = `
INSERT INTO media_repository
(media_origin, media_id, content_type, upload_name, file_size, sha256,
user_id, created_at_ms, pending, expires_at_ms)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
// CreateMedia implements storage.MediaStore.
func (s *Media) CreateMedia(ctx context.Context, m storage.Media) error {
_, err := s.db.ExecContext(ctx, s.q(insertMediaSQL),
m.Origin, m.MediaID, m.ContentType, m.UploadName, m.Size, m.SHA256,
m.UserID, m.CreatedAtMS, m.Pending, m.ExpiresAtMS)
switch {
case err == nil:
return nil
case s.isUnique(err):
return storage.ErrMediaExists
default:
return fmt.Errorf("store media %s/%s: %w", m.Origin, m.MediaID, err)
}
}
const selectMediaSQL = `
SELECT media_origin, media_id, content_type, upload_name, file_size, sha256,
user_id, created_at_ms, pending, expires_at_ms
FROM media_repository WHERE media_origin = ? AND media_id = ?`
// Media implements storage.MediaStore.
func (s *Media) Media(ctx context.Context, origin, mediaID string) (storage.Media, error) {
var m storage.Media
err := s.db.QueryRowContext(ctx, s.q(selectMediaSQL), origin, mediaID).Scan(
&m.Origin, &m.MediaID, &m.ContentType, &m.UploadName, &m.Size, &m.SHA256,
&m.UserID, &m.CreatedAtMS, &m.Pending, &m.ExpiresAtMS)
switch {
case err == nil:
return m, nil
case errors.Is(err, sql.ErrNoRows):
return storage.Media{}, storage.ErrNoSuchMedia
default:
return storage.Media{}, fmt.Errorf("read media %s/%s: %w", origin, mediaID, err)
}
}
// completeUploadSQL only touches pending media, so that two uploads racing
// for the same ID cannot both win: the second matches no row.
const completeUploadSQL = `
UPDATE media_repository
SET content_type = ?, upload_name = ?, file_size = ?, sha256 = ?,
pending = ?, expires_at_ms = 0
WHERE media_origin = ? AND media_id = ? AND pending = ?`
// CompleteUpload implements storage.MediaStore.
func (s *Media) CompleteUpload(ctx context.Context, origin, mediaID string, c storage.MediaContent) error {
res, err := s.db.ExecContext(ctx, s.q(completeUploadSQL),
c.ContentType, c.UploadName, c.Size, c.SHA256, false, origin, mediaID, true)
if err != nil {
return fmt.Errorf("complete upload %s/%s: %w", origin, mediaID, err)
}
n, err := res.RowsAffected()
if err != nil {
return fmt.Errorf("complete upload %s/%s: %w", origin, mediaID, err)
}
if n == 1 {
return nil
}
// Nothing pending matched: either there is no such media, or its
// content is already there.
if _, err := s.Media(ctx, origin, mediaID); err != nil {
return err
}
return storage.ErrMediaUploaded
}
const countPendingSQL = `
SELECT COUNT(*) FROM media_repository
WHERE pending = ? AND user_id = ? AND expires_at_ms > ?`
// PendingUploads implements storage.MediaStore.
func (s *Media) PendingUploads(ctx context.Context, userID string, nowMS int64) (int, error) {
var n int
if err := s.db.QueryRowContext(ctx, s.q(countPendingSQL), true, userID, nowMS).Scan(&n); err != nil {
return 0, fmt.Errorf("count the pending uploads of %s: %w", userID, err)
}
return n, nil
}
const deleteExpiredSQL = `
DELETE FROM media_repository WHERE pending = ? AND expires_at_ms <= ?`
// DeleteExpiredUploads implements storage.MediaStore.
func (s *Media) DeleteExpiredUploads(ctx context.Context, nowMS int64) (int, error) {
res, err := s.db.ExecContext(ctx, s.q(deleteExpiredSQL), true, nowMS)
if err != nil {
return 0, fmt.Errorf("delete expired uploads: %w", err)
}
n, err := res.RowsAffected()
if err != nil {
return 0, fmt.Errorf("delete expired uploads: %w", err)
}
return int(n), nil
}
const selectThumbnailSQL = `
SELECT media_origin, media_id, width, height, method, content_type,
file_size, sha256, created_at_ms
FROM media_thumbnails
WHERE media_origin = ? AND media_id = ? AND width = ? AND height = ? AND method = ?`
// Thumbnail implements storage.MediaStore.
func (s *Media) Thumbnail(ctx context.Context, origin, mediaID string, width, height int, method string) (storage.Thumbnail, error) {
var t storage.Thumbnail
err := s.db.QueryRowContext(ctx, s.q(selectThumbnailSQL), origin, mediaID, width, height, method).Scan(
&t.Origin, &t.MediaID, &t.Width, &t.Height, &t.Method, &t.ContentType,
&t.Size, &t.SHA256, &t.CreatedAtMS)
switch {
case err == nil:
return t, nil
case errors.Is(err, sql.ErrNoRows):
return storage.Thumbnail{}, storage.ErrNoSuchMedia
default:
return storage.Thumbnail{}, fmt.Errorf("read a thumbnail of %s/%s: %w", origin, mediaID, err)
}
}
const upsertThumbnailSQL = `
INSERT INTO media_thumbnails
(media_origin, media_id, width, height, method, content_type,
file_size, sha256, created_at_ms)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT (media_origin, media_id, width, height, method) DO UPDATE SET
content_type = excluded.content_type,
file_size = excluded.file_size,
sha256 = excluded.sha256,
created_at_ms = excluded.created_at_ms`
// StoreThumbnail implements storage.MediaStore.
func (s *Media) StoreThumbnail(ctx context.Context, t storage.Thumbnail) error {
if _, err := s.db.ExecContext(ctx, s.q(upsertThumbnailSQL),
t.Origin, t.MediaID, t.Width, t.Height, t.Method, t.ContentType,
t.Size, t.SHA256, t.CreatedAtMS); err != nil {
return fmt.Errorf("store a thumbnail of %s/%s: %w", t.Origin, t.MediaID, err)
}
return nil
}