-
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.
v1.1.0
- Loading branch information
Showing
120 changed files
with
2,771 additions
and
1,990 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
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
This file was deleted.
Oops, something went wrong.
30 changes: 2 additions & 28 deletions
30
backend/src/main/java/com/funeat/common/ImageUploader.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 |
---|---|---|
@@ -1,34 +1,8 @@ | ||
package com.funeat.common; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.UUID; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
@Component | ||
@Profile("!test") | ||
public class ImageUploader implements ImageService { | ||
public interface ImageUploader { | ||
|
||
@Value("${image.path}") | ||
private String imagePath; | ||
|
||
@Override | ||
public void upload(final MultipartFile image, final String newFileName) { | ||
final Path path = Paths.get(imagePath + newFileName); | ||
try { | ||
Files.write(path, image.getBytes()); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public String getRandomImageName(final MultipartFile image) { | ||
return UUID.randomUUID() + image.getOriginalFilename(); | ||
} | ||
String upload(final MultipartFile image); | ||
} |
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
25 changes: 25 additions & 0 deletions
25
backend/src/main/java/com/funeat/common/exception/CommonException.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,25 @@ | ||
package com.funeat.common.exception; | ||
|
||
import com.funeat.exception.CommonErrorCode; | ||
import com.funeat.exception.ErrorCode; | ||
import com.funeat.exception.GlobalException; | ||
import org.springframework.http.HttpStatus; | ||
|
||
public class CommonException extends GlobalException { | ||
|
||
public CommonException(final HttpStatus status, final ErrorCode errorCode) { | ||
super(status, errorCode); | ||
} | ||
|
||
public static class NotAllowedFileExtensionException extends CommonException { | ||
public NotAllowedFileExtensionException(final CommonErrorCode errorCode, final String extension) { | ||
super(errorCode.getStatus(), new ErrorCode<>(errorCode.getCode(), errorCode.getMessage(), extension)); | ||
} | ||
} | ||
|
||
public static class S3UploadFailException extends CommonException { | ||
public S3UploadFailException(final CommonErrorCode errorCode) { | ||
super(errorCode.getStatus(), new ErrorCode<>(errorCode.getCode(), errorCode.getMessage())); | ||
} | ||
} | ||
} |
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.funeat.common.s3; | ||
|
||
import com.amazonaws.auth.InstanceProfileCredentialsProvider; | ||
import com.amazonaws.services.s3.AmazonS3; | ||
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 AwsConfig { | ||
|
||
@Value("${cloud.aws.region.static}") | ||
private String region; | ||
|
||
@Bean | ||
public InstanceProfileCredentialsProvider awsCredentialsProvider() { | ||
return InstanceProfileCredentialsProvider.getInstance(); | ||
} | ||
|
||
@Bean | ||
public AmazonS3 amazonS3Client() { | ||
return AmazonS3ClientBuilder.standard() | ||
.withRegion(region) | ||
.withCredentials(awsCredentialsProvider()) | ||
.build(); | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
backend/src/main/java/com/funeat/common/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,82 @@ | ||
package com.funeat.common.s3; | ||
|
||
import static com.funeat.exception.CommonErrorCode.IMAGE_EXTENSION_ERROR_CODE; | ||
import static com.funeat.exception.CommonErrorCode.UNKNOWN_SERVER_ERROR_CODE; | ||
|
||
import com.amazonaws.services.s3.AmazonS3; | ||
import com.amazonaws.services.s3.model.ObjectMetadata; | ||
import com.amazonaws.services.s3.model.PutObjectRequest; | ||
import com.funeat.common.ImageUploader; | ||
import com.funeat.common.exception.CommonException.NotAllowedFileExtensionException; | ||
import com.funeat.common.exception.CommonException.S3UploadFailException; | ||
import java.io.IOException; | ||
import java.util.UUID; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
@Component | ||
@Profile("!test") | ||
public class S3Uploader implements ImageUploader { | ||
|
||
public static final String JPEG = "image/jpeg"; | ||
public static final String PNG = "image/png"; | ||
|
||
@Value("${cloud.aws.s3.bucket}") | ||
private String bucket; | ||
|
||
@Value("${cloud.aws.s3.folder}") | ||
private String folder; | ||
|
||
@Value("${cloud.aws.s3.cloudfrontPath}") | ||
private String cloudfrontPath; | ||
|
||
private final AmazonS3 amazonS3; | ||
|
||
public S3Uploader(final AmazonS3 amazonS3) { | ||
this.amazonS3 = amazonS3; | ||
} | ||
|
||
@Override | ||
public String upload(final MultipartFile image) { | ||
validateExtension(image); | ||
final String randomImageName = getRandomImageName(image); | ||
final ObjectMetadata metadata = getMetadata(image); | ||
try { | ||
final String key = folder + randomImageName; | ||
amazonS3.putObject(getPutObjectRequest(image, key, metadata)); | ||
|
||
return getCloudfrontImagePath(randomImageName); | ||
} catch (IOException e) { | ||
throw new S3UploadFailException(UNKNOWN_SERVER_ERROR_CODE); | ||
} | ||
} | ||
|
||
private void validateExtension(final MultipartFile image) { | ||
final String contentType = image.getContentType(); | ||
if (!contentType.equals(JPEG) && !contentType.equals(PNG)) { | ||
throw new NotAllowedFileExtensionException(IMAGE_EXTENSION_ERROR_CODE, contentType); | ||
} | ||
} | ||
|
||
private String getRandomImageName(final MultipartFile image) { | ||
return UUID.randomUUID() + image.getOriginalFilename(); | ||
} | ||
|
||
private ObjectMetadata getMetadata(final MultipartFile image) { | ||
final ObjectMetadata metadata = new ObjectMetadata(); | ||
metadata.setContentType(image.getContentType()); | ||
metadata.setContentLength(image.getSize()); | ||
return metadata; | ||
} | ||
|
||
private PutObjectRequest getPutObjectRequest(final MultipartFile image, final String key, | ||
final ObjectMetadata metadata) throws IOException { | ||
return new PutObjectRequest(bucket, key, image.getInputStream(), metadata); | ||
} | ||
|
||
private String getCloudfrontImagePath(final String imageName) { | ||
return cloudfrontPath + imageName; | ||
} | ||
} |
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
Oops, something went wrong.