fix: assign extension if not set (#161)

This commit is contained in:
Hayden 2022-12-01 18:10:50 -09:00 committed by GitHub
parent f42a917390
commit 73c42f4784
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

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