From 78a399eed41f34556422fbe25689175f5157cbde Mon Sep 17 00:00:00 2001 From: Nicola Murino Date: Tue, 26 Jul 2022 17:53:04 +0200 Subject: [PATCH] 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 --- go.mod | 8 ++++---- go.sum | 15 ++++++++------- httpd/api_http_user.go | 3 ++- httpd/api_utils.go | 8 ++++++++ httpd/internal_test.go | 17 +++++++++++++++++ httpd/webclient.go | 3 ++- 6 files changed, 41 insertions(+), 13 deletions(-) diff --git a/go.mod b/go.mod index d428d1c9..b1e1f5fa 100644 --- a/go.mod +++ b/go.mod @@ -31,7 +31,7 @@ require ( github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 github.com/google/uuid v1.3.0 github.com/grandcat/zeroconf v1.0.0 - github.com/hashicorp/go-hclog v1.2.1 + github.com/hashicorp/go-hclog v1.2.2 github.com/hashicorp/go-plugin v1.4.4 github.com/hashicorp/go-retryablehttp v0.7.1 github.com/jlaffaye/ftp v0.0.0-20201112195030-9aae4d151126 @@ -111,7 +111,7 @@ require ( github.com/googleapis/gax-go/v2 v2.4.0 // indirect github.com/hashicorp/go-cleanhttp v0.5.2 // indirect github.com/hashicorp/hcl v1.0.0 // indirect - github.com/hashicorp/yamux v0.1.0 // indirect + github.com/hashicorp/yamux v0.1.1 // indirect github.com/inconshreveable/mousetrap v1.0.0 // indirect github.com/jmespath/go-jmespath v0.4.0 // indirect github.com/klauspost/cpuid/v2 v2.1.0 // indirect @@ -138,7 +138,7 @@ require ( github.com/power-devops/perfstat v0.0.0-20220216144756-c35f1ee13d7c // indirect github.com/prometheus/client_model v0.2.0 // indirect github.com/prometheus/common v0.37.0 // indirect - github.com/prometheus/procfs v0.7.3 // indirect + github.com/prometheus/procfs v0.8.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/spf13/cast v1.5.0 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect @@ -154,7 +154,7 @@ require ( golang.org/x/tools v0.1.11 // indirect golang.org/x/xerrors v0.0.0-20220609144429-65e65417b02f // indirect google.golang.org/appengine v1.6.7 // indirect - google.golang.org/genproto v0.0.0-20220722212130-b98a9ff5e252 // indirect + google.golang.org/genproto v0.0.0-20220725144611-272f38e5d71b // indirect google.golang.org/grpc v1.48.0 // indirect google.golang.org/protobuf v1.28.0 // indirect gopkg.in/ini.v1 v1.66.6 // indirect diff --git a/go.sum b/go.sum index 90b93eee..06b07aa1 100644 --- a/go.sum +++ b/go.sum @@ -457,8 +457,8 @@ github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtng github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ= github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48= github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ= -github.com/hashicorp/go-hclog v1.2.1 h1:YQsLlGDJgwhXFpucSPyVbCBviQtjlHv3jLTlp8YmtEw= -github.com/hashicorp/go-hclog v1.2.1/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M= +github.com/hashicorp/go-hclog v1.2.2 h1:ihRI7YFwcZdiSD7SIenIhHfQH3OuDvWerAUBZbeQS3M= +github.com/hashicorp/go-hclog v1.2.2/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M= github.com/hashicorp/go-plugin v1.4.4 h1:NVdrSdFRt3SkZtNckJ6tog7gbpRrcbOjQi/rgF7JYWQ= github.com/hashicorp/go-plugin v1.4.4/go.mod h1:viDMjcLJuDui6pXb8U4HVfb8AamCWhHGUjr2IrTF67s= github.com/hashicorp/go-retryablehttp v0.7.1 h1:sUiuQAnLlbvmExtFQs72iFW/HXeUn8Z1aJLQ4LJJbTQ= @@ -467,8 +467,8 @@ github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= -github.com/hashicorp/yamux v0.1.0 h1:DzDIF6Sd7GD2sX0kDFpHAsJMY4L+OfTvtuaQsOYXxzk= -github.com/hashicorp/yamux v0.1.0/go.mod h1:CtWFDAQgb7dxtzFs4tWbplKIe2jSi3+5vKbgIO0SLnQ= +github.com/hashicorp/yamux v0.1.1 h1:yrQxtgseBDrq9Y652vSRDvsKCJKOUD+GzTS4Y0Y8pvE= +github.com/hashicorp/yamux v0.1.1/go.mod h1:CtWFDAQgb7dxtzFs4tWbplKIe2jSi3+5vKbgIO0SLnQ= github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= @@ -687,8 +687,9 @@ github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU= github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA= -github.com/prometheus/procfs v0.7.3 h1:4jVXhlkAyzOScmCkXBTOLRLTz8EeU+eyjrwB/EPq0VU= github.com/prometheus/procfs v0.7.3/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA= +github.com/prometheus/procfs v0.8.0 h1:ODq8ZFEaYeCaZOJlZZdJA2AbQR98dSHSM1KW/You5mo= +github.com/prometheus/procfs v0.8.0/go.mod h1:z7EfXMXOkbkqb9IINtpCn86r/to3BnA0uaxHdg830/4= github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs= github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= @@ -1224,8 +1225,8 @@ google.golang.org/genproto v0.0.0-20220616135557-88e70c0c3a90/go.mod h1:KEWEmljW google.golang.org/genproto v0.0.0-20220617124728-180714bec0ad/go.mod h1:KEWEmljWE5zPzLBa/oHl6DaEt9LmfH6WtH1OHIvleBA= google.golang.org/genproto v0.0.0-20220624142145-8cd45d7dbd1f/go.mod h1:KEWEmljWE5zPzLBa/oHl6DaEt9LmfH6WtH1OHIvleBA= google.golang.org/genproto v0.0.0-20220628213854-d9e0b6570c03/go.mod h1:KEWEmljWE5zPzLBa/oHl6DaEt9LmfH6WtH1OHIvleBA= -google.golang.org/genproto v0.0.0-20220722212130-b98a9ff5e252 h1:G5AjFxR+ibe9Taamo0TdW+iylfBYK10DSkHYdx7PZ9w= -google.golang.org/genproto v0.0.0-20220722212130-b98a9ff5e252/go.mod h1:GkXuJDJ6aQ7lnJcRF+SJVgFdQhypqgl3LB1C9vabdRE= +google.golang.org/genproto v0.0.0-20220725144611-272f38e5d71b h1:SfSkJugek6xm7lWywqth4r2iTrYLpD8lOj1nMIIhMNM= +google.golang.org/genproto v0.0.0-20220725144611-272f38e5d71b/go.mod h1:iHe1svFLAZg9VWz891+QbRMwUv9O/1Ww+/mngYeThbc= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38= google.golang.org/grpc v1.21.1/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM= diff --git a/httpd/api_http_user.go b/httpd/api_http_user.go index 846ee82c..eac3b2ed 100644 --- a/httpd/api_http_user.go +++ b/httpd/api_http_user.go @@ -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) } diff --git a/httpd/api_utils.go b/httpd/api_utils.go index 520b2f83..48340c8e 100644 --- a/httpd/api_utils.go +++ b/httpd/api_utils.go @@ -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, ) { diff --git a/httpd/internal_test.go b/httpd/internal_test.go index ffcf6747..f359b2eb 100644 --- a/httpd/internal_test.go +++ b/httpd/internal_test.go @@ -2474,6 +2474,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 diff --git a/httpd/webclient.go b/httpd/webclient.go index c602108b..892efdb5 100644 --- a/httpd/webclient.go +++ b/httpd/webclient.go @@ -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) }