Skip to content

Commit

Permalink
Merge pull request #100 from miinyeong/main
Browse files Browse the repository at this point in the history
Fix: 이미지 업로드 파일명 랜덤 생성후 저장
  • Loading branch information
mummhy0811 authored Nov 18, 2023
2 parents f88b1cc + 71273e1 commit 0c132f2
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/main/java/com/onestep/server/service/image/S3Uploader.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.UUID;


@Slf4j
Expand All @@ -35,13 +37,15 @@ public class S3Uploader {

// MultipartFile을 전달받아 File로 전환한 후 S3에 업로드
public String upload(MultipartFile multipartFile, String dirName) throws IOException {

log.info("multipartFile={}",multipartFile);
File uploadFile = convert(multipartFile); // 파일 변환할 수 없으면 에러
return upload(uploadFile, dirName);
}

// s3로 업로드
private String upload(File uploadFile, String dirName) {

String fileName = dirName + "/" + uploadFile.getName();
String uploadImageUrl = putS3(uploadFile, fileName);

Expand Down Expand Up @@ -69,8 +73,9 @@ private void removeNewFile(File targetFile) {
}

// 로컬에 파일 업로드 하기
private File convert(MultipartFile file) throws IOException {
File convertFile = new File(file.getOriginalFilename());
private File convert(MultipartFile file) throws IOException {
String storeFilename = UUID.randomUUID() + "." + extractExt(file.getOriginalFilename());
File convertFile = new File(storeFilename);
convertFile.createNewFile();
FileOutputStream fos = new FileOutputStream(convertFile);
fos.write(file.getBytes());
Expand All @@ -84,4 +89,9 @@ public void delete(String fileKey) {
amazonS3Client.deleteObject(bucket, fileKey);
}

// 파일 확장자 추출
private String extractExt(String originalFilename) {
int pos = originalFilename.lastIndexOf(".");
return originalFilename.substring(pos + 1);
}
}

0 comments on commit 0c132f2

Please sign in to comment.