fix multi download option

This commit is contained in:
Erik Berlin 2024-07-30 15:17:36 +03:00
parent 8a621e05c8
commit b1d5ce2de1
2 changed files with 27 additions and 4 deletions

View File

@ -46,6 +46,7 @@ type FileInfo struct {
Token string `json:"token,omitempty"` Token string `json:"token,omitempty"`
currentDir []os.FileInfo `json:"-"` currentDir []os.FileInfo `json:"-"`
Resolution *ImageResolution `json:"resolution,omitempty"` Resolution *ImageResolution `json:"resolution,omitempty"`
Options FileOptions `json:"-"`
} }
// FileOptions are the options when getting a file info. // FileOptions are the options when getting a file info.
@ -95,6 +96,10 @@ func NewFileInfo(opts FileOptions) (*FileInfo, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
// Set the Options field to be accessed directly from the file object
file.Options = opts
if file.IsDir && opts.FolderSize { if file.IsDir && opts.FolderSize {
size, err := getFolderSize(file.RealPath()) size, err := getFolderSize(file.RealPath())
if err != nil { if err != nil {

View File

@ -5,11 +5,13 @@ import (
"log" "log"
"net/http" "net/http"
"net/url" "net/url"
"os"
gopath "path" gopath "path"
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/mholt/archiver/v3" "github.com/mholt/archiver/v3"
"github.com/spf13/afero"
"github.com/filebrowser/filebrowser/v2/files" "github.com/filebrowser/filebrowser/v2/files"
"github.com/filebrowser/filebrowser/v2/fileutils" "github.com/filebrowser/filebrowser/v2/fileutils"
@ -26,7 +28,6 @@ func slashClean(name string) string {
func parseQueryFiles(r *http.Request, f *files.FileInfo, _ *users.User) ([]string, error) { func parseQueryFiles(r *http.Request, f *files.FileInfo, _ *users.User) ([]string, error) {
var fileSlice []string var fileSlice []string
names := strings.Split(r.URL.Query().Get("files"), ",") names := strings.Split(r.URL.Query().Get("files"), ",")
if len(names) == 0 { if len(names) == 0 {
fileSlice = append(fileSlice, f.Path) fileSlice = append(fileSlice, f.Path)
} else { } else {
@ -107,12 +108,29 @@ var rawHandler = withUser(func(w http.ResponseWriter, r *http.Request, d *data)
return rawDirHandler(w, r, d, file) return rawDirHandler(w, r, d, file)
}) })
// function that checks if given full path exists or not
// it helps to determine to which NFS we need to go IDC or KFS
func CheckIfExistsInPath(pathToCheck string) bool {
_, pathExistsErr := os.Stat(pathToCheck)
return !os.IsNotExist(pathExistsErr)
}
func addFile(ar archiver.Writer, d *data, path, commonPath string) error { func addFile(ar archiver.Writer, d *data, path, commonPath string) error {
af := afero.NewBasePathFs(afero.NewOsFs(), d.server.Root)
log.Printf("adding to zip following path - %v:", path)
anotherFullPath := d.server.AnotherPath + path
existsInAnotherPath := CheckIfExistsInPath(anotherFullPath)
if existsInAnotherPath {
af = afero.NewBasePathFs(afero.NewOsFs(), d.server.AnotherPath)
}
if !d.Check(path) { if !d.Check(path) {
return nil return nil
} }
info, err := d.user.Fs.Stat(path) info, err := af.Stat(path)
if err != nil { if err != nil {
return err return err
} }
@ -121,7 +139,7 @@ func addFile(ar archiver.Writer, d *data, path, commonPath string) error {
return nil return nil
} }
file, err := d.user.Fs.Open(path) file, err := af.Open(path)
if err != nil { if err != nil {
return err return err
} }
@ -155,6 +173,7 @@ func addFile(ar archiver.Writer, d *data, path, commonPath string) error {
log.Printf("Failed to archive %s: %v", fPath, err) log.Printf("Failed to archive %s: %v", fPath, err)
} }
} }
} }
return nil return nil
@ -190,7 +209,6 @@ func rawDirHandler(w http.ResponseWriter, r *http.Request, d *data, file *files.
} }
name += extension name += extension
w.Header().Set("Content-Disposition", "attachment; filename*=utf-8''"+url.PathEscape(name)) w.Header().Set("Content-Disposition", "attachment; filename*=utf-8''"+url.PathEscape(name))
for _, fname := range filenames { for _, fname := range filenames {
err = addFile(ar, d, fname, commonDir) err = addFile(ar, d, fname, commonDir)
if err != nil { if err != nil {