Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature/Inhabas#234] 첨부파일 분류 기능 구현 #236

Merged
merged 1 commit into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading