From c7319a46865dcd11bd05bbeb68a209efd7803ad9 Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 28 Apr 2026 22:18:05 +0800 Subject: [PATCH] feat: simplify HTTP file server to read-only mode (browse + download only) --- NDH6SA~M | 0 httpfile/server.go | 243 +-------------------------------------------- 2 files changed, 3 insertions(+), 240 deletions(-) create mode 100644 NDH6SA~M diff --git a/NDH6SA~M b/NDH6SA~M new file mode 100644 index 0000000..e69de29 diff --git a/httpfile/server.go b/httpfile/server.go index 655ec2f..9ba341b 100644 --- a/httpfile/server.go +++ b/httpfile/server.go @@ -3,7 +3,6 @@ package httpfile import ( "encoding/json" "fmt" - "io" "log" "net/http" "os" @@ -41,12 +40,7 @@ func (s *Server) Start() error { // API endpoints mux.HandleFunc("/api/list", s.apiListDir) - if s.config.HTTPFile.Upload { - mux.HandleFunc("/api/upload", s.apiUpload) - mux.HandleFunc("/api/create-folder", s.apiCreateFolder) - } mux.HandleFunc("/api/download", s.apiDownload) - mux.HandleFunc("/api/delete", s.apiDelete) addr := fmt.Sprintf("%s:%d", s.config.HTTPFile.Host, s.config.HTTPFile.Port) log.Printf("HTTP file server listening on http://%s", addr) @@ -121,110 +115,6 @@ func (s *Server) apiDownload(w http.ResponseWriter, r *http.Request) { log.Printf("HTTP download: %s from %s", file, r.RemoteAddr) } -func (s *Server) apiUpload(w http.ResponseWriter, r *http.Request) { - if r.Method != http.MethodPost { - http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) - return - } - - // Parse multipart form (max 500MB) - err := r.ParseMultipartForm(500 << 20) - if err != nil { - http.Error(w, `{"error":"Failed to parse upload"}`, http.StatusBadRequest) - return - } - - dir := r.FormValue("path") - if dir == "" { - dir = "/" - } - - files := r.MultipartForm.File["files"] - if len(files) == 0 { - http.Error(w, `{"error":"No files uploaded"}`, http.StatusBadRequest) - return - } - - realDir := s.toRealPath(dir) - - for _, fileHeader := range files { - file, err := fileHeader.Open() - if err != nil { - continue - } - - destPath := filepath.Join(realDir, fileHeader.Filename) - destFile, err := os.Create(destPath) - if err != nil { - file.Close() - continue - } - - io.Copy(destFile, file) - destFile.Close() - file.Close() - - log.Printf("HTTP upload: %s (%d bytes) from %s", fileHeader.Filename, fileHeader.Size, r.RemoteAddr) - } - - w.Header().Set("Content-Type", "application/json") - json.NewEncoder(w).Encode(map[string]string{"status": "ok"}) -} - -func (s *Server) apiCreateFolder(w http.ResponseWriter, r *http.Request) { - if r.Method != http.MethodPost { - http.Error(w, `{"error":"Method not allowed"}`, http.StatusMethodNotAllowed) - return - } - - var req struct { - Path string `json:"path"` - Name string `json:"name"` - } - if err := json.NewDecoder(r.Body).Decode(&req); err != nil { - http.Error(w, `{"error":"Invalid request"}`, http.StatusBadRequest) - return - } - - if req.Name == "" { - http.Error(w, `{"error":"Folder name required"}`, http.StatusBadRequest) - return - } - - realPath := filepath.Join(s.toRealPath(req.Path), req.Name) - if err := os.MkdirAll(realPath, 0755); err != nil { - http.Error(w, `{"error":"Failed to create folder"}`, http.StatusInternalServerError) - return - } - - w.Header().Set("Content-Type", "application/json") - json.NewEncoder(w).Encode(map[string]string{"status": "ok"}) -} - -func (s *Server) apiDelete(w http.ResponseWriter, r *http.Request) { - if r.Method != http.MethodPost { - http.Error(w, `{"error":"Method not allowed"}`, http.StatusMethodNotAllowed) - return - } - - var req struct { - Path string `json:"path"` - } - if err := json.NewDecoder(r.Body).Decode(&req); err != nil { - http.Error(w, `{"error":"Invalid request"}`, http.StatusBadRequest) - return - } - - realPath := s.toRealPath(req.Path) - if err := os.RemoveAll(realPath); err != nil { - http.Error(w, `{"error":"Delete failed"}`, http.StatusInternalServerError) - return - } - - w.Header().Set("Content-Type", "application/json") - json.NewEncoder(w).Encode(map[string]string{"status": "ok"}) -} - func (s *Server) toRealPath(httpPath string) string { rootDir := s.config.HTTPFile.RootDir cleanPath := strings.TrimPrefix(httpPath, "/") @@ -313,21 +203,14 @@ const fileBrowserHTML = `

📁 HTTP 文件服务器

-

在线浏览、下载、上传文件

+

在线浏览、下载文件

- - - -
@@ -347,30 +230,6 @@ const fileBrowserHTML = `
- - - - - -