Skip to content

Commit

Permalink
Merge pull request #19 from SWM-SMART/feat/#17
Browse files Browse the repository at this point in the history
pdf 기능 구현
  • Loading branch information
noparamin authored Oct 19, 2023
2 parents 76ca8b8 + 0f00bc9 commit 51b4e30
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
1 change: 1 addition & 0 deletions src/main/java/com/smart/watchboard/domain/File.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/com/smart/watchboard/dto/FileDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
21 changes: 21 additions & 0 deletions src/main/java/com/smart/watchboard/dto/S3Dto.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
24 changes: 14 additions & 10 deletions src/main/java/com/smart/watchboard/service/FileService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -36,20 +38,22 @@ public void createFile(FileDto fileDto) {
fileRepository.save(file);
}

public void updateFile(FileDto fileDto, long fileId) {
Optional<File> file = findFile(fileId);
public void updateFile(FileDto fileDto) {
Optional<File> 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> file = findFile(fileId);
File deletedFile = file.get();
deletedFile.setDelete(true);
fileRepository.save(deletedFile);
}

public Optional<File> findFile(long fileId) {
Expand Down

0 comments on commit 51b4e30

Please sign in to comment.