mirror of
https://github.com/tiennm99/goclaw.git
synced 2026-10-18 12:14:24 +00:00
feat(files): add download=true query param for Content-Disposition attachment header
This commit is contained in:
1 parent
258e378593
commit
bb74120e36
2 files changed
+11
No files matched your search
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -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";
|
||||
|
||||
Reference in new issue
Block a user