Skip to content

Commit

Permalink
Merge pull request #99 from TelePigeon/fix/98
Browse files Browse the repository at this point in the history
[fix] S3 이미지 업로드 및 API 수정
  • Loading branch information
tkdwns414 authored Jun 7, 2024
2 parents e065943 + 2b09dac commit 8805b95
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ dependencies {
// CI/CD health check
implementation 'org.springframework.boot:spring-boot-starter-actuator'

// Multipart file
// AWS S3
implementation("software.amazon.awssdk:bom:2.21.0")
implementation("software.amazon.awssdk:s3:2.21.0")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.springframework.data.util.Pair;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDate;
Expand All @@ -47,7 +48,7 @@ public class AnswerService {
private final FcmService fcmService;
private final S3Service s3Service;

private static String ANSWER_S3_UPLOAD_FOLDER = "/answer/";
private static String ANSWER_S3_UPLOAD_FOLDER = "answer/";

@Transactional
public Answer create(
Expand All @@ -64,10 +65,14 @@ public Answer create(
ConfidenceCreateDto.of(answerCreateDto.content())
);
Double emotion = (confidence.positive() - confidence.negative()) * 0.01;

Answer answer = answerSaver.create(
Answer.create(answerCreateDto.content(),
s3Service.uploadImage(ANSWER_S3_UPLOAD_FOLDER, answerCreateDto.image()),
emotion, question, profile)
Answer.create(
answerCreateDto.content(),
uploadImage(answerCreateDto.image()),
emotion,
question,
profile)
);

profile.updateEmotion(
Expand Down Expand Up @@ -212,4 +217,10 @@ private double CalculateEmotion(
) {
return totEmotion * 0.9 + emotion * 0.1;
}

private String uploadImage(MultipartFile image) throws IOException {
if (image != null)
return s3Service.uploadImage(ANSWER_S3_UPLOAD_FOLDER, image);
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public String uploadImage(String directoryPath, MultipartFile image) throws IOEx

RequestBody requestBody = RequestBody.fromBytes(image.getBytes());
s3Client.putObject(request, requestBody);
return key;
return s3Client.utilities().getUrl(builder -> builder.bucket(bucketName).key(key)).toExternalForm();
}

public void deleteImage(String key) throws IOException {
Expand Down

0 comments on commit 8805b95

Please sign in to comment.