-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from dsc-sookmyung/feat/File-post-API-#44
파일 등록 api #44
- Loading branch information
Showing
10 changed files
with
153 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/main/java/com/SollutionChallenge/HighLight/File/FileController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<HashMap<String, FilePostResponseDto>> 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<String, FilePostResponseDto> map = new HashMap<>(); | ||
map.put("data", fileService.addFile(user_id, folder_id, fileRequestDto)); | ||
return ResponseEntity.ok(map); | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/SollutionChallenge/HighLight/File/FilePostResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
src/main/java/com/SollutionChallenge/HighLight/File/FileRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<File, Long> { | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/SollutionChallenge/HighLight/File/FileRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.SollutionChallenge.HighLight.File; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.File; | ||
|
||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class FileRequestDto { | ||
private MultipartFile file; | ||
private String file_name; | ||
} |
61 changes: 61 additions & 0 deletions
61
src/main/java/com/SollutionChallenge/HighLight/File/FileService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
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 multipartFile = fileRequestDto.getFile(); | ||
String filename = fileRequestDto.getFile_name(); | ||
|
||
// 파일 gcs에 업로드 | ||
User currentUser = userRepository.findById(userId).get(); | ||
UploadReqDto uploadReqDto = new UploadReqDto(currentUser.getName(), userId, multipartFile); | ||
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, filename/*multipartFile.getOriginalFilename()*/,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); | ||
// } | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters