fix: match CHFS preview and listing behavior

This commit is contained in:
OpenClaw Agent
2026-09-21 18:08:35 +08:00
parent 3d65a94429
commit 871e72277c
5 changed files with 48 additions and 9 deletions
+37 -7
View File
@@ -2,9 +2,14 @@ package api
import (
"archive/zip"
"bytes"
"context"
"encoding/json"
"fmt"
"image"
_ "image/gif"
_ "image/jpeg"
"image/png"
"io"
"mime/multipart"
"net"
@@ -19,6 +24,7 @@ import (
"chfs-ng/internal/auth"
"chfs-ng/internal/opslog"
"chfs-ng/internal/storage"
"github.com/anthonynsimon/bild/transform"
"github.com/emersion/go-webdav"
)
@@ -34,6 +40,7 @@ type Server struct {
Allow string
MultiRoot bool
RemoveMode int
ImagePreview bool
Log *opslog.Logger
}
type fileInfo struct {
@@ -463,7 +470,7 @@ func (s *Server) search(w http.ResponseWriter, r *http.Request) {
return
}
needle := strings.ToLower(strings.TrimSpace(r.URL.Query().Get("str")))
var out []fileInfo
out := make([]fileInfo, 0)
var walk func(string) error
walk = func(dir string) error {
items, err := s.Store.List(dir)
@@ -502,16 +509,39 @@ func (s *Server) shared(w http.ResponseWriter, r *http.Request) {
}
func (s *Server) preview(w http.ResponseWriter, r *http.Request) {
if !account(r, s).MaskFor(strings.Trim(r.URL.Query().Get("file"), "/")).HasRead() {
http.Error(w, "forbidden", 403)
return
}
file := strings.Trim(r.URL.Query().Get("file"), "/")
file := strings.Trim(strings.ReplaceAll(r.URL.Query().Get("file"), "\\", "/"), "/")
if file == "" {
http.Error(w, "file required", 400)
return
}
s.serveFile(w, r, file, false)
if !account(r, s).MaskFor(file).HasRead() {
http.Error(w, "forbidden", 403)
return
}
if !s.ImagePreview {
writeJSONError(w, http.StatusInternalServerError, "Open image file fail!")
return
}
f, err := s.Store.Open(file)
if err != nil {
writeJSONError(w, http.StatusInternalServerError, "Open image file fail!")
return
}
defer f.Close()
src, _, err := image.Decode(f)
if err != nil {
writeJSONError(w, http.StatusInternalServerError, "Open image file fail!")
return
}
thumb := transform.Resize(src, 64, 64, transform.Linear)
var buf bytes.Buffer
if err := png.Encode(&buf, thumb); err != nil {
writeJSONError(w, http.StatusInternalServerError, "Open image file fail!")
return
}
w.Header().Set("Cache-Control", "max-age=2592000")
w.Header().Set("Content-Type", "image/png")
_, _ = w.Write(buf.Bytes())
}
func (s *Server) fileHandler(w http.ResponseWriter, r *http.Request) {