added a lot of logging

This commit is contained in:
2025-05-30 20:43:14 +02:00
parent c09a5c758e
commit a08345e676
11 changed files with 247 additions and 2 deletions

View File

@@ -29,6 +29,7 @@ import shutil
import io
import zipfile
from datetime import datetime
from utils import log_event
# Blueprint for room file operations
room_files_bp = Blueprint('room_files', __name__, url_prefix='/api/rooms')
@@ -221,6 +222,18 @@ def upload_room_file(room_id):
existing_file.uploaded_by = current_user.id
existing_file.uploaded_at = datetime.utcnow()
db.session.commit()
log_event(
event_type='file_upload',
details={
'uploaded_by': f"{current_user.username} {current_user.last_name}",
'filename': filename,
'room_id': room_id,
'path': rel_path,
'size': stat.st_size,
'overwritten': True
},
user_id=current_user.id
)
return jsonify({'success': True, 'filename': filename, 'overwritten': True})
else:
rf = RoomFile(
@@ -235,6 +248,18 @@ def upload_room_file(room_id):
)
db.session.add(rf)
db.session.commit()
log_event(
event_type='file_upload',
details={
'uploaded_by': f"{current_user.username} {current_user.last_name}",
'filename': filename,
'room_id': room_id,
'path': rel_path,
'size': stat.st_size,
'overwritten': False
},
user_id=current_user.id
)
return jsonify({'success': True, 'filename': filename})
@room_files_bp.route('/<int:room_id>/files/<filename>', methods=['GET'])
@@ -262,6 +287,18 @@ def download_room_file(room_id, filename):
file_path = os.path.join(room_dir, rel_path, filename) if rel_path else os.path.join(room_dir, filename)
if not os.path.exists(file_path):
return jsonify({'error': 'File not found'}), 404
log_event(
event_type='file_download',
details={
'downloaded_by': f"{current_user.username} {current_user.last_name}",
'filename': filename,
'room_id': room_id,
'path': rel_path,
'size': rf.size if rf else None
},
user_id=current_user.id
)
return send_from_directory(os.path.dirname(file_path), filename, as_attachment=True)
@room_files_bp.route('/<int:room_id>/files/<path:filename>', methods=['DELETE'])
@@ -294,6 +331,19 @@ def delete_file(room_id, filename):
rf.deleted_at = datetime.utcnow()
db.session.commit()
log_event(
event_type='file_delete',
details={
'deleted_by': f"{current_user.username} {current_user.last_name}",
'filename': filename,
'room_id': room_id,
'path': rel_path,
'type': rf.type,
'size': rf.size if rf.type == 'file' else None
},
user_id=current_user.id
)
return jsonify({'success': True})
@room_files_bp.route('/<int:room_id>/folders', methods=['POST'])
@@ -429,6 +479,22 @@ def rename_room_file(room_id):
item.path = item.path.replace(old_folder_path, new_folder_path, 1)
db.session.commit()
log_event(
event_type='file_rename',
details={
'renamed_by': f"{current_user.username} {current_user.last_name}",
'old_name': old_name,
'new_name': new_name,
'room_id': room_id,
'path': rel_path,
'type': rf.type,
'size': rf.size if rf.type == 'file' else None,
'is_folder': os.path.isdir(new_path)
},
user_id=current_user.id
)
return jsonify({'success': True, 'old_name': old_name, 'new_name': new_name})
@room_files_bp.route('/<int:room_id>/download-zip', methods=['POST'])
@@ -554,6 +620,20 @@ def move_room_file(room_id):
rf.modified = os.path.getmtime(target_file_path)
db.session.commit()
log_event(
event_type='file_move',
details={
'moved_by': f"{current_user.username} {current_user.last_name}",
'filename': filename,
'room_id': room_id,
'source_path': source_path,
'target_path': target_path,
'type': rf.type,
'size': rf.size if rf.type == 'file' else None
},
user_id=current_user.id
)
return jsonify({'success': True})
@room_files_bp.route('/<int:room_id>/folders', methods=['GET'])
@@ -822,6 +902,19 @@ def restore_file(room_id):
rf.deleted = False
db.session.commit()
log_event(
event_type='file_restore',
details={
'restored_by': f"{current_user.username} {current_user.last_name}",
'filename': filename,
'room_id': room_id,
'path': rel_path,
'type': rf.type,
'size': rf.size if rf.type == 'file' else None
},
user_id=current_user.id
)
return jsonify({'success': True})
@room_files_bp.route('/<int:room_id>/delete-permanent', methods=['POST'])
@@ -880,6 +973,19 @@ def delete_permanent(room_id):
for item in contained_items:
db.session.delete(item)
log_event(
event_type='file_delete_permanent',
details={
'deleted_by': f"{current_user.username} {current_user.last_name}",
'filename': rf.name,
'room_id': room_id,
'path': rf.path,
'type': rf.type,
'size': rf.size if rf.type == 'file' else None
},
user_id=current_user.id
)
except Exception as e:
print(f"Error deleting {rf.type} from storage: {e}")