Add UpdateFilePath method to update file name and path while preserving ID

This commit is contained in:
Leon Bösche
2026-02-07 23:14:38 +01:00
parent 48b5210e8d
commit 6dad9432d0
2 changed files with 27 additions and 16 deletions

View File

@@ -2280,6 +2280,9 @@ func moveOrgFileHandler(w http.ResponseWriter, r *http.Request, db *database.DB,
newPath = req.TargetPath
}
// Determine new filename from the path
newName := path.Base(newPath)
// 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 {
@@ -2294,14 +2297,11 @@ func moveOrgFileHandler(w http.ResponseWriter, r *http.Request, db *database.DB,
}
}
// Delete old file record
if err := db.DeleteFileByPath(r.Context(), &orgID, nil, req.SourcePath); err != nil {
errors.LogError(r, err, "Failed to delete old file record")
}
// Create new file record at the new location
if _, err := db.CreateFile(r.Context(), &orgID, nil, sourceFile.Name, newPath, sourceFile.Type, sourceFile.Size); err != nil {
errors.LogError(r, err, "Failed to create new file record at destination")
// Update file record path and name in-place (preserves file ID for WOPI sessions)
if err := db.UpdateFilePath(r.Context(), sourceFile.ID, newName, newPath); err != nil {
errors.LogError(r, err, "Failed to update file path")
errors.WriteError(w, errors.CodeInternal, "Server error", http.StatusInternalServerError)
return
}
auditLogger.Log(r.Context(), audit.Entry{
@@ -2508,6 +2508,9 @@ func moveUserFileHandler(w http.ResponseWriter, r *http.Request, db *database.DB
newPath = req.TargetPath
}
// Determine new filename from the path
newName := path.Base(newPath)
// 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 {
@@ -2521,14 +2524,11 @@ func moveUserFileHandler(w http.ResponseWriter, r *http.Request, db *database.DB
}
}
// Delete old file record
if err := db.DeleteFileByPath(r.Context(), nil, &userID, req.SourcePath); err != nil {
errors.LogError(r, err, "Failed to delete old file record")
}
// Create new file record at the new location
if _, err := db.CreateFile(r.Context(), nil, &userID, sourceFile.Name, newPath, sourceFile.Type, sourceFile.Size); err != nil {
errors.LogError(r, err, "Failed to create new file record at destination")
// Update file record path and name in-place (preserves file ID for WOPI sessions)
if err := db.UpdateFilePath(r.Context(), sourceFile.ID, newName, newPath); err != nil {
errors.LogError(r, err, "Failed to update file path")
errors.WriteError(w, errors.CodeInternal, "Server error", http.StatusInternalServerError)
return
}
auditLogger.Log(r.Context(), audit.Entry{