Skip to content

Commit

Permalink
[feature/Inhabas#234] 첨부파일 분류 기능 구현
Browse files Browse the repository at this point in the history
[feature/Inhabas#234] 첨부파일 분류 기능 구현
  • Loading branch information
skytin1004 authored Feb 19, 2024
2 parents 28f9e47 + 3b6ece2 commit 1ab3915
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<FileDownloadDto> images = new ArrayList<>();
private List<FileDownloadDto> otherFiles = new ArrayList<>();

public ClassifiedFiles() {}

public FileDownloadDto getThumbnail() {
return thumbnail;
}

public List<FileDownloadDto> getImages() {
return images;
}

public List<FileDownloadDto> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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<BoardFile> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 1ab3915

Please sign in to comment.