-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from kakao-tech-campus-2nd-step3/develop
Develop 정기 병합 1차
- Loading branch information
Showing
52 changed files
with
1,042 additions
and
185 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
20 changes: 20 additions & 0 deletions
20
src/main/java/com/helpmeCookies/global/config/QueryDSLConfig.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,20 @@ | ||
package com.helpmeCookies.global.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
|
||
import jakarta.persistence.EntityManager; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Configuration | ||
@RequiredArgsConstructor | ||
public class QueryDSLConfig { | ||
private final EntityManager entityManager; | ||
|
||
@Bean | ||
public JPAQueryFactory jpaQueryFactory(){ | ||
return new JPAQueryFactory(entityManager); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/com/helpmeCookies/global/config/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,31 @@ | ||
package com.helpmeCookies.global.config; | ||
|
||
import com.amazonaws.auth.AWSCredentials; | ||
import com.amazonaws.auth.AWSStaticCredentialsProvider; | ||
import com.amazonaws.auth.BasicAWSCredentials; | ||
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 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 AmazonS3 amazonS3() { | ||
AWSCredentials credentials = new BasicAWSCredentials(accessKey,secretKey); | ||
|
||
return AmazonS3ClientBuilder | ||
.standard() | ||
.withCredentials(new AWSStaticCredentialsProvider(credentials)) | ||
.withRegion(region) | ||
.build(); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/helpmeCookies/global/exception/GlobalExceptionHandler.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,15 @@ | ||
package com.helpmeCookies.global.exception; | ||
|
||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
|
||
import com.helpmeCookies.global.exception.user.ResourceNotFoundException; | ||
|
||
@ControllerAdvice | ||
public class GlobalExceptionHandler { | ||
|
||
@ExceptionHandler(ResourceNotFoundException.class) | ||
public String handleResourceNotFoundException() { | ||
return "Resource not found"; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/helpmeCookies/global/exception/user/ResourceNotFoundException.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,7 @@ | ||
package com.helpmeCookies.global.exception.user; | ||
|
||
public class ResourceNotFoundException extends RuntimeException { | ||
public ResourceNotFoundException() { | ||
super("Resource not found"); | ||
} | ||
} |
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
72 changes: 72 additions & 0 deletions
72
src/main/java/com/helpmeCookies/global/utils/AwsS3FileUtils.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,72 @@ | ||
package com.helpmeCookies.global.utils; | ||
|
||
import com.amazonaws.services.s3.AmazonS3; | ||
import com.amazonaws.services.s3.model.CannedAccessControlList; | ||
import com.amazonaws.services.s3.model.ObjectMetadata; | ||
import com.amazonaws.services.s3.model.PutObjectRequest; | ||
import com.helpmeCookies.product.dto.FileUploadResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.multipart.MultipartFile; | ||
import org.springframework.web.server.ResponseStatusException; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class AwsS3FileUtils { | ||
private final AmazonS3 amazonS3; | ||
|
||
@Value("${cloud.aws.s3.bucket}") | ||
private String bucket; | ||
|
||
//다중파일 업로드후 url 반환 | ||
public List<FileUploadResponse> uploadMultiImages(List<MultipartFile> multipartFiles) { | ||
List<FileUploadResponse> fileList = new ArrayList<>(); | ||
|
||
multipartFiles.forEach(file -> { | ||
String fileName = createFileName(file.getOriginalFilename()); //파일 이름 난수화 | ||
ObjectMetadata objectMetadata = new ObjectMetadata(); | ||
objectMetadata.setContentLength(file.getSize()); | ||
objectMetadata.setContentType(file.getContentType()); | ||
|
||
try (InputStream inputStream = file.getInputStream()) { | ||
amazonS3.putObject(new PutObjectRequest(bucket, fileName, inputStream, objectMetadata) | ||
.withCannedAcl(CannedAccessControlList.PublicRead)); | ||
} catch (IOException e) { | ||
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "파일 업로드 실패" + fileName); | ||
} | ||
|
||
fileList.add(new FileUploadResponse(amazonS3.getUrl(bucket,fileName).toString(),fileName)); | ||
}); | ||
|
||
return fileList; | ||
} | ||
|
||
public String createFileName(String fileName) { | ||
return UUID.randomUUID().toString().concat(getFileExtension(fileName)); | ||
} | ||
|
||
//TODO error handler 필요 | ||
public String getFileExtension(String fileName) { | ||
try { | ||
String extension = fileName.substring(fileName.lastIndexOf(".")).toLowerCase(); | ||
//이미지 파일 확장자 목록 | ||
List<String> allowedExtensions = Arrays.asList(".jpg", ".jpeg", ".png"); | ||
|
||
if (!allowedExtensions.contains(extension)) { | ||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "이미지 파일만 업로드가 가능합니다. 지원되지 않는 형식의 파일" + fileName); | ||
} | ||
return extension; | ||
} catch (StringIndexOutOfBoundsException e) { | ||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST,"잘못된 형식의 파일" + fileName + "입니다."); | ||
} | ||
} | ||
} |
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
16 changes: 16 additions & 0 deletions
16
src/main/java/com/helpmeCookies/product/dto/FileUploadResponse.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,16 @@ | ||
package com.helpmeCookies.product.dto; | ||
|
||
import com.helpmeCookies.product.entity.ProductImage; | ||
|
||
public record FileUploadResponse( | ||
String photoUrl, | ||
String uuid | ||
) { | ||
public ProductImage toEntity(Long productId) { | ||
return ProductImage.builder() | ||
.productId(productId) | ||
.photoUrl(photoUrl) | ||
.uuid(uuid) | ||
.build(); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/helpmeCookies/product/dto/ProductImageResponse.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,8 @@ | ||
package com.helpmeCookies.product.dto; | ||
|
||
import java.util.List; | ||
|
||
public record ProductImageResponse( | ||
List<String> urls | ||
) { | ||
} |
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
13 changes: 13 additions & 0 deletions
13
src/main/java/com/helpmeCookies/product/repository/ProductImageRepository.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,13 @@ | ||
package com.helpmeCookies.product.repository; | ||
|
||
import com.helpmeCookies.product.entity.ProductImage; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.List; | ||
|
||
@Repository | ||
public interface ProductImageRepository extends JpaRepository<ProductImage,Long> { | ||
List<ProductImage> findAllByProductId(Long productId); | ||
void deleteAllByProductId(Long productId); | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/com/helpmeCookies/product/service/ProductImageService.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.helpmeCookies.product.service; | ||
|
||
import com.helpmeCookies.global.utils.AwsS3FileUtils; | ||
import com.helpmeCookies.product.dto.FileUploadResponse; | ||
import com.helpmeCookies.product.repository.ProductImageRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class ProductImageService { | ||
private final AwsS3FileUtils awsS3FileUtils; | ||
private final ProductImageRepository productImageRepository; | ||
|
||
@Transactional | ||
public List<FileUploadResponse> uploadMultiFiles(Long productId, List<MultipartFile> files) throws IOException { | ||
List<FileUploadResponse> uploadResponses = awsS3FileUtils.uploadMultiImages(files); | ||
uploadResponses.forEach(response -> | ||
productImageRepository.save(response.toEntity(productId))); | ||
return uploadResponses; | ||
} | ||
|
||
@Transactional | ||
public void editImages(Long productId, List<MultipartFile> files) throws IOException { | ||
//우선은 전부 삭제하고 다시 업로드 | ||
//추후에 개선 예정 | ||
productImageRepository.deleteAllByProductId(productId); | ||
uploadMultiFiles(productId, files); | ||
} | ||
} |
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.