From ca96f45f845b68dfb754efa14051f2bf4be7b237 Mon Sep 17 00:00:00 2001 From: suminnnnn Date: Mon, 14 Oct 2024 10:38:58 +0900 Subject: [PATCH] =?UTF-8?q?Refactor:=20=EB=B6=88=ED=95=84=EC=9A=94?= =?UTF-8?q?=ED=95=9C=20=EB=A9=94=EC=84=9C=EB=93=9C=20=EB=B0=8F=20=EC=98=88?= =?UTF-8?q?=EC=99=B8=20=EC=B2=98=EB=A6=AC=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/link/business/LinkService.java | 47 +++++-------------- 1 file changed, 12 insertions(+), 35 deletions(-) diff --git a/src/main/java/cmc/blink/domain/link/business/LinkService.java b/src/main/java/cmc/blink/domain/link/business/LinkService.java index c662a0a..7dc3a64 100644 --- a/src/main/java/cmc/blink/domain/link/business/LinkService.java +++ b/src/main/java/cmc/blink/domain/link/business/LinkService.java @@ -431,41 +431,18 @@ private String getOpenGraphContent(OpenGraph openGraph, String property) { } private LinkResponse.LinkInfo fetchLinkInfoWithJsoup(String url) throws IOException { - try { - Document doc = Jsoup.connect(url) - .userAgent(getRandomUserAgent()) - .followRedirects(false) - .get(); - - String title = doc.title(); - String type = doc.select("meta[name=type]").attr("content"); - String contents = doc.select("meta[name=description]").attr("content"); - String imageUrl = doc.select("meta[property=og:image]").attr("content"); - - return LinkMapper.toLinkInfo(title, type, contents, imageUrl); - } catch (UnsupportedMimeTypeException e) { - return fetchLinkInfoWithBinary(url); - } - - } - - private LinkResponse.LinkInfo fetchLinkInfoWithBinary(String url) throws IOException { - try { - Connection.Response response = Jsoup.connect(url) - .ignoreContentType(true) - .execute(); - - String htmlContent = new String(response.bodyAsBytes(), StandardCharsets.UTF_8); - Document doc = Jsoup.parse(htmlContent); - - String title = doc.title(); - String contents = doc.select("meta[name=description]").attr("content"); - String imageUrl = doc.select("meta[property=og:image]").attr("content"); - - return LinkMapper.toLinkInfo(title, "", contents, imageUrl); - } catch (IOException e) { - throw new LinkException(ErrorCode.LINK_SCRAPED_FAILED); - } + Document doc = Jsoup.connect(url) + .userAgent(getRandomUserAgent()) + .ignoreContentType(true) + .followRedirects(false) + .get(); + + String title = doc.title(); + String type = doc.select("meta[name=type]").attr("content"); + String contents = doc.select("meta[name=description]").attr("content"); + String imageUrl = doc.select("meta[property=og:image]").attr("content"); + + return LinkMapper.toLinkInfo(title, type, contents, imageUrl); } @Transactional