diff --git a/src/main/java/com/grimpan/emodiary/exception/ErrorCode.java b/src/main/java/com/grimpan/emodiary/exception/ErrorCode.java index 97d2b22..785d19d 100644 --- a/src/main/java/com/grimpan/emodiary/exception/ErrorCode.java +++ b/src/main/java/com/grimpan/emodiary/exception/ErrorCode.java @@ -15,6 +15,7 @@ public enum ErrorCode { // Server SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "내부 서버 문제입니다."), + FILE_IO_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "파일 입출력 에러입니다."), // Bad Request Error NOT_EXIST_PARAM(HttpStatus.BAD_REQUEST, "요청에 필요한 데이터가 없습니다."), diff --git a/src/main/java/com/grimpan/emodiary/unit/ImageUtil.java b/src/main/java/com/grimpan/emodiary/unit/ImageUtil.java new file mode 100644 index 0000000..2464c33 --- /dev/null +++ b/src/main/java/com/grimpan/emodiary/unit/ImageUtil.java @@ -0,0 +1,51 @@ +package com.grimpan.emodiary.unit; + +import com.grimpan.emodiary.exception.CommonException; +import com.grimpan.emodiary.exception.ErrorCode; +import jakarta.xml.bind.DatatypeConverter; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.IOException; + +@Component +@RequiredArgsConstructor +public class ImageUtil { + public void decoder(String base64, String imagePath) { + String data = base64.split(",")[0]; + + byte[] imageBytes = DatatypeConverter.parseBase64Binary(data); + + try { + BufferedImage bufImg = ImageIO.read(new ByteArrayInputStream(imageBytes)); + + ImageIO.write(bufImg, "jpg", new File(imagePath)); + } catch (IOException e) { + throw new CommonException(ErrorCode.FILE_IO_ERROR, null); + } + } + + public String encoder(String imagePath) { + String base64 = null; + + try { + BufferedImage bufImg = ImageIO.read(new File(imagePath)); + + java.io.ByteArrayOutputStream os = new java.io.ByteArrayOutputStream(); + + ImageIO.write(bufImg, "jpg", os); + + base64 = DatatypeConverter.printBase64Binary(os.toByteArray()); + } catch (IOException e) { + throw new CommonException(ErrorCode.FILE_IO_ERROR, null); + } + + return base64; + } +}