Skip to content

Commit

Permalink
Fix: 로컬 경로 이미지 버그 수정 Merge
Browse files Browse the repository at this point in the history
Fix: 로컬 경로 이미지 버그 수정
  • Loading branch information
Train0303 authored Nov 11, 2023
2 parents 278d4ad + b309898 commit bd90242
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.kakao.linknamu.thirdparty.utils;
package com.kakao.linknamu.core.util;

import lombok.Getter;
import lombok.Setter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.kakao.linknamu.thirdparty.utils;
package com.kakao.linknamu.core.util;

import lombok.RequiredArgsConstructor;
import org.jsoup.Connection;
Expand All @@ -8,7 +8,10 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.Objects;


Expand Down Expand Up @@ -56,9 +59,9 @@ public String getImgUrl(String href) {
Elements imgTwitterTags = doc.select("meta[name=twitter:image]");

if (!imgOgTags.isEmpty()) {
return imgOgTags.first().attr("content");
return getValidImageUrl(imgOgTags.first().attr("content"));
} else if (!imgTwitterTags.isEmpty()) {
return imgTwitterTags.first().attr("content");
return getValidImageUrl(imgTwitterTags.first().attr("content"));
}

// 이미지를 찾지 못한 경우 기본값
Expand Down Expand Up @@ -91,9 +94,9 @@ public JsoupResult getTitleAndImgUrl(String href) {
}

if (!imgOgTags.isEmpty()) {
imageUrl = imgOgTags.first().attr("content");
imageUrl = getValidImageUrl(imgOgTags.first().attr("content"));
} else if (!imgTwitterTags.isEmpty()) {
imageUrl = imgTwitterTags.first().attr("content");
imageUrl = getValidImageUrl(imgTwitterTags.first().attr("content"));
}

// 이미지 혹은 제목을 찾지 못한 경우 기본값
Expand All @@ -115,4 +118,20 @@ private Document getDocument(String href) throws IOException{

return connection.get();
}

private String getValidImageUrl(String imgUrlString) {
try {
URL imgUrl = new URL(imgUrlString);
BufferedImage image = ImageIO.read(imgUrl);

// image인지 체크하는 로직
if (image == null) {
return DEFAULT_IMAGE;
}
} catch (IOException exception) {
return DEFAULT_IMAGE;
}

return imgUrlString;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.kakao.linknamu.core.exception.Exception400;
import com.kakao.linknamu.thirdparty.utils.JsoupUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
Expand All @@ -28,7 +27,6 @@ public class S3ImageClient {
private final JsoupUtils jsoupUtils;

public String base64ImageToS3(String base64Data) {

try {
byte[] byteImage = java.util.Base64.getDecoder().decode(base64Data);

Expand All @@ -45,7 +43,7 @@ public String base64ImageToS3(String base64Data) {
return amazonS3Client.getUrl(bucket, fileName).toString();
} catch (IllegalArgumentException e) {
//base64로 인코딩된 이미지파일이 아닐경우, 이미지Url인지 확인
if (getValidImageUrl(base64Data)) {
if (isValidImageUrl(base64Data)) {
return base64Data;
}
throw new Exception400(UtilExceptionStatus.NOT_BASE64_DATA);
Expand All @@ -61,12 +59,9 @@ public String base64ImageToS3(String base64Data, String bookmarkLink) {
}

private ByteArrayInputStream getValidImageInputStream(byte[] byteImage) {


ByteArrayInputStream imageInputStream = new ByteArrayInputStream(byteImage);

try {

BufferedImage image = ImageIO.read(imageInputStream);

// image인지 체크하는 로직
Expand All @@ -82,11 +77,8 @@ private ByteArrayInputStream getValidImageInputStream(byte[] byteImage) {
}


private Boolean getValidImageUrl(String imgUrlString) {


private Boolean isValidImageUrl(String imgUrlString) {
try {

URL imgUrl = new URL(imgUrlString);
BufferedImage image = ImageIO.read(imgUrl);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import com.kakao.linknamu.core.exception.Exception500;
import com.kakao.linknamu.kakao.KakaoExceptionStatus;
import com.kakao.linknamu.kakao.dto.KakaoSendMeResponseDto;
import com.kakao.linknamu.thirdparty.utils.JsoupResult;
import com.kakao.linknamu.thirdparty.utils.JsoupUtils;
import com.kakao.linknamu.core.util.JsoupResult;
import com.kakao.linknamu.core.util.JsoupUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
import com.kakao.linknamu.thirdparty.googledocs.entity.GooglePage;
import com.kakao.linknamu.thirdparty.googledocs.repository.GooglePageJpaRepository;
import com.kakao.linknamu.thirdparty.googledocs.util.InvalidGoogleDocsApiException;
import com.kakao.linknamu.thirdparty.utils.JsoupResult;
import com.kakao.linknamu.thirdparty.utils.JsoupUtils;
import com.kakao.linknamu.core.util.JsoupResult;
import com.kakao.linknamu.core.util.JsoupUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import com.kakao.linknamu.thirdparty.notion.repository.NotionPageJpaRepository;
import com.kakao.linknamu.thirdparty.notion.util.InvalidNotionApiException;
import com.kakao.linknamu.thirdparty.notion.util.NotionApiUriBuilder;
import com.kakao.linknamu.thirdparty.utils.JsoupResult;
import com.kakao.linknamu.thirdparty.utils.JsoupUtils;
import com.kakao.linknamu.core.util.JsoupResult;
import com.kakao.linknamu.core.util.JsoupUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.json.simple.JSONArray;
Expand Down

0 comments on commit bd90242

Please sign in to comment.