Skip to content

Commit

Permalink
로그 수정 및 이미지 링크 입력 시 저장되는 로직 추가 Merge
Browse files Browse the repository at this point in the history
로그 수정 및 이미지 링크 입력 시 저장되는 로직 추가
  • Loading branch information
Train0303 authored Nov 9, 2023
2 parents 2ed651e + c4f973b commit f91d2da
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.kakao.linknamu.core.config;

import com.kakao.linknamu.core.filter.LoggingFilter;
import com.kakao.linknamu.core.log.LoggingFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package com.kakao.linknamu.core.filter;
package com.kakao.linknamu.core.log;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.springframework.web.util.ContentCachingRequestWrapper;
import org.springframework.web.util.WebUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Base64;

@Slf4j
@WebFilter(urlPatterns = "/api/*")
Expand Down Expand Up @@ -37,12 +41,29 @@ private String getRequestBody(ContentCachingRequestWrapper request) {
byte[] buf = wrapper.getContentAsByteArray();
if (buf.length > 0) {
try {
return new String(buf, 0, buf.length, wrapper.getCharacterEncoding());
return exceptImageUrl(new String(buf, 0, buf.length, wrapper.getCharacterEncoding()));
} catch (UnsupportedEncodingException e) {
return " - ";
}
}
}
return " - ";
}

// imageUrl의 형식이 base64인 경우 로그에서 제외한다.
private String exceptImageUrl(String strJson) {
try {
JSONParser jsonParser = new JSONParser();
JSONObject obj = (JSONObject) jsonParser.parse(strJson);
if (obj.get("imageUrl") != null) {
try{
Base64.getDecoder().decode(obj.get("imageUrl").toString());
obj.remove("imageUrl");
} catch (IllegalArgumentException ignored) {}
}
return obj.toString();
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.UUID;

// S3에 이미지 파일을 저장할 수 있도록 하는 객체
Expand All @@ -34,7 +35,10 @@ public String base64ImageToS3(String base64Data, String bookmarkLink) {
if (base64Data == null) {
return jsoupUtils.getImgUrl(bookmarkLink);
}

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


InputStream imageInputStream = getValidImageInputStream(byteImage);

// AWS S3 저장 로직
Expand All @@ -47,17 +51,22 @@ public String base64ImageToS3(String base64Data, String bookmarkLink) {
amazonS3Client.putObject(new PutObjectRequest(bucket, fileName, imageInputStream, metadata));
return amazonS3Client.getUrl(bucket, fileName).toString();
} catch (IllegalArgumentException e) {
//base64로 인코딩된 이미지파일이 아닐경우, 이미지Url인지 확인
if (getValidImageUrl(base64Data)) {
return base64Data;
}
throw new Exception400(UtilExceptionStatus.NOT_BASE64_DATA);
}

}

private ByteArrayInputStream getValidImageInputStream(byte[] byteImage) {


ByteArrayInputStream imageInputStream = new ByteArrayInputStream(byteImage);

try {

BufferedImage image = ImageIO.read(imageInputStream);

// image인지 체크하는 로직
Expand All @@ -71,4 +80,24 @@ private ByteArrayInputStream getValidImageInputStream(byte[] byteImage) {
}
return imageInputStream;
}


private Boolean getValidImageUrl(String imgUrlString) {


try {

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

// image인지 체크하는 로직
if (image == null) {
throw new Exception400(UtilExceptionStatus.IMAGE_URL_INVALID);
}
} catch (IOException exception) {
throw new Exception400(UtilExceptionStatus.IMAGE_UNREADABLE_URL);
}

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ public enum UtilExceptionStatus implements BaseExceptionStatus {
NOT_BASE64_DATA("Base64 형식이 아닙니다.", 400, "04004"),
IMAGE_INVALID_DATA("유효하지 않은 이미지 데이터입니다.", 400, "04005"),

IMAGE_UNREADABLE_DATA("이미지가 손상되었거나 읽을 수 없는 형식입니다.", 400, "04006");
IMAGE_UNREADABLE_DATA("이미지가 손상되었거나 읽을 수 없는 형식입니다.", 400, "04006"),

IMAGE_URL_INVALID("유요하지 않는 이미지 링크입니다.", 400, "04007"),

IMAGE_UNREADABLE_URL("URL 형식이 아닙니다.", 400, "04008");

@Getter
private final String message;
Expand Down

0 comments on commit f91d2da

Please sign in to comment.