fix: assign extension if not set

This commit is contained in:
Hayden 2022-12-01 18:07:32 -09:00
parent f42a917390
commit a0c81d6125
No known key found for this signature in database
GPG key ID: 17CF79474E257545

View file

@ -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