mirror of
https://github.com/filebrowser/filebrowser.git
synced 2025-05-08 19:22:57 +00:00

License: MIT Signed-off-by: Henrique Dias <hacdias@gmail.com> Former-commit-id: 10657ab9cff2ee94dd7c0082bfe2cbfa07865022 [formerly 5501af1ace43a089ac4222cf9da2f5176242f817] [formerly 39c72e2e9235a822a30c0b338f55c441f3b3024d [formerly b7022bdfe32c2234e28315e01a440dc882a732c5]] Former-commit-id: 5941e4ab1295b90371ba60003f69939559dc8060 [formerly 3513d92ba403133d7b53aa19c92e4356f9e68b75] Former-commit-id: 3657f6f659e893928bd2b9f64afecdaa41d587a6
103 lines
2.5 KiB
Go
103 lines
2.5 KiB
Go
package http
|
|
|
|
import (
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
fb "github.com/filebrowser/filebrowser"
|
|
"github.com/hacdias/fileutils"
|
|
"github.com/mholt/archiver"
|
|
)
|
|
|
|
// downloadHandler creates an archive in one of the supported formats (zip, tar,
|
|
// tar.gz or tar.bz2) and sends it to be downloaded.
|
|
func downloadHandler(c *fb.Context, w http.ResponseWriter, r *http.Request) (int, error) {
|
|
// If the file isn't a directory, serve it using http.ServeFile. We display it
|
|
// inline if it is requested.
|
|
if !c.File.IsDir {
|
|
return downloadFileHandler(c, w, r)
|
|
}
|
|
|
|
query := r.URL.Query().Get("format")
|
|
files := []string{}
|
|
names := strings.Split(r.URL.Query().Get("files"), ",")
|
|
|
|
// If there are files in the query, sanitize their names.
|
|
// Otherwise, just append the current path.
|
|
if len(names) != 0 {
|
|
for _, name := range names {
|
|
// Unescape the name.
|
|
name, err := url.QueryUnescape(name)
|
|
if err != nil {
|
|
return http.StatusInternalServerError, err
|
|
}
|
|
|
|
// Clean the slashes.
|
|
name = fileutils.SlashClean(name)
|
|
files = append(files, filepath.Join(c.File.Path, name))
|
|
}
|
|
} else {
|
|
files = append(files, c.File.Path)
|
|
}
|
|
|
|
var (
|
|
extension string
|
|
ar archiver.Archiver
|
|
)
|
|
|
|
switch query {
|
|
// If the format is true, just set it to "zip".
|
|
case "zip", "true", "":
|
|
extension, ar = ".zip", archiver.Zip
|
|
case "tar":
|
|
extension, ar = ".tar", archiver.Tar
|
|
case "targz":
|
|
extension, ar = ".tar.gz", archiver.TarGz
|
|
case "tarbz2":
|
|
extension, ar = ".tar.bz2", archiver.TarBz2
|
|
case "tarxz":
|
|
extension, ar = ".tar.xz", archiver.TarXZ
|
|
default:
|
|
return http.StatusNotImplemented, nil
|
|
}
|
|
|
|
// Defines the file name.
|
|
name := c.File.Name
|
|
if name == "." || name == "" {
|
|
name = "archive"
|
|
}
|
|
name += extension
|
|
|
|
w.Header().Set("Content-Disposition", "attachment; filename*=utf-8''"+url.PathEscape(name))
|
|
err := ar.Write(w, files)
|
|
|
|
return 0, err
|
|
}
|
|
|
|
func downloadFileHandler(c *fb.Context, w http.ResponseWriter, r *http.Request) (int, error) {
|
|
file, err := os.Open(c.File.Path)
|
|
if err != nil {
|
|
return http.StatusInternalServerError, err
|
|
}
|
|
defer file.Close()
|
|
|
|
stat, err := file.Stat()
|
|
if err != nil {
|
|
return http.StatusInternalServerError, err
|
|
}
|
|
|
|
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(c.File.Name))
|
|
}
|
|
|
|
http.ServeContent(w, r, stat.Name(), stat.ModTime(), file)
|
|
|
|
return 0, nil
|
|
}
|