2019-01-06 06:44:33 +08:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2020-11-24 18:32:23 +08:00
|
|
|
"bytes"
|
2019-01-06 06:44:33 +08:00
|
|
|
"errors"
|
2020-11-24 18:32:23 +08:00
|
|
|
"io/ioutil"
|
2019-01-06 06:44:33 +08:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2020-07-19 02:10:22 +08:00
|
|
|
gopath "path"
|
2019-01-06 06:44:33 +08:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/mholt/archiver"
|
2020-11-24 18:32:23 +08:00
|
|
|
"github.com/spf13/afero"
|
2020-06-01 07:12:36 +08:00
|
|
|
|
|
|
|
"github.com/filebrowser/filebrowser/v2/files"
|
2020-09-11 22:53:37 +08:00
|
|
|
"github.com/filebrowser/filebrowser/v2/fileutils"
|
2020-06-01 07:12:36 +08:00
|
|
|
"github.com/filebrowser/filebrowser/v2/users"
|
2019-01-06 06:44:33 +08:00
|
|
|
)
|
|
|
|
|
2020-07-19 02:10:22 +08:00
|
|
|
func slashClean(name string) string {
|
|
|
|
if name == "" || name[0] != '/' {
|
|
|
|
name = "/" + name
|
|
|
|
}
|
|
|
|
return gopath.Clean(name)
|
|
|
|
}
|
|
|
|
|
2020-06-01 07:12:36 +08:00
|
|
|
func parseQueryFiles(r *http.Request, f *files.FileInfo, _ *users.User) ([]string, error) {
|
|
|
|
var fileSlice []string
|
2019-01-06 06:44:33 +08:00
|
|
|
names := strings.Split(r.URL.Query().Get("files"), ",")
|
|
|
|
|
|
|
|
if len(names) == 0 {
|
2020-06-01 07:12:36 +08:00
|
|
|
fileSlice = append(fileSlice, f.Path)
|
2019-01-06 06:44:33 +08:00
|
|
|
} else {
|
|
|
|
for _, name := range names {
|
2020-06-01 07:12:36 +08:00
|
|
|
name, err := url.QueryUnescape(strings.Replace(name, "+", "%2B", -1)) //nolint:shadow
|
2019-01-06 06:44:33 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-07-19 02:10:22 +08:00
|
|
|
name = slashClean(name)
|
2020-06-01 07:12:36 +08:00
|
|
|
fileSlice = append(fileSlice, filepath.Join(f.Path, name))
|
2019-01-06 06:44:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-01 07:12:36 +08:00
|
|
|
return fileSlice, nil
|
2019-01-06 06:44:33 +08:00
|
|
|
}
|
|
|
|
|
2020-06-01 07:12:36 +08:00
|
|
|
//nolint: goconst
|
2019-01-06 06:44:33 +08:00
|
|
|
func parseQueryAlgorithm(r *http.Request) (string, archiver.Writer, error) {
|
2020-06-01 07:12:36 +08:00
|
|
|
// TODO: use enum
|
2019-01-06 06:44:33 +08:00
|
|
|
switch r.URL.Query().Get("algo") {
|
|
|
|
case "zip", "true", "":
|
|
|
|
return ".zip", archiver.NewZip(), nil
|
|
|
|
case "tar":
|
|
|
|
return ".tar", archiver.NewTar(), nil
|
|
|
|
case "targz":
|
|
|
|
return ".tar.gz", archiver.NewTarGz(), nil
|
|
|
|
case "tarbz2":
|
|
|
|
return ".tar.bz2", archiver.NewTarBz2(), nil
|
|
|
|
case "tarxz":
|
|
|
|
return ".tar.xz", archiver.NewTarXz(), nil
|
|
|
|
case "tarlz4":
|
|
|
|
return ".tar.lz4", archiver.NewTarLz4(), nil
|
|
|
|
case "tarsz":
|
|
|
|
return ".tar.sz", archiver.NewTarSz(), nil
|
|
|
|
default:
|
|
|
|
return "", nil, errors.New("format not implemented")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 15:37:13 +08:00
|
|
|
func setContentDisposition(w http.ResponseWriter, r *http.Request, file *files.FileInfo) {
|
|
|
|
if r.URL.Query().Get("inline") == "true" {
|
|
|
|
w.Header().Set("Content-Disposition", "inline")
|
|
|
|
} else {
|
|
|
|
// As per RFC6266 section 4.3
|
|
|
|
w.Header().Set("Content-Disposition", "attachment; filename*=utf-8''"+url.PathEscape(file.Name))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-06 06:44:33 +08:00
|
|
|
var rawHandler = withUser(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
|
|
|
|
if !d.user.Perm.Download {
|
|
|
|
return http.StatusAccepted, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
file, err := files.NewFileInfo(files.FileOptions{
|
2021-01-07 18:30:17 +08:00
|
|
|
Fs: d.user.Fs,
|
|
|
|
Path: r.URL.Path,
|
|
|
|
Modify: d.user.Perm.Modify,
|
|
|
|
Expand: false,
|
|
|
|
ReadHeader: d.server.TypeDetectionByHeader,
|
|
|
|
Checker: d,
|
2019-01-06 06:44:33 +08:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return errToStatus(err), err
|
|
|
|
}
|
|
|
|
|
2020-11-24 18:32:23 +08:00
|
|
|
if files.IsNamedPipe(file.Mode) {
|
|
|
|
setContentDisposition(w, r, file)
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2019-01-06 06:44:33 +08:00
|
|
|
if !file.IsDir {
|
|
|
|
return rawFileHandler(w, r, file)
|
|
|
|
}
|
|
|
|
|
|
|
|
return rawDirHandler(w, r, d, file)
|
|
|
|
})
|
|
|
|
|
2020-09-11 22:53:37 +08:00
|
|
|
func addFile(ar archiver.Writer, d *data, path, commonPath string) error {
|
2019-01-06 06:44:33 +08:00
|
|
|
// Checks are always done with paths with "/" as path separator.
|
|
|
|
path = strings.Replace(path, "\\", "/", -1)
|
|
|
|
if !d.Check(path) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
info, err := d.user.Fs.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-24 18:32:23 +08:00
|
|
|
var (
|
|
|
|
file afero.File
|
|
|
|
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
|
2019-01-06 06:44:33 +08:00
|
|
|
}
|
|
|
|
|
2020-11-04 22:32:52 +08:00
|
|
|
if path != commonPath {
|
|
|
|
filename := strings.TrimPrefix(path, commonPath)
|
|
|
|
filename = strings.TrimPrefix(filename, "/")
|
|
|
|
err = ar.Write(archiver.File{
|
|
|
|
FileInfo: archiver.FileInfo{
|
|
|
|
FileInfo: info,
|
|
|
|
CustomName: filename,
|
|
|
|
},
|
2020-11-24 18:32:23 +08:00
|
|
|
ReadCloser: arcReadCloser,
|
2020-11-04 22:32:52 +08:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-01-06 06:44:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if info.IsDir() {
|
|
|
|
names, err := file.Readdirnames(0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, name := range names {
|
2020-09-11 22:53:37 +08:00
|
|
|
err = addFile(ar, d, filepath.Join(path, name), commonPath)
|
2019-01-06 06:44:33 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func rawDirHandler(w http.ResponseWriter, r *http.Request, d *data, file *files.FileInfo) (int, error) {
|
|
|
|
filenames, err := parseQueryFiles(r, file, d.user)
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
|
|
|
extension, ar, err := parseQueryAlgorithm(r)
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
|
|
|
name := file.Name
|
|
|
|
if name == "." || name == "" {
|
|
|
|
name = "archive"
|
|
|
|
}
|
|
|
|
name += extension
|
|
|
|
w.Header().Set("Content-Disposition", "attachment; filename*=utf-8''"+url.PathEscape(name))
|
|
|
|
|
|
|
|
err = ar.Create(w)
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
defer ar.Close()
|
|
|
|
|
2020-09-11 22:53:37 +08:00
|
|
|
commonDir := fileutils.CommonPrefix('/', filenames...)
|
|
|
|
|
2019-01-06 06:44:33 +08:00
|
|
|
for _, fname := range filenames {
|
2020-09-11 22:53:37 +08:00
|
|
|
err = addFile(ar, d, fname, commonDir)
|
2019-01-06 06:44:33 +08:00
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func rawFileHandler(w http.ResponseWriter, r *http.Request, file *files.FileInfo) (int, error) {
|
|
|
|
fd, err := file.Fs.Open(file.Path)
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
defer fd.Close()
|
|
|
|
|
2020-06-25 15:37:13 +08:00
|
|
|
setContentDisposition(w, r, file)
|
2019-01-06 06:44:33 +08:00
|
|
|
|
|
|
|
http.ServeContent(w, r, file.Name, file.ModTime, fd)
|
|
|
|
return 0, nil
|
|
|
|
}
|