package org.example.service; import jakarta.servlet.http.HttpServletResponse; import lombok.RequiredArgsConstructor; import org.example.config.ImageUploadValidator; import org.example.dto.response.AttachmentResponse; import org.example.exception.BadRequestException; import org.example.exception.NotFoundException; import org.example.model.Attachment; import org.example.model.User; import org.example.repository.AttachmentRepository; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.time.Instant; import java.util.List; import java.util.UUID; @Service @RequiredArgsConstructor public class AttachmentService { private final AttachmentRepository attachmentRepository; private final S3StorageService s3StorageService; private final ImageUploadValidator validator; private final ChatService chatService; private final UserService userService; @Transactional public AttachmentResponse upload( UUID chatId, String filename, String contentTypeHeader, InputStream body ) throws IOException { User user = userService.getCurrentUser(); chatService.requireMember(chatId); ImageUploadValidator.ValidatedImage img = validator.validate(filename, body); UUID id = UUID.randomUUID(); String objectKey = "chats/" + chatId + "/" + id; s3StorageService.put( objectKey, new ByteArrayInputStream(img.bytes()), img.bytes().length, img.contentType() ); Attachment saved = attachmentRepository.save(Attachment.builder() .id(id) .chatId(chatId) .ownerId(user.getId()) .objectKey(objectKey) .filename(img.safeFilename()) .contentType(img.contentType()) .sizeBytes(img.bytes().length) .sha256(img.sha256Hex()) .status("ready") .errorCode(null) .variants(List.of()) .createdAt(Instant.now()) .updatedAt(Instant.now()) .build()); return toResponse(saved); } @Transactional(readOnly = true) public Attachment getForMember(UUID attachmentId){ Attachment attachment = attachmentRepository.findById(attachmentId) .orElseThrow(()-> new NotFoundException("Вложение не найдено")); chatService.requireMember(attachment.getChatId()); return attachment; } @Transactional(readOnly = true) public AttachmentResponse getMeta(UUID attachmentId){ return toResponse(getForMember(attachmentId)); } @Transactional(readOnly = true) public void writeContent(UUID attachmentId, String variant, HttpServletResponse response) throws IOException { if (variant != null && !variant.isBlank() && !"original".equals(variant)){ throw new BadRequestException("Unsupported variant"); } Attachment attachment = getForMember(attachmentId); response.setStatus(HttpStatus.OK.value()); response.setContentType(attachment.getContentType()); response.setContentLengthLong(attachment.getSizeBytes()); s3StorageService.getTo(attachment.getObjectKey(), response.getOutputStream()); response.flushBuffer(); } static AttachmentResponse toResponse(Attachment a) { return AttachmentResponse.builder() .id(a.getId()) .chatId(a.getChatId()) .ownerId(a.getOwnerId()) .filename(a.getFilename()) .contentType(a.getContentType()) .sizeBytes(a.getSizeBytes()) .sha256(a.getSha256()) .status(a.getStatus()) .variants(a.getVariants() == null ? List.of() : a.getVariants()) .errorCode(a.getErrorCode()) .createdAt(a.getCreatedAt()) .updatedAt(a.getUpdatedAt()) .build(); } }