improve (zip): no need to zip single file

This commit is contained in:
Mickael Kerjean
2023-04-28 08:55:53 +10:00
parent eb3f00096a
commit 1f52ea90e4
2 changed files with 20 additions and 11 deletions

View File

@ -175,9 +175,12 @@ class FileSystem {
} }
zip(paths) { zip(paths) {
const url = appendShareToUrl( let url;
"/api/files/zip?" + paths.map((p) => "path=" + prepare(p)).join("&"), if (paths.length === 1) {
); url = appendShareToUrl("/api/files/cat?path=" + prepare(paths[0]) + "&name=" + basename(paths[0]));
} else {
url = appendShareToUrl("/api/files/zip?" + paths.map((p) => "path=" + prepare(p)).join("&"));
}
window.open(url); window.open(url);
return Promise.resolve(); return Promise.resolve();
} }

View File

@ -8,6 +8,7 @@ import (
"hash/fnv" "hash/fnv"
"io" "io"
"net/http" "net/http"
"net/url"
"os" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
@ -139,7 +140,13 @@ func FileLs(ctx *App, res http.ResponseWriter, req *http.Request) {
} }
func FileCat(ctx *App, res http.ResponseWriter, req *http.Request) { func FileCat(ctx *App, res http.ResponseWriter, req *http.Request) {
header := res.Header() var (
file io.ReadCloser
contentLength int64 = -1
needToCreateCache bool = false
query url.Values = req.URL.Query()
header http.Header = res.Header()
)
http.SetCookie(res, &http.Cookie{ http.SetCookie(res, &http.Cookie{
Name: "download", Name: "download",
Value: "", Value: "",
@ -151,7 +158,7 @@ func FileCat(ctx *App, res http.ResponseWriter, req *http.Request) {
SendErrorResult(res, ErrPermissionDenied) SendErrorResult(res, ErrPermissionDenied)
return return
} }
path, err := PathBuilder(ctx, req.URL.Query().Get("path")) path, err := PathBuilder(ctx, query.Get("path"))
if err != nil { if err != nil {
Log.Debug("cat::path '%s'", err.Error()) Log.Debug("cat::path '%s'", err.Error())
SendErrorResult(res, err) SendErrorResult(res, err)
@ -166,10 +173,6 @@ func FileCat(ctx *App, res http.ResponseWriter, req *http.Request) {
} }
} }
var file io.ReadCloser
var contentLength int64 = -1
var needToCreateCache bool = false
// use our cache if necessary (range request) when possible // use our cache if necessary (range request) when possible
if req.Header.Get("range") != "" { if req.Header.Get("range") != "" {
ctx.Session["_path"] = path ctx.Session["_path"] = path
@ -185,7 +188,7 @@ func FileCat(ctx *App, res http.ResponseWriter, req *http.Request) {
} }
// perform the actual `cat` if needed // perform the actual `cat` if needed
mType := GetMimeType(req.URL.Query().Get("path")) mType := GetMimeType(query.Get("path"))
if file == nil { if file == nil {
if file, err = ctx.Backend.Cat(path); err != nil { if file, err = ctx.Backend.Cat(path); err != nil {
Log.Debug("cat::backend '%s'", err.Error()) Log.Debug("cat::backend '%s'", err.Error())
@ -202,7 +205,7 @@ func FileCat(ctx *App, res http.ResponseWriter, req *http.Request) {
} }
// plugin hooks // plugin hooks
if thumb := req.URL.Query().Get("thumbnail"); thumb == "true" { if thumb := query.Get("thumbnail"); thumb == "true" {
for plgMType, plgHandler := range Hooks.Get.Thumbnailer() { for plgMType, plgHandler := range Hooks.Get.Thumbnailer() {
if plgMType != mType { if plgMType != mType {
continue continue
@ -299,6 +302,9 @@ func FileCat(ctx *App, res http.ResponseWriter, req *http.Request) {
if header.Get("Content-Security-Policy") == "" { if header.Get("Content-Security-Policy") == "" {
header.Set("Content-Security-Policy", "default-src 'none'; img-src 'self'; media-src 'self'; style-src 'unsafe-inline'; font-src data:; script-src-elem 'self'") header.Set("Content-Security-Policy", "default-src 'none'; img-src 'self'; media-src 'self'; style-src 'unsafe-inline'; font-src data:; script-src-elem 'self'")
} }
if fname := query.Get("name"); fname != "" {
header.Set("Content-Disposition", "attachment; filename=\""+fname+"\"")
}
header.Set("Accept-Ranges", "bytes") header.Set("Accept-Ranges", "bytes")
// Send data to the client // Send data to the client