Drag and drop files between folders
This commit is contained in:
@@ -263,6 +263,24 @@ func (s *Server) handleDeleteFile(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, 200, map[string]string{"status": "deleted"})
|
||||
}
|
||||
|
||||
func (s *Server) handleMoveFile(w http.ResponseWriter, r *http.Request) {
|
||||
var req struct {
|
||||
From string `json:"from"`
|
||||
To string `json:"to"`
|
||||
}
|
||||
if err := decodeBody(r, &req); err != nil || req.From == "" || req.To == "" {
|
||||
writeJSON(w, 400, map[string]string{"error": "from and to required"})
|
||||
return
|
||||
}
|
||||
|
||||
userID := getUserID(r)
|
||||
if err := files.MoveFile(s.dataDir, userID, req.From, req.To); err != nil {
|
||||
writeJSON(w, 500, map[string]string{"error": "move failed"})
|
||||
return
|
||||
}
|
||||
writeJSON(w, 200, map[string]string{"status": "moved"})
|
||||
}
|
||||
|
||||
func (s *Server) handleSharedFiles(w http.ResponseWriter, r *http.Request) {
|
||||
// TODO: query permissions table for files shared with this user
|
||||
// For now return empty list
|
||||
|
||||
@@ -35,6 +35,7 @@ func NewRouter(db *sql.DB, dataDir, secret string) http.Handler {
|
||||
mux.HandleFunc("POST /api/files/create", s.requireAuth(s.handleCreateFile))
|
||||
mux.HandleFunc("POST /api/files/create-folder", s.requireAuth(s.handleCreateFolder))
|
||||
mux.HandleFunc("POST /api/files/delete", s.requireAuth(s.handleDeleteFile))
|
||||
mux.HandleFunc("POST /api/files/move", s.requireAuth(s.handleMoveFile))
|
||||
mux.HandleFunc("POST /api/files/search", s.requireAuth(s.handleSearchFiles))
|
||||
mux.HandleFunc("POST /api/files/shared", s.requireAuth(s.handleListSharedFiles))
|
||||
|
||||
|
||||
@@ -63,6 +63,19 @@ func DeleteFile(dataDir, userID, relPath string) error {
|
||||
return os.RemoveAll(p)
|
||||
}
|
||||
|
||||
// MoveFile moves a file or folder to a new path.
|
||||
func MoveFile(dataDir, userID, fromRel, toRel string) error {
|
||||
from := safePath(dataDir, userID, fromRel)
|
||||
to := safePath(dataDir, userID, toRel)
|
||||
if from == "" || to == "" {
|
||||
return os.ErrPermission
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Dir(to), 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
return os.Rename(from, to)
|
||||
}
|
||||
|
||||
// ListTree returns the file tree for a user.
|
||||
func ListTree(dataDir, userID string) ([]FileInfo, error) {
|
||||
root := UserDir(dataDir, userID)
|
||||
|
||||
Reference in New Issue
Block a user