package ru.mtusi.messenger.controller; import jakarta.servlet.http.HttpServletRequest; import org.springframework.core.io.InputStreamResource; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.security.core.Authentication; import org.springframework.web.bind.annotation.*; import ru.mtusi.messenger.dto.Attachment; import ru.mtusi.messenger.entity.AttachmentEntity; import ru.mtusi.messenger.entity.ChatEntity; import ru.mtusi.messenger.exception.NotFoundException; import ru.mtusi.messenger.exception.UnauthorizedException; import ru.mtusi.messenger.repository.AttachmentRepository; import ru.mtusi.messenger.repository.ChatRepository; import ru.mtusi.messenger.service.AttachmentService; import ru.mtusi.messenger.service.StorageService; import java.io.InputStream; import java.util.UUID; @RestController @RequestMapping("/api/v1") public class AttachmentController { private final AttachmentService attachmentService; private final AttachmentRepository attachmentRepository; private final ChatRepository chatRepository; private final StorageService storageService; public AttachmentController(AttachmentService attachmentService, AttachmentRepository attachmentRepository, ChatRepository chatRepository, StorageService storageService) { this.attachmentService = attachmentService; this.attachmentRepository = attachmentRepository; this.chatRepository = chatRepository; this.storageService = storageService; } @PostMapping("/chats/{chat_id}/attachments") @ResponseStatus(HttpStatus.ACCEPTED) public Attachment upload( Authentication auth, @PathVariable("chat_id") UUID chatId, @RequestParam("filename") String filename, @RequestHeader("Content-Type") String contentType, InputStream inputStream, HttpServletRequest request) throws Exception { UUID callerId = getUserId(auth); long size = request.getContentLengthLong(); return attachmentService.upload(callerId, chatId, filename, contentType, inputStream, size); } @GetMapping("/attachments/{attachment_id}") public Attachment getMetadata( Authentication auth, @PathVariable("attachment_id") UUID attachmentId) { UUID callerId = getUserId(auth); AttachmentEntity entity = attachmentRepository.findById(attachmentId) .orElseThrow(() -> new NotFoundException("Attachment not found")); verifyAccess(callerId, entity.getChatId()); return attachmentService.toDto(entity); } @GetMapping("/attachments/{attachment_id}/content") public ResponseEntity download( Authentication auth, @PathVariable("attachment_id") UUID attachmentId, @RequestParam(value = "variant", required = false) String variantName) { UUID callerId = getUserId(auth); AttachmentEntity entity = attachmentRepository.findById(attachmentId) .orElseThrow(() -> new NotFoundException("Attachment not found")); verifyAccess(callerId, entity.getChatId()); String keyToDownload = entity.getObjectKey(); String contentType = entity.getContentType(); String filename = entity.getFilename(); if (variantName != null && !variantName.isBlank()) { var variantOpt = entity.getVariants().stream() .filter(v -> v.getName().equals(variantName)) .findFirst(); if (variantOpt.isPresent()) { var variant = variantOpt.get(); keyToDownload = variant.getObjectKey(); contentType = variant.getContentType(); filename = variantName + ".jpg"; } } InputStream stream = storageService.download(keyToDownload); return ResponseEntity.ok() .contentType(MediaType.parseMediaType(contentType)) .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + filename + "\"") .body(new InputStreamResource(stream)); } private void verifyAccess(UUID callerId, UUID chatId) { ChatEntity chat = chatRepository.findById(chatId) .orElseThrow(() -> new NotFoundException("Chat not found")); boolean isMember = chat.getMembers().stream().anyMatch(u -> u.getId().equals(callerId)); if (!isMember) throw new NotFoundException("Chat not found"); } private UUID getUserId(Authentication auth) { if (auth == null || !(auth.getPrincipal() instanceof UUID)) { throw new UnauthorizedException("Not authenticated"); } return (UUID) auth.getPrincipal(); } }