From 544b5afc0d3a259c9b578c133eceafe4609e8192 Mon Sep 17 00:00:00 2001 From: heimoshuiyu Date: Mon, 18 Apr 2022 02:09:57 +0800 Subject: [PATCH] Fix: add filename when download file --- pkg/api/handle_get_file_info.go | 7 +++++++ pkg/api/handle_stream.go | 14 ++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/pkg/api/handle_get_file_info.go b/pkg/api/handle_get_file_info.go index 681283e..98a9d52 100644 --- a/pkg/api/handle_get_file_info.go +++ b/pkg/api/handle_get_file_info.go @@ -5,6 +5,7 @@ import ( "io" "log" "net/http" + "net/url" "os" "strconv" ) @@ -111,6 +112,12 @@ func (api *API) HandleGetFileDirect(w http.ResponseWriter, r *http.Request) { return } + // set header for filename + filename := file.Filename + // encode filename to URL + filename = url.PathEscape(filename) + w.Header().Set("Content-Disposition", "inline; filename*=UTF-8''"+filename) + log.Println("[api] Get direct raw file", path) http.ServeFile(w, r, path) diff --git a/pkg/api/handle_stream.go b/pkg/api/handle_stream.go index f9d47bf..bd585ab 100644 --- a/pkg/api/handle_stream.go +++ b/pkg/api/handle_stream.go @@ -6,6 +6,7 @@ import ( "log" "msw-open-music/pkg/database" "net/http" + "net/url" "os" "os/exec" "strconv" @@ -66,6 +67,13 @@ func (api *API) HandleGetFileStream(w http.ResponseWriter, r *http.Request) { api.HandleErrorStringCode(w, r, `ffmpeg config not found`, 404) return } + + // set headers for filename + filename := file.Filename + "." + ffmpegConfig.Name + "." + ffmpegConfig.Format + filename = url.PathEscape(filename) + // replace invalid characters + w.Header().Set("Content-Disposition", "inline; filename*=UTF-8''"+filename) + args := strings.Split(ffmpegConfig.Args, " ") startArgs := []string{"-threads", strconv.FormatInt(api.APIConfig.FfmpegThreads, 10), "-i", path} endArgs := []string{"-f", ffmpegConfig.Format, "-"} @@ -191,6 +199,12 @@ func (api *API) HandleGetFileStreamDirect(w http.ResponseWriter, r *http.Request api.Tmpfs.Record(path) } + // set headers for filename + filename := ids[0] + "." + ffmpegConfig.Name + "." + ffmpegConfig.Format + filename = url.PathEscape(filename) + // replace invalid characters + w.Header().Set("Content-Disposition", "inline; filename*=UTF-8''"+filename) + log.Println("[api] Get direct cached file", path) http.ServeFile(w, r, path)