package server import ( "encoding/json" "net/http" ) type renamePathRequest struct { OldPath string `json:"old_path"` NewPath string `json:"new_path"` Recursive bool `json:"recursive"` } type renamePreview struct { Count int `json:"count"` } func (s *Server) handleRenamePath(w http.ResponseWriter, r *http.Request) { if !requirePathMutationPost(w, r) { return } var req renamePathRequest r.Body = http.MaxBytesReader(w, r.Body, 1<<20) if err := json.NewDecoder(r.Body).Decode(&req); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } req.OldPath = normalizeAuthorizationPath(req.OldPath) req.NewPath = normalizeAuthorizationPath(req.NewPath) if req.OldPath == "" || req.NewPath == "" || req.OldPath == "/" || req.NewPath == "/" { http.Error(w, "invalid path", http.StatusBadRequest) return } snapshot, err := s.authorizedMutationSnapshot(r, "rename", "create", req.OldPath, req.NewPath, req.Recursive) if err != nil { writePathMutationError(w, err) return } if r.URL.Query().Get("preview") == "1" { w.Header().Set("Content-Type", "application/json") _ = json.NewEncoder(w).Encode(renamePreview{Count: len(snapshot.Paths())}) return } count, err := s.store.RenamePathsInDomainChecked(s.domainUUIDForRequest(r), req.OldPath, req.NewPath, req.Recursive, snapshot) if err != nil { writePathMutationError(w, err) return } w.Header().Set("Content-Type", "application/json") _ = json.NewEncoder(w).Encode(renamePreview{Count: count}) }