Skip to content

Commit

Permalink
feat : 모자이크 동영상 처리 후 client에 영상 파일 알려주는 코드로 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
sycuuui committed Apr 15, 2024
1 parent 1856125 commit f661b15
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,23 @@ public ResponseEntity<Resource> sendToFlask(@RequestParam String imageUrl) throw
return videoService.sendToFlask(imageUrl);
}

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

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

@PostMapping("/falsk-mosaic")
public ResponseEntity<Resource> sendToFlaskWithImagesAndVideo(@RequestParam String folderName,@RequestBody VideoReq videoReq) throws IOException {
return videoService.sendToFlaskWithImagesAndVideo(folderName,videoReq);
public ApplicationResponse<VideoRes> sendToFlaskWithImagesAndVideo(@RequestParam String folderName,@RequestBody VideoReq videoReq) throws IOException {
return ApplicationResponse.ok(videoService.sendToFlaskWithImagesAndVideo(folderName,videoReq));
}
@PostMapping("/test-falsk-mosaic")
public ResponseEntity<Resource> sendToFlaskWithImagesAndVideoTest(@RequestParam String folderName,@RequestBody VideoReq videoReq) throws IOException {
return videoService.sendToFlaskWithImagesAndVideoTest(folderName,videoReq);
}

}
16 changes: 13 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,19 @@
package com.capic.server.domain.video.dto;

import com.fasterxml.jackson.annotation.JsonInclude;
import jakarta.validation.constraints.Null;

import java.util.List;

public record VideoRes(
String folderName
String folderName,
String videoName,
@Null
@JsonInclude(JsonInclude.Include.NON_NULL)
List<String> imageNames
) {
public static VideoRes of(String folderName){
return new VideoRes(folderName);
public static VideoRes of(String folderName,String videoName,
List<String> imageNames){
return new VideoRes(folderName,videoName,imageNames);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@
@RequiredArgsConstructor
public class VideoService {
private final S3Client s3Client;
public VideoRes createFolder(){
String folderName = UUID.randomUUID().toString();
return VideoRes.of(folderName);
}
// public VideoRes createFolder(){
// String folderName = UUID.randomUUID().toString();
// return VideoRes.of(folderName);
// }

public S3ObjectInputStream getFile(String imageUrl) {
// Business Logic
Expand Down Expand Up @@ -132,7 +132,7 @@ public String getFilename() {


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

//여기에 나중에 update 구현
// s3Client.update(folderName + "/" + videoReq.videoName(),)
s3Client.update(folderName + "/" + videoReq.videoName(), (MultipartFile) resource);
return VideoRes.of(folderName, videoReq.videoName(), null);
}

//flask에 이미지, 동영상 보내기
public ResponseEntity<Resource> sendToFlaskWithImagesAndVideoTest(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.imageName()) {
// 이미지 파일 가져오기
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.imageName().get(i);
body.add("image" + (i + 1), new ByteArrayResource(content) {
@Override
public String getFilename() {
return imageName; // 이미지 파일 이름 가져오기
}
});
}

body.add("imageSize",imageContents.size());

// 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public class SwaggerConfig {

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

String jwtSchemeName = "JWT Authentication";
Expand Down
19 changes: 2 additions & 17 deletions src/main/java/com/capic/server/global/exception/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,8 @@ public enum ErrorCode {
KAKAO_USER_EXCEPTION(HttpStatus.INTERNAL_SERVER_ERROR, 3001, "카카오 프로필 정보를 가져오는 과정에서 오류가 발생했습니디."),
WRONG_TOKEN_EXCEPTION(HttpStatus.UNAUTHORIZED, 3002, "유효하지 않은 토큰입니다."),
LOGOUT_TOKEN_EXCEPTION(HttpStatus.UNAUTHORIZED, 3003, "로그아웃된 토큰입니다"),
WRONG_TOKEN(HttpStatus.UNAUTHORIZED, 3004, "유효하지 않은 토큰입니다."),


//4000: Apply Error
NOT_APPLY_EXCEPTION(HttpStatus.BAD_REQUEST,4000,"지원 기간 지났습니다"),
NOT_FOUND_POST_EXCEPTION(HttpStatus.NOT_FOUND,4001,"존재하지 않는 글입니다."),
NOT_FOUND_APPLY_EXCEPTION(HttpStatus.NOT_FOUND,4002,"존재하지 않는 지원서입니다."),
ALREADY_APPLY_EXCEPTION(HttpStatus.BAD_REQUEST,4003,"이미 지원했습니다."),
ALREADY_DECISION_EXCEPION(HttpStatus.BAD_REQUEST,4004,"이미 지원 결정했습니다."),
NOT_RECRUITING_EXCEPION(HttpStatus.BAD_REQUEST,4005,"이 공고는 모집 중이 아닙니다."),
NOT_FOUND_CATEGORY_EXCEPTION(HttpStatus.NOT_FOUND,4006,"카테고리가 없습니다"),
OVER_APPLY_EXCEPTION(HttpStatus.NOT_FOUND,4007,"지원 파트 정원이 찼습니다."),

//5000: Post Error
NOT_POST_EXCEPTION(HttpStatus.BAD_REQUEST,5000,"공고를 더 이상 생성할 수 없습니다"),
POST_VALUE_EXCEPTION(HttpStatus.BAD_REQUEST,5001,"올바르지 않은 요청 값입니다."),
NOT_FOUNT_SCRAP_EXCEPTION(HttpStatus.NOT_FOUND,5002,"스크랩 정보가 존재하지 않습니다.");
WRONG_TOKEN(HttpStatus.UNAUTHORIZED, 3004, "유효하지 않은 토큰입니다.");

private final HttpStatus httpStatus;
private final Integer code;
private final String message;
Expand Down

0 comments on commit f661b15

Please sign in to comment.