-
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 #19 from gyeongnam-gyeongmae/dev/feat/save-image
[#9] 이미지 저장 구현
- Loading branch information
Showing
14 changed files
with
368 additions
and
47 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
39 changes: 0 additions & 39 deletions
39
src/main/java/megabrain/gyeongnamgyeongmae/domain/domain/Image.java
This file was deleted.
Oops, something went wrong.
35 changes: 35 additions & 0 deletions
35
src/main/java/megabrain/gyeongnamgyeongmae/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,35 @@ | ||
package megabrain.gyeongnamgyeongmae.domain.image.Controller; | ||
|
||
|
||
import io.swagger.v3.oas.annotations.Operation; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import megabrain.gyeongnamgyeongmae.domain.image.Service.ImageService; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
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; | ||
|
||
|
||
@RequiredArgsConstructor | ||
@RestController | ||
@RequestMapping("api/{from}/{id}/images/") | ||
public class ImageController { | ||
|
||
private final ImageService imageService; | ||
|
||
@PostMapping(value = "/upload", consumes = { | ||
MediaType.MULTIPART_FORM_DATA_VALUE | ||
}) | ||
@Operation(summary = "이미지", description = "이미지 업로드") | ||
public ResponseEntity<HttpStatus> uploadImage( | ||
@RequestPart("file") List<MultipartFile> files, @PathVariable String from, @PathVariable Long id) throws IOException { | ||
imageService.uploadImage(files, from, id); | ||
|
||
return new ResponseEntity<>(HttpStatus.OK); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/megabrain/gyeongnamgyeongmae/domain/image/Service/AwsS3Service.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,45 @@ | ||
package megabrain.gyeongnamgyeongmae.domain.image.Service; | ||
|
||
import com.amazonaws.SdkClientException; | ||
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 org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.IOException; | ||
|
||
|
||
@Service | ||
public class AwsS3Service { | ||
|
||
private final AmazonS3 amazonS3; | ||
|
||
@Autowired | ||
public AwsS3Service(AmazonS3 amazonS3) { | ||
this.amazonS3 = amazonS3; | ||
} | ||
|
||
@Value("${cloud.aws.s3.bucket}") | ||
private String bucket; | ||
|
||
public String upload(MultipartFile file, String filename) throws IOException { | ||
ObjectMetadata objectMetadata = new ObjectMetadata(); | ||
objectMetadata.setContentType(file.getContentType()); | ||
objectMetadata.setContentLength(file.getSize()); | ||
|
||
try { | ||
amazonS3.putObject(new PutObjectRequest(bucket, filename, file.getInputStream(), objectMetadata) | ||
.withCannedAcl(CannedAccessControlList.PublicRead)); | ||
} catch (SdkClientException e) { | ||
e.printStackTrace(); | ||
System.out.println("업로드 실패: " + e.getMessage()); | ||
} | ||
|
||
return amazonS3.getUrl(bucket, filename).toString(); | ||
} | ||
} | ||
|
13 changes: 13 additions & 0 deletions
13
src/main/java/megabrain/gyeongnamgyeongmae/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,13 @@ | ||
package megabrain.gyeongnamgyeongmae.domain.image.Service; | ||
|
||
|
||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
public interface ImageService { | ||
|
||
void uploadImage(List<MultipartFile> images, String from, Long id) throws IOException; | ||
|
||
} |
100 changes: 100 additions & 0 deletions
100
src/main/java/megabrain/gyeongnamgyeongmae/domain/image/Service/ImageServiceImpl.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,100 @@ | ||
package megabrain.gyeongnamgyeongmae.domain.image.Service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import megabrain.gyeongnamgyeongmae.domain.auctionItem.domain.entity.AuctionItem; | ||
import megabrain.gyeongnamgyeongmae.domain.auctionItem.domain.entity.Comment; | ||
import megabrain.gyeongnamgyeongmae.domain.auctionItem.domain.repostiory.AuctionItemCommentRepository; | ||
import megabrain.gyeongnamgyeongmae.domain.auctionItem.domain.repostiory.AuctionItemRepository; | ||
import megabrain.gyeongnamgyeongmae.domain.image.domain.repository.ImageRepository; | ||
import megabrain.gyeongnamgyeongmae.domain.image.dto.FileType; | ||
import megabrain.gyeongnamgyeongmae.domain.image.domain.entity.Image; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class ImageServiceImpl implements ImageService { | ||
|
||
private final ImageRepository imageRepository; | ||
private final AwsS3Service awsS3Service; | ||
private final AuctionItemRepository auctionItemRepository; | ||
private final AuctionItemCommentRepository commentRepository; | ||
|
||
@Override | ||
public void uploadImage(List<MultipartFile> images, String from, Long id) throws IOException { | ||
String whereFrom = checkImageUploadFind(from); | ||
AuctionItem auctionItem = null; | ||
Comment comment = null; | ||
if (whereFrom.equals("AuctionItem")) { | ||
auctionItem = checkIsRealIdAuctionItem(id); | ||
} | ||
if (whereFrom.equals("Comment")) { | ||
comment = checkIsRealIdComment(id); | ||
} | ||
upload(images, whereFrom, auctionItem, comment); | ||
} | ||
|
||
private AuctionItem checkIsRealIdAuctionItem(Long id) { | ||
return auctionItemRepository.findById(id).orElseThrow(() -> new RuntimeException("존재하지 않는 경매")); | ||
} | ||
|
||
private Comment checkIsRealIdComment(Long id) { | ||
return commentRepository.findById(id).orElseThrow(() -> new RuntimeException("존재하지 않는 댓글")); | ||
} | ||
|
||
private void upload(List<MultipartFile> images, String from, AuctionItem auctionItem, Comment comment) throws IOException { | ||
for (MultipartFile file : images) { | ||
String originalFilename = file.getOriginalFilename(); | ||
|
||
if (originalFilename == null) { | ||
throw new RuntimeException("파일 확장자 없음"); | ||
} | ||
|
||
String fileExtension = originalFilename.substring(originalFilename.lastIndexOf(".") + 1); | ||
|
||
if (!FileType.isValid(fileExtension)) { | ||
throw new RuntimeException("이미지 파일 아님"); | ||
} | ||
|
||
String fileName = uploadFileName(from, fileExtension); | ||
awsS3Service.upload(file, fileName); | ||
|
||
String fileShow = "https://d231cnlxdxmjew.cloudfront.net/" + fileName; | ||
Image image = Image.builder() | ||
.imageFrom(from) | ||
.name(originalFilename) | ||
.url(fileShow) | ||
.build(); | ||
image.setAuctionItem(auctionItem); | ||
image.setComment(comment); | ||
imageRepository.save(image); | ||
} | ||
} | ||
|
||
private String uploadFileName(String from, String fileExtension) { | ||
return from + "/" + (UUID.randomUUID().toString().replace("-", "") + "." + fileExtension); | ||
|
||
} | ||
|
||
private String checkImageUploadFind(String from) { | ||
if (from.equals("Profile")) { | ||
return "Profile"; | ||
} | ||
if (from.equals("AuctionItem")) { | ||
return "AuctionItem"; | ||
} | ||
if (from.equals("Comment")) { | ||
return "Comment"; | ||
} | ||
throw new RuntimeException("이미지 파라미터 잘못"); | ||
} | ||
|
||
} | ||
|
||
|
||
|
||
|
45 changes: 45 additions & 0 deletions
45
src/main/java/megabrain/gyeongnamgyeongmae/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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package megabrain.gyeongnamgyeongmae.domain.image.domain.entity; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
import megabrain.gyeongnamgyeongmae.domain.auctionItem.domain.entity.AuctionItem; | ||
import megabrain.gyeongnamgyeongmae.domain.auctionItem.domain.entity.Comment; | ||
import javax.persistence.*; | ||
|
||
@NoArgsConstructor | ||
@Entity | ||
@Table(name = "Image") | ||
@Getter | ||
@Setter | ||
public class Image { | ||
|
||
@Id | ||
@Column(name = "image_url") | ||
private String imageUrl; | ||
|
||
@Column(name = "image_name") | ||
private String imageName; | ||
|
||
@Column(name = "image_from") | ||
private String imageFrom; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "auction_id") | ||
private AuctionItem auctionItem; | ||
|
||
@Column(name = "removed") | ||
private boolean removed; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name ="comment_id") | ||
private Comment comment; | ||
|
||
@Builder | ||
public Image(String name, String url, String imageFrom) { | ||
this.imageName = name; | ||
this.imageUrl = url; | ||
this.imageFrom = imageFrom; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...ain/java/megabrain/gyeongnamgyeongmae/domain/image/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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package megabrain.gyeongnamgyeongmae.domain.image.domain.repository; | ||
|
||
import megabrain.gyeongnamgyeongmae.domain.image.domain.entity.Image; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.stereotype.Repository; | ||
import java.util.List; | ||
|
||
@Repository | ||
public interface ImageRepository extends JpaRepository<Image, Long> { | ||
|
||
@Query(value = "SELECT DISTINCT * FROM Image where auction_id = :id", nativeQuery = true) | ||
List<Image> findImageByAuctionItemId(Long id); | ||
} |
Oops, something went wrong.