Skip to content

Commit

Permalink
[api-server-v1] fix: 비디오 블록 생성 오류 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
gdtknight committed Oct 5, 2023
1 parent ad44838 commit d28bcad
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 71 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package kr.joberchip.server.v1.block.controller;

import java.util.UUID;
import kr.joberchip.server.v1._config.security.CustomUserDetails;
import kr.joberchip.server.v1._utils.ApiResponse;
import kr.joberchip.server.v1.block.controller.dto.BlockResponseDTO;
import kr.joberchip.server.v1.block.controller.dto.VideoBlockDTO;
import kr.joberchip.server.v1.block.service.VideoBlockService;
import kr.joberchip.server.v1.page.service.SharePagePrivilegeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

@Slf4j
Expand All @@ -16,10 +19,18 @@
public class VideoBlockController {

private final VideoBlockService videoBlockService;
private final SharePagePrivilegeService sharePagePrivilegeService;

@PostMapping
public ApiResponse.Result<BlockResponseDTO> createVideoBlock(
@PathVariable UUID pageId, VideoBlockDTO videoBlockRequestDTO) {
@AuthenticationPrincipal CustomUserDetails loginUser,
@PathVariable UUID pageId,
VideoBlockDTO videoBlockRequestDTO) {

log.info("[TextBlockController] Login User : {}", loginUser);
log.info("[TextBlockController] Current Page Id : {}", pageId);

sharePagePrivilegeService.checkEditPrivilege(loginUser.user().getUserId(), pageId);

BlockResponseDTO responseDTO = videoBlockService.createVideoBlock(pageId, videoBlockRequestDTO);

Expand All @@ -28,19 +39,31 @@ public ApiResponse.Result<BlockResponseDTO> createVideoBlock(

@PutMapping("/{blockId}")
public ApiResponse.Result<BlockResponseDTO> modifyVideoBlock(
@AuthenticationPrincipal CustomUserDetails loginUser,
@PathVariable UUID pageId,
@PathVariable UUID blockId,
@RequestBody VideoBlockDTO videoBlockRequestDTO) {

BlockResponseDTO response =
videoBlockService.modifyVideoBlock(pageId, blockId, videoBlockRequestDTO);
log.info("[TextBlockController] Login User : {}", loginUser);
log.info("[TextBlockController] Current Page Id : {}", pageId);

sharePagePrivilegeService.checkEditPrivilege(loginUser.user().getUserId(), pageId);

BlockResponseDTO response = videoBlockService.modifyVideoBlock(blockId, videoBlockRequestDTO);

return ApiResponse.success(response);
}

@DeleteMapping("/{blockId}")
public ApiResponse.Result<Object> deleteVideoBlock(
@PathVariable UUID pageId, @PathVariable UUID blockId) {
@AuthenticationPrincipal CustomUserDetails loginUser,
@PathVariable UUID pageId,
@PathVariable UUID blockId) {

log.info("[TextBlockController] Login User : {}", loginUser);
log.info("[TextBlockController] Current Page Id : {}", pageId);

sharePagePrivilegeService.checkEditPrivilege(loginUser.user().getUserId(), pageId);

videoBlockService.deleteVideoBlock(pageId, blockId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public record VideoBlockDTO(
@RequestParam Boolean visible) {

public VideoBlock toEntity() {
if ("".equals(videoLink)) return VideoBlock.of(title, description, x, y, w, h, visible);
if (videoLink == null || "".equalsIgnoreCase(videoLink)) return VideoBlock.of(title, description, x, y, w, h);

return VideoBlock.of(title, description, videoLink, x, y, w, h, visible);
return VideoBlock.of(title, description, videoLink, x, y, w, h);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package kr.joberchip.server.v1.block.service;

import java.util.UUID;
import javax.persistence.EntityNotFoundException;
import kr.joberchip.core.block.VideoBlock;
import kr.joberchip.core.page.SharePage;
import kr.joberchip.server.v1._errors.ErrorMessage;
Expand All @@ -27,7 +26,9 @@ public class VideoBlockService {
public BlockResponseDTO createVideoBlock(UUID pageId, VideoBlockDTO videoBlockDTO) {

SharePage parentPage =
sharePageRepository.findById(pageId).orElseThrow(EntityNotFoundException::new);
sharePageRepository
.findById(pageId)
.orElseThrow(() -> new ApiClientException(ErrorMessage.SHARE_PAGE_ENTITY_NOT_FOUND));

VideoBlock videoBlock = videoBlockDTO.toEntity();

Expand All @@ -43,16 +44,21 @@ public BlockResponseDTO createVideoBlock(UUID pageId, VideoBlockDTO videoBlockDT
return BlockResponseDTO.fromEntity(videoBlock);
}

public BlockResponseDTO modifyVideoBlock(UUID pageId, UUID blockId, VideoBlockDTO videoBlockDTO) {
public BlockResponseDTO modifyVideoBlock(UUID blockId, VideoBlockDTO videoBlockDTO) {
VideoBlock videoBlock =
videoBlockRepository.findById(blockId).orElseThrow(EntityNotFoundException::new);
videoBlockRepository
.findById(blockId)
.orElseThrow(() -> new ApiClientException(ErrorMessage.BLOCK_ENTITY_NOT_FOUND));

if (videoBlockDTO.title() != null) videoBlock.setTitle(videoBlockDTO.title());

if (videoBlockDTO.description() != null) videoBlock.setDescription(videoBlockDTO.description());

if (videoBlockDTO.attachedVideo() != null) {
s3StorageService.delete(videoBlock.getVideoLink());
videoBlock.setVideoLink(s3StorageService.store(videoBlockDTO.attachedVideo()));
}

if (videoBlockDTO.visible() != null) videoBlock.setVisible(videoBlockDTO.visible());

videoBlockRepository.save(videoBlock);
Expand All @@ -64,67 +70,19 @@ public void deleteVideoBlock(UUID pageId, UUID blockId) {
SharePage sharePage =
sharePageRepository
.findSharePageByObjectId(pageId)
.orElseThrow(() -> new ApiClientException(ErrorMessage.ENTITY_NOT_FOUND));
.orElseThrow(() -> new ApiClientException(ErrorMessage.SHARE_PAGE_ENTITY_NOT_FOUND));

log.info("[ImageBlockController] Current SharePage : {}", sharePage);

VideoBlock videoBlock =
videoBlockRepository
.findById(blockId)
.orElseThrow(() -> new ApiClientException(ErrorMessage.ENTITY_NOT_FOUND));
.orElseThrow(() -> new ApiClientException(ErrorMessage.BLOCK_ENTITY_NOT_FOUND));

s3StorageService.delete(videoBlock.getVideoLink());

sharePage.getVideoBlocks().remove(videoBlock);

videoBlockRepository.delete(videoBlock);
}

// 영상 업로드
// public void uploadVideo(VideoBlockRequestDTO videoBlockRequestDTO) {
// String originalFileName = videoBlockRequestDTO.videoFile().getOriginalFilename();
//
// String folderName = generateFolderName(originalFileName);
// Path folderPath = Path.of(uploadDir, folderName);
//
// try {
// if (!Files.exists(folderPath)) {
// Files.createDirectories(folderPath);
// }
//
// String inputFilePath = folderPath.resolve(originalFileName).toString();
// Files.copy(
// videoBlockRequestDTO.videoFile().getInputStream(),
// Paths.get(inputFilePath),
// StandardCopyOption.REPLACE_EXISTING);
//
// String outputFileName = UUID.randomUUID() + ".mp4";
// String outputFilePath = folderPath.resolve(outputFileName).toString();
//
// // ffmpeg를 사용하여 확장자를 변경
// String[] ffmpegCommand = {
// ffmpegPath, "-i", inputFilePath, "-c:v", "copy", "-c:a", "aac", outputFilePath
// };
//
// ProcessBuilder pb = new ProcessBuilder(ffmpegCommand);
// pb.inheritIO().start().waitFor();
//
// AttachedFile attachedFile = AttachedFile.of(ContentType.MP4, outputFileName);
// AttachedFile savedAttachedFile = attachedFileRepository.save(attachedFile);
//
// VideoBlock videoBlock = new VideoBlock();
// videoBlock.modifyTitle(originalFileName);
//
// VideoBlockFile videoBlockFile = new VideoBlockFile();
// videoBlockFile.setVideoBlock(videoBlock);
// videoBlockFile.setAttachedFile(savedAttachedFile);
// VideoBlock savedVideoBlock = videoBlockRepository.save(videoBlock);
//
// } catch (IOException e) {
// log.error("영상 업로드 중 오류 발생", e);
// } catch (InterruptedException e) {
// log.error("ffmpeg 프로세스 실행 중 오류 발생", e);
// }
// }

}
19 changes: 8 additions & 11 deletions libs/core/src/main/java/kr/joberchip/core/block/VideoBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,30 @@ public static VideoBlock of(String title, String description) {
}

public static VideoBlock of(
String title, String description, Integer x, Integer y, Integer w, Integer h, Boolean visible) {
String title, String description, Integer x, Integer y, Integer w, Integer h) {
VideoBlock generated = new VideoBlock(title, description);

generated.setX(x);
generated.setY(y);
generated.setW(w);
generated.setH(h);
generated.setVisible(visible);

return generated;
}

public static VideoBlock of(
String title,
String description,
String videoLink,
Integer x,
Integer y,
Integer w,
Integer h,
Boolean visible) {
String title,
String description,
String videoLink,
Integer x,
Integer y,
Integer w,
Integer h) {
VideoBlock generated = new VideoBlock(title, description);
generated.setX(x);
generated.setY(y);
generated.setW(w);
generated.setH(h);
generated.setVisible(visible);
return new VideoBlock(title, description, videoLink);
}
}

0 comments on commit d28bcad

Please sign in to comment.