Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backend] fix : s3 파일 이름 변경 #72

Merged
merged 2 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.isp.backend.domain.gpt.service;

import com.isp.backend.domain.country.entity.Country;
import com.isp.backend.domain.gpt.config.GptConfig;
import com.isp.backend.domain.gpt.constant.ParsingConstants;
import com.isp.backend.domain.gpt.dto.request.GptRequest;
Expand All @@ -10,6 +11,7 @@
import com.isp.backend.domain.gpt.entity.GptMessage;
import com.isp.backend.domain.gpt.entity.GptSchedule;
import com.isp.backend.domain.gpt.entity.GptScheduleParser;
import com.isp.backend.domain.schedule.service.ScheduleService;
import com.isp.backend.global.s3.service.S3ImageService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -32,7 +34,7 @@
public class GptService {
private final RestTemplate restTemplate;
private final GptScheduleParser gptScheduleParser;
private final S3ImageService s3ImageService;
private final ScheduleService scheduleService;

@Value("${api-key.chat-gpt}")
private String apiKey;
Expand All @@ -55,6 +57,7 @@ public GptScheduleResponse getResponse(HttpEntity<GptRequest> chatGptRequestHttp
GptConfig.CHAT_URL,
chatGptRequestHttpEntity,
GptResponse.class);

List<GptSchedule> gptSchedules = gptScheduleParser.parseScheduleText(getScheduleText(responseEntity));

return new GptScheduleResponse(gptSchedules);
Expand All @@ -77,7 +80,8 @@ public GptSchedulesResponse askQuestion(GptScheduleRequest questionRequestDTO) {
.build()
);

String countryImage = s3ImageService.get(questionRequestDTO.getDestination());
Country country = scheduleService.validateCountry(questionRequestDTO.getDestination());
String countryImage = country.getImageUrl();
List<GptScheduleResponse> schedules = new ArrayList<>();
for(int i = 0; i < 3; i++) {
schedules.add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public enum ErrorCode {
ACCESS_TOKEN_IS_INVALID(HttpStatus.UNAUTHORIZED, "U004", "엑세스 토큰이 유효하지 않습니다."),
REFRESH_TOKEN_IS_INVALID(HttpStatus.UNAUTHORIZED, "U005", "리프레시 토큰이 유효하지 않습니다."),

// Image
DIRECTORY_NAME_NOTFOUND(HttpStatus.NOT_FOUND,"I001","S3에서 해당 디렉토리의 이름을 찾을 수 없습니다."),

// Schedule
COUNTRY_NOT_FOUND(HttpStatus.NOT_FOUND, "S001", "여행할 국가를 찾을 수 없습니다."),
SCHEDULE_NOT_FOUND(HttpStatus.NOT_FOUND, "S002", "여행 일정을 찾을 수 없습니다."),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.isp.backend.global.exception.Image;

import com.isp.backend.global.exception.CustomException;
import com.isp.backend.global.exception.ErrorCode;


public class DirectoryNameNotFoundException extends CustomException {

public DirectoryNameNotFoundException() {
super(ErrorCode.DIRECTORY_NAME_NOTFOUND);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
@RestController
@RequiredArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true)
@RequestMapping("images")
@RequestMapping("/images")
public class S3ImageController {
S3ImageService s3ImageService;

@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public String create(@RequestParam("file") MultipartFile file) {
return s3ImageService.create(file);
public String create(@RequestParam("file") MultipartFile file,
@RequestParam("directory") String directory) {
return s3ImageService.create(file, directory);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

@Repository
@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true)
Expand All @@ -24,14 +25,15 @@ public S3ImageRepository(S3Template s3template, @Value("${spring.cloud.aws.s3.bu
this.bucketName = bucketName;
}

public String save(MultipartFile file) {
String key = S3BucketDirectory.IMAGE.getDirectory() + file.getOriginalFilename();
public String save(MultipartFile file, String directory) {
String uniqueFileName = UUID.randomUUID() + "-" + file.getOriginalFilename() ;
String key = directory + uniqueFileName;
final S3Resource result = s3template.upload(bucketName, key, getInputStream(file));
return getUrl(result);
}

public String find(String filename) {
String key = S3BucketDirectory.IMAGE.getDirectory() + filename;
public String find(String directory, String filename) {
String key = directory + filename;
final S3Resource result = s3template.download(bucketName, key);
return getUrl(result);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.isp.backend.global.s3.service;

import com.isp.backend.global.exception.Image.DirectoryNameNotFoundException;
import com.isp.backend.global.s3.constant.S3BucketDirectory;
import com.isp.backend.global.s3.repository.S3ImageRepository;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
Expand All @@ -13,11 +15,22 @@
public class S3ImageService {
S3ImageRepository s3ImageRepository;

public String create(MultipartFile file) {
return s3ImageRepository.save(file);
public String create(MultipartFile file, String directory) {
String mappedDirectory = mapDirectory(directory);
return s3ImageRepository.save(file, mappedDirectory);
}

public String get(String fileName) {
return s3ImageRepository.find(fileName);
public String get(String directory, String fileName) {
return s3ImageRepository.find(directory, fileName);
}

private String mapDirectory(String directory) {
if (S3BucketDirectory.valueOf(directory) == S3BucketDirectory.IMAGE ||
S3BucketDirectory.valueOf(directory) == S3BucketDirectory.PHOTO) {
return S3BucketDirectory.valueOf(directory).getDirectory();
} else {
throw new DirectoryNameNotFoundException();
}
}

}
Loading