Skip to content

Commit

Permalink
Merge pull request #58 from BbeumbungE/dev
Browse files Browse the repository at this point in the history
Deploy: v2 배포
  • Loading branch information
ah9mon authored Sep 26, 2023
2 parents 285bba8 + 6dfb113 commit 96f680f
Show file tree
Hide file tree
Showing 72 changed files with 952 additions and 195 deletions.
6 changes: 6 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ dependencies {
// S3
implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE'

// google vision api
implementation 'com.google.cloud:google-cloud-vision:2.0.0'

// bad-word-filter
implementation 'io.github.vaneproject:badwordfiltering:1.0.0'

compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testCompileOnly 'org.projectlombok:lombok'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public ResponseEntity<Response> updateSketchAndCanvas(
) throws IOException {
String sketch = s3ImageUploadService.uploadFile(sketchFile, s3PathBuildService.buildPath(profileId, "sketch"));
Response response = canvasConvertService.updateSketchAndCanvas(profileId, canvasId, sketch);
return ResponseEntity.status(HttpStatus.NO_CONTENT).body(response);
return ResponseEntity.status(HttpStatus.OK).body(response);
}

@GetMapping("/profile/{profileId}/sse")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
import com.siliconvalley.domain.rabbitMQ.service.ConvertRequestSender;
import com.siliconvalley.domain.sse.application.CanvasSseEmitterFInder;
import com.siliconvalley.domain.sse.application.ConvertResultSender;
import com.siliconvalley.domain.sse.application.SseEmitterFinder;
import com.siliconvalley.domain.sse.application.SseEmitterSender;
import com.siliconvalley.global.common.dto.Response;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -55,6 +53,6 @@ public void updateConvertedData(SketchConversionResponse response){
canvas.updateCanvas(response.getCanvasUrl());
Long profileId = canvas.getProfile().getId();
ConvertEventDto convertEventDto = new ConvertEventDto(canvas.getId(), response.getCanvasUrl());
convertResultSender.send(canvasSseEmitterFInder.findByProfileId(profileId), convertEventDto, profileId);
convertResultSender.send(canvasSseEmitterFInder.findByProfileId(profileId), convertEventDto, profileId, "drawing");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.siliconvalley.domain.canvas.domain.Canvas;
import com.siliconvalley.domain.image.service.S3ImageUploadService;
import com.siliconvalley.domain.post.service.RankCachingService;
import com.siliconvalley.domain.rabbitMQ.code.RabbitMQCode;
import com.siliconvalley.domain.rabbitMQ.service.ConvertRequestSender;
import com.siliconvalley.global.common.dto.Response;
Expand All @@ -19,13 +20,14 @@ public class CanvasUpdateService {

private final S3ImageUploadService s3ImageUploadService;
private final ConvertRequestSender convertRequestSender;
private final RankCachingService rankCachingService;

public Response updateSketchAndCanvas(Canvas canvas, String newSketch, Long profileId){
s3ImageUploadService.deleteImage(canvas.getCanvas());
s3ImageUploadService.deleteImage(canvas.getSketch());
canvas.updateSketch(newSketch);
convertRequestSender.sendSketchConversionRequest(newSketch, canvas.getId(), profileId, canvas.getSubject());
return Response.of(RabbitMQCode.CONVERSION_RESPONSE_SUCCESS);
return Response.of(RabbitMQCode.CONVERSION_RESPONSE_SUCCESS, rankCachingService.getTopPostThisWeek(canvas.getSubject().getId()));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.siliconvalley.domain.google.service;

import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.vision.v1.*;
import com.google.cloud.vision.v1.ImageAnnotatorClient;
import com.google.cloud.vision.v1.ImageAnnotatorSettings;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class GoogleVisionApiService {

@Value("${google.service.account.key}")
private String serviceAccountKey;

public Map<String, Float> detectObjects(String filePath) {
List<AnnotateImageRequest> requests = new ArrayList<>();
Map<String, Float> results = new HashMap<>();

try {
ImageSource imageSource = ImageSource.newBuilder().setImageUri(filePath).build();
Image img = Image.newBuilder().setSource(imageSource).build();
Feature feat = Feature.newBuilder().setType(Feature.Type.OBJECT_LOCALIZATION).build();
AnnotateImageRequest request = AnnotateImageRequest.newBuilder()
.addFeatures(feat)
.setImage(img)
.build();
requests.add(request);

GoogleCredentials credentials = GoogleCredentials.fromStream(
new ByteArrayInputStream(serviceAccountKey.getBytes(StandardCharsets.UTF_8))
);
ImageAnnotatorSettings settings = ImageAnnotatorSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credentials))
.build();

try (ImageAnnotatorClient client = ImageAnnotatorClient.create(settings)) {
BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
List<AnnotateImageResponse> responses = response.getResponsesList();

for (AnnotateImageResponse res : responses) {
if (res.hasError()) {
results.put("Error", -1f);
log.info("Error occurred: " + res.getError().getMessage());
continue;
}

for (LocalizedObjectAnnotation entity : res.getLocalizedObjectAnnotationsList()) {
log.info("Detected object: " + entity.getName() + " with confidence: " + entity.getScore());
results.put(entity.getName().toLowerCase(), entity.getScore());
}
}
}
} catch (IOException e) {
results.put("Error", -1f);
log.error("An IOException occurred: ", e);
}

return results;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.siliconvalley.domain.google.service;

import com.siliconvalley.domain.canvas.dao.CanvasFindDao;
import com.siliconvalley.domain.canvas.domain.Canvas;
import com.siliconvalley.domain.stage.domain.Score;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

@Service
@RequiredArgsConstructor
@Transactional
@Slf4j
public class VisionDetectingService {

private final GoogleVisionApiService visionService;
private final CanvasFindDao canvasFindDao;

public Score calculateCanvasScore(Long canvasId) {
Canvas canvas = canvasFindDao.findById(canvasId);
Map<String, Float> detectResult = visionService.detectObjects(canvas.getCanvas());
Float detectionScore = detectResult.get(canvas.getSubject().getPix2Pix().getVisionName());

return Score.determineScore(detectionScore);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.siliconvalley.domain.image.service.S3ImageUploadService;
import com.siliconvalley.domain.image.service.S3PathBuildService;
import com.siliconvalley.domain.item.item.dao.SubjectItemFindDao;
import com.siliconvalley.domain.item.item.dto.SubjectItemCreateRequest;
import com.siliconvalley.domain.item.item.application.SubjectItemCreateService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
Expand All @@ -12,7 +11,6 @@
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.IOException;

@RestController
Expand All @@ -33,13 +31,13 @@ public class SubjectItemApi {
@PostMapping
public ResponseEntity createSubjectItem(
@RequestParam("subjectImage") MultipartFile subjectImage,
@RequestParam("sketchImage") MultipartFile sketchImage,
@RequestParam("itemPrice") Long itemPrice,
@RequestParam("subjectName") String subjectName
@RequestParam("subjectName") String subjectName,
@RequestParam("modelName") String modelName,
@RequestParam("visionName") String visionName
) throws IOException {
String subjectImgUrl = s3ImageUploadService.uploadFile(subjectImage, s3PathBuildService.buildPathForItem("subject"));
String sketchImgUrl = s3ImageUploadService.uploadFile(sketchImage, s3PathBuildService.buildPathForItem("subject/sketch"));
return ResponseEntity.status(HttpStatus.CREATED).body(subjectItemCreateService.createSubjectItem(itemPrice, subjectName, subjectImgUrl, sketchImgUrl));
return ResponseEntity.status(HttpStatus.CREATED).body(subjectItemCreateService.createSubjectItem(itemPrice, subjectName, subjectImgUrl, modelName, visionName));
}

@GetMapping
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
import com.siliconvalley.domain.item.subject.domain.Subject;
import com.siliconvalley.domain.notification.application.NotificationPushService;
import com.siliconvalley.domain.notification.domain.NotificationType;
import com.siliconvalley.domain.pix2pix.domain.Pix2Pix;
import com.siliconvalley.global.common.dto.Response;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;

@Service
@Transactional
@RequiredArgsConstructor
Expand All @@ -22,12 +21,13 @@ public class SubjectItemCreateService {
private final ItemRepository itemRepository;
private final NotificationPushService notificationPushService;

public Response createSubjectItem(Long itemPrice, String subjectName, String subjectImgUrl, String sketchImgUrl) throws IOException {
public Response createSubjectItem(Long itemPrice, String subjectName, String subjectImgUrl, String modelName, String visionName) {

Item item = Item.toEntity(itemPrice);

Subject subject = Subject.toEntity(subjectName, subjectImgUrl, item);
// Item과 Subject빌드 및 연관관계 매핑
item.addSubject(Subject.toEntity(subjectName, subjectImgUrl, sketchImgUrl, item));
subject.setPix2Pix(Pix2Pix.toEntity(subject, modelName, visionName));
item.addSubject(subject);

// Item이 저장될 때 Subject 자동 저장
itemRepository.save(item);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public Subject getSubject(Item item, String subjectImgUrl, String sketchImgUrl)
return Subject.builder()
.subjectName(subjectName)
.subjectImage(subjectImgUrl)
.sketch(sketchImgUrl)
.item(item)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@

public class InvalidCategoryException extends InvalidValueException {
public InvalidCategoryException(String value) {
super(value, ErrorCode.INVALID_CATEROTY);
super(value, ErrorCode.INVALID_CATEGORY);
}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
package com.siliconvalley.domain.item.subject.api;

import com.siliconvalley.domain.stage.application.StageUpdateService;
import com.siliconvalley.domain.image.service.S3ImageUploadService;
import com.siliconvalley.domain.image.service.S3PathBuildService;
import com.siliconvalley.domain.item.subject.application.SketchCreateService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.IOException;

@RestController
@RequestMapping("/api/subjects")
@RequiredArgsConstructor
public class SubjectApi {

private final StageUpdateService stageUpdateService;
private final S3ImageUploadService s3ImageUploadService;
private final S3PathBuildService s3PathBuildService;
private final SketchCreateService sketchCreateService;

@PatchMapping("/{subjectId}/stages/{stageId}")
public ResponseEntity setSubjectToStage(
@PostMapping("/{subjectId}/sketches")
public ResponseEntity addSketchToSubject(
@PathVariable(name = "subjectId") Long subjectId,
@PathVariable(name = "stageId") Long stageId
) {
return ResponseEntity.status(HttpStatus.NO_CONTENT).body(stageUpdateService.updateStage(subjectId, stageId));
@RequestParam(name = "sketchImage") MultipartFile sketchImage
) throws IOException {
String sketchImageUrl = s3ImageUploadService.uploadFile(sketchImage, s3PathBuildService.buildPathForItem("sketch"));
return ResponseEntity.status(HttpStatus.CREATED).body(sketchCreateService.createSketch(subjectId, sketchImageUrl));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.siliconvalley.domain.item.subject.application;

import com.siliconvalley.domain.item.subject.code.SketchCode;
import com.siliconvalley.domain.item.subject.dao.SketchRepository;
import com.siliconvalley.domain.item.subject.dao.SubjectFindDao;
import com.siliconvalley.domain.item.subject.domain.Sketch;
import com.siliconvalley.domain.item.subject.domain.Subject;
import com.siliconvalley.domain.item.subject.dto.SketchPostSuccessResponse;
import com.siliconvalley.global.common.dto.Response;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
@RequiredArgsConstructor
public class SketchCreateService {

private final SubjectFindDao subjectFindDao;
private final SketchRepository sketchRepository;

public Response createSketch(Long subjectId, String sketchImageUrl) {
Subject subject = subjectFindDao.findById(subjectId);
Sketch sketch = Sketch.toEntity(sketchImageUrl, subject);
sketchRepository.save(sketch);
return Response.of(SketchCode.CREATE_SUCCESS, new SketchPostSuccessResponse(sketch));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.siliconvalley.domain.item.subject.code;

import com.siliconvalley.global.common.code.ResponseCode;
import org.springframework.http.HttpStatus;

public enum SketchCode implements ResponseCode {

CREATE_SUCCESS(201, "스케치 생성에 성공했습니다.", HttpStatus.CREATED);

private int code;
private String message;

private HttpStatus httpStatus;

SketchCode (int code, String message, HttpStatus httpStatus) {
this.code = code;
this.message = message;
this.httpStatus = httpStatus;
}

@Override
public int getCode() {
return this.code;
}

@Override
public String getMessage() {
return this.message;
}

@Override
public HttpStatus getHttpStatus() {
return this.httpStatus;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.siliconvalley.domain.item.subject.dao;

import com.siliconvalley.domain.item.subject.domain.Sketch;
import org.springframework.data.jpa.repository.JpaRepository;

public interface SketchRepository extends JpaRepository<Sketch, Long> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.siliconvalley.domain.item.subject.domain.Subject;
import com.siliconvalley.domain.item.subject.exception.SubjectNotFoundException;
import com.siliconvalley.domain.pix2pix.dto.Pix2PixAndSubjectDto;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
Expand All @@ -26,4 +27,11 @@ public Subject findById(Long subjectId) {
subjectOptional.orElseThrow(() -> new SubjectNotFoundException(subjectId));
return subjectOptional.get();
}

@Cacheable(key = "'subject:' + #subjectId", value = "subjectCache")
public Pix2PixAndSubjectDto findSubjectForCachingById(Long subjectId){
Optional<Subject> subject = subjectRepository.findById(subjectId);
subject.orElseThrow(() -> new SubjectNotFoundException(subjectId));
return new Pix2PixAndSubjectDto(subject.get());
}
}
Loading

0 comments on commit 96f680f

Please sign in to comment.