-
Notifications
You must be signed in to change notification settings - Fork 1
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 #75 from one-dream-us/dev
뉴스사 조회 및 이미지 관리(S3) 기능 추가
- Loading branch information
Showing
19 changed files
with
369 additions
and
33 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
33 changes: 33 additions & 0 deletions
33
src/main/java/com/onedreamus/project/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,33 @@ | ||
package com.onedreamus.project.global.config.s3; | ||
|
||
import com.amazonaws.auth.AWSCredentials; | ||
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() { | ||
AWSCredentials basicAWSCredentials = new BasicAWSCredentials(accessKey, secretKey); | ||
return (AmazonS3Client) AmazonS3ClientBuilder | ||
.standard() | ||
.withCredentials(new AWSStaticCredentialsProvider(basicAWSCredentials)) | ||
.withRegion(region) | ||
.build(); | ||
} | ||
} |
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/onedreamus/project/global/s3/ImageCategory.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.onedreamus.project.global.s3; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public enum ImageCategory { | ||
|
||
THUMBNAIL("thumbnail"); | ||
|
||
private final String name; | ||
} |
93 changes: 93 additions & 0 deletions
93
src/main/java/com/onedreamus/project/global/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,93 @@ | ||
package com.onedreamus.project.global.s3; | ||
|
||
import com.amazonaws.SdkClientException; | ||
import com.amazonaws.services.s3.AmazonS3Client; | ||
import com.amazonaws.services.s3.model.CannedAccessControlList; | ||
import com.amazonaws.services.s3.model.ObjectMetadata; | ||
import com.amazonaws.services.s3.model.PutObjectRequest; | ||
import com.onedreamus.project.global.exception.ErrorCode; | ||
import com.onedreamus.project.thisismoney.exception.S3Exception; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class S3Uploader { | ||
|
||
private final AmazonS3Client amazonS3Client; | ||
|
||
@Value("${cloud.aws.s3.bucketname}") | ||
private String bucket; | ||
|
||
/** | ||
* <p>S3 이미지 업로드</p> | ||
* S3에 이미지 업로드 후, 이미지 URL 반환 | ||
* @param multipartFile | ||
* @param imageCategory | ||
* @return | ||
*/ | ||
public String uploadMultipartFileByStream(MultipartFile multipartFile, ImageCategory imageCategory) { | ||
|
||
String filename = multipartFile.getOriginalFilename(); | ||
String newFilename = createFileName(filename, imageCategory); | ||
|
||
ObjectMetadata metadata = new ObjectMetadata(); | ||
metadata.setContentType(getContentType(filename)); | ||
metadata.setContentLength(multipartFile.getSize()); | ||
|
||
try { | ||
amazonS3Client.putObject( | ||
new PutObjectRequest(bucket, newFilename, multipartFile.getInputStream(), metadata)); | ||
} catch (IOException e) { | ||
throw new S3Exception(ErrorCode.IMAGE_UPLOAD_FAIL); | ||
} | ||
|
||
return amazonS3Client.getUrl(bucket, newFilename).toString(); | ||
} | ||
|
||
/** | ||
* <p>S3 이미지 삭제</p> | ||
* @param fileName | ||
*/ | ||
public void deleteFile(String fileName) { | ||
try { | ||
amazonS3Client.deleteObject(bucket, fileName); | ||
log.info(" S3 객체 삭제 : {}", fileName); | ||
} catch (SdkClientException e) { | ||
throw new S3Exception(ErrorCode.AWS_SDK_ERROR); | ||
} | ||
} | ||
|
||
private String createFileName(String fileName, ImageCategory imageCategory) { | ||
String random = UUID.randomUUID().toString(); | ||
return imageCategory.getName() + "-" + random + fileName; | ||
} | ||
|
||
private String getContentType(String filename) { | ||
int idx = filename.lastIndexOf("."); | ||
String extension = filename.substring(idx + 1); | ||
return "image/" + extension; | ||
} | ||
|
||
private void deleteUploadedImageList(List<String> imageUrlList) { | ||
for (String imageUrl : imageUrlList) { | ||
deleteFile(getFileName(imageUrl)); | ||
} | ||
} | ||
|
||
@NotNull | ||
public String getFileName(String imageUrl) { | ||
return imageUrl.substring(imageUrl.lastIndexOf("/") + 1); | ||
} | ||
} |
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
10 changes: 10 additions & 0 deletions
10
src/main/java/com/onedreamus/project/thisismoney/exception/S3Exception.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,10 @@ | ||
package com.onedreamus.project.thisismoney.exception; | ||
|
||
import com.onedreamus.project.global.exception.CustomException; | ||
import com.onedreamus.project.global.exception.ErrorCode; | ||
|
||
public class S3Exception extends CustomException { | ||
public S3Exception(ErrorCode errorCode) { | ||
super(errorCode); | ||
} | ||
} |
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
23 changes: 23 additions & 0 deletions
23
src/main/java/com/onedreamus/project/thisismoney/model/dto/AgencySearch.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,23 @@ | ||
package com.onedreamus.project.thisismoney.model.dto; | ||
|
||
import com.onedreamus.project.thisismoney.model.entity.Agency; | ||
import lombok.*; | ||
|
||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Getter | ||
@Setter | ||
@Builder | ||
public class AgencySearch { | ||
|
||
private Integer id; | ||
private String name; | ||
|
||
public static AgencySearch from(Agency agency) { | ||
return AgencySearch.builder() | ||
.id(agency.getId()) | ||
.name(agency.getName()) | ||
.build(); | ||
} | ||
|
||
} |
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
33 changes: 33 additions & 0 deletions
33
src/main/java/com/onedreamus/project/thisismoney/model/dto/ScheduledNewsRequest.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,33 @@ | ||
package com.onedreamus.project.thisismoney.model.dto; | ||
|
||
import jakarta.validation.Valid; | ||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.NotEmpty; | ||
import lombok.*; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.util.List; | ||
|
||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Getter | ||
@Setter | ||
@Builder | ||
public class ScheduledNewsRequest { | ||
|
||
private String title; // 뉴스 제목 | ||
private String thumbnailUrl; // 썸네일 URL | ||
private String originalLink; // 기사 원본 링크 | ||
private String newsAgency; // 뉴스 업로드한 에이전시 | ||
private List<DictionarySentenceRequest> dictionarySentenceList; | ||
|
||
public static ScheduledNewsRequest from(NewsRequest newsRequest, String thumbnailUrl) { | ||
return ScheduledNewsRequest.builder() | ||
.title(newsRequest.getTitle()) | ||
.thumbnailUrl(thumbnailUrl) | ||
.originalLink(newsRequest.getOriginalLink()) | ||
.newsAgency(newsRequest.getNewsAgency()) | ||
.dictionarySentenceList(newsRequest.getDictionarySentenceList()) | ||
.build(); | ||
} | ||
} |
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
29 changes: 29 additions & 0 deletions
29
src/main/java/com/onedreamus/project/thisismoney/model/entity/Agency.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,29 @@ | ||
package com.onedreamus.project.thisismoney.model.entity; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import lombok.*; | ||
|
||
@Entity | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Setter | ||
@Getter | ||
@Builder | ||
public class Agency { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Integer id; | ||
|
||
private String name; | ||
|
||
public static Agency from(String name) { | ||
return Agency.builder() | ||
.name(name) | ||
.build(); | ||
} | ||
|
||
} |
Oops, something went wrong.