Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix(attachments): presign desktop inline media (#5946)
kami156c592c9d03
diff --git a/server/internal/handler/file.go b/server/internal/handler/file.goindex 883af4bbf7..932192cda1 100644--- a/server/internal/handler/file.go+++ b/server/internal/handler/file.go@@ -571,7 +571,28 @@ func (h *Handler) GetAttachmentByID(w http.ResponseWriter, r *http.Request) {return}- writeJSON(w, http.StatusOK, h.attachmentToResponse(att))+ resp := h.attachmentToResponse(att)+ // Token-mode clients use this authenticated endpoint to replace the+ // auth-gated API path with a URL that native media elements can load.+ // Assert the same storage.DownloadPresigner that resolveAttachmentDownloadMode+ // keys its presign decision on, so the mode resolution and the presign call+ // can never disagree. An empty content disposition inherits the object's+ // stored Content-Disposition (inline for media, attachment otherwise), which+ // keeps images renderable inline while preserving the original download+ // filename.+ if h.CFSigner == nil && h.resolveAttachmentDownloadMode(att.Url) == attachmentDownloadModePresign {+ if presigner, ok := h.Storage.(storage.DownloadPresigner); ok {+ key := h.Storage.KeyFromURL(att.Url)+ signedURL, err := presigner.PresignGetWithContentDisposition(r.Context(), key, h.attachmentDownloadURLTTL(), "")+ if err != nil {+ slog.Warn("failed to presign inline attachment URL", "id", uuidToString(att.ID), "key", key, "error", err)+ } else {+ resp.DownloadURL = signedURL+ }+ }+ }++ writeJSON(w, http.StatusOK, resp)}func (h *Handler) loadAttachmentForRequest(w http.ResponseWriter, r *http.Request) (db.Attachment, bool) {diff --git a/server/internal/handler/file_test.go b/server/internal/handler/file_test.goindex 66b1ce73d2..4d6480bc97 100644--- a/server/internal/handler/file_test.go+++ b/server/internal/handler/file_test.go@@ -642,6 +642,64 @@ func TestAttachmentToResponse_NonCloudFrontUsesDownloadEndpoint(t *testing.T) {}}+func TestGetAttachmentByID_AutoPublicEndpointReturnsPresignedDownloadURL(t *testing.T) {+ store := &mockStorageNoCdn{}+ origStorage := testHandler.Storage+ origCfg := testHandler.cfg+ origSigner := testHandler.CFSigner+ testHandler.Storage = store+ testHandler.cfg.AttachmentDownloadMode = "auto"+ testHandler.CFSigner = nil+ t.Cleanup(func() {+ testHandler.Storage = origStorage+ testHandler.cfg = origCfg+ testHandler.CFSigner = origSigner+ })++ key := "downloads/desktop-inline.png"+ id := seedAttachmentURL(+ t,+ "https://s3.example.com/test-bucket/"+key,+ "desktop-inline.png",+ "image/png",+ 10,+ )++ req := httptest.NewRequest("GET", "/api/attachments/"+id, nil)+ req.Header.Set("X-User-ID", testUserID)+ req.Header.Set("X-Workspace-ID", testWorkspaceID)+ rctx := chi.NewRouteContext()+ rctx.URLParams.Add("id", id)+ req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx))+ w := httptest.NewRecorder()++ testHandler.GetAttachmentByID(w, req)++ if w.Code != http.StatusOK {+ t.Fatalf("status = %d, want 200; body=%s", w.Code, w.Body.String())+ }+ var resp AttachmentResponse+ if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {+ t.Fatalf("decode response: %v; body=%s", err, w.Body.String())+ }+ parsed, err := url.Parse(resp.DownloadURL)+ if err != nil {+ t.Fatalf("parse download_url: %v", err)+ }+ if got := parsed.Query().Get("X-Amz-Signature"); got != "mock" {+ t.Fatalf("download_url = %q, want S3 presigned URL", resp.DownloadURL)+ }+ if got := parsed.Query().Get("response-content-disposition"); got != "" {+ t.Fatalf("response-content-disposition = %q, want inline-loadable URL", got)+ }+ if want := "/api/attachments/" + id + "/download"; resp.MarkdownURL != want {+ t.Fatalf("markdown_url = %q, want stable URL %q", resp.MarkdownURL, want)+ }+ if len(store.presignCalls) != 1 || store.presignCalls[0] != key {+ t.Fatalf("presign calls = %v, want [%s]", store.presignCalls, key)+ }+}+func TestDownloadAttachment_CloudFrontRedirectSignsAttachmentDisposition(t *testing.T) {origStorage := testHandler.StorageorigCfg := testHandler.cfg