-
Notifications
You must be signed in to change notification settings - Fork 0
링크 생성 시 OG 이미지 S3 업로드 및 중복 방지 구현 #165
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
ckdals4600
merged 2 commits into
main
from
feature/#163-og-image-s3-upload-when-link-add
Jan 4, 2026
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
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
9 changes: 9 additions & 0 deletions
9
src/main/java/com/sofa/linkiving/domain/link/abstraction/ImageUploader.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,9 @@ | ||
| package com.sofa.linkiving.domain.link.abstraction; | ||
|
|
||
| public interface ImageUploader { | ||
| /** | ||
| * 외부 이미지 URL을 입력받아 스토리지에 저장하고, 접근 가능한 URL을 반환한다. | ||
| * 실패 시 null 값을 반환한다 (Soft Fail). | ||
| */ | ||
| String uploadFromUrl(String originalUrl); | ||
| } | ||
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
16 changes: 16 additions & 0 deletions
16
src/main/java/com/sofa/linkiving/infra/s3/DefaultUrlConnectionFactory.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,16 @@ | ||
| package com.sofa.linkiving.infra.s3; | ||
|
|
||
| import java.io.IOException; | ||
| import java.net.URL; | ||
| import java.net.URLConnection; | ||
|
|
||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Component | ||
| public class DefaultUrlConnectionFactory implements UrlConnectionFactory { | ||
|
|
||
| @Override | ||
| public URLConnection createConnection(String url) throws IOException { | ||
| return new URL(url).openConnection(); | ||
| } | ||
| } |
92 changes: 92 additions & 0 deletions
92
src/main/java/com/sofa/linkiving/infra/s3/S3ImageUploader.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,92 @@ | ||
| package com.sofa.linkiving.infra.s3; | ||
|
|
||
| import java.io.InputStream; | ||
| import java.net.URLConnection; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.UUID; | ||
|
|
||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import com.sofa.linkiving.domain.link.abstraction.ImageUploader; | ||
|
|
||
| import io.awspring.cloud.s3.S3Template; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
|
|
||
| @Slf4j | ||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class S3ImageUploader implements ImageUploader { | ||
|
|
||
| private final S3Template s3Template; | ||
| private final UrlConnectionFactory urlConnectionFactory; | ||
|
|
||
| @Value("${spring.cloud.aws.s3.bucket}") | ||
| private String bucketName; | ||
|
|
||
| @Value("${spring.cloud.aws.region.static}") | ||
| private String region; | ||
|
|
||
| @Override | ||
| public String uploadFromUrl(String originalUrl) { | ||
| if (originalUrl == null || originalUrl.isBlank()) { | ||
| return null; | ||
| } | ||
|
|
||
| try { | ||
| String s3Key = generateUniqueKeyFromUrl(originalUrl); | ||
|
|
||
| String s3Url = buildS3Url(s3Key); | ||
|
|
||
| if (s3Template.objectExists(bucketName, s3Key)) { | ||
| log.info("Image already exists (Cache Hit): {} -> {}", originalUrl, s3Key); | ||
| return s3Url; | ||
| } | ||
|
|
||
| URLConnection connection = urlConnectionFactory.createConnection(originalUrl); | ||
| connection.setConnectTimeout(3000); | ||
| connection.setReadTimeout(3000); | ||
|
|
||
| String contentType = connection.getContentType(); | ||
| if (contentType == null || !contentType.startsWith("image/")) { | ||
| log.warn("Not Image: {}", originalUrl); | ||
| return null; | ||
| } | ||
|
|
||
| try (InputStream inputStream = connection.getInputStream()) { | ||
| s3Template.upload(bucketName, s3Key, inputStream, null); | ||
| log.info("Image uploaded: {}", s3Key); | ||
| return s3Url; | ||
| } | ||
|
|
||
| } catch (Exception e) { | ||
| log.warn("Upload failed, falling back to original URL: {}", e.getMessage()); | ||
| return null; | ||
| } | ||
Goder-0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| private String buildS3Url(String key) { | ||
| return String.format("https://%s.s3.%s.amazonaws.com/%s", bucketName, region, key); | ||
| } | ||
|
|
||
| private String generateUniqueKeyFromUrl(String url) { | ||
| try { | ||
| String extension = "jpg"; | ||
| int lastDotIndex = url.lastIndexOf('.'); | ||
| if (lastDotIndex > 0 && lastDotIndex < url.length() - 1) { | ||
| String ext = url.substring(lastDotIndex + 1); | ||
| if (ext.contains("?")) { | ||
| ext = ext.substring(0, ext.indexOf("?")); | ||
| } | ||
| if (ext.length() <= 4 && ext.matches("[a-zA-Z]+")) { | ||
| extension = ext; | ||
| } | ||
| } | ||
| UUID uuid = UUID.nameUUIDFromBytes(url.getBytes(StandardCharsets.UTF_8)); | ||
| return "links/" + uuid + "." + extension; | ||
| } catch (Exception e) { | ||
| return "links/" + UUID.randomUUID() + ".jpg"; | ||
| } | ||
| } | ||
| } | ||
8 changes: 8 additions & 0 deletions
8
src/main/java/com/sofa/linkiving/infra/s3/UrlConnectionFactory.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,8 @@ | ||
| package com.sofa.linkiving.infra.s3; | ||
|
|
||
| import java.io.IOException; | ||
| import java.net.URLConnection; | ||
|
|
||
| public interface UrlConnectionFactory { | ||
| URLConnection createConnection(String url) throws IOException; | ||
| } |
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.
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.