Skip to content

Commit

Permalink
#2 folder 생성 코드 구현, image와 동영상 모두 flask에 보내는 코드 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
sycuuui committed Apr 5, 2024
1 parent f56eadc commit 0290f5f
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.capic.server.domain.video.controller;

import com.amazonaws.services.s3.model.S3ObjectInputStream;
import com.capic.server.domain.video.dto.VideoRes;
import org.springframework.core.io.Resource;
import com.capic.server.domain.video.dto.VideoReq;
import com.capic.server.domain.video.service.VideoService;
Expand All @@ -11,6 +12,7 @@
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;


@RequiredArgsConstructor
Expand All @@ -36,4 +38,15 @@ public S3ObjectInputStream addApply(@RequestParam String imageUrl){
public ResponseEntity<Resource> sendToFlask(@RequestParam String imageUrl) throws IOException {
return videoService.sendToFlask(imageUrl);
}

@PostMapping("/falsk-with-images")
public ResponseEntity<Resource> sendToFlaskWithImages(@RequestParam String folderName,@RequestBody VideoReq videoReq) throws IOException {
return videoService.sendToFlaskWithImages(folderName,videoReq);
}

@GetMapping("/s3-folder")
public ApplicationResponse<VideoRes> sendToFlaskWithImages() {
return ApplicationResponse.ok(videoService.createFolder());
}

}
10 changes: 3 additions & 7 deletions src/main/java/com/capic/server/domain/video/dto/VideoReq.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package com.capic.server.domain.video.dto;

import com.capic.server.domain.video.entity.Video;
import java.util.List;

public record VideoReq(
Long url
String videoName, List<String> images
) {
public Video toEntity(String url){
return Video.builder()
.url(url)
.build();
}

}
6 changes: 3 additions & 3 deletions src/main/java/com/capic/server/domain/video/dto/VideoRes.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.capic.server.domain.video.dto;

public record VideoRes(
String url
String folderName
) {
public static VideoRes of(String url){
return new VideoRes(url);
public static VideoRes of(String folderName){
return new VideoRes(folderName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

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 lombok.RequiredArgsConstructor;
import org.springframework.core.io.ByteArrayResource;
Expand All @@ -17,13 +19,21 @@
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;


@Service
@Transactional
@RequiredArgsConstructor
public class VideoService {
private final S3Client s3Client;
public VideoRes createFolder(){
String folderName = UUID.randomUUID().toString();
return VideoRes.of(folderName);
}

public S3ObjectInputStream getFile(String imageUrl) {
// Validation

Expand Down Expand Up @@ -61,5 +71,55 @@ public String getFilename() {
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.body(resource);
}
public ResponseEntity<Resource> sendToFlaskWithImages(String folderName, VideoReq videoReq) throws IOException{
// 동영상 파일 가져오기
S3ObjectInputStream videoFile = s3Client.get(folderName + "/" + videoReq.videoName());
byte[] videoContent = IOUtils.toByteArray(videoFile);

// 이미지 파일들을 바이트 배열로 변환하여 리스트에 추가
List<byte[]> imageContents = new ArrayList<>();
for (String imageName : videoReq.images()) {
// 이미지 파일 가져오기
S3ObjectInputStream imageFile = s3Client.get(folderName + "/" + imageName);
byte[] imageContent = IOUtils.toByteArray(imageFile);
imageContents.add(imageContent);
}

// Multipart 요청 생성
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add("video", new ByteArrayResource(videoContent) {
@Override
public String getFilename() {
return videoReq.videoName(); // 동영상 파일 이름 가져오기
}
});

// 이미지 파일들을 요청에 추가
for (int i = 0; i < imageContents.size(); i++) {
byte[] content = imageContents.get(i);
final String imageName = videoReq.images().get(i);
body.add("image" + (i + 1), new ByteArrayResource(content) {
@Override
public String getFilename() {
return imageName; // 이미지 파일 이름 가져오기
}
});
}

// HTTP 요청 설정
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);

// Flask 서버로 요청 보내기
RestTemplate restTemplate = new RestTemplate();
String url = "http://127.0.0.1:5000/video"; // Flask 서버 URL
ResponseEntity<byte[]> response = restTemplate.postForEntity(url, requestEntity, byte[].class);

// Flask에서 반환된 파일을 다시 클라이언트에게 반환
ByteArrayResource resource = new ByteArrayResource(response.getBody());
return ResponseEntity.ok()
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.body(resource);
}
}

0 comments on commit 0290f5f

Please sign in to comment.