From ac62a738b990bac044551d83155bc117336b9a95 Mon Sep 17 00:00:00 2001 From: kms Date: Thu, 19 Oct 2023 10:32:38 +0900 Subject: [PATCH 1/2] :hammer: fix: update files for pdf upload --- .../common/support/AudioConcatenator.java | 4 ++-- .../common/support/AwsS3Uploader.java | 21 +++++++++------- .../controller/GraphController.java | 2 +- .../com/smart/watchboard/domain/File.java | 1 + .../com/smart/watchboard/dto/FileDto.java | 11 +++++---- .../smart/watchboard/service/FileService.java | 24 +++++++++++-------- 6 files changed, 37 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/smart/watchboard/common/support/AudioConcatenator.java b/src/main/java/com/smart/watchboard/common/support/AudioConcatenator.java index d4e3e45..7f4548b 100644 --- a/src/main/java/com/smart/watchboard/common/support/AudioConcatenator.java +++ b/src/main/java/com/smart/watchboard/common/support/AudioConcatenator.java @@ -32,10 +32,10 @@ public void concatenateAudioFiles(MultipartFile multipartFile1, Long documentId, // Merge the data into a single byte array byte[] mergedData = mergeWavData(data2, data1); MultipartFile multipartFile = convertByteArrayToMultipartFile(mergedData, fileName, contentType); - awsS3Uploader.uploadFile(multipartFile, documentId, fileId); + //awsS3Uploader.uploadFile(multipartFile, documentId, fileId); writeWavFile(outputPath, mergedData); } else { - awsS3Uploader.uploadFile(multipartFile1, documentId, fileId); + //awsS3Uploader.uploadFile(multipartFile1, documentId, fileId); writeWavFile(outputPath, data1); } diff --git a/src/main/java/com/smart/watchboard/common/support/AwsS3Uploader.java b/src/main/java/com/smart/watchboard/common/support/AwsS3Uploader.java index 7de45dd..5e1ca52 100644 --- a/src/main/java/com/smart/watchboard/common/support/AwsS3Uploader.java +++ b/src/main/java/com/smart/watchboard/common/support/AwsS3Uploader.java @@ -4,6 +4,7 @@ import com.amazonaws.services.s3.AmazonS3Client; import com.amazonaws.services.s3.model.*; import com.smart.watchboard.dto.FileDto; +import com.smart.watchboard.dto.S3Dto; import com.smart.watchboard.service.FileService; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -25,23 +26,25 @@ public class AwsS3Uploader { @Value("${cloud.aws.s3.bucket}") private String bucket; - public String uploadFile(MultipartFile multipartFile, Long documentId, Long fileId) { + public String uploadFile(S3Dto s3Dto) { ObjectMetadata objectMetadata = new ObjectMetadata(); - objectMetadata.setContentType(multipartFile.getContentType()); - objectMetadata.setContentLength(multipartFile.getSize()); + objectMetadata.setContentType(s3Dto.getFile().getContentType()); + objectMetadata.setContentLength(s3Dto.getFile().getSize()); - String fileName = S3_BUCKET_DIRECTORY_NAME + "/" + documentId + "." + multipartFile.getOriginalFilename(); + String directoryName = s3Dto.getFile().getContentType(); + String fileName = directoryName + "/" + s3Dto.getDocumentId() + "." + s3Dto.getFile().getOriginalFilename(); - try (InputStream inputStream = multipartFile.getInputStream()) { + try (InputStream inputStream = s3Dto.getFile().getInputStream()) { amazonS3Client.putObject(new PutObjectRequest(bucket, fileName, inputStream, objectMetadata) .withCannedAcl(CannedAccessControlList.PublicRead)); String path = amazonS3Client.getResourceUrl(bucket, fileName); - FileDto fileDto = new FileDto(multipartFile, path, documentId); - if (fileId == null) { + System.out.println(s3Dto.getFile().getContentType()); + FileDto fileDto = new FileDto(s3Dto.getFile(), path, s3Dto.getFile().getContentType(), s3Dto.getDocumentId(), s3Dto.getFileId()); + if (s3Dto.getFileId() == null) { fileService.createFile(fileDto); - } else if (fileId != null) { - fileService.updateFile(fileDto, fileId); + } else if (s3Dto.getFileId() != null) { + fileService.updateFile(fileDto); } } catch (IOException e) { log.error("S3 파일 업로드에 실패했습니다. {}", e.getMessage()); diff --git a/src/main/java/com/smart/watchboard/controller/GraphController.java b/src/main/java/com/smart/watchboard/controller/GraphController.java index c2b61a6..a767d56 100644 --- a/src/main/java/com/smart/watchboard/controller/GraphController.java +++ b/src/main/java/com/smart/watchboard/controller/GraphController.java @@ -31,7 +31,7 @@ public class GraphController { @PostMapping("/{documentID}") @Operation(summary = "마인드맵 생성", description = "음성 데이터를 받아 ai 서버에 마인드맵 요청한다.") public ResponseEntity getMindmap(@PathVariable(value = "documentID") long documentId, @RequestParam("audioFile") MultipartFile audioFile, @RequestParam(value = "fileID", required = false) Long fileId, @RequestHeader("Authorization") String accessToken) { - awsS3Uploader.uploadFile(audioFile, documentId, fileId); + //awsS3Uploader.uploadFile(audioFile, documentId, fileId); //awsS3Uploader.deleteFile(); return new ResponseEntity<>("{\"root\":1,\"keywords\":[\"나는\",\"eat\",\"food\",\"today\"],\"graph\":{\"1\":[0,2],\"2\":[3]}}", HttpStatus.OK); diff --git a/src/main/java/com/smart/watchboard/domain/File.java b/src/main/java/com/smart/watchboard/domain/File.java index 0e328e5..cd69f1c 100644 --- a/src/main/java/com/smart/watchboard/domain/File.java +++ b/src/main/java/com/smart/watchboard/domain/File.java @@ -24,6 +24,7 @@ public class File { @Column(columnDefinition = "varchar(1000)", nullable = false) private String path; + private String fileType; private Long size; private Instant createdAt; diff --git a/src/main/java/com/smart/watchboard/dto/FileDto.java b/src/main/java/com/smart/watchboard/dto/FileDto.java index 80eb5cc..88427f6 100644 --- a/src/main/java/com/smart/watchboard/dto/FileDto.java +++ b/src/main/java/com/smart/watchboard/dto/FileDto.java @@ -7,14 +7,17 @@ @Getter @Setter public class FileDto { - private MultipartFile audioFile; + private MultipartFile file; private String path; + private String fileType; private Long documentId; - private String accessToken; + private Long fileId; - public FileDto(MultipartFile multipartFile, String path, Long documentId) { - this.audioFile = multipartFile; + public FileDto(MultipartFile multipartFile, String path, String fileType, Long documentId, Long fileId) { + this.file = multipartFile; this.path = path; + this.fileType = fileType; this.documentId = documentId; + this.fileId = fileId; } } diff --git a/src/main/java/com/smart/watchboard/service/FileService.java b/src/main/java/com/smart/watchboard/service/FileService.java index e02c4d4..174731c 100644 --- a/src/main/java/com/smart/watchboard/service/FileService.java +++ b/src/main/java/com/smart/watchboard/service/FileService.java @@ -22,11 +22,13 @@ public class FileService { public void createFile(FileDto fileDto) { Document document = whiteboardService.findDoc(fileDto.getDocumentId()); + String key = fileDto.getFileType() + "/" + fileDto.getDocumentId() + "." + fileDto.getFile().getOriginalFilename(); File file = File.builder() - .fileName(fileDto.getAudioFile().getOriginalFilename()) - .objectKey("File/" + fileDto.getDocumentId() + "." + fileDto.getAudioFile().getOriginalFilename()) + .fileName(fileDto.getFile().getOriginalFilename()) + .objectKey(key) .path(fileDto.getPath()) - .size(fileDto.getAudioFile().getSize()) + .fileType(fileDto.getFileType()) + .size(fileDto.getFile().getSize()) .createdAt(Instant.now()) .modifiedAt(Instant.now()) .isDelete(false) @@ -36,20 +38,22 @@ public void createFile(FileDto fileDto) { fileRepository.save(file); } - public void updateFile(FileDto fileDto, long fileId) { - Optional file = findFile(fileId); + public void updateFile(FileDto fileDto) { + Optional file = findFile(fileDto.getFileId()); File updatedFile = file.get(); - updatedFile.setFileName(fileDto.getAudioFile().getOriginalFilename()); + updatedFile.setFileName(fileDto.getFile().getOriginalFilename()); updatedFile.setPath(fileDto.getPath()); - updatedFile.setSize(fileDto.getAudioFile().getSize()); + updatedFile.setSize(fileDto.getFile().getSize()); updatedFile.setCreatedAt(Instant.now()); updatedFile.setModifiedAt(Instant.now()); - fileRepository.save(updatedFile); } - public void deleteFile(MultipartFile multipartFile, long documentId) { - return; + public void deleteFile(long fileId) { + Optional file = findFile(fileId); + File deletedFile = file.get(); + deletedFile.setDelete(true); + fileRepository.save(deletedFile); } public Optional findFile(long fileId) { From 0f00bc9ac0e68950e8508b0fe259da54dfa0bea8 Mon Sep 17 00:00:00 2001 From: kms Date: Thu, 19 Oct 2023 10:33:24 +0900 Subject: [PATCH 2/2] :sparkles: feat: create pdf features --- .../controller/LearningFileController.java | 49 +++++++++++++++++++ .../java/com/smart/watchboard/dto/S3Dto.java | 21 ++++++++ 2 files changed, 70 insertions(+) create mode 100644 src/main/java/com/smart/watchboard/controller/LearningFileController.java create mode 100644 src/main/java/com/smart/watchboard/dto/S3Dto.java diff --git a/src/main/java/com/smart/watchboard/controller/LearningFileController.java b/src/main/java/com/smart/watchboard/controller/LearningFileController.java new file mode 100644 index 0000000..8976bdb --- /dev/null +++ b/src/main/java/com/smart/watchboard/controller/LearningFileController.java @@ -0,0 +1,49 @@ +package com.smart.watchboard.controller; + +import com.smart.watchboard.common.support.AwsS3Uploader; +import com.smart.watchboard.dto.FileDto; +import com.smart.watchboard.dto.S3Dto; +import com.smart.watchboard.service.FileService; +import io.swagger.v3.oas.annotations.tags.Tag; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; + +import javax.sound.sampled.UnsupportedAudioFileException; +import java.io.IOException; + +@RestController +@RequestMapping("/documents") +@Tag(name = "pdf 학습파일 API", description = "학습파일 관련 API(mock)") +@RequiredArgsConstructor +@Slf4j +public class LearningFileController { + private final AwsS3Uploader awsS3Uploader; + private final FileService fileService; + + @PostMapping("/{documentID}/pdf") + public ResponseEntity uploadLearningFile(@PathVariable(value = "documentID") long documentId, @RequestParam("pdfFile") MultipartFile pdfFile, @RequestParam(value = "fileID", required = false) Long fileId, @RequestHeader("Authorization") String accessToken) throws UnsupportedAudioFileException, IOException { + S3Dto s3Dto = new S3Dto(pdfFile, documentId, fileId); + awsS3Uploader.uploadFile(s3Dto); + + return new ResponseEntity<>("", HttpStatus.OK); + } + + @PutMapping("/{documentID}/pdf") + public ResponseEntity updateLearningFile(@PathVariable(value = "documentID") long documentId, @RequestParam("pdfFile") MultipartFile pdfFile, @RequestParam(value = "fileID", required = false) Long fileId, @RequestHeader("Authorization") String accessToken) throws UnsupportedAudioFileException, IOException { + S3Dto s3Dto = new S3Dto(pdfFile, documentId, fileId); + awsS3Uploader.uploadFile(s3Dto); + + return new ResponseEntity<>("", HttpStatus.OK); + } + + @DeleteMapping("/{documentID}/pdf") + public ResponseEntity deleteLearningFile(@PathVariable(value = "documentID") long documentId, @RequestParam(value = "fileID", required = false) Long fileId, @RequestHeader("Authorization") String accessToken) throws UnsupportedAudioFileException, IOException { + fileService.deleteFile(fileId); + + return new ResponseEntity<>("", HttpStatus.OK); + } +} diff --git a/src/main/java/com/smart/watchboard/dto/S3Dto.java b/src/main/java/com/smart/watchboard/dto/S3Dto.java new file mode 100644 index 0000000..6d62c0d --- /dev/null +++ b/src/main/java/com/smart/watchboard/dto/S3Dto.java @@ -0,0 +1,21 @@ +package com.smart.watchboard.dto; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import lombok.Setter; +import org.springframework.web.multipart.MultipartFile; + +@Getter +@Setter +@RequiredArgsConstructor +public class S3Dto { + private MultipartFile file; + private Long documentId; + private Long fileId; + + public S3Dto(MultipartFile file, long documentId, Long fileId) { + this.file = file; + this.documentId = documentId; + this.fileId = fileId; + } +}