fix: restore CHFS shared path compatibility

This commit is contained in:
OpenClaw Agent
2026-09-21 19:33:16 +08:00
parent b372cf090d
commit d6199ef3d1
+49 -4
View File
@@ -6,6 +6,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"html"
"image" "image"
_ "image/gif" _ "image/gif"
_ "image/jpeg" _ "image/jpeg"
@@ -14,6 +15,7 @@ import (
"mime/multipart" "mime/multipart"
"net" "net"
"net/http" "net/http"
"net/url"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
@@ -71,6 +73,7 @@ func (s *Server) Handler() http.Handler {
mux.HandleFunc("/chfs/search", s.search) mux.HandleFunc("/chfs/search", s.search)
mux.HandleFunc("/chfs/downloaddir/", s.downloadDir) mux.HandleFunc("/chfs/downloaddir/", s.downloadDir)
mux.HandleFunc("/chfs/shared", s.shared) mux.HandleFunc("/chfs/shared", s.shared)
mux.HandleFunc("/chfs/shared/", s.shared)
mux.HandleFunc("/chfs/preview", s.preview) mux.HandleFunc("/chfs/preview", s.preview)
mux.HandleFunc("/", s.fileHandler) mux.HandleFunc("/", s.fileHandler)
return s.ipFilter(mux) return s.ipFilter(mux)
@@ -510,11 +513,53 @@ func (s *Server) search(w http.ResponseWriter, r *http.Request) {
} }
func (s *Server) shared(w http.ResponseWriter, r *http.Request) { func (s *Server) shared(w http.ResponseWriter, r *http.Request) {
file := strings.Trim(r.URL.Query().Get("file"), "/") if r.URL.Path == "/chfs/shared" {
if file == "" { location := "shared/"
file = strings.TrimPrefix(r.URL.Path, "/chfs/shared/") if r.URL.RawQuery != "" {
location += "?" + r.URL.RawQuery
} }
s.serveFile(w, r, file, true) http.Redirect(w, r, location, http.StatusMovedPermanently)
return
}
p := strings.Trim(strings.TrimPrefix(r.URL.Path, "/chfs/shared/"), "/")
fi, err := s.Store.Stat(p)
if err != nil {
http.NotFound(w, r)
return
}
if fi.IsDir() {
items, err := s.Store.List(p)
if err != nil {
http.NotFound(w, r)
return
}
w.Header().Set("Content-Disposition", `attachment;filename="shared"`)
w.Header().Set("Content-Type", "text/html; charset=utf-8")
_, _ = io.WriteString(w, "<pre>\n")
for _, item := range items {
name := item.Name()
href := url.PathEscape(name)
if item.IsDir() {
href += "/"
}
_, _ = fmt.Fprintf(w, "<a href=\"%s\">%s</a>\n", href, html.EscapeString(name)+func() string {
if item.IsDir() {
return "/"
}
return ""
}())
}
_, _ = io.WriteString(w, "</pre>\n")
return
}
f, err := s.Store.Open(p)
if err != nil {
http.NotFound(w, r)
return
}
defer f.Close()
w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment;filename="%s"`, fi.Name()))
http.ServeContent(w, r, fi.Name(), fi.ModTime(), f)
} }
func (s *Server) preview(w http.ResponseWriter, r *http.Request) { func (s *Server) preview(w http.ResponseWriter, r *http.Request) {