From c994ef9d3f8ffaf9ceea090468f3f9800a489020 Mon Sep 17 00:00:00 2001 From: sycuuui <102959791+sycuuui@users.noreply.github.com> Date: Fri, 5 Apr 2024 18:43:16 +0900 Subject: [PATCH] =?UTF-8?q?#2=20feat=20:=20flask=EC=97=90=20=EB=8F=99?= =?UTF-8?q?=EC=98=81=EC=83=81=20=EC=A0=84=EC=86=A1=20=EB=B0=8F=20=EB=B0=98?= =?UTF-8?q?=ED=99=98=EA=B0=92=20=EB=B0=B0=EC=97=B4=EC=9D=84=20s3=EC=97=90?= =?UTF-8?q?=20=EC=97=85=EB=A1=9C=EB=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../video/controller/VideoController.java | 17 +++-- .../domain/video/service/VideoService.java | 69 ++++++++++++++++++- .../capic/server/global/util/s3/S3Client.java | 12 +--- 3 files changed, 78 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/capic/server/domain/video/controller/VideoController.java b/src/main/java/com/capic/server/domain/video/controller/VideoController.java index 6e892ce..2d56e78 100644 --- a/src/main/java/com/capic/server/domain/video/controller/VideoController.java +++ b/src/main/java/com/capic/server/domain/video/controller/VideoController.java @@ -39,14 +39,19 @@ public ResponseEntity sendToFlask(@RequestParam String imageUrl) throw return videoService.sendToFlask(imageUrl); } - @PostMapping("/falsk-with-images") - public ResponseEntity sendToFlaskWithImages(@RequestParam String folderName,@RequestBody VideoReq videoReq) throws IOException { - return videoService.sendToFlaskWithImages(folderName,videoReq); - } - - @GetMapping("/s3-folder") + @GetMapping("/folder") public ApplicationResponse sendToFlaskWithImages() { return ApplicationResponse.ok(videoService.createFolder()); } + @PostMapping("/falsk-target") + public ResponseEntity sendToFlaskWithImages(@RequestParam String folderName, String videoName) throws IOException { + return videoService.sendToFlaskWithVideo(folderName,videoName); + } + + @PostMapping("/falsk-mosaic") + public ResponseEntity sendToFlaskWithImagesAndVideo(@RequestParam String folderName,@RequestBody VideoReq videoReq) throws IOException { + return videoService.sendToFlaskWithImagesAndVideo(folderName,videoReq); + } + } diff --git a/src/main/java/com/capic/server/domain/video/service/VideoService.java b/src/main/java/com/capic/server/domain/video/service/VideoService.java index 7e311f6..c339951 100644 --- a/src/main/java/com/capic/server/domain/video/service/VideoService.java +++ b/src/main/java/com/capic/server/domain/video/service/VideoService.java @@ -1,10 +1,12 @@ package com.capic.server.domain.video.service; +import com.amazonaws.services.s3.model.ObjectMetadata; import com.amazonaws.services.s3.model.S3ObjectInputStream; import com.amazonaws.util.IOUtils; import com.capic.server.domain.video.dto.VideoReq; import com.capic.server.domain.video.dto.VideoRes; import com.capic.server.global.util.s3.S3Client; +import com.fasterxml.jackson.databind.ObjectMapper; import lombok.RequiredArgsConstructor; import org.springframework.core.io.ByteArrayResource; import org.springframework.core.io.Resource; @@ -17,8 +19,13 @@ import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; +import com.fasterxml.jackson.core.type.TypeReference; +import org.springframework.web.multipart.MultipartFile; +import java.io.ByteArrayInputStream; import java.io.IOException; +import java.io.InputStream; +import java.net.URL; import java.util.ArrayList; import java.util.List; import java.util.UUID; @@ -35,13 +42,12 @@ public VideoRes createFolder(){ } public S3ObjectInputStream getFile(String imageUrl) { - // Validation - // Business Logic S3ObjectInputStream file = s3Client.get(imageUrl); // Response return file; } + public ResponseEntity sendToFlask(String imageUrl) throws IOException { S3ObjectInputStream file = s3Client.get(imageUrl); byte[] content = IOUtils.toByteArray(file); @@ -71,7 +77,62 @@ public String getFilename() { .contentType(MediaType.APPLICATION_OCTET_STREAM) .body(resource); } - public ResponseEntity sendToFlaskWithImages(String folderName, VideoReq videoReq) throws IOException{ + //flask에 동영상 전송, 이미지들을 배열로 받아 s3에 업로드 + public ResponseEntity sendToFlaskWithVideo(String folderName,String VideoName) throws IOException{ + S3ObjectInputStream file = s3Client.get(folderName + "/" + VideoName); + byte[] content = IOUtils.toByteArray(file); + + MultiValueMap body = new LinkedMultiValueMap<>(); + body.add("file", new ByteArrayResource(content) { + @Override + public String getFilename() { + return VideoName; // 파일 이름 지정 + } + }); + + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.MULTIPART_FORM_DATA); + + HttpEntity> requestEntity = new HttpEntity<>(body, headers); + + RestTemplate restTemplate = new RestTemplate(); + + String url = "http://127.0.0.1:5000/video"; + + ResponseEntity response = restTemplate.postForEntity(url, requestEntity, byte[].class); + + // Flask에서 반환된 파일을 다시 클라이언트에게 반환 + ByteArrayResource resource = new ByteArrayResource(response.getBody()); + //Flask에서 받은 이미지들 s3에 업로드 + ObjectMapper objectMapper = new ObjectMapper(); + List imageList = objectMapper.readValue(response.getBody(), new TypeReference>() {}); + + for (int i = 0; i < imageList.size(); i++) { + String imageUrl = imageList.get(i); + + URL imageURL = new URL(imageUrl); + String imageName = i + 1 + "_" + imageURL.getPath().substring(imageURL.getPath().lastIndexOf("/") + 1); + + // 이미지 다운로드 + InputStream imageStream = imageURL.openStream(); + byte[] imageData = IOUtils.toByteArray(imageStream); + + // 이미지를 S3에 업로드 + ObjectMetadata metadata = new ObjectMetadata(); + metadata.setContentLength(imageData.length); + metadata.setContentType("image/jpeg"); // 이미지 타입에 따라 변경 + + String s3ImagePath = folderName + "/" + imageName; + s3Client.upload(s3ImagePath, (MultipartFile) new ByteArrayInputStream(imageData), metadata); + } + + return ResponseEntity.ok() + .contentType(MediaType.APPLICATION_OCTET_STREAM) + .body(resource); + } + + //flask에 이미지, 동영상 보내기 + public ResponseEntity sendToFlaskWithImagesAndVideo(String folderName, VideoReq videoReq) throws IOException{ // 동영상 파일 가져오기 S3ObjectInputStream videoFile = s3Client.get(folderName + "/" + videoReq.videoName()); byte[] videoContent = IOUtils.toByteArray(videoFile); @@ -118,6 +179,8 @@ public String getFilename() { // Flask에서 반환된 파일을 다시 클라이언트에게 반환 ByteArrayResource resource = new ByteArrayResource(response.getBody()); + + //여기에 나중에 update 구현 return ResponseEntity.ok() .contentType(MediaType.APPLICATION_OCTET_STREAM) .body(resource); diff --git a/src/main/java/com/capic/server/global/util/s3/S3Client.java b/src/main/java/com/capic/server/global/util/s3/S3Client.java index 1b6a7c3..a21f8a9 100644 --- a/src/main/java/com/capic/server/global/util/s3/S3Client.java +++ b/src/main/java/com/capic/server/global/util/s3/S3Client.java @@ -24,20 +24,12 @@ public class S3Client { @Value("${cloud.aws.baseUrl}") private String baseUrl; - public String upload(MultipartFile multipartFile, String dirName) { + public void upload(String imageUrl,MultipartFile multipartFile, ObjectMetadata objectMetadata) { // Validation if(multipartFile.isEmpty()) { throw new ApplicationException(ErrorCode.INVALID_VALUE_EXCEPTION); } - // Business Logic - String uuid = UUID.randomUUID().toString(); - String imageUrl = dirName + "/" + uuid + "_" + multipartFile.getOriginalFilename(); - - ObjectMetadata objectMetadata = new ObjectMetadata(); - objectMetadata.setContentType(multipartFile.getContentType()); - objectMetadata.setContentLength(multipartFile.getSize()); - // Check File upload try { amazonS3Client.putObject(new PutObjectRequest(bucket, imageUrl, multipartFile.getInputStream(), objectMetadata) @@ -46,8 +38,6 @@ public String upload(MultipartFile multipartFile, String dirName) { throw new RuntimeException(e); } - // Response - return baseUrl + imageUrl; } public String update(String existingImageUrl, MultipartFile newFile) {