forked from mtusi2026/mtusi2026lab4
на 3 готово
This commit is contained in:
@@ -1,19 +1,23 @@
|
||||
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 org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
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
|
||||
@@ -51,14 +55,17 @@ public class AttachmentService {
|
||||
Attachment saved = attachmentRepository.save(Attachment.builder()
|
||||
.id(id)
|
||||
.chatId(chatId)
|
||||
.uploaderId(user.getId())
|
||||
.ownerId(user.getId())
|
||||
.objectKey(objectKey)
|
||||
.originalFilename(img.safeFilename())
|
||||
.filename(img.safeFilename())
|
||||
.contentType(img.contentType())
|
||||
.sizeBytes(img.bytes().length)
|
||||
.checksumSha256(img.sha256Hex())
|
||||
.width(img.width())
|
||||
.height(img.height())
|
||||
.sha256(img.sha256Hex())
|
||||
.status("ready")
|
||||
.errorCode(null)
|
||||
.variants(List.of())
|
||||
.createdAt(Instant.now())
|
||||
.updatedAt(Instant.now())
|
||||
.build());
|
||||
|
||||
return toResponse(saved);
|
||||
@@ -72,19 +79,47 @@ public class AttachmentService {
|
||||
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())
|
||||
.uploaderId(a.getUploaderId())
|
||||
.originalFilename(a.getOriginalFilename())
|
||||
.ownerId(a.getOwnerId())
|
||||
.filename(a.getFilename())
|
||||
.contentType(a.getContentType())
|
||||
.sizeBytes(a.getSizeBytes())
|
||||
.checksumSha256(a.getChecksumSha256())
|
||||
.width(a.getWidth())
|
||||
.height(a.getHeight())
|
||||
.sha256(a.getSha256())
|
||||
.status(a.getStatus())
|
||||
.variants(a.getVariants() == null ? List.of() : a.getVariants())
|
||||
.errorCode(a.getErrorCode())
|
||||
.createdAt(a.getCreatedAt())
|
||||
.updatedAt(a.getUpdatedAt())
|
||||
.build();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user