chore: don't break folder download if any file processing causes an error
parent
5072bbb2cb
commit
f2b5dd3787
29
http/raw.go
29
http/raw.go
|
@ -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,18 +114,15 @@ 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)
|
||||||
file, err = d.user.Fs.Open(path)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
arcReadCloser = file
|
|
||||||
}
|
|
||||||
|
|
||||||
if path != commonPath {
|
if path != commonPath {
|
||||||
filename := strings.TrimPrefix(path, commonPath)
|
filename := strings.TrimPrefix(path, commonPath)
|
||||||
|
@ -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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue