파일검색 수정
This commit is contained in:
@@ -291,11 +291,13 @@ public class FIleChecker {
|
||||
int maxDepth,
|
||||
String sortType,
|
||||
int startPos,
|
||||
int limit) {
|
||||
int endPos) {
|
||||
|
||||
Path startPath = Paths.get(dir);
|
||||
String dirPath = dir;
|
||||
|
||||
int limit = endPos - startPos + 1;
|
||||
|
||||
Set<String> targetExtensions = createExtensionSet(extension);
|
||||
|
||||
List<Basic> fileList = new ArrayList<>();
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.kamco.cd.kamcoback.mapsheet.dto;
|
||||
|
||||
import com.kamco.cd.kamcoback.common.utils.FIleChecker;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import java.util.List;
|
||||
@@ -142,9 +143,9 @@ public class FileDto {
|
||||
private final String dirPath;
|
||||
private final int fileTotCnt;
|
||||
private final long fileTotSize;
|
||||
private final List<Basic> files;
|
||||
private final List<FIleChecker.Basic> files;
|
||||
|
||||
public FilesDto(String dirPath, int fileTotCnt, long fileTotSize, List<Basic> files) {
|
||||
public FilesDto(String dirPath, int fileTotCnt, long fileTotSize, List<FIleChecker.Basic> files) {
|
||||
|
||||
this.dirPath = dirPath;
|
||||
this.fileTotCnt = fileTotCnt;
|
||||
|
||||
@@ -141,123 +141,46 @@ public class MapSheetMngFileCheckerService {
|
||||
public FilesDto getFilesAll(SrchFilesDto srchDto) {
|
||||
|
||||
String dirPath = srchDto.getDirPath();
|
||||
String extension = srchDto.getExtension();
|
||||
String sortType = srchDto.getSortType();
|
||||
int startPos = srchDto.getStartPos();
|
||||
int endPos = srchDto.getEndPos();
|
||||
File dir = new File(dirPath);
|
||||
File[] fileList = dir.listFiles();
|
||||
|
||||
List<FileDto.Basic> files = new ArrayList<>();
|
||||
SimpleDateFormat dttmFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
List<FIleChecker.Basic> files = FIleChecker.getFilesFromAllDepth(
|
||||
srchDto.getDirPath(),
|
||||
"*",
|
||||
srchDto.getExtension(),
|
||||
1,
|
||||
srchDto.getSortType(),
|
||||
startPos,
|
||||
endPos);
|
||||
|
||||
int fileListPos = 0;
|
||||
int fileTotCnt = 0;
|
||||
long fileTotSize = 0;
|
||||
|
||||
if (fileList != null) {
|
||||
if (sortType.equals("name")) {
|
||||
Arrays.sort(fileList);
|
||||
} else if (sortType.equals("date")) {
|
||||
Arrays.sort(fileList, Comparator.comparingLong(File::lastModified));
|
||||
}
|
||||
|
||||
for (File file : fileList) {
|
||||
if (file.isFile()) { // 파일인 경우만
|
||||
if (extension.equals("*") || file.getName().endsWith("." + extension)) {
|
||||
|
||||
fileListPos = fileListPos + 1;
|
||||
|
||||
if (startPos <= fileListPos && endPos >= fileListPos) {
|
||||
|
||||
// 생성자를 통해 객체를 만들고 리스트에 추가
|
||||
String fileName = file.getName();
|
||||
String parentPath = file.getParent();
|
||||
String fullPath = file.getAbsolutePath();
|
||||
String ext = FilenameUtils.getExtension(fileName);
|
||||
|
||||
Path path = Paths.get(parentPath);
|
||||
String parentFolderNm = path.getFileName().toString();
|
||||
|
||||
long fileSize = file.length();
|
||||
String lastModified = dttmFormat.format(new Date(file.lastModified()));
|
||||
|
||||
files.add(
|
||||
new FileDto.Basic(
|
||||
fileName, parentFolderNm, parentPath, fullPath, ext, fileSize, lastModified));
|
||||
|
||||
fileTotCnt = fileTotCnt + 1;
|
||||
fileTotSize = fileTotSize + fileSize;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
int fileTotCnt = files.size();
|
||||
long fileTotSize = FIleChecker.getFileTotSize(files);
|
||||
|
||||
return new FilesDto(dirPath, fileTotCnt, fileTotSize, files);
|
||||
}
|
||||
|
||||
public FilesDto getFilesDepthAll(SrchFilesDepthDto srchDto) {
|
||||
|
||||
Path startPath = Paths.get(srchDto.getDirPath());
|
||||
int maxDepth = srchDto.getMaxDepth();
|
||||
String dirPath = srchDto.getDirPath();
|
||||
String extension = srchDto.getExtension();
|
||||
String sortType = srchDto.getSortType();
|
||||
|
||||
int startPos = srchDto.getStartPos();
|
||||
int endPos = srchDto.getEndPos();
|
||||
int limit = endPos - startPos + 1;
|
||||
|
||||
Set<String> targetExtensions = createExtensionSet(extension);
|
||||
List<FIleChecker.Basic> files = FIleChecker.getFilesFromAllDepth(
|
||||
srchDto.getDirPath(),
|
||||
"*",
|
||||
srchDto.getExtension(),
|
||||
srchDto.getMaxDepth(),
|
||||
srchDto.getSortType(),
|
||||
startPos,
|
||||
endPos);
|
||||
|
||||
List<FileDto.Basic> fileDtoList = new ArrayList<>();
|
||||
SimpleDateFormat dttmFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
int fileListPos = 0;
|
||||
int fileTotCnt = files.size();
|
||||
long fileTotSize = FIleChecker.getFileTotSize(files);
|
||||
|
||||
int fileTotCnt = 0;
|
||||
long fileTotSize = 0;
|
||||
|
||||
try (Stream<Path> stream = Files.walk(startPath, maxDepth)) {
|
||||
|
||||
fileDtoList =
|
||||
stream
|
||||
.filter(Files::isRegularFile)
|
||||
.filter(
|
||||
p ->
|
||||
extension == null
|
||||
|| extension.equals("")
|
||||
|| extension.equals("*")
|
||||
|| targetExtensions.contains(extractExtension(p)))
|
||||
.sorted(getFileComparator(sortType))
|
||||
.skip(startPos)
|
||||
.limit(limit)
|
||||
.map(
|
||||
path -> {
|
||||
int depth = path.getNameCount();
|
||||
|
||||
String fileNm = path.getFileName().toString();
|
||||
String ext = FilenameUtils.getExtension(fileNm);
|
||||
String parentFolderNm = path.getParent().getFileName().toString();
|
||||
String parentPath = path.getParent().toString();
|
||||
String fullPath = path.toAbsolutePath().toString();
|
||||
|
||||
File file = new File(fullPath);
|
||||
long fileSize = file.length();
|
||||
String lastModified = dttmFormat.format(new Date(file.lastModified()));
|
||||
|
||||
return new FileDto.Basic(
|
||||
fileNm, parentFolderNm, parentPath, fullPath, ext, fileSize, lastModified);
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
|
||||
fileTotCnt = fileDtoList.size();
|
||||
fileTotSize = fileDtoList.stream().mapToLong(FileDto.Basic::getFileSize).sum();
|
||||
|
||||
} catch (IOException e) {
|
||||
System.err.println("파일 I/O 오류 발생: " + e.getMessage());
|
||||
}
|
||||
|
||||
return new FilesDto(dirPath, fileTotCnt, fileTotSize, fileDtoList);
|
||||
return new FilesDto(dirPath, fileTotCnt, fileTotSize, files);
|
||||
}
|
||||
|
||||
public Set<String> createExtensionSet(String extensionString) {
|
||||
|
||||
@@ -48,192 +48,6 @@ public class MapSheetMngService {
|
||||
private final MapSheetMngCoreService mapSheetMngCoreService;
|
||||
private final FileConfig fileConfig;
|
||||
|
||||
public FilesDto getFilesAll(SrchFilesDto srchDto) {
|
||||
|
||||
String dirPath = srchDto.getDirPath();
|
||||
int startPos = srchDto.getStartPos();
|
||||
int limit = startPos - srchDto.getEndPos() + 1;
|
||||
|
||||
List<FIleChecker.Basic> files = FIleChecker.getFilesFromAllDepth(
|
||||
srchDto.getDirPath(),
|
||||
"*",
|
||||
srchDto.getExtension(),
|
||||
100,
|
||||
srchDto.getSortType(),
|
||||
startPos,
|
||||
limit);
|
||||
|
||||
int fileListPos = 0;
|
||||
int fileTotCnt = files.size();
|
||||
long fileTotSize = FIleChecker.getFileTotSize(files);
|
||||
|
||||
//List<Basic> files = new ArrayList<>();
|
||||
//SimpleDateFormat dttmFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
|
||||
//int fileListPos = 0;
|
||||
//int fileTotCnt = 0;
|
||||
//long fileTotSize = 0;
|
||||
/*
|
||||
if (fileList != null) {
|
||||
if (sortType.equals("name")) {
|
||||
Arrays.sort(fileList);
|
||||
} else if (sortType.equals("date")) {
|
||||
Arrays.sort(fileList, Comparator.comparingLong(File::lastModified));
|
||||
}
|
||||
|
||||
for (File file : fileList) {
|
||||
if (file.isFile()) { // 파일인 경우만
|
||||
if (extension.equals("*") || file.getName().endsWith("." + extension)) {
|
||||
|
||||
fileListPos = fileListPos + 1;
|
||||
|
||||
if (startPos <= fileListPos && endPos >= fileListPos) {
|
||||
|
||||
// 생성자를 통해 객체를 만들고 리스트에 추가
|
||||
String fileName = file.getName();
|
||||
String parentPath = file.getParent();
|
||||
String fullPath = file.getAbsolutePath();
|
||||
String ext = FilenameUtils.getExtension(fileName);
|
||||
|
||||
Path path = Paths.get(parentPath);
|
||||
String parentFolderNm = path.getFileName().toString();
|
||||
|
||||
long fileSize = file.length();
|
||||
String lastModified = dttmFormat.format(new Date(file.lastModified()));
|
||||
|
||||
files.add(
|
||||
new Basic(
|
||||
fileName, parentFolderNm, parentPath, fullPath, ext, fileSize, lastModified));
|
||||
|
||||
fileTotCnt = fileTotCnt + 1;
|
||||
fileTotSize = fileTotSize + fileSize;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
return new FilesDto(dirPath, fileTotCnt, fileTotSize, files);
|
||||
}
|
||||
|
||||
public FilesDto getFilesDepthAll(SrchFilesDepthDto srchDto) {
|
||||
|
||||
Path startPath = Paths.get(srchDto.getDirPath());
|
||||
int maxDepth = srchDto.getMaxDepth();
|
||||
String dirPath = srchDto.getDirPath();
|
||||
String extension = srchDto.getExtension();
|
||||
String sortType = srchDto.getSortType();
|
||||
|
||||
int startPos = srchDto.getStartPos();
|
||||
int endPos = srchDto.getEndPos();
|
||||
int limit = endPos - startPos + 1;
|
||||
|
||||
Set<String> targetExtensions = createExtensionSet(extension);
|
||||
|
||||
List<Basic> fileDtoList = new ArrayList<>();
|
||||
SimpleDateFormat dttmFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
|
||||
int fileTotCnt = 0;
|
||||
long fileTotSize = 0;
|
||||
|
||||
try (Stream<Path> stream = Files.walk(startPath, maxDepth)) {
|
||||
|
||||
fileDtoList =
|
||||
stream
|
||||
.filter(Files::isRegularFile)
|
||||
.filter(
|
||||
p ->
|
||||
extension == null
|
||||
|| extension.equals("")
|
||||
|| extension.equals("*")
|
||||
|| targetExtensions.contains(extractExtension(p)))
|
||||
.sorted(getFileComparator(sortType))
|
||||
.skip(startPos)
|
||||
.limit(limit)
|
||||
.map(
|
||||
path -> {
|
||||
int depth = path.getNameCount();
|
||||
|
||||
String fileNm = path.getFileName().toString();
|
||||
String ext = FilenameUtils.getExtension(fileNm);
|
||||
String parentFolderNm = path.getParent().getFileName().toString();
|
||||
String parentPath = path.getParent().toString();
|
||||
String fullPath = path.toAbsolutePath().toString();
|
||||
|
||||
File file = new File(fullPath);
|
||||
long fileSize = file.length();
|
||||
String lastModified = dttmFormat.format(new Date(file.lastModified()));
|
||||
|
||||
return new Basic(
|
||||
fileNm, parentFolderNm, parentPath, fullPath, ext, fileSize, lastModified);
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
|
||||
fileTotCnt = fileDtoList.size();
|
||||
fileTotSize = fileDtoList.stream().mapToLong(Basic::getFileSize).sum();
|
||||
|
||||
} catch (IOException e) {
|
||||
System.err.println("파일 I/O 오류 발생: " + e.getMessage());
|
||||
}
|
||||
|
||||
return new FilesDto(dirPath, fileTotCnt, fileTotSize, fileDtoList);
|
||||
}
|
||||
|
||||
public Set<String> createExtensionSet(String extensionString) {
|
||||
if (extensionString == null || extensionString.isBlank()) {
|
||||
return Set.of();
|
||||
}
|
||||
|
||||
// "java, class" -> ["java", " class"] -> [".java", ".class"]
|
||||
return Arrays.stream(extensionString.split(","))
|
||||
.map(ext -> ext.trim())
|
||||
.filter(ext -> !ext.isEmpty())
|
||||
.map(ext -> "." + ext.toLowerCase())
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
public String extractExtension(Path path) {
|
||||
String filename = path.getFileName().toString();
|
||||
int lastDotIndex = filename.lastIndexOf('.');
|
||||
|
||||
// 확장자가 없거나 파일명이 .으로 끝나는 경우
|
||||
if (lastDotIndex == -1 || lastDotIndex == filename.length() - 1) {
|
||||
return ""; // 빈 문자열 반환
|
||||
}
|
||||
|
||||
// 확장자 추출 및 소문자 변환
|
||||
return filename.substring(lastDotIndex).toLowerCase();
|
||||
}
|
||||
|
||||
public Comparator<Path> getFileComparator(String sortType) {
|
||||
|
||||
// 파일 이름 비교 기본 Comparator (대소문자 무시)
|
||||
Comparator<Path> nameComparator =
|
||||
Comparator.comparing(path -> path.getFileName().toString(), CASE_INSENSITIVE_ORDER);
|
||||
|
||||
Comparator<Path> dateComparator =
|
||||
Comparator.comparing(
|
||||
path -> {
|
||||
try {
|
||||
return Files.getLastModifiedTime(path);
|
||||
} catch (IOException e) {
|
||||
return FileTime.fromMillis(0);
|
||||
}
|
||||
});
|
||||
|
||||
if ("name desc".equalsIgnoreCase(sortType)) {
|
||||
return nameComparator.reversed();
|
||||
} else if ("date".equalsIgnoreCase(sortType)) {
|
||||
return dateComparator;
|
||||
} else if ("date desc".equalsIgnoreCase(sortType)) {
|
||||
return dateComparator.reversed();
|
||||
} else {
|
||||
return nameComparator;
|
||||
}
|
||||
}
|
||||
|
||||
public List<MngDto> findMapSheetMngList() {
|
||||
return mapSheetMngCoreService.findMapSheetMngList();
|
||||
|
||||
Reference in New Issue
Block a user