Skip to content

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

serve webdav: implement download-directory-as-zip

Leo24c752ed9e48
diff --git a/cmd/serve/webdav/webdav.go b/cmd/serve/webdav/webdav.go
index fbc64b7a4..7ea259737 100644
--- a/cmd/serve/webdav/webdav.go
+++ b/cmd/serve/webdav/webdav.go
@@ -45,6 +45,10 @@ var OptionsInfo = fs.Options{{
Name: "disable_dir_list",
Default: false,
Help: "Disable HTML directory list on GET request for a directory",
+}, {
+ Name: "disable_zip",
+ Default: false,
+ Help: "Disable zip download of directories",
}}.
Add(libhttp.ConfigInfo).
Add(libhttp.AuthConfigInfo).
@@ -57,6 +61,7 @@ type Options struct {
Template libhttp.TemplateConfig
EtagHash string `config:"etag_hash"`
DisableDirList bool `config:"disable_dir_list"`
+ DisableZip bool `config:"disable_zip"`
}
// Opt is options set by command line flags
@@ -408,6 +413,24 @@ func (w *WebDAV) serveDir(rw http.ResponseWriter, r *http.Request, dirRemote str
return
}
dir := node.(*vfs.Dir)
+
+ if r.URL.Query().Get("download") == "zip" && !w.opt.DisableZip {
+ fs.Infof(dirRemote, "%s: Zipping directory", r.RemoteAddr)
+ zipName := path.Base(dirRemote)
+ if dirRemote == "" {
+ zipName = "root"
+ }
+ rw.Header().Set("Content-Disposition", "attachment; filename=\""+zipName+".zip\"")
+ rw.Header().Set("Content-Type", "application/zip")
+ rw.Header().Set("Last-Modified", time.Now().UTC().Format(http.TimeFormat))
+ err := vfs.CreateZip(ctx, dir, rw)
+ if err != nil {
+ serve.Error(ctx, dirRemote, rw, "Failed to create zip", err)
+ return
+ }
+ return
+ }
+
dirEntries, err := dir.ReadDirAll()
if err != nil {
@@ -417,6 +440,7 @@ func (w *WebDAV) serveDir(rw http.ResponseWriter, r *http.Request, dirRemote str
// Make the entries for display
directory := serve.NewDirectory(dirRemote, w.server.HTMLTemplate())
+ directory.DisableZip = w.opt.DisableZip
for _, node := range dirEntries {
if vfscommon.Opt.NoModTime {
directory.AddHTMLEntry(node.Path(), node.IsDir(), node.Size(), time.Time{})