chore: don't break folder download if any file processing causes an error

pull/1499/head
Oleg Lobanov 2021-07-26 10:41:56 +02:00
parent 5072bbb2cb
commit f2b5dd3787
No known key found for this signature in database
GPG Key ID: 7CC64E41212621B0
1 changed files with 15 additions and 20 deletions

View File

@ -1,21 +1,18 @@
package http package http
import ( import (
"bytes"
"errors" "errors"
"io/ioutil" "log"
"net/http" "net/http"
"net/url" "net/url"
gopath "path" gopath "path"
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/mholt/archiver"
"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"
"github.com/filebrowser/filebrowser/v2/users" "github.com/filebrowser/filebrowser/v2/users"
"github.com/mholt/archiver"
) )
func slashClean(name string) string { func slashClean(name string) string {
@ -117,19 +114,16 @@ func addFile(ar archiver.Writer, d *data, path, commonPath string) error {
return err return err
} }
var ( if !info.IsDir() && !info.Mode().IsRegular() {
file afero.File return nil
arcReadCloser = ioutil.NopCloser(&bytes.Buffer{})
)
if !files.IsNamedPipe(info.Mode()) {
file, err = d.user.Fs.Open(path)
if err != nil {
return err
}
defer file.Close()
arcReadCloser = file
} }
file, err := d.user.Fs.Open(path)
if err != nil {
return err
}
defer file.Close()
if path != commonPath { if path != commonPath {
filename := strings.TrimPrefix(path, commonPath) filename := strings.TrimPrefix(path, commonPath)
filename = strings.TrimPrefix(filename, string(filepath.Separator)) filename = strings.TrimPrefix(filename, string(filepath.Separator))
@ -138,7 +132,7 @@ func addFile(ar archiver.Writer, d *data, path, commonPath string) error {
FileInfo: info, FileInfo: info,
CustomName: filename, CustomName: filename,
}, },
ReadCloser: arcReadCloser, ReadCloser: file,
}) })
if err != nil { if err != nil {
return err return err
@ -152,9 +146,10 @@ func addFile(ar archiver.Writer, d *data, path, commonPath string) error {
} }
for _, name := range names { for _, name := range names {
err = addFile(ar, d, filepath.Join(path, name), commonPath) fPath := filepath.Join(path, name)
err = addFile(ar, d, fPath, commonPath)
if err != nil { if err != nil {
return err log.Printf("Failed to archive %s: %v", fPath, err)
} }
} }
} }
@ -196,7 +191,7 @@ func rawDirHandler(w http.ResponseWriter, r *http.Request, d *data, file *files.
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 {
return http.StatusInternalServerError, err log.Printf("Failed to archive %s: %v", fname, err)
} }
} }