mirror of
https://github.com/drakkan/sftpgo.git
synced 2025-12-07 06:40:54 +03:00
webclient: allow to download multiple files as zip
This commit is contained in:
@@ -3,14 +3,19 @@ package httpd
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/go-chi/render"
|
||||
"github.com/klauspost/compress/zip"
|
||||
|
||||
"github.com/drakkan/sftpgo/common"
|
||||
"github.com/drakkan/sftpgo/dataprovider"
|
||||
"github.com/drakkan/sftpgo/logger"
|
||||
)
|
||||
|
||||
func sendAPIResponse(w http.ResponseWriter, r *http.Request, err error, message string, code int) {
|
||||
@@ -90,3 +95,74 @@ func getSearchFilters(w http.ResponseWriter, r *http.Request) (int, int, string,
|
||||
|
||||
return limit, offset, order, err
|
||||
}
|
||||
|
||||
func renderCompressedFiles(w http.ResponseWriter, conn *Connection, baseDir string, files []string) {
|
||||
w.Header().Set("Content-Type", "application/zip")
|
||||
w.Header().Set("Accept-Ranges", "none")
|
||||
w.Header().Set("Content-Transfer-Encoding", "binary")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
||||
wr := zip.NewWriter(w)
|
||||
|
||||
for _, file := range files {
|
||||
fullPath := path.Join(baseDir, file)
|
||||
if err := addZipEntry(wr, conn, fullPath, baseDir); err != nil {
|
||||
panic(http.ErrAbortHandler)
|
||||
}
|
||||
}
|
||||
if err := wr.Close(); err != nil {
|
||||
conn.Log(logger.LevelWarn, "unable to close zip file: %v", err)
|
||||
panic(http.ErrAbortHandler)
|
||||
}
|
||||
}
|
||||
|
||||
func addZipEntry(wr *zip.Writer, conn *Connection, entryPath, baseDir string) error {
|
||||
info, err := conn.Stat(entryPath, 1)
|
||||
if err != nil {
|
||||
conn.Log(logger.LevelDebug, "unable to add zip entry %#v, stat error: %v", entryPath, err)
|
||||
return err
|
||||
}
|
||||
if info.IsDir() {
|
||||
_, err := wr.Create(getZipEntryName(entryPath, baseDir) + "/")
|
||||
if err != nil {
|
||||
conn.Log(logger.LevelDebug, "unable to create zip entry %#v: %v", entryPath, err)
|
||||
return err
|
||||
}
|
||||
contents, err := conn.ReadDir(entryPath)
|
||||
if err != nil {
|
||||
conn.Log(logger.LevelDebug, "unable to add zip entry %#v, read dir error: %v", entryPath, err)
|
||||
return err
|
||||
}
|
||||
for _, info := range contents {
|
||||
fullPath := path.Join(entryPath, info.Name())
|
||||
if err := addZipEntry(wr, conn, fullPath, baseDir); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if !info.Mode().IsRegular() {
|
||||
// we only allow regular files
|
||||
conn.Log(logger.LevelDebug, "skipping zip entry for non regular file %#v", entryPath)
|
||||
return nil
|
||||
}
|
||||
reader, err := conn.getFileReader(entryPath, 0, http.MethodGet)
|
||||
if err != nil {
|
||||
conn.Log(logger.LevelDebug, "unable to add zip entry %#v, cannot open file: %v", entryPath, err)
|
||||
return err
|
||||
}
|
||||
defer reader.Close()
|
||||
|
||||
f, err := wr.Create(getZipEntryName(entryPath, baseDir))
|
||||
if err != nil {
|
||||
conn.Log(logger.LevelDebug, "unable to create zip entry %#v: %v", entryPath, err)
|
||||
return err
|
||||
}
|
||||
_, err = io.Copy(f, reader)
|
||||
return err
|
||||
}
|
||||
|
||||
func getZipEntryName(entryPath, baseDir string) string {
|
||||
entryPath = strings.TrimPrefix(entryPath, baseDir)
|
||||
return strings.TrimPrefix(entryPath, "/")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user