Skip to content

Commit

Permalink
Merge pull request #50 from dsc-sookmyung/feat/File-post-API-#44
Browse files Browse the repository at this point in the history
파일 등록 api #44
  • Loading branch information
gdakate authored Mar 28, 2023
2 parents 1165a99 + b603f6e commit cfde7ad
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 18 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'

}


Expand Down
11 changes: 5 additions & 6 deletions src/main/java/com/SollutionChallenge/HighLight/File/File.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,19 @@ 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;

@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;
}

Expand Down
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);
}

}
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;
}
}
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> {
}
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;
}
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);
// }

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ public class GCSController {
@Autowired
private Storage storage;

@PostMapping("/upload")
public ResponseEntity<Void> uploadNewFile(UploadReqDto dto) throws IOException {
gcsService.uploadNewFile(dto);
return new ResponseEntity<>(HttpStatus.OK);
//@PostMapping("/upload")
public String uploadNewFile(UploadReqDto dto, String filename, Long folderId) throws IOException {
return gcsService.uploadNewFile(dto, filename, folderId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -9,6 +11,7 @@
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.UUID;

@Service
Expand All @@ -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, 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+"/"+filename;
// 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/"+bucketName+"/"+uploadFilePath;
}


Expand Down

0 comments on commit cfde7ad

Please sign in to comment.