fix folder rename not retaining files

This commit is contained in:
2025-05-27 11:52:51 +02:00
parent dca23787e4
commit f0a2f28f8e
2 changed files with 17 additions and 0 deletions

View File

@@ -303,6 +303,23 @@ def rename_room_file(room_id):
# Update RoomFile entry # Update RoomFile entry
rf.name = new_name rf.name = new_name
rf.modified = os.path.getmtime(new_path) rf.modified = os.path.getmtime(new_path)
# If this is a folder, update paths of all contained files and subfolders
if os.path.isdir(new_path):
old_folder_path = os.path.join(rel_path, old_name) if rel_path else old_name
new_folder_path = os.path.join(rel_path, new_name) if rel_path else new_name
# Get all files and folders that are under the old path
contained_items = RoomFile.query.filter(
RoomFile.room_id == room_id,
RoomFile.path.like(f"{old_folder_path}%")
).all()
# Update their paths
for item in contained_items:
# Replace the old folder path with the new one in the item's path
item.path = item.path.replace(old_folder_path, new_folder_path, 1)
db.session.commit() db.session.commit()
return jsonify({'success': True, 'old_name': old_name, 'new_name': new_name}) return jsonify({'success': True, 'old_name': old_name, 'new_name': new_name})