FIX: Create new file record at destination when moving file
This commit is contained in:
@@ -1294,6 +1294,37 @@ func moveOrgFileHandler(w http.ResponseWriter, r *http.Request, db *database.DB,
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get source file details before moving
|
||||||
|
sourceFiles, err := db.GetOrgFiles(r.Context(), orgID, userID, "/", "", 0, 1000)
|
||||||
|
if err != nil {
|
||||||
|
errors.LogError(r, err, "Failed to get org files")
|
||||||
|
errors.WriteError(w, errors.CodeInternal, "Server error", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var sourceFile *database.File
|
||||||
|
for i := range sourceFiles {
|
||||||
|
if sourceFiles[i].Path == req.SourcePath {
|
||||||
|
sourceFile = &sourceFiles[i]
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if sourceFile == nil {
|
||||||
|
errors.WriteError(w, errors.CodeInvalidArgument, "Source file not found", http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Determine new file name
|
||||||
|
var newPath string
|
||||||
|
if strings.HasSuffix(req.TargetPath, "/") {
|
||||||
|
// Moving into a folder
|
||||||
|
newPath = path.Join(req.TargetPath, sourceFile.Name)
|
||||||
|
} else {
|
||||||
|
// Moving/renaming to a specific path
|
||||||
|
newPath = req.TargetPath
|
||||||
|
}
|
||||||
|
|
||||||
// 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 {
|
||||||
@@ -1301,18 +1332,23 @@ func moveOrgFileHandler(w http.ResponseWriter, r *http.Request, db *database.DB,
|
|||||||
} else {
|
} else {
|
||||||
sourceRel := strings.TrimPrefix(req.SourcePath, "/")
|
sourceRel := strings.TrimPrefix(req.SourcePath, "/")
|
||||||
sourcePath := path.Join("/orgs", orgID.String(), sourceRel)
|
sourcePath := path.Join("/orgs", orgID.String(), sourceRel)
|
||||||
targetRel := strings.TrimPrefix(req.TargetPath, "/")
|
targetRel := strings.TrimPrefix(newPath, "/")
|
||||||
targetPath := path.Join("/orgs", orgID.String(), targetRel)
|
targetPath := path.Join("/orgs", orgID.String(), targetRel)
|
||||||
if err := storageClient.Move(r.Context(), sourcePath, targetPath); err != nil {
|
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 move in Nextcloud (continuing with database operation)")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete old file record from database - file will be synced with new path from storage
|
// Delete old file record
|
||||||
if err := db.DeleteFileByPath(r.Context(), &orgID, nil, req.SourcePath); err != nil {
|
if err := db.DeleteFileByPath(r.Context(), &orgID, nil, req.SourcePath); err != nil {
|
||||||
errors.LogError(r, err, "Failed to delete old file record")
|
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")
|
||||||
|
}
|
||||||
|
|
||||||
auditLogger.Log(r.Context(), audit.Entry{
|
auditLogger.Log(r.Context(), audit.Entry{
|
||||||
UserID: &userID,
|
UserID: &userID,
|
||||||
OrgID: &orgID,
|
OrgID: &orgID,
|
||||||
|
|||||||
Reference in New Issue
Block a user