Fix: add filename when download file

This commit is contained in:
2022-04-18 02:09:57 +08:00
parent 6e9e7252b2
commit 544b5afc0d
2 changed files with 21 additions and 0 deletions

View File

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

View File

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