package server import ( "encoding/json" "errors" "net/http" "pageswiki/internal/store" ) // Kept for older in-process callers; the registered route uses handleRenamePath. func (s *Server) handleRenamePathAPI(w http.ResponseWriter, r *http.Request) { s.handleRenamePath(w, r) } func requirePathMutationPost(w http.ResponseWriter, r *http.Request) bool { if r.Method == http.MethodPost { return true } w.Header().Set("Allow", http.MethodPost) http.Error(w, "method not allowed", http.StatusMethodNotAllowed) return false } func writePathMutationError(w http.ResponseWriter, err error) { status := http.StatusInternalServerError switch { case errors.Is(err, store.ErrAccessDenied): status = http.StatusForbidden case errors.Is(err, store.ErrPathConflict), errors.Is(err, store.ErrPathSnapshotChanged): status = http.StatusConflict case errors.Is(err, store.ErrPathNotFound): status = http.StatusNotFound case errors.Is(err, store.ErrPathMutationTooLarge): status = http.StatusRequestEntityTooLarge } http.Error(w, err.Error(), status) } func (s *Server) handleRemovePathAPI(w http.ResponseWriter, r *http.Request) { if !requirePathMutationPost(w, r) { return } var req struct { Path string `json:"path"` Paths []string `json:"paths"` Recursive bool `json:"recursive"` } 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 } if req.Path != "" { req.Paths = append(req.Paths, req.Path) } if len(req.Paths) == 0 || len(req.Paths) > 1000 { http.Error(w, "between 1 and 1000 paths are required", http.StatusBadRequest) return } unique := map[string]bool{} var snapshots []*store.PathMutationSnapshot bindingCount := 0 for _, path := range req.Paths { path = normalizeAuthorizationPath(path) if path == "" || path == "/" { http.Error(w, "invalid path", http.StatusBadRequest) return } if unique[path] { continue } unique[path] = true snapshot, err := s.authorizedMutationSnapshot(r, "delete", "", path, "", req.Recursive) if err != nil { writePathMutationError(w, err) return } bindingCount += len(snapshot.Paths()) if bindingCount > store.MaxPathMutationPaths { writePathMutationError(w, store.ErrPathMutationTooLarge) return } snapshots = append(snapshots, snapshot) } if _, err := s.store.RemovePathsInDomainChecked(s.domainUUIDForRequest(r), snapshots); err != nil { writePathMutationError(w, err) return } w.WriteHeader(http.StatusNoContent) } func (s *Server) handleCopyPathAPI(w http.ResponseWriter, r *http.Request) { if !requirePathMutationPost(w, r) { return } var req struct { SourcePath string `json:"source_path"` DestinationPath string `json:"destination_path"` Recursive bool `json:"recursive"` } 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.SourcePath = normalizeAuthorizationPath(req.SourcePath) req.DestinationPath = normalizeAuthorizationPath(req.DestinationPath) if req.SourcePath == "" || req.DestinationPath == "" || req.SourcePath == "/" || req.DestinationPath == "/" { http.Error(w, "invalid path", http.StatusBadRequest) return } snapshot, err := s.authorizedMutationSnapshot(r, "read", "create", req.SourcePath, req.DestinationPath, req.Recursive) if err != nil { writePathMutationError(w, err) return } count, err := s.store.CopyPathsInDomainChecked(s.domainUUIDForRequest(r), req.SourcePath, req.DestinationPath, req.Recursive, snapshot) if err != nil { writePathMutationError(w, err) return } w.Header().Set("Content-Type", "application/json") _ = json.NewEncoder(w).Encode(map[string]int{"copied": count}) }