From 73c42f47844cf7e010e4f40c7deb953e8a35de73 Mon Sep 17 00:00:00 2001 From: Hayden <64056131+hay-kot@users.noreply.github.com> Date: Thu, 1 Dec 2022 18:10:50 -0900 Subject: [PATCH] fix: assign extension if not set (#161) --- .../app/api/handlers/v1/v1_ctrl_items_attachments.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/backend/app/api/handlers/v1/v1_ctrl_items_attachments.go b/backend/app/api/handlers/v1/v1_ctrl_items_attachments.go index e776e9a..4eae453 100644 --- a/backend/app/api/handlers/v1/v1_ctrl_items_attachments.go +++ b/backend/app/api/handlers/v1/v1_ctrl_items_attachments.go @@ -4,6 +4,8 @@ import ( "errors" "fmt" "net/http" + "path/filepath" + "strings" "github.com/hay-kot/homebox/backend/internal/core/services" "github.com/hay-kot/homebox/backend/internal/data/ent/attachment" @@ -113,7 +115,15 @@ func (ctrl *V1Controller) HandleItemAttachmentDownload() server.HandlerFunc { return validate.NewRequestError(err, http.StatusInternalServerError) } - w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", doc.Title)) + ext := filepath.Ext(doc.Path) + + title := doc.Title + + if !strings.HasSuffix(doc.Title, ext) { + title = doc.Title + ext + } + + w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", title)) w.Header().Set("Content-Type", "application/octet-stream") http.ServeFile(w, r, doc.Path) return nil