File preview

This commit is contained in:
2025-06-01 12:31:10 +02:00
parent c0a97a1714
commit aeefd17b10
8 changed files with 280 additions and 8 deletions

View File

@@ -275,41 +275,53 @@ def upload_room_file(room_id):
@login_required
def download_room_file(room_id, filename):
"""
Download a file from a room.
Download or preview a file from a room.
Args:
room_id (int): ID of the room containing the file
filename (str): Name of the file to download
filename (str): Name of the file to download/preview
Returns:
File download response or error message
File download/preview response or error message
"""
room = Room.query.get_or_404(room_id)
if not user_has_permission(room, 'can_download'):
abort(403)
rel_path = clean_path(request.args.get('path', ''))
preview_mode = request.args.get('preview', 'false').lower() == 'true'
# Lookup in RoomFile
rf = RoomFile.query.filter_by(room_id=room_id, name=filename, path=rel_path).first()
if not rf or rf.type != 'file':
return jsonify({'error': 'File not found'}), 404
room_dir = get_room_dir(room_id)
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 the event
log_event(
event_type='file_download',
event_type='file_download' if not preview_mode else 'file_preview',
details={
'downloaded_by': f"{current_user.username} {current_user.last_name}",
'user': f"{current_user.username} {current_user.last_name}",
'filename': filename,
'room_id': room_id,
'path': rel_path,
'size': rf.size if rf else None
'size': rf.size if rf else None,
'preview': preview_mode
},
user_id=current_user.id
)
db.session.commit()
return send_from_directory(os.path.dirname(file_path), filename, as_attachment=True)
# For preview mode, we don't set as_attachment
return send_from_directory(
os.path.dirname(file_path),
filename,
as_attachment=not preview_mode
)
@room_files_bp.route('/<int:room_id>/files/<path:filename>', methods=['DELETE'])
@login_required