Redesign file context menus and download archives using global preference
This commit is contained in:
1 parent
6aca927851
commit
042392f611
22 files changed
+530
-363
No files matched your search
@@ -0,0 +1,87 @@
|
||||
package fbhttp
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"bytes"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/filebrowser/filebrowser/v2/settings"
|
||||
"github.com/filebrowser/filebrowser/v2/users"
|
||||
)
|
||||
|
||||
func TestDownloadUsesConfiguredArchiveFormat(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
if err := os.Mkdir(filepath.Join(root, "folder"), 0750); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(root, "folder", "note.txt"), []byte("hello"), 0640); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
key := []byte("archive-test-key")
|
||||
perm := users.Permissions{Download: true}
|
||||
store := scopedUserStorage(t, root, perm, key)
|
||||
if err := store.Settings.Save(&settings.Settings{Key: key, DownloadArchiveFormat: "tar"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
signed := signToken(t, perm, key)
|
||||
request := authorizeTestRequest(httptest.NewRequest(http.MethodGet, "/folder", nil), signed)
|
||||
recorder := httptest.NewRecorder()
|
||||
handle(rawHandler, "", store, &settings.Server{}).ServeHTTP(recorder, request)
|
||||
if recorder.Code != http.StatusOK || !strings.Contains(recorder.Header().Get("Content-Disposition"), ".tar") {
|
||||
t.Fatalf("status=%d disposition=%s", recorder.Code, recorder.Header().Get("Content-Disposition"))
|
||||
}
|
||||
reader := tar.NewReader(bytes.NewReader(recorder.Body.Bytes()))
|
||||
found := false
|
||||
for {
|
||||
header, err := reader.Next()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if strings.HasSuffix(header.Name, "note.txt") {
|
||||
data, err := io.ReadAll(reader)
|
||||
if err != nil || string(data) != "hello" {
|
||||
t.Fatalf("unexpected data %q: %v", data, err)
|
||||
}
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatal("download omitted file")
|
||||
}
|
||||
// A regular file must remain unarchived regardless of global preference.
|
||||
request = authorizeTestRequest(httptest.NewRequest(http.MethodGet, "/folder/note.txt", nil), signed)
|
||||
recorder = httptest.NewRecorder()
|
||||
handle(rawHandler, "", store, &settings.Server{}).ServeHTTP(recorder, request)
|
||||
if recorder.Code != 200 || recorder.Body.String() != "hello" {
|
||||
t.Fatalf("single-file download changed: %d %q", recorder.Code, recorder.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestArchiveFormatFallbackAndExplicitRequest(t *testing.T) {
|
||||
for _, value := range []string{"", "invalid"} {
|
||||
if (&settings.Settings{DownloadArchiveFormat: value}).ArchiveFormat() != "zip" {
|
||||
t.Fatal("missing ZIP fallback")
|
||||
}
|
||||
}
|
||||
for _, value := range []string{"zip", "tar", "targz", "tarbz2", "tarxz", "tarlz4", "tarsz", "tarbr", "tarzst"} {
|
||||
if !settings.ValidArchiveFormat(value) {
|
||||
t.Fatalf("rejected %s", value)
|
||||
}
|
||||
}
|
||||
if settings.ValidArchiveFormat("invalid") {
|
||||
t.Fatal("accepted unknown format")
|
||||
}
|
||||
ext, _, err := parseQueryAlgorithm(httptest.NewRequest("GET", "/?algo=zip", nil), "tar")
|
||||
if err != nil || ext != ".zip" {
|
||||
t.Fatalf("explicit format lost: %s %v", ext, err)
|
||||
}
|
||||
}
|
||||
+11
-3
@@ -38,8 +38,12 @@ func parseQueryFiles(r *http.Request, f *files.FileInfo, _ *users.User) ([]strin
|
||||
return fileSlice, nil
|
||||
}
|
||||
|
||||
func parseQueryAlgorithm(r *http.Request) (string, archives.Archival, error) {
|
||||
switch r.URL.Query().Get("algo") {
|
||||
func parseQueryAlgorithm(r *http.Request, defaultFormat string) (string, archives.Archival, error) {
|
||||
format := r.URL.Query().Get("algo")
|
||||
if format == "" || format == "true" {
|
||||
format = defaultFormat
|
||||
}
|
||||
switch format {
|
||||
case "zip", "true", "":
|
||||
return ".zip", archives.Zip{}, nil
|
||||
case "tar":
|
||||
@@ -174,7 +178,11 @@ func rawDirHandler(w http.ResponseWriter, r *http.Request, d *data, file *files.
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
extension, archiver, err := parseQueryAlgorithm(r)
|
||||
format := "zip"
|
||||
if d.settings != nil {
|
||||
format = d.settings.ArchiveFormat()
|
||||
}
|
||||
extension, archiver, err := parseQueryAlgorithm(r, format)
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
)
|
||||
|
||||
type settingsData struct {
|
||||
DownloadArchiveFormat string `json:"downloadArchiveFormat"`
|
||||
Signup bool `json:"signup"`
|
||||
HideLoginButton bool `json:"hideLoginButton"`
|
||||
CreateUserDir bool `json:"createUserDir"`
|
||||
@@ -25,6 +26,7 @@ type settingsData struct {
|
||||
|
||||
var settingsGetHandler = withAdmin(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
|
||||
data := &settingsData{
|
||||
DownloadArchiveFormat: d.settings.ArchiveFormat(),
|
||||
Signup: d.settings.Signup,
|
||||
HideLoginButton: d.settings.HideLoginButton,
|
||||
CreateUserDir: d.settings.CreateUserDir,
|
||||
@@ -50,6 +52,10 @@ var settingsPutHandler = withAdmin(func(w http.ResponseWriter, r *http.Request,
|
||||
return http.StatusBadRequest, err
|
||||
}
|
||||
|
||||
if !settings.ValidArchiveFormat(req.DownloadArchiveFormat) {
|
||||
return http.StatusBadRequest, nil
|
||||
}
|
||||
d.settings.DownloadArchiveFormat = req.DownloadArchiveFormat
|
||||
d.settings.Signup = false
|
||||
d.settings.CreateUserDir = req.CreateUserDir
|
||||
d.settings.MinimumPasswordLength = req.MinimumPasswordLength
|
||||
|
||||
Reference in new issue
Block a user