Skip to content

Commit

Permalink
FU-334 chore: develop 브랜치와 conflict 해결
Browse files Browse the repository at this point in the history
  • Loading branch information
yuseok0215 committed Nov 5, 2024
2 parents 2e52686 + 1a58e0a commit 364f612
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public enum CommonErrorCode implements ErrorCode {
ACCESS_DENIED(403, "You don't have permission to access this resource."),
RESOURCE_NOT_FOUND(500, "Resource not exists"),
INTERNAL_SERVER_ERROR(500, "Internal server error"),
IO_EXCEPTION(500, "IoException occurred");
IO_EXCEPTION(500, "IoException occurred"),
MAXIMUM_UPLOAD_SIZE_EXCEEDED(500, "Maximum upload size exceeded");

private final int httpStatus;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import com.foru.freebe.errors.errorcode.AwsErrorCode;
import com.foru.freebe.errors.errorcode.CommonErrorCode;
import com.foru.freebe.errors.errorcode.ErrorCode;
import com.foru.freebe.errors.exception.JwtTokenException;
Expand Down Expand Up @@ -61,7 +60,7 @@ public ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotVali
@Override
public ResponseEntity<Object> handleMaxUploadSizeExceededException(MaxUploadSizeExceededException e,

Check warning on line 61 in src/main/java/com/foru/freebe/errors/handler/GlobalExceptionHandler.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

@NotNull/@Nullable problems

Not annotated parameter overrides @NonNullApi parameter

Check warning on line 61 in src/main/java/com/foru/freebe/errors/handler/GlobalExceptionHandler.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

@NotNull/@Nullable problems

Not annotated parameter overrides @NonNullApi parameter
HttpHeaders headers, HttpStatusCode status, WebRequest request) {

Check warning on line 62 in src/main/java/com/foru/freebe/errors/handler/GlobalExceptionHandler.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

@NotNull/@Nullable problems

Not annotated parameter overrides @NonNullApi parameter

Check warning on line 62 in src/main/java/com/foru/freebe/errors/handler/GlobalExceptionHandler.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

@NotNull/@Nullable problems

Not annotated parameter overrides @NonNullApi parameter

Check warning on line 62 in src/main/java/com/foru/freebe/errors/handler/GlobalExceptionHandler.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

@NotNull/@Nullable problems

Not annotated parameter overrides @NonNullApi parameter

Check warning on line 62 in src/main/java/com/foru/freebe/errors/handler/GlobalExceptionHandler.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

@NotNull/@Nullable problems

Not annotated parameter overrides @NonNullApi parameter

Check warning on line 62 in src/main/java/com/foru/freebe/errors/handler/GlobalExceptionHandler.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

@NotNull/@Nullable problems

Not annotated parameter overrides @NonNullApi parameter

Check warning on line 62 in src/main/java/com/foru/freebe/errors/handler/GlobalExceptionHandler.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

@NotNull/@Nullable problems

Not annotated parameter overrides @NonNullApi parameter
ErrorCode errorCode = AwsErrorCode.MAXIMUM_UPLOAD_SIZE_EXCEEDED;
ErrorCode errorCode = CommonErrorCode.MAXIMUM_UPLOAD_SIZE_EXCEEDED;
return handleExceptionInternal(e, errorCode);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.stereotype.Service;

import com.foru.freebe.errors.errorcode.CommonErrorCode;
import com.foru.freebe.errors.errorcode.ProductImageErrorCode;
import com.foru.freebe.errors.exception.RestApiException;
import com.foru.freebe.member.entity.Member;
import com.foru.freebe.product.dto.customer.ProductDetailResponse;
Expand Down Expand Up @@ -55,18 +56,26 @@ public List<ProductListResponse> getProductList(String profileName) {

List<ProductListResponse> productListResponseList = new ArrayList<>();
for (Product product : products) {
List<ProductImage> productImage = productImageRepository.findByProduct(product);

ProductListResponse productListResponse = ProductListResponse.builder()
.productId(product.getId())
.productTitle(product.getTitle())
.basicPrice(product.getBasicPrice())
.productRepresentativeImageUrl(productImage.get(0).getThumbnailUrl())
.productRepresentativeImageUrl(getRepresentativeProductImage(product))
.build();

productListResponseList.add(productListResponse);
}

return productListResponseList;
}

private String getRepresentativeProductImage(Product product) {

Check warning on line 72 in src/main/java/com/foru/freebe/product/service/CustomerProductService.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Check Kotlin and Java source code coverage

Method `getRepresentativeProductImage` coverage is below the threshold 50%

Check warning on line 72 in src/main/java/com/foru/freebe/product/service/CustomerProductService.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Check Kotlin and Java source code coverage

Method `getRepresentativeProductImage` coverage is below the threshold 50%
List<ProductImage> productImage = productImageRepository.findByProduct(product);

return productImage.stream()
.filter(image -> image.getImageOrder() == 0)
.map(ProductImage::getThumbnailUrl)
.findFirst()
.orElseThrow(() -> new RestApiException(ProductImageErrorCode.PRODUCT_IMAGE_NOT_FOUND));
}
}
108 changes: 73 additions & 35 deletions src/main/java/com/foru/freebe/s3/S3ImageService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.foru.freebe.s3;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
Expand All @@ -12,6 +13,8 @@
import java.util.List;
import java.util.UUID;

import javax.imageio.ImageIO;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
Expand All @@ -28,11 +31,14 @@
import com.foru.freebe.errors.errorcode.AwsErrorCode;
import com.foru.freebe.errors.errorcode.CommonErrorCode;
import com.foru.freebe.errors.exception.RestApiException;
import com.foru.freebe.s3.model.ImageSize;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Service
@RequiredArgsConstructor
@Slf4j
public class S3ImageService {

Check warning on line 42 in src/main/java/com/foru/freebe/s3/S3ImageService.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Check Kotlin and Java source code coverage

Class `S3ImageService` coverage is below the threshold 50%

Check warning on line 42 in src/main/java/com/foru/freebe/s3/S3ImageService.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Check Kotlin and Java source code coverage

Class `S3ImageService` coverage is below the threshold 50%
private final AmazonS3 amazonS3;

Expand Down Expand Up @@ -98,6 +104,19 @@ public SingleImageLink imageUploadToS3(MultipartFile image, S3ImageType s3ImageT
return new SingleImageLink(originUrl, thumbnailUrl);
}

public void deleteImageFromS3(String imageAddress) {

Check warning on line 107 in src/main/java/com/foru/freebe/s3/S3ImageService.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Check Kotlin and Java source code coverage

Method `deleteImageFromS3` coverage is below the threshold 50%

Check warning on line 107 in src/main/java/com/foru/freebe/s3/S3ImageService.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Check Kotlin and Java source code coverage

Method `deleteImageFromS3` coverage is below the threshold 50%
String key = getKeyFromImageAddress(imageAddress);
try {
amazonS3.deleteObject(new DeleteObjectRequest(bucketName, key));
} catch (AmazonS3Exception e) {
throw new RestApiException(AwsErrorCode.AMAZON_S3_EXCEPTION);
} catch (AmazonServiceException e) {
throw new RestApiException(AwsErrorCode.AMAZON_SERVICE_EXCEPTION);
} catch (Exception e) {
throw new RestApiException(AwsErrorCode.DELETE_OBJECT_EXCEPTION);
}
}

private int determineThumbnailSize(S3ImageType s3ImageType) {

Check warning on line 120 in src/main/java/com/foru/freebe/s3/S3ImageService.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Check Kotlin and Java source code coverage

Method `determineThumbnailSize` coverage is below the threshold 50%

Check warning on line 120 in src/main/java/com/foru/freebe/s3/S3ImageService.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Check Kotlin and Java source code coverage

Method `determineThumbnailSize` coverage is below the threshold 50%
int size;

Expand All @@ -111,62 +130,86 @@ private int determineThumbnailSize(S3ImageType s3ImageType) {
}

private String uploadOriginalImage(MultipartFile image, S3ImageType s3ImageType, Long memberId) throws IOException {

Check warning on line 132 in src/main/java/com/foru/freebe/s3/S3ImageService.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Check Kotlin and Java source code coverage

Method `uploadOriginalImage` coverage is below the threshold 50%

Check warning on line 132 in src/main/java/com/foru/freebe/s3/S3ImageService.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Check Kotlin and Java source code coverage

Method `uploadOriginalImage` coverage is below the threshold 50%
String originUrl = null;

Check warning on line 133 in src/main/java/com/foru/freebe/s3/S3ImageService.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Unused assignment

Variable `originUrl` initializer `null` is redundant

Check warning on line 133 in src/main/java/com/foru/freebe/s3/S3ImageService.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Unused assignment

Variable `originUrl` initializer `null` is redundant
String originKey = generateImagePath(image, s3ImageType, memberId, true);
try {
ObjectMetadata metadata = setImageMetadata(image);

uploadToS3(originKey, image.getInputStream(), metadata);

originUrl = amazonS3.getUrl(bucketName, originKey).toString();
} catch (Exception e) {
log.error("Failed to upload original image: {}", e.getMessage());
throw e;
}
return originUrl;
}

private ObjectMetadata setImageMetadata(MultipartFile image) {

Check warning on line 148 in src/main/java/com/foru/freebe/s3/S3ImageService.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Check Kotlin and Java source code coverage

Method `setImageMetadata` coverage is below the threshold 50%

Check warning on line 148 in src/main/java/com/foru/freebe/s3/S3ImageService.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Check Kotlin and Java source code coverage

Method `setImageMetadata` coverage is below the threshold 50%
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(image.getSize());
metadata.setContentType(image.getContentType());

uploadToS3(originKey, image.getInputStream(), metadata);

return amazonS3.getUrl(bucketName, originKey).toString();
return metadata;
}

private String uploadThumbnailImage(MultipartFile image, S3ImageType s3ImageType, Long memberId,

Check warning on line 155 in src/main/java/com/foru/freebe/s3/S3ImageService.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Check Kotlin and Java source code coverage

Method `uploadThumbnailImage` coverage is below the threshold 50%

Check warning on line 155 in src/main/java/com/foru/freebe/s3/S3ImageService.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Check Kotlin and Java source code coverage

Method `uploadThumbnailImage` coverage is below the threshold 50%
int thumbnailSize) throws IOException {

String thumbnailKey = generateImagePath(image, s3ImageType, memberId, false);

try (InputStream originalImageStream = image.getInputStream();
ByteArrayOutputStream thumbnailOutputStream = new ByteArrayOutputStream()) {

resizeForThumbnail(thumbnailSize, originalImageStream, thumbnailOutputStream);
ImageSize thumbnail = calculateThumbnailSize(image, thumbnailSize);

InputStream thumbnailInputStream = new ByteArrayInputStream(thumbnailOutputStream.toByteArray());
resizeForThumbnail(thumbnail.getWidth(), thumbnail.getHeight(), originalImageStream, thumbnailOutputStream);

ObjectMetadata thumbnailMetadata = createMetadataForThumbnail(image,
thumbnailOutputStream);
try (InputStream thumbnailInputStream = new ByteArrayInputStream(thumbnailOutputStream.toByteArray())) {
ObjectMetadata thumbnailMetadata = createMetadataForThumbnail(image, thumbnailOutputStream);

uploadToS3(thumbnailKey, thumbnailInputStream, thumbnailMetadata);
uploadToS3(thumbnailKey, thumbnailInputStream, thumbnailMetadata);

return amazonS3.getUrl(bucketName, thumbnailKey).toString();
return amazonS3.getUrl(bucketName, thumbnailKey).toString();
}
}
}

private ImageSize calculateThumbnailSize(MultipartFile image, int thumbnailSize) throws IOException {

Check warning on line 177 in src/main/java/com/foru/freebe/s3/S3ImageService.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Check Kotlin and Java source code coverage

Method `calculateThumbnailSize` coverage is below the threshold 50%

Check warning on line 177 in src/main/java/com/foru/freebe/s3/S3ImageService.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Check Kotlin and Java source code coverage

Method `calculateThumbnailSize` coverage is below the threshold 50%
ImageSize originalSize = getOriginalSize(image);

int thumbnailWidth;
int thumbnailHeight;

if (originalSize.getWidth() < originalSize.getHeight()) {
thumbnailWidth = thumbnailSize;
thumbnailHeight = (int)((thumbnailSize / (double)originalSize.getWidth()) * originalSize.getHeight());
} else {
thumbnailHeight = thumbnailSize;
thumbnailWidth = (int)((thumbnailSize / (double)originalSize.getHeight()) * originalSize.getWidth());
}

return new ImageSize(thumbnailWidth, thumbnailHeight);
}

private ImageSize getOriginalSize(MultipartFile image) throws IOException {

Check warning on line 194 in src/main/java/com/foru/freebe/s3/S3ImageService.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Check Kotlin and Java source code coverage

Method `getOriginalSize` coverage is below the threshold 50%

Check warning on line 194 in src/main/java/com/foru/freebe/s3/S3ImageService.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Check Kotlin and Java source code coverage

Method `getOriginalSize` coverage is below the threshold 50%
BufferedImage bufferedImage = ImageIO.read(image.getInputStream());
int originalWidth = bufferedImage.getWidth();
int originalHeight = bufferedImage.getHeight();
return new ImageSize(originalWidth, originalHeight);
}

private ObjectMetadata createMetadataForThumbnail(MultipartFile image,
ByteArrayOutputStream thumbnailOutputStream) {
private ObjectMetadata createMetadataForThumbnail(MultipartFile image, ByteArrayOutputStream outputStream) {

Check warning on line 201 in src/main/java/com/foru/freebe/s3/S3ImageService.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Check Kotlin and Java source code coverage

Method `createMetadataForThumbnail` coverage is below the threshold 50%

Check warning on line 201 in src/main/java/com/foru/freebe/s3/S3ImageService.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Check Kotlin and Java source code coverage

Method `createMetadataForThumbnail` coverage is below the threshold 50%
ObjectMetadata thumbnailMetadata = new ObjectMetadata();
thumbnailMetadata.setContentType(image.getContentType());
thumbnailMetadata.setContentLength(thumbnailOutputStream.size());
thumbnailMetadata.setContentLength(outputStream.size());
return thumbnailMetadata;
}

private void resizeForThumbnail(int thumbnailSize, InputStream originalImageStream,
ByteArrayOutputStream thumbnailOutputStream) throws IOException {
private void resizeForThumbnail(int width, int thumbnailSize, InputStream originalImageStream,

Check warning on line 208 in src/main/java/com/foru/freebe/s3/S3ImageService.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Check Kotlin and Java source code coverage

Method `resizeForThumbnail` coverage is below the threshold 50%

Check warning on line 208 in src/main/java/com/foru/freebe/s3/S3ImageService.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Check Kotlin and Java source code coverage

Method `resizeForThumbnail` coverage is below the threshold 50%
ByteArrayOutputStream outputStream) throws IOException {
Thumbnails.of(originalImageStream)
.size(thumbnailSize, thumbnailSize)
.toOutputStream(thumbnailOutputStream);
}

public void deleteImageFromS3(String imageAddress) {
String key = getKeyFromImageAddress(imageAddress);
try {
amazonS3.deleteObject(new DeleteObjectRequest(bucketName, key));
} catch (AmazonS3Exception e) {
throw new RestApiException(AwsErrorCode.AMAZON_S3_EXCEPTION);
} catch (AmazonServiceException e) {
throw new RestApiException(AwsErrorCode.AMAZON_SERVICE_EXCEPTION);
} catch (Exception e) {
throw new RestApiException(AwsErrorCode.DELETE_OBJECT_EXCEPTION);
}
.size(width, thumbnailSize)
.toOutputStream(outputStream);
}

private String generateImagePath(MultipartFile image, S3ImageType s3ImageType, Long memberId, Boolean isOrigin) {

Check warning on line 215 in src/main/java/com/foru/freebe/s3/S3ImageService.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Check Kotlin and Java source code coverage

Method `generateImagePath` coverage is below the threshold 50%

Check warning on line 215 in src/main/java/com/foru/freebe/s3/S3ImageService.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Check Kotlin and Java source code coverage

Method `generateImagePath` coverage is below the threshold 50%
Expand Down Expand Up @@ -197,11 +240,6 @@ private void uploadToS3(String key, InputStream imageInputStream, ObjectMetadata
}
}

private void addImageUrlFromS3(String key, List<String> originalImageUrls) {
String imageUrl = amazonS3.getUrl(bucketName, key).toString();
originalImageUrls.add(imageUrl);
}

private String getKeyFromImageAddress(String imageAddress) {

Check warning on line 243 in src/main/java/com/foru/freebe/s3/S3ImageService.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Check Kotlin and Java source code coverage

Method `getKeyFromImageAddress` coverage is below the threshold 50%

Check warning on line 243 in src/main/java/com/foru/freebe/s3/S3ImageService.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Check Kotlin and Java source code coverage

Method `getKeyFromImageAddress` coverage is below the threshold 50%
try {
URL url = new URL(imageAddress);
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/foru/freebe/s3/model/ImageSize.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.foru.freebe.s3.model;

import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class ImageSize {

Check warning on line 8 in src/main/java/com/foru/freebe/s3/model/ImageSize.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Check Kotlin and Java source code coverage

Class `ImageSize` coverage is below the threshold 50%

Check warning on line 8 in src/main/java/com/foru/freebe/s3/model/ImageSize.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Check Kotlin and Java source code coverage

Class `ImageSize` coverage is below the threshold 50%
private int width;
private int height;
}
2 changes: 1 addition & 1 deletion src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ spring:
multipart:
enabled: true
max-file-size: 10MB

max-request-size: 100MB
cloud:
aws:
credentials:
Expand Down

0 comments on commit 364f612

Please sign in to comment.