-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b6f0793
commit 97a7c1e
Showing
8 changed files
with
168 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/main/java/com/tukorea/planding/domain/group/service/GroupRoomFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.tukorea.planding.domain.group.service; | ||
|
||
import com.tukorea.planding.domain.group.dto.request.GroupCreateRequest; | ||
import com.tukorea.planding.domain.group.entity.GroupRoom; | ||
import com.tukorea.planding.domain.user.entity.User; | ||
import com.tukorea.planding.global.config.s3.S3Uploader; | ||
import com.tukorea.planding.global.error.BusinessException; | ||
import com.tukorea.planding.global.error.ErrorCode; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.IOException; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class GroupRoomFactory { | ||
|
||
private final S3Uploader s3Uploader; | ||
|
||
public GroupRoom createGroupRoom(GroupCreateRequest createGroupRoom, User user, MultipartFile thumbnailFile) { | ||
GroupRoom newGroupRoom = GroupRoom.createGroupRoom(createGroupRoom, user); | ||
|
||
if (thumbnailFile != null && !thumbnailFile.isEmpty()) { | ||
try { | ||
String thumbnailUrl = s3Uploader.saveGroup(thumbnailFile); | ||
newGroupRoom.updateThumbnail(thumbnailUrl); | ||
} catch (IOException e) { | ||
throw new BusinessException(ErrorCode.FILE_UPLOAD_ERROR); | ||
} | ||
} | ||
|
||
return newGroupRoom; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/main/java/com/tukorea/planding/global/config/s3/S3Config.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.tukorea.planding.global.config.s3; | ||
|
||
import com.amazonaws.auth.AWSStaticCredentialsProvider; | ||
import com.amazonaws.auth.BasicAWSCredentials; | ||
import com.amazonaws.services.s3.AmazonS3Client; | ||
import com.amazonaws.services.s3.AmazonS3ClientBuilder; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class S3Config { | ||
@Value("${cloud.aws.credentials.access-key}") | ||
private String accessKey; | ||
@Value("${cloud.aws.credentials.secret-key}") | ||
private String secretKey; | ||
@Value("${cloud.aws.region.static}") | ||
private String region; | ||
|
||
@Bean | ||
public AmazonS3Client amazonS3Client() { | ||
BasicAWSCredentials awsCredentials= new BasicAWSCredentials(accessKey, secretKey); | ||
return (AmazonS3Client) AmazonS3ClientBuilder.standard() | ||
.withRegion(region) | ||
.withCredentials(new AWSStaticCredentialsProvider(awsCredentials)) | ||
.build(); | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
src/main/java/com/tukorea/planding/global/config/s3/S3Uploader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package com.tukorea.planding.global.config.s3; | ||
|
||
import com.amazonaws.services.s3.AmazonS3Client; | ||
import com.amazonaws.services.s3.model.CannedAccessControlList; | ||
import com.amazonaws.services.s3.model.PutObjectRequest; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.util.Optional; | ||
import java.util.UUID; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
@Slf4j | ||
public class S3Uploader { | ||
private final AmazonS3Client amazonS3Client; | ||
|
||
@Value("${cloud.aws.s3.bucket}") | ||
private String bucket; | ||
|
||
private final String PROFILE_IMG_DIR = "profile/"; | ||
private final String GROUP_THUMBNAIL = "thumbnail/"; | ||
|
||
public String saveProfile(MultipartFile multipartFile) throws IOException { | ||
File uploadFile = convert(multipartFile) | ||
.orElseThrow(() -> new IllegalArgumentException("MultipartFile -> File 전환 실패")); | ||
return uploadT(uploadFile, PROFILE_IMG_DIR); | ||
} | ||
|
||
public String saveGroup(MultipartFile multipartFile) throws IOException { | ||
File uploadFile = convert(multipartFile) | ||
.orElseThrow(() -> new IllegalArgumentException("MultipartFile -> File 전환 실패")); | ||
return uploadT(uploadFile, GROUP_THUMBNAIL); | ||
} | ||
|
||
private String uploadT(File uploadFile, String dirName) { | ||
String fileName = dirName + "/" + UUID.randomUUID() + uploadFile.getName(); | ||
String uploadImageUrl = putS3(uploadFile, fileName); | ||
removeNewFile(uploadFile); // 로컬에 생성된 File 삭제 (MultipartFile -> File 전환 하며 로컬에 파일 생성됨) | ||
return uploadImageUrl; // 업로드된 파일의 S3 URL 주소 반환 | ||
} | ||
|
||
private void removeNewFile(File targetFile) { | ||
if (targetFile.delete()) { | ||
log.info("파일이 삭제되었습니다."); | ||
} else { | ||
log.info("파일이 삭제되지 못했습니다."); | ||
} | ||
} | ||
|
||
private String putS3(File uploadFile, String fileName) { | ||
amazonS3Client.putObject( | ||
new PutObjectRequest(bucket, fileName, uploadFile) | ||
.withCannedAcl(CannedAccessControlList.PublicRead) // PublicRead 권한으로 업로드 됨 | ||
); | ||
return amazonS3Client.getUrl(bucket, fileName).toString(); | ||
} | ||
|
||
public Optional<File> convert(MultipartFile file) throws IOException { | ||
// mac 환경에서 파일이름에 공백이 허용이 되기때문에 공백을 다른문자로 대체해야함 | ||
String newFilename = file.getOriginalFilename().replaceAll("\\s+", "_"); | ||
|
||
// 임시 파일 생성 | ||
File convertFile = Files.createTempFile(newFilename, null).toFile(); | ||
try (FileOutputStream fos = new FileOutputStream(convertFile)) { | ||
fos.write(file.getBytes()); | ||
} catch (IOException e) { | ||
// 파일 변환 실패 시 임시 파일 삭제 | ||
convertFile.delete(); | ||
throw e; | ||
} | ||
return Optional.of(convertFile); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters