download as zip: improve filename

include username and also filename/directory name if the user downloads
a single file/directory

Signed-off-by: Nicola Murino <nicola.murino@gmail.com>
This commit is contained in:
Nicola Murino 2022-07-26 18:24:17 +02:00
parent ec5da8b4a5
commit e3c5cf981f
No known key found for this signature in database
GPG key ID: 2F1FB59433D5A8CB
4 changed files with 29 additions and 2 deletions

View file

@ -390,7 +390,8 @@ func getUserFilesAsZipStream(w http.ResponseWriter, r *http.Request) {
filesList = util.RemoveDuplicates(filesList, false)
w.Header().Set("Content-Disposition", "attachment; filename=\"sftpgo-download.zip\"")
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"",
getCompressedFileName(connection.GetUsername(), filesList)))
renderCompressedFiles(w, connection, baseDir, filesList, nil)
}

View file

@ -221,6 +221,14 @@ func renderAPIDirContents(w http.ResponseWriter, r *http.Request, contents []os.
render.JSON(w, r, results)
}
func getCompressedFileName(username string, files []string) string {
if len(files) == 1 {
name := path.Base(files[0])
return fmt.Sprintf("%s-%s.zip", username, strings.TrimSuffix(name, path.Ext(name)))
}
return fmt.Sprintf("%s-download.zip", username)
}
func renderCompressedFiles(w http.ResponseWriter, conn *Connection, baseDir string, files []string,
share *dataprovider.Share,
) {

View file

@ -2531,6 +2531,23 @@ func TestSecureMiddlewareIntegration(t *testing.T) {
assert.Len(t, server.binding.Security.proxyHeaders, 0)
}
func TestGetCompressedFileName(t *testing.T) {
username := "test"
res := getCompressedFileName(username, []string{"single dir"})
require.Equal(t, fmt.Sprintf("%s-single dir.zip", username), res)
res = getCompressedFileName(username, []string{"file1", "file2"})
require.Equal(t, fmt.Sprintf("%s-download.zip", username), res)
res = getCompressedFileName(username, []string{"file1.txt"})
require.Equal(t, fmt.Sprintf("%s-file1.zip", username), res)
// now files with full paths
res = getCompressedFileName(username, []string{"/dir/single dir"})
require.Equal(t, fmt.Sprintf("%s-single dir.zip", username), res)
res = getCompressedFileName(username, []string{"/adir/file1", "/adir/file2"})
require.Equal(t, fmt.Sprintf("%s-download.zip", username), res)
res = getCompressedFileName(username, []string{"/sub/dir/file1.txt"})
require.Equal(t, fmt.Sprintf("%s-file1.zip", username), res)
}
func TestWebAdminSetupWithInstallCode(t *testing.T) {
installationCode = "1234"
// delete all the admins

View file

@ -653,7 +653,8 @@ func (s *httpdServer) handleWebClientDownloadZip(w http.ResponseWriter, r *http.
return
}
w.Header().Set("Content-Disposition", "attachment; filename=\"sftpgo-download.zip\"")
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"",
getCompressedFileName(connection.GetUsername(), filesList)))
renderCompressedFiles(w, connection, name, filesList, nil)
}