-
Notifications
You must be signed in to change notification settings - Fork 0
[FEATURE] 워터마크 처리 추상화 및 프로파일 스위칭 도입 #127
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
2 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
70 changes: 70 additions & 0 deletions
70
src/main/java/ditda/backend/domain/commission/draft/processor/LocalWatermarkProcessor.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,70 @@ | ||
| package ditda.backend.domain.commission.draft.processor; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
|
|
||
| import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
| import org.springframework.scheduling.annotation.Async; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import ditda.backend.domain.commission.draft.service.DraftWatermarkTransitionService; | ||
| import ditda.backend.global.image.WatermarkImageProcessor; | ||
| import ditda.backend.global.image.dto.WatermarkedImage; | ||
| import ditda.backend.global.image.exception.ImageProcessingException; | ||
| import ditda.backend.global.s3.enums.BucketType; | ||
| import ditda.backend.global.s3.enums.S3ContentType; | ||
| import ditda.backend.global.s3.manager.S3FileManager; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
|
|
||
| @Slf4j | ||
| @Component | ||
| @RequiredArgsConstructor | ||
| @ConditionalOnProperty(name = "watermark.mode", havingValue = "local") | ||
| public class LocalWatermarkProcessor implements WatermarkProcessor { | ||
|
|
||
| private static final BucketType BUCKET = BucketType.PRIVATE; | ||
|
|
||
| private final WatermarkImageProcessor watermarkImageProcessor; | ||
| private final S3FileManager s3FileManager; | ||
| private final WatermarkKeyResolver watermarkKeyResolver; | ||
| private final DraftWatermarkTransitionService draftWatermarkTransitionService; | ||
|
|
||
| // 로컬 구현체의 경우에는 리소스 제한을 위해 단일 스레드로 진행 | ||
| @Async("watermarkExecutor") | ||
| @Override | ||
| public void process(Long draftFileId, String originalKey) { | ||
|
|
||
| long start = System.nanoTime(); | ||
| try { | ||
| String watermarkedKey = createWatermarked(originalKey); | ||
| draftWatermarkTransitionService.complete(draftFileId, watermarkedKey); | ||
| log.info("워터마크 완료. draftFileId={}, elapsedMs={}", draftFileId, elapsedMs(start)); | ||
| } catch (ImageProcessingException exception) { | ||
| log.error("워터마크 영구 실패(이미지 문제). draftFileId={}", draftFileId, exception); | ||
| draftWatermarkTransitionService.failPermanently(draftFileId); | ||
| } catch (Exception e) { | ||
| log.error("워터마크 실패. draftFileId={}, elapsedMs={}", draftFileId, elapsedMs(start), e); | ||
| draftWatermarkTransitionService.fail(draftFileId); | ||
| } | ||
| } | ||
|
|
||
| private String createWatermarked(String originalKey) throws IOException { | ||
|
|
||
| byte[] watermarked; | ||
| // 원본 s3 다운로드 후 워터마크 진행 | ||
| try (InputStream original = s3FileManager.download(BUCKET, originalKey)) { | ||
| WatermarkedImage image = watermarkImageProcessor.createWatermarkedPreview(original); | ||
| watermarked = image.bytes(); | ||
| } | ||
|
|
||
| String watermarkedKey = watermarkKeyResolver.resolve(originalKey); | ||
| s3FileManager.upload(BUCKET, watermarkedKey, watermarked, S3ContentType.PNG.getContentType()); | ||
|
|
||
| return watermarkedKey; | ||
| } | ||
|
|
||
| private long elapsedMs(long start) { | ||
| return (System.nanoTime() - start) / 1_000_000; | ||
| } | ||
| } | ||
24 changes: 24 additions & 0 deletions
24
src/main/java/ditda/backend/domain/commission/draft/processor/WatermarkKeyResolver.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,24 @@ | ||
| package ditda.backend.domain.commission.draft.processor; | ||
|
|
||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Component | ||
| public class WatermarkKeyResolver { | ||
|
|
||
| private static final String WATERMARK_DIR = "wm"; | ||
|
|
||
| public String resolve(String originalKey) { | ||
|
|
||
| // 워터마크 출력 키 생성 | ||
| int lastSlash = originalKey.lastIndexOf('/'); | ||
| if (lastSlash < 0) { | ||
| throw new IllegalArgumentException("유효하지 않은 S3 키: " + originalKey); | ||
| } | ||
| String dir = originalKey.substring(0, lastSlash); | ||
| String filename = originalKey.substring(lastSlash + 1); | ||
| int lastDot = filename.lastIndexOf('.'); | ||
| String baseName = lastDot > 0 ? filename.substring(0, lastDot) : filename; | ||
|
|
||
| return dir + "/" + WATERMARK_DIR + "/" + baseName + ".png"; | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
src/main/java/ditda/backend/domain/commission/draft/processor/WatermarkProcessor.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,12 @@ | ||
| package ditda.backend.domain.commission.draft.processor; | ||
|
|
||
| /** | ||
| * 워터마크 처리 주체 추상화. | ||
| * 구현체는 처리 완료/실패 시 상태 전이까지 책임진다. | ||
| * - local: 서버 내 처리 (LocalWatermarkProcessor) | ||
| * - lambda: AWS Lambda 위임 (LambdaWatermarkProcessor) | ||
| */ | ||
| public interface WatermarkProcessor { | ||
|
|
||
| void process(Long draftFileId, String originalKey); | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,3 +26,6 @@ spring: | |
| swagger: | ||
| server-url: http://localhost:8080 | ||
| server-description: Local Server | ||
|
|
||
| watermark: | ||
| mode: local | ||
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.
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.