Skip to content

Commit f661b15

Browse files
committed
feat : 모자이크 동영상 처리 후 client에 영상 파일 알려주는 코드로 변경
1 parent 1856125 commit f661b15

File tree

5 files changed

+87
-34
lines changed

5 files changed

+87
-34
lines changed

src/main/java/com/capic/server/domain/video/controller/VideoController.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,23 @@ public ResponseEntity<Resource> sendToFlask(@RequestParam String imageUrl) throw
3939
return videoService.sendToFlask(imageUrl);
4040
}
4141

42-
@GetMapping("/folder")
43-
public ApplicationResponse<VideoRes> sendToFlaskWithImages() {
44-
return ApplicationResponse.ok(videoService.createFolder());
45-
}
42+
// @GetMapping("/folder")
43+
// public ApplicationResponse<VideoRes> sendToFlaskWithImages() {
44+
// return ApplicationResponse.ok(videoService.createFolder());
45+
// }
4646

4747
@PostMapping("/falsk-target")
4848
public ResponseEntity<Resource> sendToFlaskWithImages(@RequestParam String folderName, String videoName) throws IOException {
4949
return videoService.sendToFlaskWithVideo(folderName,videoName);
5050
}
5151

5252
@PostMapping("/falsk-mosaic")
53-
public ResponseEntity<Resource> sendToFlaskWithImagesAndVideo(@RequestParam String folderName,@RequestBody VideoReq videoReq) throws IOException {
54-
return videoService.sendToFlaskWithImagesAndVideo(folderName,videoReq);
53+
public ApplicationResponse<VideoRes> sendToFlaskWithImagesAndVideo(@RequestParam String folderName,@RequestBody VideoReq videoReq) throws IOException {
54+
return ApplicationResponse.ok(videoService.sendToFlaskWithImagesAndVideo(folderName,videoReq));
55+
}
56+
@PostMapping("/test-falsk-mosaic")
57+
public ResponseEntity<Resource> sendToFlaskWithImagesAndVideoTest(@RequestParam String folderName,@RequestBody VideoReq videoReq) throws IOException {
58+
return videoService.sendToFlaskWithImagesAndVideoTest(folderName,videoReq);
5559
}
5660

5761
}
Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
package com.capic.server.domain.video.dto;
22

3+
import com.fasterxml.jackson.annotation.JsonInclude;
4+
import jakarta.validation.constraints.Null;
5+
6+
import java.util.List;
7+
38
public record VideoRes(
4-
String folderName
9+
String folderName,
10+
String videoName,
11+
@Null
12+
@JsonInclude(JsonInclude.Include.NON_NULL)
13+
List<String> imageNames
514
) {
6-
public static VideoRes of(String folderName){
7-
return new VideoRes(folderName);
15+
public static VideoRes of(String folderName,String videoName,
16+
List<String> imageNames){
17+
return new VideoRes(folderName,videoName,imageNames);
818
}
919
}

src/main/java/com/capic/server/domain/video/service/VideoService.java

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@
3636
@RequiredArgsConstructor
3737
public class VideoService {
3838
private final S3Client s3Client;
39-
public VideoRes createFolder(){
40-
String folderName = UUID.randomUUID().toString();
41-
return VideoRes.of(folderName);
42-
}
39+
// public VideoRes createFolder(){
40+
// String folderName = UUID.randomUUID().toString();
41+
// return VideoRes.of(folderName);
42+
// }
4343

4444
public S3ObjectInputStream getFile(String imageUrl) {
4545
// Business Logic
@@ -132,7 +132,7 @@ public String getFilename() {
132132

133133

134134
//flask에 이미지, 동영상 보내기
135-
public ResponseEntity<Resource> sendToFlaskWithImagesAndVideo(String folderName, VideoReq videoReq) throws IOException{
135+
public VideoRes sendToFlaskWithImagesAndVideo(String folderName, VideoReq videoReq) throws IOException{
136136
// 동영상 파일 가져오기
137137
S3ObjectInputStream videoFile = s3Client.get(folderName + "/" + videoReq.videoName());
138138
byte[] videoContent = IOUtils.toByteArray(videoFile);
@@ -183,7 +183,61 @@ public String getFilename() {
183183
ByteArrayResource resource = new ByteArrayResource(response.getBody());
184184

185185
//여기에 나중에 update 구현
186-
// s3Client.update(folderName + "/" + videoReq.videoName(),)
186+
s3Client.update(folderName + "/" + videoReq.videoName(), (MultipartFile) resource);
187+
return VideoRes.of(folderName, videoReq.videoName(), null);
188+
}
189+
190+
//flask에 이미지, 동영상 보내기
191+
public ResponseEntity<Resource> sendToFlaskWithImagesAndVideoTest(String folderName, VideoReq videoReq) throws IOException{
192+
// 동영상 파일 가져오기
193+
S3ObjectInputStream videoFile = s3Client.get(folderName + "/" + videoReq.videoName());
194+
byte[] videoContent = IOUtils.toByteArray(videoFile);
195+
196+
// 이미지 파일들을 바이트 배열로 변환하여 리스트에 추가
197+
List<byte[]> imageContents = new ArrayList<>();
198+
for (String imageName : videoReq.imageName()) {
199+
// 이미지 파일 가져오기
200+
S3ObjectInputStream imageFile = s3Client.get(folderName + "/" + imageName);
201+
byte[] imageContent = IOUtils.toByteArray(imageFile);
202+
imageContents.add(imageContent);
203+
}
204+
205+
// Multipart 요청 생성
206+
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
207+
body.add("video", new ByteArrayResource(videoContent) {
208+
@Override
209+
public String getFilename() {
210+
return videoReq.videoName(); // 동영상 파일 이름 가져오기
211+
}
212+
});
213+
214+
// 이미지 파일들을 요청에 추가
215+
for (int i = 0; i < imageContents.size(); i++) {
216+
byte[] content = imageContents.get(i);
217+
final String imageName = videoReq.imageName().get(i);
218+
body.add("image" + (i + 1), new ByteArrayResource(content) {
219+
@Override
220+
public String getFilename() {
221+
return imageName; // 이미지 파일 이름 가져오기
222+
}
223+
});
224+
}
225+
226+
body.add("imageSize",imageContents.size());
227+
228+
// HTTP 요청 설정
229+
HttpHeaders headers = new HttpHeaders();
230+
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
231+
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
232+
233+
// Flask 서버로 요청 보내기
234+
RestTemplate restTemplate = new RestTemplate();
235+
String url = "http://127.0.0.1:5000/video"; // Flask 서버 URL
236+
ResponseEntity<byte[]> response = restTemplate.postForEntity(url, requestEntity, byte[].class);
237+
238+
// Flask에서 반환된 파일을 다시 클라이언트에게 반환
239+
ByteArrayResource resource = new ByteArrayResource(response.getBody());
240+
187241
return ResponseEntity.ok()
188242
.contentType(MediaType.APPLICATION_OCTET_STREAM)
189243
.body(resource);

src/main/java/com/capic/server/global/config/SwaggerConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ public class SwaggerConfig {
1313

1414
@Bean
1515
public OpenAPI openAPI() {
16-
Info info = new Info().title("Gongjakso Server Swagger UI")
17-
.description("공작소 서버 API 테스트 페이지입니다.")
16+
Info info = new Info().title("Capic Swagger UI")
17+
.description("Capic API 테스트 페이지.")
1818
.version("v0.0.1");
1919

2020
String jwtSchemeName = "JWT Authentication";

src/main/java/com/capic/server/global/exception/ErrorCode.java

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,8 @@ public enum ErrorCode {
2727
KAKAO_USER_EXCEPTION(HttpStatus.INTERNAL_SERVER_ERROR, 3001, "카카오 프로필 정보를 가져오는 과정에서 오류가 발생했습니디."),
2828
WRONG_TOKEN_EXCEPTION(HttpStatus.UNAUTHORIZED, 3002, "유효하지 않은 토큰입니다."),
2929
LOGOUT_TOKEN_EXCEPTION(HttpStatus.UNAUTHORIZED, 3003, "로그아웃된 토큰입니다"),
30-
WRONG_TOKEN(HttpStatus.UNAUTHORIZED, 3004, "유효하지 않은 토큰입니다."),
31-
32-
33-
//4000: Apply Error
34-
NOT_APPLY_EXCEPTION(HttpStatus.BAD_REQUEST,4000,"지원 기간 지났습니다"),
35-
NOT_FOUND_POST_EXCEPTION(HttpStatus.NOT_FOUND,4001,"존재하지 않는 글입니다."),
36-
NOT_FOUND_APPLY_EXCEPTION(HttpStatus.NOT_FOUND,4002,"존재하지 않는 지원서입니다."),
37-
ALREADY_APPLY_EXCEPTION(HttpStatus.BAD_REQUEST,4003,"이미 지원했습니다."),
38-
ALREADY_DECISION_EXCEPION(HttpStatus.BAD_REQUEST,4004,"이미 지원 결정했습니다."),
39-
NOT_RECRUITING_EXCEPION(HttpStatus.BAD_REQUEST,4005,"이 공고는 모집 중이 아닙니다."),
40-
NOT_FOUND_CATEGORY_EXCEPTION(HttpStatus.NOT_FOUND,4006,"카테고리가 없습니다"),
41-
OVER_APPLY_EXCEPTION(HttpStatus.NOT_FOUND,4007,"지원 파트 정원이 찼습니다."),
42-
43-
//5000: Post Error
44-
NOT_POST_EXCEPTION(HttpStatus.BAD_REQUEST,5000,"공고를 더 이상 생성할 수 없습니다"),
45-
POST_VALUE_EXCEPTION(HttpStatus.BAD_REQUEST,5001,"올바르지 않은 요청 값입니다."),
46-
NOT_FOUNT_SCRAP_EXCEPTION(HttpStatus.NOT_FOUND,5002,"스크랩 정보가 존재하지 않습니다.");
30+
WRONG_TOKEN(HttpStatus.UNAUTHORIZED, 3004, "유효하지 않은 토큰입니다.");
31+
4732
private final HttpStatus httpStatus;
4833
private final Integer code;
4934
private final String message;

0 commit comments

Comments
 (0)