Improve error handling in WebDAV file move operations and ensure target directory exists
This commit is contained in:
@@ -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
|
// Get or create user's WebDAV client and move in Nextcloud
|
||||||
storageClient, err := getUserWebDAVClient(r.Context(), db, userID, cfg.NextcloudURL, cfg.NextcloudUser, cfg.NextcloudPass)
|
storageClient, err := getUserWebDAVClient(r.Context(), db, userID, cfg.NextcloudURL, cfg.NextcloudUser, cfg.NextcloudPass)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errors.LogError(r, err, "Failed to get user WebDAV client (continuing with database operation)")
|
errors.LogError(r, err, "Failed to get user WebDAV client")
|
||||||
} else {
|
errors.WriteError(w, errors.CodeInternal, "Storage error", http.StatusInternalServerError)
|
||||||
sourceRel := strings.TrimPrefix(req.SourcePath, "/")
|
return
|
||||||
sourcePath := path.Join("/orgs", orgID.String(), sourceRel)
|
}
|
||||||
targetRel := strings.TrimPrefix(newPath, "/")
|
|
||||||
targetPath := path.Join("/orgs", orgID.String(), targetRel)
|
sourceRel := strings.TrimPrefix(req.SourcePath, "/")
|
||||||
if err := storageClient.Move(r.Context(), sourcePath, targetPath); err != nil {
|
sourcePath := path.Join("/orgs", orgID.String(), sourceRel)
|
||||||
errors.LogError(r, err, "Failed to move in Nextcloud (continuing with database operation)")
|
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)
|
// 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
|
// Get or create user's WebDAV client and move in Nextcloud
|
||||||
storageClient, err := getUserWebDAVClient(r.Context(), db, userID, cfg.NextcloudURL, cfg.NextcloudUser, cfg.NextcloudPass)
|
storageClient, err := getUserWebDAVClient(r.Context(), db, userID, cfg.NextcloudURL, cfg.NextcloudUser, cfg.NextcloudPass)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errors.LogError(r, err, "Failed to get user WebDAV client (continuing with database operation)")
|
errors.LogError(r, err, "Failed to get user WebDAV client")
|
||||||
} else {
|
errors.WriteError(w, errors.CodeInternal, "Storage error", http.StatusInternalServerError)
|
||||||
// User files are stored directly in the user's WebDAV root (no /users/{id} prefix)
|
return
|
||||||
sourcePath := "/" + strings.TrimPrefix(req.SourcePath, "/")
|
}
|
||||||
targetPath := "/" + strings.TrimPrefix(newPath, "/")
|
|
||||||
if err := storageClient.Move(r.Context(), sourcePath, targetPath); err != nil {
|
// User files are stored directly in the user's WebDAV root (no /users/{id} prefix)
|
||||||
errors.LogError(r, err, "Failed to move in Nextcloud (continuing with database operation)")
|
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)
|
// Update file record path and name in-place (preserves file ID for WOPI sessions)
|
||||||
|
|||||||
@@ -240,6 +240,11 @@ func (c *WebDAVClient) Move(ctx context.Context, sourcePath, targetPath string)
|
|||||||
return fmt.Errorf("no webdav client configured")
|
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, "/")
|
sourceRel := strings.TrimLeft(sourcePath, "/")
|
||||||
targetRel := strings.TrimLeft(targetPath, "/")
|
targetRel := strings.TrimLeft(targetPath, "/")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user