feature: move folders
This commit is contained in:
@@ -1067,6 +1067,92 @@ func (s *Server) handleDocumentArchivePath(w http.ResponseWriter, r *http.Reques
|
||||
writeJSONWithStatus(w, http.StatusOK, map[string]string{"status": "archived"})
|
||||
}
|
||||
|
||||
func (s *Server) handleFolderMutation(w http.ResponseWriter, r *http.Request) {
|
||||
folderPath, err := unescapeDocumentRoutePath(chi.URLParam(r, "*"))
|
||||
if err != nil {
|
||||
writeJSONWithStatus(w, http.StatusBadRequest, map[string]string{"error": "invalid folder path"})
|
||||
return
|
||||
}
|
||||
switch {
|
||||
case strings.HasSuffix(folderPath, "/move"):
|
||||
s.handleFolderMovePath(w, r, strings.TrimSuffix(folderPath, "/move"))
|
||||
case strings.HasSuffix(folderPath, "/archive"):
|
||||
s.handleFolderArchivePath(w, r, strings.TrimSuffix(folderPath, "/archive"))
|
||||
default:
|
||||
writeJSONWithStatus(w, http.StatusNotFound, map[string]string{"error": "unsupported folder operation"})
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) handleFolderMovePath(w http.ResponseWriter, r *http.Request, folderPath string) {
|
||||
folder, err := docs.NormalizeFolderPath(folderPath)
|
||||
if err != nil {
|
||||
writeJSONWithStatus(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
var req struct {
|
||||
Path string `json:"path"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
writeJSONWithStatus(w, http.StatusBadRequest, map[string]string{"error": "invalid request body"})
|
||||
return
|
||||
}
|
||||
destination, err := docs.NormalizeFolderPath(req.Path)
|
||||
if err != nil {
|
||||
writeJSONWithStatus(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if !s.canWriteFolder(r, folder) || !s.canWriteFolder(r, documentFolder(destination)) {
|
||||
writeJSONWithStatus(w, http.StatusForbidden, map[string]string{"error": "permission denied"})
|
||||
return
|
||||
}
|
||||
|
||||
moved, err := s.documents.MoveFolder(r.Context(), folder, destination)
|
||||
if err != nil {
|
||||
var conflict *docs.DocumentMoveConflictError
|
||||
switch {
|
||||
case errors.As(err, &conflict):
|
||||
writeJSONWithStatus(w, http.StatusConflict, map[string]string{"error": conflict.Error()})
|
||||
case errors.Is(err, sql.ErrNoRows):
|
||||
writeJSONWithStatus(w, http.StatusNotFound, map[string]string{"error": "folder not found"})
|
||||
default:
|
||||
writeJSONWithStatus(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
||||
}
|
||||
return
|
||||
}
|
||||
writeJSONWithStatus(w, http.StatusOK, map[string]any{
|
||||
"status": "moved",
|
||||
"path": destination,
|
||||
"url": "/?folder=" + queryEscape(destination),
|
||||
"count": moved,
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Server) handleFolderArchivePath(w http.ResponseWriter, r *http.Request, folderPath string) {
|
||||
folder, err := docs.NormalizeFolderPath(folderPath)
|
||||
if err != nil {
|
||||
writeJSONWithStatus(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if !s.canWriteFolder(r, folder) {
|
||||
writeJSONWithStatus(w, http.StatusForbidden, map[string]string{"error": "permission denied"})
|
||||
return
|
||||
}
|
||||
|
||||
archived, err := s.documents.ArchiveFolder(r.Context(), folder)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
writeJSONWithStatus(w, http.StatusNotFound, map[string]string{"error": "folder not found"})
|
||||
return
|
||||
}
|
||||
writeJSONWithStatus(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
writeJSONWithStatus(w, http.StatusOK, map[string]any{
|
||||
"status": "archived",
|
||||
"count": archived,
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Server) handleDocumentRestorePath(w http.ResponseWriter, r *http.Request, pagePath string) {
|
||||
if pagePath == "" {
|
||||
writeJSONWithStatus(w, http.StatusBadRequest, map[string]string{"error": "document path is required"})
|
||||
|
||||
Reference in New Issue
Block a user