feat: implement configurable delete modes
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"mime/multipart"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@@ -25,6 +26,7 @@ type Server struct {
|
||||
WebDAVRoot string
|
||||
Allow string
|
||||
MultiRoot bool
|
||||
RemoveMode int
|
||||
}
|
||||
type fileInfo struct {
|
||||
Name string `json:"name"`
|
||||
@@ -156,6 +158,7 @@ func (s *Server) rename(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
func (s *Server) remove(w http.ResponseWriter, r *http.Request) {
|
||||
a := account(r, s)
|
||||
_ = r.ParseMultipartForm(16 << 20)
|
||||
_ = r.ParseForm()
|
||||
items := r.Form["filepath"]
|
||||
if len(items) == 0 {
|
||||
@@ -166,13 +169,38 @@ func (s *Server) remove(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "forbidden", 403)
|
||||
return
|
||||
}
|
||||
if e := s.Store.Remove(p); e != nil {
|
||||
if e := s.removePath(p); e != nil {
|
||||
http.Error(w, e.Error(), 500)
|
||||
return
|
||||
}
|
||||
}
|
||||
writeJSON(w, map[string]any{"ok": true})
|
||||
}
|
||||
func (s *Server) removePath(p string) error {
|
||||
mode := s.RemoveMode
|
||||
if mode == 0 {
|
||||
mode = 1
|
||||
}
|
||||
t, ok := s.Store.(storage.Trashable)
|
||||
switch mode {
|
||||
case 1:
|
||||
if !ok {
|
||||
return fmt.Errorf("storage does not support system trash")
|
||||
}
|
||||
return t.Trash(p)
|
||||
case 2:
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return t.MoveToTrash(p, filepath.Join(home, ".chfs_trashbin"))
|
||||
case 3:
|
||||
return s.Store.Remove(p)
|
||||
default:
|
||||
return fmt.Errorf("invalid file.remove mode: %d", mode)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) exist(w http.ResponseWriter, r *http.Request) {
|
||||
p := strings.Trim(strings.ReplaceAll(r.URL.Query().Get("file"), "\\", "/"), "/")
|
||||
_, e := s.Store.Stat(p)
|
||||
|
||||
Reference in New Issue
Block a user