Skip to content

Commit

Permalink
feat: 파일 업로드 에러 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
km2535 committed Dec 5, 2024
1 parent 07b5a0b commit d3a4fe6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import lombok.RequiredArgsConstructor;

Expand All @@ -21,12 +22,13 @@ public class ClubImageFacade {

@Transactional
public String saveImage(ImageUploadRequest request) {
// uuid 객체 생성
MultipartFile originFile = request.multipartFile();

String uuid = UUID.randomUUID().toString();
String extension = getFileExtension(Objects.requireNonNull(request.multipartFile().getOriginalFilename()));
String extension = getFileExtension(Objects.requireNonNull(originFile.getOriginalFilename()));

// 비동기 처리
eventPublisher.publishEvent(new ClubImageEvent(request.multipartFile(), uuid));
eventPublisher.publishEvent(new ClubImageEvent(originFile, uuid));

// url 만드는 곳으로 전달
return preReturnUrl(uuid, extension);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Objects;

import org.badminton.api.common.exception.EmptyFileException;
import org.badminton.api.common.exception.FileSizeOverException;
Expand Down Expand Up @@ -37,13 +36,16 @@ public String uploadFile(MultipartFile uploadFile, String uuid) {
if (uploadFile.getSize() > MAX_FILE_SIZE) {
throw new FileSizeOverException(uploadFile.getSize());
}
log.info("42 체크 전 : {}", uploadFile.getName());

if (uploadFile.isEmpty() || Objects.isNull(uploadFile.getOriginalFilename())) {
throw new EmptyFileException();
}
// if (uploadFile.isEmpty() || Objects.isNull(uploadFile.getOriginalFilename())) {
// throw new EmptyFileException();
// }
log.info("uploadFile try 전 : {}", uploadFile.getName());
try {
String fileExtension = getFileExtension(uploadFile.getOriginalFilename());
byte[] processedImage = processImage(uploadFile, fileExtension);
log.info(" try processedImage : {}", processedImage);
String newFileExtension = determineNewFileExtension(fileExtension);
String fileName = makeFileName(newFileExtension, uuid);

Expand All @@ -54,6 +56,7 @@ public String uploadFile(MultipartFile uploadFile, String uuid) {
s3Client.putObject(new PutObjectRequest(bucket, fileName,
new ByteArrayInputStream(processedImage), objectMetadata)
.withCannedAcl(CannedAccessControlList.PublicRead));
log.info(" s3Client 실행 : {}", fileName);

return toCloudFrontUrl(s3Client.getUrl(bucket, fileName).toString());

Expand Down

0 comments on commit d3a4fe6

Please sign in to comment.