From e7e421f0d2e7272c3c965f94ad6754b5f4ea4d75 Mon Sep 17 00:00:00 2001 From: kryptonite43 Date: Tue, 28 Mar 2023 15:52:52 +0900 Subject: [PATCH 1/3] =?UTF-8?q?[feat]=20=ED=8C=8C=EC=9D=BC=20=EB=93=B1?= =?UTF-8?q?=EB=A1=9D=20API=20=EB=A1=9C=EC=A7=81=20#44?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 + .../HighLight/File/File.java | 11 ++- .../HighLight/File/FileController.java | 29 ++++++++ .../HighLight/File/FilePostResponseDto.java | 18 +++++ .../HighLight/File/FileRepository.java | 2 + .../HighLight/File/FileRequestDto.java | 17 +++++ .../HighLight/File/FileService.java | 69 +++++++++++++++++++ .../HighLight/controller/GCSController.java | 7 +- .../HighLight/dto/UploadReqDto.java | 5 +- .../HighLight/service/GCSService.java | 18 +++-- 10 files changed, 160 insertions(+), 18 deletions(-) create mode 100644 src/main/java/com/SollutionChallenge/HighLight/File/FileController.java create mode 100644 src/main/java/com/SollutionChallenge/HighLight/File/FilePostResponseDto.java create mode 100644 src/main/java/com/SollutionChallenge/HighLight/File/FileRequestDto.java create mode 100644 src/main/java/com/SollutionChallenge/HighLight/File/FileService.java diff --git a/build.gradle b/build.gradle index 0288955..3c95e2d 100644 --- a/build.gradle +++ b/build.gradle @@ -37,6 +37,8 @@ dependencies { implementation group: 'org.springframework.cloud', name: 'spring-cloud-gcp-storage', version: '1.2.5.RELEASE' annotationProcessor 'org.projectlombok:lombok' // implementation 'mysql:mysql-connector-java' + implementation 'org.springframework:spring-test:5.3.9' + } diff --git a/src/main/java/com/SollutionChallenge/HighLight/File/File.java b/src/main/java/com/SollutionChallenge/HighLight/File/File.java index d73fd05..a7b295d 100644 --- a/src/main/java/com/SollutionChallenge/HighLight/File/File.java +++ b/src/main/java/com/SollutionChallenge/HighLight/File/File.java @@ -31,7 +31,7 @@ public class File { @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "user_id", nullable = false) - private User userId; + private User user; @Column(nullable = false) private String fileName; @@ -39,12 +39,11 @@ public class File { @Column(nullable = false) private String fileUrl; - public static File createFile(Long id, User userId, String fileName, String fileUrl) { + public static File createFile(User user, String fileName, String fileUrl) { File file= new File(); - file.id=id; - file.userId=userId; - file.fileName=fileName; - file.fileUrl=fileUrl; + file.user = user; + file.fileName = fileName; + file.fileUrl = fileUrl; return file; } diff --git a/src/main/java/com/SollutionChallenge/HighLight/File/FileController.java b/src/main/java/com/SollutionChallenge/HighLight/File/FileController.java new file mode 100644 index 0000000..fe7278d --- /dev/null +++ b/src/main/java/com/SollutionChallenge/HighLight/File/FileController.java @@ -0,0 +1,29 @@ +package com.SollutionChallenge.HighLight.File; + +import com.SollutionChallenge.HighLight.auth.JwtTokenUtil; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestHeader; +import org.springframework.web.bind.annotation.RestController; + +import java.io.IOException; +import java.util.HashMap; + +@RestController +@RequiredArgsConstructor +public class FileController { + private final FileService fileService; + private final JwtTokenUtil jwtTokenUtil; + + @PostMapping("/folder/{folder_id}/files") + public ResponseEntity> addFile(@RequestHeader("token") String jwtToken, FileRequestDto fileRequestDto, @PathVariable Long folder_id) throws IOException { + System.out.println("jwtToken: " + jwtToken); + Long user_id = Long.valueOf(jwtTokenUtil.getUserIdFromToken(jwtToken)); + HashMap map = new HashMap<>(); + map.put("data", fileService.addFile(user_id, folder_id, fileRequestDto)); + return ResponseEntity.ok(map); + } + + } \ No newline at end of file diff --git a/src/main/java/com/SollutionChallenge/HighLight/File/FilePostResponseDto.java b/src/main/java/com/SollutionChallenge/HighLight/File/FilePostResponseDto.java new file mode 100644 index 0000000..70face1 --- /dev/null +++ b/src/main/java/com/SollutionChallenge/HighLight/File/FilePostResponseDto.java @@ -0,0 +1,18 @@ +package com.SollutionChallenge.HighLight.File; + + +import lombok.*; + +@Getter +@Setter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class FilePostResponseDto { + private Long file_id; + private int expected_sec; + + @Builder + public FilePostResponseDto(Long file_id, int expected_sec) { + this.file_id = file_id; + this.expected_sec = expected_sec; + } +} diff --git a/src/main/java/com/SollutionChallenge/HighLight/File/FileRepository.java b/src/main/java/com/SollutionChallenge/HighLight/File/FileRepository.java index 0c5b04f..691bfcb 100644 --- a/src/main/java/com/SollutionChallenge/HighLight/File/FileRepository.java +++ b/src/main/java/com/SollutionChallenge/HighLight/File/FileRepository.java @@ -1,6 +1,8 @@ package com.SollutionChallenge.HighLight.File; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; +@Repository public interface FileRepository extends JpaRepository { } diff --git a/src/main/java/com/SollutionChallenge/HighLight/File/FileRequestDto.java b/src/main/java/com/SollutionChallenge/HighLight/File/FileRequestDto.java new file mode 100644 index 0000000..4b6a1c2 --- /dev/null +++ b/src/main/java/com/SollutionChallenge/HighLight/File/FileRequestDto.java @@ -0,0 +1,17 @@ +package com.SollutionChallenge.HighLight.File; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +import java.io.File; + +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +public class FileRequestDto { + private File file; + private String file_name; +} diff --git a/src/main/java/com/SollutionChallenge/HighLight/File/FileService.java b/src/main/java/com/SollutionChallenge/HighLight/File/FileService.java new file mode 100644 index 0000000..0990c72 --- /dev/null +++ b/src/main/java/com/SollutionChallenge/HighLight/File/FileService.java @@ -0,0 +1,69 @@ +package com.SollutionChallenge.HighLight.File; + +import com.SollutionChallenge.HighLight.User.Entity.User; +import com.SollutionChallenge.HighLight.User.UserRepository; +import com.SollutionChallenge.HighLight.controller.GCSController; +import com.SollutionChallenge.HighLight.dto.UploadReqDto; +import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.annotation.Autowired; +import java.io.FileInputStream; +import java.io.IOException; + +import org.springframework.mock.web.MockMultipartFile; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.multipart.MultipartFile; +import org.springframework.web.multipart.commons.CommonsMultipartFile; + +import java.io.*; +import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +@RequiredArgsConstructor +@Service +public class FileService { + private final FileRepository fileRepository; + private final UserRepository userRepository; + private final GCSController gcsController; + + @Transactional + public FilePostResponseDto addFile(Long userId, Long folderId, FileRequestDto fileRequestDto) throws IOException { + // 받아온 파일을 multipartfile로 변환 + File file = fileRequestDto.getFile(); + String filename = fileRequestDto.getFile_name(); + Path path = Paths.get(file.getPath()); +// String name = file.getName(); + String originalFileName = file.getName(); + String contentType = Files.probeContentType(path); + + byte[] content = Files.readAllBytes(path); + MultipartFile multipartFile = new MockMultipartFile(filename, originalFileName, contentType, content); + + // 파일 gcs에 업로드 + User currentUser = userRepository.findById(userId).get(); + UploadReqDto uploadReqDto = new UploadReqDto(currentUser.getName(), userId, multipartFile); + String uploadedFileUrl = gcsController.uploadNewFile(uploadReqDto, folderId); + + // 후 createFile(User user, String fileName, String fileUrl)로 filerepository에 저장 + com.SollutionChallenge.HighLight.File.File newFile = com.SollutionChallenge.HighLight.File.File.createFile(currentUser, multipartFile.getName(),uploadedFileUrl); + fileRepository.save(newFile); + + /* ml에서 변환 예상 시간 받아오는 코드 작성 */ + int expected_sec = 300; // 임의로 지정 + + return FilePostResponseDto.builder() + .file_id(newFile.getId()) + .expected_sec(expected_sec) + .build(); + } + + +// @Autowired +// public FileService(FileRepository fileRepository) { +// this.fileRepository = fileRepository; +// this.file = File.createFile(null, null, null); +// } + +} \ No newline at end of file diff --git a/src/main/java/com/SollutionChallenge/HighLight/controller/GCSController.java b/src/main/java/com/SollutionChallenge/HighLight/controller/GCSController.java index 558b2c0..6c3324d 100644 --- a/src/main/java/com/SollutionChallenge/HighLight/controller/GCSController.java +++ b/src/main/java/com/SollutionChallenge/HighLight/controller/GCSController.java @@ -19,10 +19,9 @@ public class GCSController { @Autowired private Storage storage; - @PostMapping("/upload") - public ResponseEntity uploadNewFile(UploadReqDto dto) throws IOException { - gcsService.uploadNewFile(dto); - return new ResponseEntity<>(HttpStatus.OK); + //@PostMapping("/upload") + public String uploadNewFile(UploadReqDto dto, Long folderId) throws IOException { + return gcsService.uploadNewFile(dto, folderId); } } diff --git a/src/main/java/com/SollutionChallenge/HighLight/dto/UploadReqDto.java b/src/main/java/com/SollutionChallenge/HighLight/dto/UploadReqDto.java index d60ccce..d8e2afa 100644 --- a/src/main/java/com/SollutionChallenge/HighLight/dto/UploadReqDto.java +++ b/src/main/java/com/SollutionChallenge/HighLight/dto/UploadReqDto.java @@ -9,6 +9,7 @@ @Setter @Builder public class UploadReqDto { - private String userName; - private MultipartFile uploadedfile; + private String user_name; + private Long user_id; + private MultipartFile uploaded_file; } \ No newline at end of file diff --git a/src/main/java/com/SollutionChallenge/HighLight/service/GCSService.java b/src/main/java/com/SollutionChallenge/HighLight/service/GCSService.java index e4b7c3f..7b2351c 100644 --- a/src/main/java/com/SollutionChallenge/HighLight/service/GCSService.java +++ b/src/main/java/com/SollutionChallenge/HighLight/service/GCSService.java @@ -1,6 +1,8 @@ package com.SollutionChallenge.HighLight.service; +import com.google.cloud.storage.Blob; +import com.google.cloud.storage.BlobId; import com.google.cloud.storage.BlobInfo; import com.google.cloud.storage.Storage; import com.SollutionChallenge.HighLight.dto.UploadReqDto; @@ -9,6 +11,7 @@ import org.springframework.stereotype.Service; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.util.UUID; @Service @@ -19,17 +22,20 @@ public class GCSService { @Value("${spring.cloud.gcp.storage.bucket}") private String bucketName; - public void uploadNewFile(UploadReqDto dto) throws IOException { - String uuid = UUID.randomUUID().toString(); // Google Cloud Storage에 저장될 파일 이름 - String ext = dto.getUploadedfile().getContentType(); // 파일의 형식 ex) JPG - + public String uploadNewFile(UploadReqDto dto, Long folderId) throws IOException { +// String uuid = UUID.randomUUID().toString(); // Google Cloud Storage에 저장될 파일 이름 + String ext = dto.getUploaded_file().getContentType(); // 파일의 형식 ex) JPG + Long user_id = dto.getUser_id(); + String uploadFilePath = user_id+"/"+folderId+"/"+dto.getUploaded_file().getName(); // Cloud에 이미지 업로드 BlobInfo blobInfo = storage.create( - BlobInfo.newBuilder(bucketName, uuid) + BlobInfo.newBuilder(bucketName, uploadFilePath) .setContentType(ext) .build(), - dto.getUploadedfile().getInputStream() + dto.getUploaded_file().getInputStream() ); + System.out.println("업로드 경로: " + uploadFilePath); + return "https://storage.googleapis.com/"+uploadFilePath; } From c96732613fd6231eba88e13470892cb7dbb0949c Mon Sep 17 00:00:00 2001 From: kryptonite43 Date: Tue, 28 Mar 2023 16:09:31 +0900 Subject: [PATCH 2/3] =?UTF-8?q?[feat]=20=ED=8C=8C=EC=9D=BC=20=EB=93=B1?= =?UTF-8?q?=EB=A1=9D=20API=20=EA=B5=AC=ED=98=84=20-=20=ED=8C=8C=EC=9D=BC?= =?UTF-8?q?=20=EC=9D=B4=EB=A6=84=20=EB=B3=80=EA=B2=BD=20=EB=AC=B8=EC=A0=9C?= =?UTF-8?q?=20#44?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../HighLight/File/FileRequestDto.java | 3 ++- .../HighLight/File/FileService.java | 12 ++---------- .../HighLight/service/GCSService.java | 4 ++-- 3 files changed, 6 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/SollutionChallenge/HighLight/File/FileRequestDto.java b/src/main/java/com/SollutionChallenge/HighLight/File/FileRequestDto.java index 4b6a1c2..214f2d7 100644 --- a/src/main/java/com/SollutionChallenge/HighLight/File/FileRequestDto.java +++ b/src/main/java/com/SollutionChallenge/HighLight/File/FileRequestDto.java @@ -4,6 +4,7 @@ import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; +import org.springframework.web.multipart.MultipartFile; import java.io.File; @@ -12,6 +13,6 @@ @NoArgsConstructor @AllArgsConstructor public class FileRequestDto { - private File file; + private MultipartFile file; private String file_name; } diff --git a/src/main/java/com/SollutionChallenge/HighLight/File/FileService.java b/src/main/java/com/SollutionChallenge/HighLight/File/FileService.java index 0990c72..5fceeeb 100644 --- a/src/main/java/com/SollutionChallenge/HighLight/File/FileService.java +++ b/src/main/java/com/SollutionChallenge/HighLight/File/FileService.java @@ -30,16 +30,8 @@ public class FileService { @Transactional public FilePostResponseDto addFile(Long userId, Long folderId, FileRequestDto fileRequestDto) throws IOException { - // 받아온 파일을 multipartfile로 변환 - File file = fileRequestDto.getFile(); + MultipartFile multipartFile = fileRequestDto.getFile(); String filename = fileRequestDto.getFile_name(); - Path path = Paths.get(file.getPath()); -// String name = file.getName(); - String originalFileName = file.getName(); - String contentType = Files.probeContentType(path); - - byte[] content = Files.readAllBytes(path); - MultipartFile multipartFile = new MockMultipartFile(filename, originalFileName, contentType, content); // 파일 gcs에 업로드 User currentUser = userRepository.findById(userId).get(); @@ -47,7 +39,7 @@ public FilePostResponseDto addFile(Long userId, Long folderId, FileRequestDto fi String uploadedFileUrl = gcsController.uploadNewFile(uploadReqDto, folderId); // 후 createFile(User user, String fileName, String fileUrl)로 filerepository에 저장 - com.SollutionChallenge.HighLight.File.File newFile = com.SollutionChallenge.HighLight.File.File.createFile(currentUser, multipartFile.getName(),uploadedFileUrl); + com.SollutionChallenge.HighLight.File.File newFile = com.SollutionChallenge.HighLight.File.File.createFile(currentUser, multipartFile.getOriginalFilename(),uploadedFileUrl); fileRepository.save(newFile); /* ml에서 변환 예상 시간 받아오는 코드 작성 */ diff --git a/src/main/java/com/SollutionChallenge/HighLight/service/GCSService.java b/src/main/java/com/SollutionChallenge/HighLight/service/GCSService.java index 7b2351c..f2460d1 100644 --- a/src/main/java/com/SollutionChallenge/HighLight/service/GCSService.java +++ b/src/main/java/com/SollutionChallenge/HighLight/service/GCSService.java @@ -26,7 +26,7 @@ public String uploadNewFile(UploadReqDto dto, Long folderId) throws IOException // String uuid = UUID.randomUUID().toString(); // Google Cloud Storage에 저장될 파일 이름 String ext = dto.getUploaded_file().getContentType(); // 파일의 형식 ex) JPG Long user_id = dto.getUser_id(); - String uploadFilePath = user_id+"/"+folderId+"/"+dto.getUploaded_file().getName(); + String uploadFilePath = user_id+"/"+folderId+"/"+dto.getUploaded_file().getOriginalFilename(); // Cloud에 이미지 업로드 BlobInfo blobInfo = storage.create( BlobInfo.newBuilder(bucketName, uploadFilePath) @@ -35,7 +35,7 @@ public String uploadNewFile(UploadReqDto dto, Long folderId) throws IOException dto.getUploaded_file().getInputStream() ); System.out.println("업로드 경로: " + uploadFilePath); - return "https://storage.googleapis.com/"+uploadFilePath; + return "https://storage.googleapis.com/"+bucketName+"/"+uploadFilePath; } From b603f6eb9ce63d22d401b49bc3d6ec6af1d829e8 Mon Sep 17 00:00:00 2001 From: kryptonite43 Date: Tue, 28 Mar 2023 16:13:59 +0900 Subject: [PATCH 3/3] =?UTF-8?q?[feat]=20=ED=8C=8C=EC=9D=BC=20=EB=93=B1?= =?UTF-8?q?=EB=A1=9D=20API=20=EC=99=84=EC=84=B1=20#44?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/SollutionChallenge/HighLight/File/FileService.java | 4 ++-- .../HighLight/controller/GCSController.java | 4 ++-- .../com/SollutionChallenge/HighLight/service/GCSService.java | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/SollutionChallenge/HighLight/File/FileService.java b/src/main/java/com/SollutionChallenge/HighLight/File/FileService.java index 5fceeeb..8fe1ee1 100644 --- a/src/main/java/com/SollutionChallenge/HighLight/File/FileService.java +++ b/src/main/java/com/SollutionChallenge/HighLight/File/FileService.java @@ -36,10 +36,10 @@ public FilePostResponseDto addFile(Long userId, Long folderId, FileRequestDto fi // 파일 gcs에 업로드 User currentUser = userRepository.findById(userId).get(); UploadReqDto uploadReqDto = new UploadReqDto(currentUser.getName(), userId, multipartFile); - String uploadedFileUrl = gcsController.uploadNewFile(uploadReqDto, folderId); + String uploadedFileUrl = gcsController.uploadNewFile(uploadReqDto, filename, folderId); // 후 createFile(User user, String fileName, String fileUrl)로 filerepository에 저장 - com.SollutionChallenge.HighLight.File.File newFile = com.SollutionChallenge.HighLight.File.File.createFile(currentUser, multipartFile.getOriginalFilename(),uploadedFileUrl); + com.SollutionChallenge.HighLight.File.File newFile = com.SollutionChallenge.HighLight.File.File.createFile(currentUser, filename/*multipartFile.getOriginalFilename()*/,uploadedFileUrl); fileRepository.save(newFile); /* ml에서 변환 예상 시간 받아오는 코드 작성 */ diff --git a/src/main/java/com/SollutionChallenge/HighLight/controller/GCSController.java b/src/main/java/com/SollutionChallenge/HighLight/controller/GCSController.java index 6c3324d..d1579d7 100644 --- a/src/main/java/com/SollutionChallenge/HighLight/controller/GCSController.java +++ b/src/main/java/com/SollutionChallenge/HighLight/controller/GCSController.java @@ -20,8 +20,8 @@ public class GCSController { private Storage storage; //@PostMapping("/upload") - public String uploadNewFile(UploadReqDto dto, Long folderId) throws IOException { - return gcsService.uploadNewFile(dto, folderId); + public String uploadNewFile(UploadReqDto dto, String filename, Long folderId) throws IOException { + return gcsService.uploadNewFile(dto, filename, folderId); } } diff --git a/src/main/java/com/SollutionChallenge/HighLight/service/GCSService.java b/src/main/java/com/SollutionChallenge/HighLight/service/GCSService.java index f2460d1..9509835 100644 --- a/src/main/java/com/SollutionChallenge/HighLight/service/GCSService.java +++ b/src/main/java/com/SollutionChallenge/HighLight/service/GCSService.java @@ -22,11 +22,11 @@ public class GCSService { @Value("${spring.cloud.gcp.storage.bucket}") private String bucketName; - public String uploadNewFile(UploadReqDto dto, Long folderId) throws IOException { + public String uploadNewFile(UploadReqDto dto, String filename, Long folderId) throws IOException { // String uuid = UUID.randomUUID().toString(); // Google Cloud Storage에 저장될 파일 이름 String ext = dto.getUploaded_file().getContentType(); // 파일의 형식 ex) JPG Long user_id = dto.getUser_id(); - String uploadFilePath = user_id+"/"+folderId+"/"+dto.getUploaded_file().getOriginalFilename(); + String uploadFilePath = user_id+"/"+folderId+"/"+filename; // Cloud에 이미지 업로드 BlobInfo blobInfo = storage.create( BlobInfo.newBuilder(bucketName, uploadFilePath)