Files
memos/server/fileserver
boojack 3feb08737b fix(server): log file-serving failures and answer missing local files as JSON
WriteError now logs the cause of a server error, which the public JSON
body never carries, unless the client has already gone away. The header
reset and envelope move into a shared writer the panic recoverer uses
too, so one failure logs once. The doc comment states the header contract.

A local attachment whose file is gone from disk answers a JSON 404 like
every other storage failure instead of net/http's plain-text page.

The export handler no longer strips the archive headers itself and logs
only the committed case the wrapper cannot see. The fake S3 and local
storage test setups are shared helpers, and the 416 test pins that
Content-Range survives the header reset.
2026-11-01 12:30:49 +08:00
..

Fileserver Package

The fileserver package serves binary content (attachments, avatars) over plain HTTP instead of gRPC, so that HTTP range requests work — required for Safari video/audio playback (RFC 9110 §14). Metadata stays on the gRPC API; only bytes are served here.

Endpoints

GET /file/attachments/:uid[/:filename]   # attachment binary
    ?thumbnail=true                      # JPEG thumbnail for supported image types
    ?motion=true                         # embedded motion-photo video clip
    ?share_token={uid}                   # access via a memo share link
GET /file/users/:identifier/avatar       # user avatar (by username)
GET /file/users/:identifier/export       # Memos export file of that user (administrators only; HEAD = preflight)

Authentication

Handled by server/auth (see authenticator.go and token.go for the constants and token formats). Priority: Authorization: Bearer header (access token or personal access token) first, then the refresh token cookie. See getCurrentUser in fileserver.go.

Authorization

Attachment access follows memo visibility, evaluated by core/access.CheckMemoReadContext:

  • Public memo: no auth required (when the instance allows anonymous access)
  • Protected memo: any authenticated user
  • Private memo: creator only
  • Valid share_token: grants access to that memo's attachments
  • Unlinked attachment (no memo): creator or admin only

Avatars are public on instances that allow anonymous access; private instances require authentication.

The export route requires an instance administrator and may name any user. It streams the ZIP as core/memoexport writes it, admits one export at a time (a second request gets 429), and charges the caller's archive_user budget. HEAD runs the same checks and answers with the headers alone. The archive itself is assembled by server/api, which the server injects as MemoExporter; without it the route answers 404.

Serving behavior

  • Video/audio are streamed with range-request support (http.ServeFile / http.ServeContent for local and database storage); S3-backed media is proxied with ranged GetObject requests.
  • Thumbnails are generated at max 600px, cached in {data_dir}/.thumbnail_cache/, with a semaphore capping concurrent generation. Images with HDR/wide-gamut metadata are served as originals, since re-encoding would strip it.
  • Motion photos have their embedded video extracted and cached in {data_dir}/.motion_cache/.
  • XSS prevention: script-capable MIME types are rewritten to application/octet-stream, non-media files get Content-Disposition: attachment, and all responses carry X-Content-Type-Options: nosniff plus a restrictive Content-Security-Policy.
  • Caching: public attachments get public, no-cache; private ones private, no-store; avatars and thumbnails public, max-age=3600.

Testing

Unit tests live in fileserver_test.go, covering permission checks, streaming, thumbnails, and metadata detection. Manual checks:

curl "http://localhost:8081/file/attachments/{uid}/file.jpg"
curl -H "Range: bytes=0-999" "http://localhost:8081/file/attachments/{uid}/video.mp4"