Skip to content

Commit

Permalink
[feat] 특정 페이지 조회 API #41
Browse files Browse the repository at this point in the history
  • Loading branch information
kryptonite43 committed Mar 27, 2023
1 parent c5956bc commit ead04a2
Show file tree
Hide file tree
Showing 15 changed files with 215 additions and 10 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ dependencies {
implementation 'io.jsonwebtoken:jjwt-api:0.11.1'
runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.11.1', 'io.jsonwebtoken:jjwt-jackson:0.11.1'
implementation 'org.springframework.cloud:spring-cloud-gcp-starter-storage:1.2.8.RELEASE'
implementation 'com.googlecode.json-simple:json-simple:1.1.1'
// implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'
// implementation 'org.springframework.boot:spring-boot-starter-oauth2-resource-server'
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/SollutionChallenge/HighLight/File/File.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ public class File {
@Column(nullable = false)
private String fileUrl;

private static File createFile(Long id, User userId, String fileName, String fileUrl) {
public static File createFile(Long id, User userId, String fileName, String fileUrl) {
File file= new File();
file.id=id;
file.userId=userId;
file.fileName=fileName;
file.fileUrl=fileUrl;
return new File();
return file;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.SollutionChallenge.HighLight.File;

import org.springframework.data.jpa.repository.JpaRepository;

public interface FileRepository extends JpaRepository<File, Long> {
}
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
@AllArgsConstructor
@NoArgsConstructor
public class FullTextInfoDto {
private String full_text;
private String audio_url;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.SollutionChallenge.HighLight.File;

import lombok.*;

import java.util.List;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class GetPageResDto {
private Long page_id;
private FullTextInfoDto full_text;
private List<TextInfoDto> text;
private List<ImageInfoDto> image;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.SollutionChallenge.HighLight.File;

import lombok.*;

@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class ImageInfoDto {
private Long img_idx;
private String img_url;
private String audio_url;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.SollutionChallenge.HighLight.File;

import lombok.*;

import java.util.List;
@Getter
@Setter
@NoArgsConstructor
public class SendPageResDto {
private Long page_id;
private String full_audio_url;
private List<SendTextInfoDto> text;
private List<ImageInfoDto> image;

@Builder
public SendPageResDto(Long page_id, String full_audio_url, List<SendTextInfoDto> text, List<ImageInfoDto> image) {
this.page_id = page_id;
this.full_audio_url = full_audio_url;
this.text = text;
this.image = image;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.SollutionChallenge.HighLight.File;

import lombok.*;

@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class SendTextInfoDto {
private String audio_url;
private int font_size;
private String text_content;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.SollutionChallenge.HighLight.File;

import lombok.*;

@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class TextInfoDto {
private String audio_url;
private int font_size;
private String text; // text_content
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.SollutionChallenge.HighLight;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;


Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,29 @@
package com.SollutionChallenge.HighLight.Page;

import com.SollutionChallenge.HighLight.File.SendPageResDto;
import com.SollutionChallenge.HighLight.auth.JwtTokenUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;


import java.util.HashMap;

@RestController
@RequiredArgsConstructor
public class PageController {
private final PageService pageService;
private final JwtTokenUtil jwtTokenUtil;
@GetMapping("/files/{file_id}/page/{page_id}")
public ResponseEntity<HashMap<String, SendPageResDto>> getPageContents(@RequestHeader("token") String jwtToken, @PathVariable Long file_id, @PathVariable Long page_id) throws JsonProcessingException {
System.out.println("jwtToken: " + jwtToken);
Long user_id = Long.valueOf(jwtTokenUtil.getUserIdFromToken(jwtToken));
HashMap<String, SendPageResDto> map = new HashMap<>();
map.put("data", pageService.getPageContents(user_id, file_id, page_id));
return ResponseEntity.ok(map);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.SollutionChallenge.HighLight.Page;

import com.SollutionChallenge.HighLight.File.*;
import com.SollutionChallenge.HighLight.User.Entity.User;
import com.SollutionChallenge.HighLight.User.UserRepository;
import com.SollutionChallenge.HighLight.auth.JwtTokenUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
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 org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@RequiredArgsConstructor
@Service
public class PageService {
private final FileRepository fileRepository;
private final UserRepository userRepository;
@Autowired
private Storage storage;
@Transactional
public SendPageResDto getPageContents(Long userId, Long fileId, Long pageId) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
// User user = userRepository.findById(1L).get(); // api 테스트용 파일생성 코드 - 유저 정보 임의로 가져옴
User user = userRepository.findById(userId).get();
System.out.println("userid: "+userId);
// File testfile = File.createFile(1L, user, "StallingsOS8e-Chap04", "파일링크"); // api 테스트용 파일 생성 코드
// fileRepository.save(testfile); // api테스트용 파일 생성 코드

// 파일 repository에서 파일 찾기
Optional<File> wantedFile = fileRepository.findById(fileId);

// GCS에서 해당하는 페이지 json 받아오기
if (wantedFile.isPresent()) {
File target = wantedFile.get();
String fileName = target.getFileName();
// String downloadFileName = "userid/"+fileName+"_json_folder/"+pageId+"/"+fileName+"_"+pageId+".json"; // api 테스트용 파일 생성 코드
String downloadFileName = userId+"/"+fileName+"_json_folder/"+pageId+"/"+fileName+"_"+pageId+".json"; // 실제 코드

System.out.println("다운로드 경로: " + downloadFileName);
BlobId blobId = BlobId.of("cloud_storage_leturn", downloadFileName);
Blob blob = storage.get(blobId);
byte[] content = blob.getContent();
String targetJson = new String(content, StandardCharsets.UTF_8);

// 보내줄 형식 맞추어 다시 dto로 매핑하기
GetPageResDto pageResDto = objectMapper.readValue(targetJson, GetPageResDto.class);
List<TextInfoDto> textInfoDto = pageResDto.getText();
List<SendTextInfoDto> sendTextInfoDto = new ArrayList<SendTextInfoDto>();
for (TextInfoDto textDto: textInfoDto) {
SendTextInfoDto sendDto = new SendTextInfoDto();
sendDto.setText_content(textDto.getText());
sendDto.setFont_size(textDto.getFont_size());
sendDto.setAudio_url(textDto.getAudio_url());
sendTextInfoDto.add(sendDto);
}
return SendPageResDto.builder()
.page_id(pageResDto.getPage_id())
.full_audio_url(pageResDto.getFull_text().getAudio_url())
.text(sendTextInfoDto)
.image(pageResDto.getImage())
.build();
}
else {
return new SendPageResDto();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ public JwtTokenUtil(
this.tokenValidityInMilliseconds = tokenValidityInSeconds * 1000;
}

public String getUsernameFromToken(String token) {
// public String getUsernameFromToken(String token) {
// return getClaimFromToken(token, Claims::getSubject);
// }

public String getUserIdFromToken(String token) {
return getClaimFromToken(token, Claims::getSubject);
}

Expand All @@ -51,7 +55,7 @@ private Boolean isTokenExpired(String token) {

public String generateToken(User user) {
Map<String, Object> claims = new HashMap<>();
return doGenerateToken(claims, user.getName());
return doGenerateToken(claims, String.valueOf(user.getId()));
}

private String doGenerateToken(Map<String, Object> claims, String subject) {
Expand All @@ -65,7 +69,7 @@ private String doGenerateToken(Map<String, Object> claims, String subject) {
}

public Boolean validateToken(String token, User user) {
final String username = getUsernameFromToken(token);
return (username.equals(user.getName()) && !isTokenExpired(token));
final Long userId = Long.valueOf(getUserIdFromToken(token));
return (userId.equals(user.getId()) && !isTokenExpired(token));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public class GCSController {
@Autowired
private Storage storage;


@PostMapping("/upload")
public ResponseEntity<Void> uploadNewFile(UploadReqDto dto) throws IOException {
gcsService.uploadNewFile(dto);
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ spring.servlet.multipart.maxRequestSize=100MB
#JPA
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.show_sql=true
#spring.jpa.properties.hibernate.show_sql=true
#spring.jpa.properties.hibernate.format_sql=true

logging.level.org.hibernate.SQL=debug
Expand Down

0 comments on commit ead04a2

Please sign in to comment.