Skip to content

Commit

Permalink
[feat] 특정 파일 조회 API #48
Browse files Browse the repository at this point in the history
  • Loading branch information
kryptonite43 committed Mar 28, 2023
1 parent b603f6e commit f3e2113
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
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 org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
Expand All @@ -26,4 +23,14 @@ public ResponseEntity<HashMap<String, FilePostResponseDto>> addFile(@RequestHead
return ResponseEntity.ok(map);
}

}

// 특정 파일 조회
@GetMapping("/files/{file_id}")
public ResponseEntity<HashMap<String, GetFileResponseDto>> getFile(@RequestHeader("token") String jwtToken, @PathVariable Long file_id) {
System.out.println("jwtToken: " + jwtToken);
Long user_id = Long.valueOf(jwtTokenUtil.getUserIdFromToken(jwtToken));
HashMap<String, GetFileResponseDto> map = new HashMap<>();
map.put("data", fileService.getFile(user_id, file_id));
return ResponseEntity.ok(map);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,27 @@
import com.SollutionChallenge.HighLight.User.UserRepository;
import com.SollutionChallenge.HighLight.controller.GCSController;
import com.SollutionChallenge.HighLight.dto.UploadReqDto;
import com.google.cloud.storage.Blob;
import com.google.cloud.storage.BlobId;
import com.google.cloud.storage.Storage;
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;
import java.util.Optional;

@RequiredArgsConstructor
@Service
public class FileService {
private final FileRepository fileRepository;
private final UserRepository userRepository;
private final GCSController gcsController;
@Autowired
private Storage storage;

@Transactional
public FilePostResponseDto addFile(Long userId, Long folderId, FileRequestDto fileRequestDto) throws IOException {
Expand All @@ -51,11 +49,35 @@ public FilePostResponseDto addFile(Long userId, Long folderId, FileRequestDto fi
.build();
}

@Transactional
public GetFileResponseDto getFile(Long userId, Long fileId) {
// 파일 repository에서 파일 찾기
Optional<File> wantedFile = fileRepository.findById(fileId);
// GCS에서 파일 폴더 찾아서 페이지 몇갠지 세어보기
if (wantedFile.isPresent()) {
File target = wantedFile.get();
String fileName = target.getFileName();
int pageId = 1;
boolean isExist = true;
while (isExist) {
// String filesPath = "userid/"+fileName+"_json_folder/"+pageId+"/"+fileName+"_"+pageId+".json"; // 테스트용 코드
String filesPath = userId+"/"+fileName+"_json_folder/"+pageId+"/"+fileName+"_"+pageId+".json"; // 실제 코드
System.out.println("파일 경로: " + filesPath);
BlobId blobId = BlobId.of("cloud_storage_leturn", filesPath);

// @Autowired
// public FileService(FileRepository fileRepository) {
// this.fileRepository = fileRepository;
// this.file = File.createFile(null, null, null);
// }

try {
Blob blob = storage.get(blobId);
isExist = blob.exists();
} catch (NullPointerException e) {
System.out.println("count: "+pageId+" is not present");
return new GetFileResponseDto(fileId, pageId-1);
}
if (isExist) {
System.out.println("count: "+pageId+" is present");
pageId++;
}
}
}
return new GetFileResponseDto();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.SollutionChallenge.HighLight.File;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class GetFileResponseDto {
private Long file_id;
private int page_count;
}

0 comments on commit f3e2113

Please sign in to comment.