-
Notifications
You must be signed in to change notification settings - Fork 0
OG 태그 메타 정보 제공 API 구현 #118
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
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
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
13 changes: 13 additions & 0 deletions
13
src/main/java/com/sofa/linkiving/domain/link/dto/OgTagDto.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,13 @@ | ||
| package com.sofa.linkiving.domain.link.dto; | ||
|
|
||
| import lombok.Builder; | ||
|
|
||
| @Builder | ||
| public record OgTagDto( | ||
| String title, | ||
| String description, | ||
| String image, | ||
| String url | ||
| ) { | ||
| public static final OgTagDto EMPTY = new OgTagDto("", "", "", ""); | ||
| } |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/sofa/linkiving/domain/link/dto/request/MetaScrapeReq.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.sofa.linkiving.domain.link.dto.request; | ||
|
|
||
| import io.swagger.v3.oas.annotations.media.Schema; | ||
| import jakarta.validation.constraints.NotBlank; | ||
|
|
||
| public record MetaScrapeReq( | ||
| @Schema(description = "메타 정보를 수집할 URL", example = "https://example.com", requiredMode = Schema.RequiredMode.REQUIRED) | ||
| @NotBlank(message = "URL은 필수입니다") | ||
| String url | ||
| ) { | ||
| } |
28 changes: 28 additions & 0 deletions
28
src/main/java/com/sofa/linkiving/domain/link/dto/response/MetaScrapeRes.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,28 @@ | ||
| package com.sofa.linkiving.domain.link.dto.response; | ||
|
|
||
| import com.sofa.linkiving.domain.link.dto.OgTagDto; | ||
|
|
||
| import io.swagger.v3.oas.annotations.media.Schema; | ||
|
|
||
| public record MetaScrapeRes( | ||
| @Schema(description = "페이지 제목", example = "Example Domain") | ||
| String title, | ||
|
|
||
| @Schema(description = "페이지 설명", example = "This domain is for use in illustrative examples...") | ||
| String description, | ||
|
|
||
| @Schema(description = "이미지 URL", example = "https://example.com/image.jpg") | ||
| String image, | ||
|
|
||
| @Schema(description = "페이지 URL", example = "https://example.com") | ||
| String url | ||
| ) { | ||
| public static MetaScrapeRes from(OgTagDto ogTag) { | ||
| return new MetaScrapeRes( | ||
| ogTag.title(), | ||
| ogTag.description(), | ||
| ogTag.image(), | ||
| ogTag.url() | ||
| ); | ||
| } | ||
| } |
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
51 changes: 51 additions & 0 deletions
51
src/main/java/com/sofa/linkiving/domain/link/util/OgTagCrawler.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,51 @@ | ||
| package com.sofa.linkiving.domain.link.util; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| import org.jsoup.Jsoup; | ||
| import org.jsoup.nodes.Document; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import com.sofa.linkiving.domain.link.dto.OgTagDto; | ||
|
|
||
| import lombok.extern.slf4j.Slf4j; | ||
|
|
||
| @Slf4j | ||
| @Component | ||
| public class OgTagCrawler { | ||
|
|
||
| private static final int TIMEOUT_MS = 5000; | ||
| private final UrlValidator urlValidator; | ||
|
|
||
| public OgTagCrawler(UrlValidator urlValidator) { | ||
| this.urlValidator = urlValidator; | ||
| } | ||
|
|
||
| public OgTagDto crawl(String url) { | ||
| // SSRF 방어: URL 안전성 검증 | ||
| urlValidator.validateSafeUrl(url); | ||
|
|
||
| try { | ||
| Document document = Jsoup.connect(url) | ||
| .timeout(TIMEOUT_MS) | ||
| .userAgent("Mozilla/5.0") | ||
| .get(); | ||
|
|
||
| return OgTagDto.builder() | ||
| .title(getMetaTag(document, "og:title")) | ||
| .description(getMetaTag(document, "og:description")) | ||
| .image(getMetaTag(document, "og:image")) | ||
| .url(getMetaTag(document, "og:url")) | ||
| .build(); | ||
|
|
||
| } catch (IOException e) { | ||
| log.warn("OG 태그 크롤링 실패: {}", url, e); | ||
| return OgTagDto.EMPTY; | ||
| } | ||
| } | ||
|
|
||
| private String getMetaTag(Document document, String property) { | ||
| return document.select("meta[property=" + property + "]") | ||
| .attr("content"); | ||
| } | ||
| } |
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.