blob 공통으로 분리

This commit is contained in:
dyhj625 2025-02-25 10:50:29 +09:00
parent 41372391de
commit 231b763f7c
2 changed files with 45 additions and 20 deletions

View File

@ -6,6 +6,7 @@ import java.nio.charset.StandardCharsets;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
import io.company.localhost.utils.BlobUtil;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -196,24 +197,6 @@ public class localbordService {
return boardMapper.selectCountCommentReactions(boardId); return boardMapper.selectCountCommentReactions(boardId);
} }
private String procBlobToString(Object blob) {
try {
if (blob instanceof String) {
return (String) blob; // 이미 문자열이면 그대로 반환
} else if (blob instanceof java.sql.Blob) {
java.sql.Blob sqlBlob = (java.sql.Blob) blob;
long blobLength = sqlBlob.length();
byte[] blobBytes = sqlBlob.getBytes(1, (int) blobLength);
return new String(blobBytes, StandardCharsets.UTF_8); // SQL BLOB 바이트 배열 문자열 변환
} else {
throw new UnsupportedOperationException("Unsupported blob type: " + blob.getClass()); // 지원되지 않는 타입이면 예외 발생
}
} catch (Exception e) {
throw new RuntimeException("Failed to convert Blob to String: " + e.getMessage(), e); // 변환 실패 예외 처리
}
}
private String procFirstImageUrl(String jsonContent) { private String procFirstImageUrl(String jsonContent) {
try { try {
// JSON 유효성 검사 // JSON 유효성 검사
@ -292,7 +275,7 @@ public class localbordService {
// Blob 데이터를 문자열로 변환 // Blob 데이터를 문자열로 변환
Object content = boardDetail.get("content"); Object content = boardDetail.get("content");
if (content != null) { if (content != null) {
String contentString = procBlobToString(content); // Blob을 문자열로 변환 String contentString = BlobUtil.procBlobToString(content); // Blob을 문자열로 변환
boardDetail.put("content", contentString); // JSON 변환 가능 boardDetail.put("content", contentString); // JSON 변환 가능
} }
} }
@ -309,7 +292,7 @@ public class localbordService {
post.put("dislikeCount", reactions.getOrDefault("dislikeCount", 0)); post.put("dislikeCount", reactions.getOrDefault("dislikeCount", 0));
Object content = post.get("content"); Object content = post.get("content");
String contentString = procBlobToString(content); String contentString = BlobUtil.procBlobToString(content);
post.put("content", contentString); post.put("content", contentString);
String firstImageUrl = procFirstImageUrl(contentString); String firstImageUrl = procFirstImageUrl(contentString);

View File

@ -0,0 +1,42 @@
/************************************************************
*
* @packageName : io.company.localhost.utils
* @fileName : BlobUtil.java
* @author : 서지희
* @date : 25.02.25
* @description :
*
* ===========================================================
* DATE AUTHOR NOTE
* -----------------------------------------------------------
* 25.02.25 서지희 최초 생성
*
*************************************************************/
package io.company.localhost.utils;
import java.nio.charset.StandardCharsets;
import java.sql.Blob;
public class BlobUtil {
private BlobUtil() {
// 객체 생성 방지 (유틸리티 클래스는 인스턴스를 만들 필요가 없음)
}
public static String procBlobToString(Object blob) {
try {
if (blob instanceof String) {
return (String) blob; // 이미 문자열이면 그대로 반환
} else if (blob instanceof Blob) {
Blob sqlBlob = (Blob) blob;
long blobLength = sqlBlob.length();
byte[] blobBytes = sqlBlob.getBytes(1, (int) blobLength);
return new String(blobBytes, StandardCharsets.UTF_8); // SQL BLOB 바이트 배열 문자열 변환
} else {
throw new UnsupportedOperationException("Unsupported blob type: " + blob.getClass()); // 지원되지 않는 타입이면 예외 발생
}
} catch (Exception e) {
throw new RuntimeException("Failed to convert Blob to String: " + e.getMessage(), e); // 변환 실패 예외 처리
}
}
}