-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
17 이미지 업로드 구현
- Loading branch information
Showing
23 changed files
with
294 additions
and
207 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
60 changes: 60 additions & 0 deletions
60
backend/src/main/java/com/hjjang/backend/domain/image/controller/ImageController.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,60 @@ | ||
package com.hjjang.backend.domain.image.controller; | ||
|
||
import com.hjjang.backend.domain.image.domain.entity.Image; | ||
import com.hjjang.backend.domain.image.dto.ImagePathResponse; | ||
import com.hjjang.backend.domain.image.service.ImageService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import static com.hjjang.backend.global.util.HttpStatusResponseEntity.RESPONSE_OK; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/api/images") | ||
public class ImageController { | ||
|
||
private final ImageService imageService; | ||
|
||
@GetMapping() | ||
public ResponseEntity<List<ImagePathResponse>> findAllImage() { | ||
return ResponseEntity.ok(imageService | ||
.findAll() | ||
.stream() | ||
.map(ImagePathResponse::of) | ||
.collect(Collectors.toList()) | ||
); | ||
} | ||
|
||
@PostMapping() | ||
public ResponseEntity<ImagePathResponse> uploadImage(@RequestParam("images") MultipartFile multipartFile) throws IOException { | ||
return ResponseEntity.ok(ImagePathResponse | ||
.of(imageService | ||
.uploadNewImage(multipartFile) | ||
) | ||
); | ||
} | ||
|
||
@GetMapping("/{imageId}") | ||
public ResponseEntity<ImagePathResponse> findImage(@PathVariable Long imageId) { | ||
return ResponseEntity.ok(ImagePathResponse | ||
.of(imageService | ||
.findImageById(imageId) | ||
) | ||
); | ||
} | ||
|
||
@DeleteMapping("/{imageId}") | ||
public ResponseEntity<HttpStatus> deleteImage(@PathVariable Long imageId) { | ||
Image image = imageService.findImageById(imageId); | ||
imageService.removeImage(image); | ||
|
||
return RESPONSE_OK; | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...kend/infra/image/domain/entity/Image.java → ...end/domain/image/domain/entity/Image.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
4 changes: 2 additions & 2 deletions
4
...ge/domain/repository/ImageRepository.java → ...ge/domain/repository/ImageRepository.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
20 changes: 20 additions & 0 deletions
20
backend/src/main/java/com/hjjang/backend/domain/image/dto/ImagePathResponse.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.hjjang.backend.domain.image.dto; | ||
|
||
import com.hjjang.backend.domain.image.domain.entity.Image; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
@Builder | ||
@Getter | ||
public class ImagePathResponse { | ||
|
||
private Long id; | ||
private String path; | ||
|
||
public static ImagePathResponse of(Image image) { | ||
return ImagePathResponse.builder() | ||
.id(image.getId()) | ||
.path(image.getPath()) | ||
.build(); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...age/exception/ImageNotFoundException.java → ...age/exception/ImageNotFoundException.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
40 changes: 40 additions & 0 deletions
40
backend/src/main/java/com/hjjang/backend/domain/image/service/ImageService.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,40 @@ | ||
package com.hjjang.backend.domain.image.service; | ||
|
||
import com.hjjang.backend.domain.image.domain.repository.ImageRepository; | ||
import com.hjjang.backend.domain.image.domain.entity.Image; | ||
import com.hjjang.backend.domain.image.exception.ImageNotFoundException; | ||
import com.hjjang.backend.infra.aws.service.S3ImageUploader; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import javax.transaction.Transactional; | ||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional | ||
public class ImageService { | ||
private final ImageRepository imageRepository; | ||
|
||
private final S3ImageUploader imageUploader; | ||
|
||
public List<Image> findAll() { | ||
return imageRepository.findAll(); | ||
} | ||
|
||
public Image findImageById(Long imageId) { | ||
return imageRepository.findById(imageId).orElseThrow(ImageNotFoundException::new); | ||
} | ||
|
||
public Image uploadNewImage(MultipartFile multipartFile) throws IOException { | ||
return imageRepository.save(new Image(imageUploader.upload(multipartFile))); | ||
} | ||
|
||
public void removeImage(Image image) { | ||
image.removeImage(); | ||
imageRepository.save(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/hjjang/backend/domain/image/service/LocalImageUploader.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.hjjang.backend.domain.image.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
@Service | ||
public class LocalImageUploader extends ImageUploader{ | ||
|
||
@Override | ||
public String upload(MultipartFile multipartFile) throws IOException { | ||
File uploadFile = | ||
this.localUpload(multipartFile) // 파일 변환할 수 없으면 에러 | ||
.orElseThrow( | ||
() -> new IllegalArgumentException("error: MultipartFile -> File convert fail")); | ||
return uploadFile.getName(); | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
backend/src/main/java/com/hjjang/backend/domain/image/service/Path.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,14 @@ | ||
package com.hjjang.backend.domain.image.service; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public enum Path { | ||
imageSavePath(System.getProperty("user.dir") + "/backend/src/main/resources/images/"); | ||
|
||
private final String path; | ||
Path(String path) { | ||
this.path = path; | ||
} | ||
|
||
} |
2 changes: 0 additions & 2 deletions
2
backend/src/main/java/com/hjjang/backend/domain/post/domain/entity/Post.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
31 changes: 31 additions & 0 deletions
31
backend/src/main/java/com/hjjang/backend/infra/aws/config/AmazonS3Config.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.hjjang.backend.infra.aws.config; | ||
|
||
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 AmazonS3Config { | ||
|
||
@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 awsCreds = new BasicAWSCredentials(accessKey, secretKey); | ||
return (AmazonS3Client) AmazonS3ClientBuilder.standard() | ||
.withRegion(region) | ||
.withCredentials(new AWSStaticCredentialsProvider(awsCreds)) | ||
.build(); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
backend/src/main/java/com/hjjang/backend/infra/aws/service/S3ImageUploader.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,59 @@ | ||
package com.hjjang.backend.infra.aws.service; | ||
|
||
import com.amazonaws.services.s3.AmazonS3Client; | ||
import com.amazonaws.services.s3.model.CannedAccessControlList; | ||
import com.amazonaws.services.s3.model.PutObjectRequest; | ||
import com.hjjang.backend.domain.image.service.ImageUploader; | ||
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.IOException; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
@Service | ||
public class S3ImageUploader extends ImageUploader { | ||
|
||
private final AmazonS3Client amazonS3Client; | ||
|
||
@Value("${cloud.aws.s3.bucket}") | ||
public String bucket; // S3 버킷 이름 | ||
|
||
@Override | ||
public String upload(MultipartFile multipartFile) throws IOException { | ||
File uploadFile = | ||
this.localUpload(multipartFile) // 파일 변환할 수 없으면 에러 | ||
.orElseThrow( | ||
() -> new IllegalArgumentException("error: MultipartFile -> File convert fail")); | ||
|
||
return upload(uploadFile); | ||
} | ||
|
||
// S3로 파일 업로드하기 | ||
private String upload(File uploadFile) { | ||
String uploadImageUrl = putS3(uploadFile, "static/" + uploadFile.getName()); // s3로 업로드 | ||
removeNewFile(uploadFile); | ||
return uploadImageUrl; | ||
} | ||
|
||
// S3로 업로드 | ||
private String putS3(File uploadFile, String fileName) { | ||
amazonS3Client.putObject( | ||
new PutObjectRequest(bucket, fileName, uploadFile) | ||
.withCannedAcl(CannedAccessControlList.PublicRead)); | ||
return amazonS3Client.getUrl(bucket, fileName).toString(); | ||
} | ||
|
||
// 로컬에 저장된 이미지 지우기 | ||
private void removeNewFile(File targetFile) { | ||
if (targetFile.delete()) { | ||
log.info("File delete success"); | ||
return; | ||
} | ||
log.info("File delete fail"); | ||
} | ||
} |
16 changes: 0 additions & 16 deletions
16
backend/src/main/java/com/hjjang/backend/infra/image/component/ImageUploader.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.