Improve error handling in WebDAV file move operations and ensure target directory exists

This commit is contained in:
Leon Bösche
2026-02-07 23:35:48 +01:00
parent 6dad9432d0
commit b1ac8ce102
2 changed files with 30 additions and 17 deletions

View File

@@ -2286,15 +2286,19 @@ func moveOrgFileHandler(w http.ResponseWriter, r *http.Request, db *database.DB,
// Get or create user's WebDAV client and move in Nextcloud
storageClient, err := getUserWebDAVClient(r.Context(), db, userID, cfg.NextcloudURL, cfg.NextcloudUser, cfg.NextcloudPass)
if err != nil {
errors.LogError(r, err, "Failed to get user WebDAV client (continuing with database operation)")
} else {
sourceRel := strings.TrimPrefix(req.SourcePath, "/")
sourcePath := path.Join("/orgs", orgID.String(), sourceRel)
targetRel := strings.TrimPrefix(newPath, "/")
targetPath := path.Join("/orgs", orgID.String(), targetRel)
if err := storageClient.Move(r.Context(), sourcePath, targetPath); err != nil {
errors.LogError(r, err, "Failed to move in Nextcloud (continuing with database operation)")
}
errors.LogError(r, err, "Failed to get user WebDAV client")
errors.WriteError(w, errors.CodeInternal, "Storage error", http.StatusInternalServerError)
return
}
sourceRel := strings.TrimPrefix(req.SourcePath, "/")
sourcePath := path.Join("/orgs", orgID.String(), sourceRel)
targetRel := strings.TrimPrefix(newPath, "/")
targetPath := path.Join("/orgs", orgID.String(), targetRel)
if err := storageClient.Move(r.Context(), sourcePath, targetPath); err != nil {
errors.LogError(r, err, "Failed to move in Nextcloud")
errors.WriteError(w, errors.CodeInternal, "Failed to move file in storage", http.StatusInternalServerError)
return
}
// Update file record path and name in-place (preserves file ID for WOPI sessions)
@@ -2514,14 +2518,18 @@ func moveUserFileHandler(w http.ResponseWriter, r *http.Request, db *database.DB
// Get or create user's WebDAV client and move in Nextcloud
storageClient, err := getUserWebDAVClient(r.Context(), db, userID, cfg.NextcloudURL, cfg.NextcloudUser, cfg.NextcloudPass)
if err != nil {
errors.LogError(r, err, "Failed to get user WebDAV client (continuing with database operation)")
} else {
// User files are stored directly in the user's WebDAV root (no /users/{id} prefix)
sourcePath := "/" + strings.TrimPrefix(req.SourcePath, "/")
targetPath := "/" + strings.TrimPrefix(newPath, "/")
if err := storageClient.Move(r.Context(), sourcePath, targetPath); err != nil {
errors.LogError(r, err, "Failed to move in Nextcloud (continuing with database operation)")
}
errors.LogError(r, err, "Failed to get user WebDAV client")
errors.WriteError(w, errors.CodeInternal, "Storage error", http.StatusInternalServerError)
return
}
// User files are stored directly in the user's WebDAV root (no /users/{id} prefix)
sourcePath := "/" + strings.TrimPrefix(req.SourcePath, "/")
targetPath := "/" + strings.TrimPrefix(newPath, "/")
if err := storageClient.Move(r.Context(), sourcePath, targetPath); err != nil {
errors.LogError(r, err, "Failed to move in Nextcloud")
errors.WriteError(w, errors.CodeInternal, "Failed to move file in storage", http.StatusInternalServerError)
return
}
// Update file record path and name in-place (preserves file ID for WOPI sessions)

View File

@@ -240,6 +240,11 @@ func (c *WebDAVClient) Move(ctx context.Context, sourcePath, targetPath string)
return fmt.Errorf("no webdav client configured")
}
// Ensure target parent directory exists before moving
if err := c.ensureParent(ctx, targetPath); err != nil {
return fmt.Errorf("failed to create target directory: %w", err)
}
sourceRel := strings.TrimLeft(sourcePath, "/")
targetRel := strings.TrimLeft(targetPath, "/")