"""Private, card-scoped attachment validation and storage.""" import hashlib from pathlib import PurePath from uuid import uuid4 from django.conf import settings from django.core.exceptions import ValidationError from django.core.files.base import ContentFile from django.db import transaction from django.db.models import Sum from django.utils.text import get_valid_filename from .models import Activity, Card, CardAttachment, CardComment TEXT_EXTENSIONS = {'.txt', '.md', '.py', '.js', '.ts', '.tsx', '.jsx', '.json', '.css', '.html', '.xml', '.csv', '.yaml', '.yml', '.sh', '.diff', '.patch'} SIGNATURES = {'.png': (b'\x89PNG\r\n\x1a\n', 'image/png'), '.jpg': (b'\xff\xd8\xff', 'image/jpeg'), '.jpeg': (b'\xff\xd8\xff', 'image/jpeg'), '.webp': (b'RIFF', 'image/webp'), '.pdf': (b'%PDF-', 'application/pdf')} MAX_MCP_BYTES = 2 * 1024 * 1024 def attachment_data(item): return {'id': str(item.pk), 'card_id': str(item.card_id), 'comment_id': item.comment_id, 'title': item.title, 'original_filename': item.original_filename, 'mime_type': item.mime_type, 'byte_size': item.byte_size, 'sha256': item.sha256, 'description': item.description, 'uploaded_by_id': item.uploaded_by_id, 'source': item.source, 'source_path': item.source_path, 'source_commit': item.source_commit, 'created_at': item.created_at.isoformat()} def _validated_name(name): # Reject path components rather than silently changing their meaning. if not name or len(name) > 255 or '\\' in name or '/' in name or name in {'.', '..'}: raise ValidationError('Use a filename without a path.') clean = get_valid_filename(name) if not clean or clean.startswith('.'): raise ValidationError('Invalid filename.') return clean def _detected_type(name, data): suffix = PurePath(name).suffix.lower() if suffix in TEXT_EXTENSIONS: if b'\x00' in data: raise ValidationError('Text files must not contain binary data.') try: data.decode('utf-8') except UnicodeDecodeError: raise ValidationError('Text files must use UTF-8.') return 'text/plain' if suffix in SIGNATURES: signature, mime = SIGNATURES[suffix] if not data.startswith(signature): raise ValidationError('File contents do not match the extension.') if suffix == '.webp' and data[8:12] != b'WEBP': raise ValidationError('Invalid WebP file.') return mime raise ValidationError('Unsupported file type.') @transaction.atomic def upload_attachment(user, card, *, filename, data, comment_id=None, title='', description='', source='web', source_path='', source_commit=''): if len(data) > settings.PROJECTS_ATTACHMENT_MAX_BYTES: raise ValidationError('File exceeds the per-file size limit.') if not data: raise ValidationError('File is empty.') filename = _validated_name(filename) mime = _detected_type(filename, data) if len(title) > 255 or len(description) > 2000 or len(source_path) > 500: raise ValidationError('Attachment metadata is too long.') if source_commit and (len(source_commit) not in (40, 64) or any(c not in '0123456789abcdefABCDEF' for c in source_commit)): raise ValidationError('Source commit must be a Git SHA.') Card.objects.select_for_update().get(pk=card.pk) qs = CardAttachment.objects.filter(card=card) if qs.count() >= settings.PROJECTS_ATTACHMENT_MAX_FILES: raise ValidationError('Card attachment count limit reached.') if (qs.aggregate(total=Sum('byte_size'))['total'] or 0) + len(data) > settings.PROJECTS_ATTACHMENT_MAX_CARD_BYTES: raise ValidationError('Card attachment storage limit reached.') comment = None if comment_id is not None: comment = CardComment.objects.filter(card=card, pk=comment_id).first() if comment is None: raise ValidationError('Progress note does not belong to this card.') item = CardAttachment(card=card, comment=comment, original_filename=filename, title=title.strip() or filename, mime_type=mime, byte_size=len(data), sha256=hashlib.sha256(data).hexdigest(), description=description, uploaded_by=user, source=source, source_path=source_path, source_commit=source_commit) item.file.save(f'{uuid4().hex}{PurePath(filename).suffix.lower()}', ContentFile(data), save=False) try: item.full_clean() item.save() except Exception: item.file.delete(save=False) raise Activity.objects.create(project=card.project, card=card, actor=user, action='attachment.added', detail={'attachment_id': str(item.pk), 'comment_id': comment_id}, origin=source) return item def delete_attachment(user, item, *, source='web'): Activity.objects.create(project=item.card.project, card=item.card, actor=user, action='attachment.deleted', detail={'attachment_id': str(item.pk), 'comment_id': item.comment_id}, origin=source) item.delete()