Skip to content

Commit

Permalink
Merge pull request #48 from SWM-SMART/feat/#47
Browse files Browse the repository at this point in the history
파일명 수정 구현
  • Loading branch information
noparamin authored Nov 11, 2023
2 parents 459cc6f + 5f9e3ae commit ce5be3d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public String uploadFile(S3Dto s3Dto) {
objectMetadata.setContentLength(s3Dto.getFile().getSize());

String directoryName = s3Dto.getFile().getContentType();
String fileName = directoryName + "/" + s3Dto.getDocumentId() + "." + s3Dto.getFile().getOriginalFilename();
String fileName = directoryName + "/" + s3Dto.getUserId() + "_" + s3Dto.getDocumentId() + "." + s3Dto.getDataType();

try (InputStream inputStream = s3Dto.getFile().getInputStream()) {
amazonS3Client.putObject(new PutObjectRequest(bucket, fileName, inputStream, objectMetadata)
Expand All @@ -67,7 +67,7 @@ public String uploadTextPdfFile(S3Dto s3Dto) {
objectMetadata.setContentLength(s3Dto.getFile().getSize());

String directoryName = s3Dto.getFile().getContentType();
String fileName = directoryName + "/" + s3Dto.getDocumentId() + "." + s3Dto.getFile().getOriginalFilename();
String fileName = directoryName + "/" + s3Dto.getUserId() + "_" + s3Dto.getDocumentId() + "." + s3Dto.getDataType();

try (InputStream inputStream = s3Dto.getFile().getInputStream()) {
amazonS3Client.putObject(new PutObjectRequest(bucket, fileName, inputStream, objectMetadata)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import static com.smart.watchboard.common.support.PdfConverter.convertStringToPdf;

Expand All @@ -43,19 +44,22 @@ public class AudioFileController {
private final MindmapService mindmapService;
private final WhiteboardService whiteboardService;
private final KeywordService keywordService;
private final JwtService jwtService;

@PostMapping("/{documentID}/audio")
public ResponseEntity<?> uploadAudioFile(@PathVariable(value = "documentID") long documentId, @RequestParam("audio") MultipartFile audioFile, @RequestHeader("Authorization") String accessToken) throws UnsupportedAudioFileException, IOException, DocumentException {
// 토큰 검증
Optional<Long> id = jwtService.extractUserId(accessToken);
Long userId = id.orElse(null);

// s3에 오디오 파일 저장
S3Dto s3Dto = new S3Dto(audioFile, documentId);
S3Dto s3Dto = new S3Dto(audioFile, documentId, userId, "mp3");
String path = awsS3Uploader.uploadFile(s3Dto);
// STT
//String sttResult = sttService.getSTT(path);
ResponseEntity<String> sttResponseEntity = sttService.getSTT(path);
String sttResult = sttService.getText(sttResponseEntity);
// convert
String sttFileName = "sttResult.pdf";
String sttFileName = String.valueOf(userId) + "_" + String.valueOf(documentId) + ".pdf";
File textPdfFile = convertStringToPdf(sttResult, sttFileName);

String contentType = "application/pdf";
Expand All @@ -64,7 +68,7 @@ public ResponseEntity<?> uploadAudioFile(@PathVariable(value = "documentID") lon

FileInputStream fileInputStream = new FileInputStream(textPdfFile);
MultipartFile multipartFile = new MockMultipartFile(name, originalFilename, contentType, fileInputStream);
S3Dto s3DtoForSTT = new S3Dto(multipartFile, 26L);
S3Dto s3DtoForSTT = new S3Dto(multipartFile, documentId, userId, "pdf");
String textPdfPath = awsS3Uploader.uploadTextPdfFile(s3DtoForSTT);

List<SttData> data = sttService.getSTTData(sttResponseEntity);
Expand All @@ -87,7 +91,10 @@ public ResponseEntity<?> uploadAudioFile(@PathVariable(value = "documentID") lon

@PutMapping("/{documentID}/audio")
public ResponseEntity<?> updateAudioFile(@PathVariable(value = "documentID") long documentId, @RequestParam("audio") MultipartFile audioFile, @RequestHeader("Authorization") String accessToken) throws UnsupportedAudioFileException, IOException, DocumentException {
S3Dto s3Dto = new S3Dto(audioFile, documentId);
Optional<Long> id = jwtService.extractUserId(accessToken);
Long userId = id.orElse(null);

S3Dto s3Dto = new S3Dto(audioFile, documentId, userId, "mp3");
String path = awsS3Uploader.uploadFile(s3Dto);

ResponseEntity<String> sttResponseEntity = sttService.getSTT(path);
Expand All @@ -96,7 +103,8 @@ public ResponseEntity<?> updateAudioFile(@PathVariable(value = "documentID") lon
lectureNoteService.updateLectureNote(documentId, data, sttResult);

// convert
String sttFileName = "sttResult.pdf";

String sttFileName = String.valueOf(userId) + "_" + String.valueOf(documentId) + ".pdf";
File textPdfFile = convertStringToPdf(sttResult, sttFileName);

String contentType = "application/pdf";
Expand All @@ -105,7 +113,7 @@ public ResponseEntity<?> updateAudioFile(@PathVariable(value = "documentID") lon

FileInputStream fileInputStream = new FileInputStream(textPdfFile);
MultipartFile multipartFile = new MockMultipartFile(name, originalFilename, contentType, fileInputStream);
S3Dto s3DtoForSTT = new S3Dto(multipartFile, 26L);
S3Dto s3DtoForSTT = new S3Dto(multipartFile, documentId, userId, "pdf");
String textPdfPath = awsS3Uploader.uploadTextPdfFile(s3DtoForSTT);

ResponseEntity<KeywordsBodyDto> responseEntity = requestService.requestSTTKeywords(textPdfPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/documents")
Expand All @@ -30,10 +31,14 @@ public class LearningFileController {
private final WhiteboardService whiteboardService;
private final SummaryService summaryService;
private final KeywordService keywordService;
private final JwtService jwtService;

@PostMapping("/{documentID}/pdf")
public ResponseEntity<?> uploadLearningFile(@PathVariable(value = "documentID") long documentId, @RequestParam("pdf") MultipartFile pdfFile, @RequestHeader("Authorization") String accessToken) throws UnsupportedAudioFileException, IOException {
S3Dto s3Dto = new S3Dto(pdfFile, documentId);
Optional<Long> id = jwtService.extractUserId(accessToken);
Long userId = id.orElse(null);

S3Dto s3Dto = new S3Dto(pdfFile, documentId, userId, "pdf");
String path = awsS3Uploader.uploadFile(s3Dto);
ResponseEntity<KeywordsBodyDto> responseEntity = requestService.requestPdfKeywords(path);
keywordService.createKeywords(responseEntity, documentId);
Expand All @@ -47,7 +52,10 @@ public ResponseEntity<?> uploadLearningFile(@PathVariable(value = "documentID")

@PutMapping("/{documentID}/pdf")
public ResponseEntity<?> updateLearningFile(@PathVariable(value = "documentID") long documentId, @RequestParam("pdf") MultipartFile pdfFile, @RequestHeader("Authorization") String accessToken) throws UnsupportedAudioFileException, IOException {
S3Dto s3Dto = new S3Dto(pdfFile, documentId);
Optional<Long> id = jwtService.extractUserId(accessToken);
Long userId = id.orElse(null);

S3Dto s3Dto = new S3Dto(pdfFile, documentId, userId, "pdf");
String path = awsS3Uploader.uploadFile(s3Dto);
ResponseEntity<KeywordsBodyDto> responseEntity = requestService.requestPdfKeywords(path);
keywordService.renewKeywords(responseEntity, documentId);
Expand Down Expand Up @@ -76,7 +84,10 @@ public ResponseEntity<?> getLearningFile(@PathVariable(value = "documentID") lon

@GetMapping("/test")
public ResponseEntity<?> test(@RequestHeader("Authorization") String accessToken, @RequestParam("pdf") MultipartFile pdfFile) throws JsonProcessingException {
S3Dto s3Dto = new S3Dto(pdfFile, 11L);
Optional<Long> id = jwtService.extractUserId(accessToken);
Long userId = id.orElse(null);

S3Dto s3Dto = new S3Dto(pdfFile, 11L, userId, "pdf");
String path = awsS3Uploader.uploadFile(s3Dto);
//System.out.println(path);
//String path = "abcd";
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/com/smart/watchboard/dto/S3Dto.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
public class S3Dto {
private MultipartFile file;
private Long documentId;
//private Long fileId;
private Long userId;
private String dataType;

public S3Dto(MultipartFile file, long documentId) {
public S3Dto(MultipartFile file, Long documentId, Long userId, String dataType) {
this.file = file;
this.documentId = documentId;
//this.fileId = fileId;
this.userId = userId;
this.dataType = dataType;
}
}

0 comments on commit ce5be3d

Please sign in to comment.