From 3b6ece24de438c9e0924ff0cc3966a218eee1c1d Mon Sep 17 00:00:00 2001 From: skytin1004 Date: Tue, 20 Feb 2024 08:48:19 +0900 Subject: [PATCH] =?UTF-8?q?[feature/Inhabas#234]=EC=B2=A8=EB=B6=80?= =?UTF-8?q?=ED=8C=8C=EC=9D=BC=20=EB=B6=84=EB=A5=98=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/global/util/ClassifiedFiles.java | 39 +++++++++++++++++++ .../api/global/util/ClassifyFiles.java | 28 +++++++++++++ .../com/inhabas/api/global/util/FileUtil.java | 12 ++++++ 3 files changed, 79 insertions(+) create mode 100644 resource-server/src/main/java/com/inhabas/api/global/util/ClassifiedFiles.java create mode 100644 resource-server/src/main/java/com/inhabas/api/global/util/ClassifyFiles.java diff --git a/resource-server/src/main/java/com/inhabas/api/global/util/ClassifiedFiles.java b/resource-server/src/main/java/com/inhabas/api/global/util/ClassifiedFiles.java new file mode 100644 index 00000000..0d0feaa2 --- /dev/null +++ b/resource-server/src/main/java/com/inhabas/api/global/util/ClassifiedFiles.java @@ -0,0 +1,39 @@ +package com.inhabas.api.global.util; + +import java.util.ArrayList; +import java.util.List; + +import com.inhabas.api.domain.file.dto.FileDownloadDto; + +// ClassifyFiles 에 의해 분류된 파일들이 ClassifiedFiles 형태로 반환됨. +public class ClassifiedFiles { + private FileDownloadDto thumbnail; + private List images = new ArrayList<>(); + private List otherFiles = new ArrayList<>(); + + public ClassifiedFiles() {} + + public FileDownloadDto getThumbnail() { + return thumbnail; + } + + public List getImages() { + return images; + } + + public List getOtherFiles() { + return otherFiles; + } + + public void setThumbnail(FileDownloadDto thumbnail) { + this.thumbnail = thumbnail; + } + + public void addImage(FileDownloadDto image) { + this.images.add(image); + } + + public void addOtherFile(FileDownloadDto file) { + this.otherFiles.add(file); + } +} diff --git a/resource-server/src/main/java/com/inhabas/api/global/util/ClassifyFiles.java b/resource-server/src/main/java/com/inhabas/api/global/util/ClassifyFiles.java new file mode 100644 index 00000000..f43491f7 --- /dev/null +++ b/resource-server/src/main/java/com/inhabas/api/global/util/ClassifyFiles.java @@ -0,0 +1,28 @@ +package com.inhabas.api.global.util; + +import java.util.List; + +import com.inhabas.api.domain.file.domain.BoardFile; +import com.inhabas.api.domain.file.dto.FileDownloadDto; + +// 첨부파일을 썸네일, 이미지, (이미지가 아닌) 기타 파일로 분류 +public class ClassifyFiles { + + public static ClassifiedFiles classifyFiles(List files) { + ClassifiedFiles result = new ClassifiedFiles(); + + for (BoardFile file : files) { + FileDownloadDto fileDto = new FileDownloadDto(file.getName(), file.getUrl()); + if (FileUtil.isImageFile(file.getName())) { + result.addImage(fileDto); + if (result.getThumbnail() == null) { + result.setThumbnail(fileDto); // 첫 번째 이미지 파일을 썸네일로 선택 + } + } else { + result.addOtherFile(fileDto); + } + } + + return result; + } +} diff --git a/resource-server/src/main/java/com/inhabas/api/global/util/FileUtil.java b/resource-server/src/main/java/com/inhabas/api/global/util/FileUtil.java index eff9ebbe..0f379b08 100644 --- a/resource-server/src/main/java/com/inhabas/api/global/util/FileUtil.java +++ b/resource-server/src/main/java/com/inhabas/api/global/util/FileUtil.java @@ -14,6 +14,8 @@ public class FileUtil { "exe", "bat", "cmd", "sh", "js", "jsp", "php", "asp", "html" }; + private static final String[] IMAGE_EXTENSIONS = {"jpg", "jpeg", "png", "gif", "bmp", "webp"}; + public static boolean isValidFileName(String fileName) { if (fileName == null || fileName.trim().isEmpty()) { @@ -49,6 +51,16 @@ public static boolean isValidFileName(String fileName) { return true; } + public static boolean isImageFile(String fileName) { + String extension = getExtension(fileName).toLowerCase(); + for (String imageExtension : IMAGE_EXTENSIONS) { + if (extension.equals(imageExtension)) { + return true; + } + } + return false; + } + public static String getExtension(String fileName) { int lastIndex = fileName.lastIndexOf("."); if (lastIndex > 0) {