-
Notifications
You must be signed in to change notification settings - Fork 0
[FEAT] #83: 이벤트 이미지 연결 API 구현 #84
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/main/java/com/saferoute/domain/congestion/dto/request/ConnectEventImageRequest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package com.saferoute.domain.congestion.dto.request; | ||
|
|
||
| import jakarta.validation.constraints.NotBlank; | ||
| import jakarta.validation.constraints.NotNull; | ||
| import jakarta.validation.constraints.PositiveOrZero; | ||
|
|
||
| public record ConnectEventImageRequest( | ||
| @NotBlank String eventImageKey, | ||
| @NotNull @PositiveOrZero Long uploadedAt | ||
| ) { | ||
| } |
142 changes: 142 additions & 0 deletions
142
src/main/java/com/saferoute/domain/congestion/service/CongestionEventImageService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| package com.saferoute.domain.congestion.service; | ||
|
|
||
| import com.saferoute.domain.congestion.dto.request.ConnectEventImageRequest; | ||
| import com.saferoute.domain.device.service.DeviceAuthorizationService; | ||
| import com.saferoute.domain.telemetry.dynamo.entity.EventProcessingStatus; | ||
| import com.saferoute.domain.telemetry.dynamo.entity.ImageUploadStatus; | ||
| import com.saferoute.domain.telemetry.dynamo.entity.ObservationItem; | ||
| import com.saferoute.domain.telemetry.dynamo.repository.ObservationRepository; | ||
| import com.saferoute.global.api.error.CongestionErrorCode; | ||
| import com.saferoute.global.api.exception.ApiException; | ||
| import com.saferoute.global.security.DevicePrincipal; | ||
| import com.saferoute.infrastructure.s3.service.S3Service; | ||
| import com.saferoute.infrastructure.websocket.service.TrainingEventPublisher; | ||
| import java.util.Objects; | ||
| import java.util.UUID; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| @Transactional(readOnly = true) | ||
| public class CongestionEventImageService { | ||
|
|
||
| private static final String TRAINING_PREFIX = "training"; | ||
| private static final String EVENTS_DIRECTORY = "events"; | ||
| private static final String JPEG_SUFFIX = ".jpg"; | ||
|
|
||
| private final ObservationRepository observationRepository; | ||
| private final DeviceAuthorizationService deviceAuthorizationService; | ||
| private final S3Service s3Service; | ||
| private final TrainingEventPublisher trainingEventPublisher; | ||
|
|
||
| public void connectImage( | ||
| DevicePrincipal principal, | ||
| UUID eventId, | ||
| ConnectEventImageRequest request | ||
| ) { | ||
| ObservationItem item = findEvent(eventId); | ||
| deviceAuthorizationService.validateCctv(principal, item.getCctvCode()); | ||
| validateEventProcessed(item); | ||
| validateObjectKey(item, eventId, request.eventImageKey()); | ||
|
|
||
| if (isSameCompletedImage(item, request)) { | ||
| publishImageUpdated(item); | ||
| return; | ||
| } | ||
| validateImageState(item); | ||
|
|
||
| if (!s3Service.objectExists(request.eventImageKey())) { | ||
| throw new ApiException(CongestionErrorCode.EVENT_IMAGE_OBJECT_NOT_FOUND); | ||
| } | ||
|
|
||
| if (!observationRepository.completeImageUpload( | ||
| eventId.toString(), request.eventImageKey(), request.uploadedAt() | ||
| )) { | ||
| ObservationItem latest = findEvent(eventId); | ||
| if (!isSameCompletedImage(latest, request)) { | ||
| throw new ApiException(CongestionErrorCode.EVENT_IMAGE_STATE_CONFLICT); | ||
| } | ||
| publishImageUpdated(latest); | ||
| return; | ||
| } | ||
|
|
||
| item.setEventImageKey(request.eventImageKey()); | ||
| item.setImageUploadedAt(request.uploadedAt()); | ||
| item.setImageUploadStatus(ImageUploadStatus.COMPLETED); | ||
| publishImageUpdated(item); | ||
| } | ||
|
|
||
| private ObservationItem findEvent(UUID eventId) { | ||
| return observationRepository.findByEventId(eventId.toString()) | ||
| .orElseThrow(() -> new ApiException(CongestionErrorCode.EVENT_NOT_FOUND)); | ||
| } | ||
|
|
||
| private void validateEventProcessed(ObservationItem item) { | ||
| if (item.getEventStatus() != EventProcessingStatus.PROCESSED) { | ||
| throw new ApiException(CongestionErrorCode.EVENT_NOT_PROCESSED); | ||
| } | ||
| } | ||
|
|
||
| private void validateImageState(ObservationItem item) { | ||
| if (item.getImageUploadStatus() != null | ||
| && item.getImageUploadStatus() != ImageUploadStatus.PENDING | ||
| && item.getImageUploadStatus() != ImageUploadStatus.FAILED) { | ||
| throw new ApiException(CongestionErrorCode.EVENT_IMAGE_STATE_CONFLICT); | ||
| } | ||
| } | ||
|
|
||
| private void validateObjectKey(ObservationItem item, UUID eventId, String objectKey) { | ||
| String[] segments = objectKey.split("/", -1); | ||
| if (segments.length != 5 | ||
| || !TRAINING_PREFIX.equals(segments[0]) | ||
| || !EVENTS_DIRECTORY.equals(segments[2]) | ||
| || !segments[4].endsWith(JPEG_SUFFIX)) { | ||
| throw new ApiException(CongestionErrorCode.EVENT_IMAGE_KEY_INVALID); | ||
| } | ||
|
|
||
| UUID sessionId = parseCanonicalUuid(segments[1]); | ||
| String imageEventId = segments[4].substring(0, segments[4].length() - JPEG_SUFFIX.length()); | ||
| UUID keyEventId = parseCanonicalUuid(imageEventId); | ||
| boolean sameIdentity = Objects.equals(item.getTrainingSessionId(), sessionId.toString()) | ||
| && Objects.equals(item.getCctvCode(), segments[3]) | ||
| && eventId.equals(keyEventId); | ||
| if (!sameIdentity) { | ||
| throw new ApiException(CongestionErrorCode.EVENT_IMAGE_IDENTITY_MISMATCH); | ||
| } | ||
| } | ||
|
|
||
| private UUID parseCanonicalUuid(String value) { | ||
| try { | ||
| UUID uuid = UUID.fromString(value); | ||
| if (!uuid.toString().equals(value)) { | ||
| throw new IllegalArgumentException("non-canonical UUID"); | ||
| } | ||
| return uuid; | ||
| } catch (IllegalArgumentException exception) { | ||
| throw new ApiException(CongestionErrorCode.EVENT_IMAGE_KEY_INVALID, exception); | ||
| } | ||
| } | ||
|
|
||
| private boolean isSameCompletedImage( | ||
| ObservationItem item, | ||
| ConnectEventImageRequest request | ||
| ) { | ||
| return isCompletedStatus(item.getImageUploadStatus()) | ||
| && Objects.equals(item.getEventImageKey(), request.eventImageKey()) | ||
| && Objects.equals(item.getImageUploadedAt(), request.uploadedAt()); | ||
| } | ||
|
|
||
| @SuppressWarnings("deprecation") | ||
| private boolean isCompletedStatus(ImageUploadStatus status) { | ||
| return status == ImageUploadStatus.COMPLETED || status == ImageUploadStatus.UPLOADED; | ||
| } | ||
|
|
||
| private void publishImageUpdated(ObservationItem item) { | ||
| trainingEventPublisher.publishCongestionImageUpdated( | ||
| UUID.fromString(item.getTrainingSessionId()), | ||
| item | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.