feat(files): add download=true query param for Content-Disposition attachment header

This commit is contained in:
viettranx committed 2026-03-25 18:26:04 +07:00
1 parent 258e378593
commit bb74120e36
2 files changed
+11

No files matched your search

+6
View File
@@ -1,6 +1,7 @@
package http
import (
"fmt"
"log/slog"
"mime"
"net/http"
@@ -132,6 +133,11 @@ func (h *FilesHandler) handleServe(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", ct)
}
// Trigger browser download with original filename when ?download=true
if r.URL.Query().Get("download") == "true" {
w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, filepath.Base(absPath)))
}
http.ServeFile(w, r, absPath)
}
+5
View File
@@ -165,6 +165,11 @@ export function toFileUrl(path: string): string {
return `/v1/files/${basename}`;
}
/** Append ?download=true to a URL for Content-Disposition: attachment. */
export function toDownloadUrl(url: string): string {
return url + (url.includes("?") ? "&" : "?") + "download=true";
}
/** Determine MediaItem kind from MIME type */
export function mediaKindFromMime(mime: string): "image" | "video" | "audio" | "document" | "code" {
if (mime.startsWith("image/")) return "image";