Phase 6: media
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
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
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>
This commit is contained in:
1 parent
6aaf554336
commit
92836f8dcd
40 files changed
+4729
-63
No files matched your search
@@ -0,0 +1,97 @@
|
||||
package federationapi
|
||||
|
||||
import (
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/textproto"
|
||||
|
||||
"github.com/dariuszjaniak/warp/server/internal/httputil"
|
||||
"github.com/dariuszjaniak/warp/server/internal/mediaapi"
|
||||
)
|
||||
|
||||
// The media endpoints of Matrix 1.11: how another server downloads media
|
||||
// uploaded here. They serve local media only — a server asking for a
|
||||
// third server's media asks that server — and answer in multipart/mixed,
|
||||
// a JSON part for metadata (an empty object, for now) and then the
|
||||
// content.
|
||||
|
||||
func (s *Server) registerMediaRoutes(authed func(string, authedHandler)) {
|
||||
if s.media == nil {
|
||||
return
|
||||
}
|
||||
const v1 = "/_matrix/federation/v1"
|
||||
authed("GET "+v1+"/media/download/{mediaID}", s.handleMediaDownload)
|
||||
authed("GET "+v1+"/media/thumbnail/{mediaID}", s.handleMediaThumbnail)
|
||||
}
|
||||
|
||||
func (s *Server) handleMediaDownload(w http.ResponseWriter, r *http.Request, _ string, _ []byte) error {
|
||||
wait, err := mediaapi.WaitParam(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
content, err := s.media.Download(r.Context(), s.serverName, r.PathValue("mediaID"),
|
||||
mediaapi.FetchOptions{Wait: wait, LocalOnly: true})
|
||||
if err != nil {
|
||||
return mediaapi.HTTPError(err)
|
||||
}
|
||||
defer func() { _ = content.Body.Close() }()
|
||||
writeMultipart(w, r, content, mediaapi.ContentDisposition(content.ContentType, content.FileName))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) handleMediaThumbnail(w http.ResponseWriter, r *http.Request, _ string, _ []byte) error {
|
||||
req, err := mediaapi.ThumbnailParams(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
wait, err := mediaapi.WaitParam(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
content, err := s.media.Thumbnail(r.Context(), s.serverName, r.PathValue("mediaID"), req,
|
||||
mediaapi.FetchOptions{Wait: wait, LocalOnly: true})
|
||||
if err != nil {
|
||||
return mediaapi.HTTPError(err)
|
||||
}
|
||||
defer func() { _ = content.Body.Close() }()
|
||||
writeMultipart(w, r, content,
|
||||
mediaapi.ContentDisposition(content.ContentType, mediaapi.ThumbnailFileName(content.ContentType)))
|
||||
return nil
|
||||
}
|
||||
|
||||
// writeMultipart sends content the way the federation media endpoints do.
|
||||
//
|
||||
// The status goes out first, so a failure after it can only cut the
|
||||
// answer short — which the other server sees as a broken multipart body —
|
||||
// and is only logged.
|
||||
func writeMultipart(w http.ResponseWriter, r *http.Request, content mediaapi.Content, disposition string) {
|
||||
mw := multipart.NewWriter(w)
|
||||
w.Header().Set("Content-Type", "multipart/mixed; boundary="+mw.Boundary())
|
||||
mediaapi.SetSecurityHeaders(w.Header())
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
||||
err := func() error {
|
||||
metadata, err := mw.CreatePart(textproto.MIMEHeader{"Content-Type": {"application/json"}})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := metadata.Write([]byte("{}")); err != nil {
|
||||
return err
|
||||
}
|
||||
part, err := mw.CreatePart(textproto.MIMEHeader{
|
||||
"Content-Type": {content.ContentType},
|
||||
"Content-Disposition": {disposition},
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := io.Copy(part, content.Body); err != nil {
|
||||
return err
|
||||
}
|
||||
return mw.Close()
|
||||
}()
|
||||
if err != nil {
|
||||
httputil.LoggerFrom(r.Context()).Debug("sending media over federation failed", "error", err)
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,7 @@ import (
|
||||
"github.com/dariuszjaniak/warp/server/internal/federation"
|
||||
"github.com/dariuszjaniak/warp/server/internal/httputil"
|
||||
"github.com/dariuszjaniak/warp/server/internal/matrix/id"
|
||||
"github.com/dariuszjaniak/warp/server/internal/mediaapi"
|
||||
"github.com/dariuszjaniak/warp/server/internal/roomserver"
|
||||
"github.com/dariuszjaniak/warp/server/internal/storage"
|
||||
"github.com/dariuszjaniak/warp/server/internal/syncapi"
|
||||
@@ -55,8 +56,13 @@ type ServerOptions struct {
|
||||
Ephemeral EphemeralSink
|
||||
Profiles Profiles
|
||||
Store storage.FederationStore
|
||||
Logger *slog.Logger
|
||||
Now func() time.Time
|
||||
|
||||
// Media serves this server's media to others. Nil leaves the media
|
||||
// endpoints unregistered.
|
||||
Media *mediaapi.API
|
||||
|
||||
Logger *slog.Logger
|
||||
Now func() time.Time
|
||||
}
|
||||
|
||||
// Server is the inbound Server-Server API.
|
||||
@@ -68,6 +74,7 @@ type Server struct {
|
||||
ephemeral EphemeralSink
|
||||
profiles Profiles
|
||||
store storage.FederationStore
|
||||
media *mediaapi.API
|
||||
log *slog.Logger
|
||||
now func() time.Time
|
||||
}
|
||||
@@ -94,6 +101,7 @@ func NewServer(opts ServerOptions) (*Server, error) {
|
||||
ephemeral: opts.Ephemeral,
|
||||
profiles: opts.Profiles,
|
||||
store: opts.Store,
|
||||
media: opts.Media,
|
||||
log: opts.Logger,
|
||||
now: opts.Now,
|
||||
}, nil
|
||||
@@ -142,6 +150,8 @@ func (s *Server) RegisterRoutes(r *httputil.Router) {
|
||||
authed("GET "+v1+"/query/profile", s.handleQueryProfile)
|
||||
authed("GET "+v1+"/publicRooms", s.handlePublicRoomsGet)
|
||||
authed("POST "+v1+"/publicRooms", s.handlePublicRoomsPost)
|
||||
|
||||
s.registerMediaRoutes(authed)
|
||||
}
|
||||
|
||||
// authenticated checks X-Matrix and the allow/deny lists before the
|
||||
|
||||
Reference in new issue
Block a user