FileInspector 추가

master
mjkhan21 7 months ago
parent 6090d28439
commit 9470af6768

@ -0,0 +1,44 @@
package cokr.xit.base.file;
import java.io.File;
import java.net.URLConnection;
import java.util.List;
import java.util.stream.Stream;
import cokr.xit.foundation.AbstractComponent;
import cokr.xit.foundation.data.DataObject;
public class FileInspector extends AbstractComponent {
private DataObject getInfo(File file) {
String name = file.getName(),
type = file.isDirectory() ? "dir" : file.isFile() ? "file" : "unknown";
return new DataObject()
.set("name", name)
.set("path", file.getPath())
.set("parent", file.getParent())
.set("length", file.length())
.set("type", type)
.set("mimeType", URLConnection.guessContentTypeFromName(name));
}
private File getDir(String path) {
File dir = new File(path);
if (!dir.exists())
throw new RuntimeException(path + " does not exist");
if (!dir.isDirectory())
throw new RuntimeException(path + " is not a directory");
return dir;
}
public List<DataObject> getDirList(String path) {
File dir = getDir(path);
File[] subs = dir.listFiles(File::isDirectory);
return Stream.of(subs).map(this::getInfo).toList();
}
public List<DataObject> getFileList(String dir) {
File[] files = getDir(dir).listFiles(File::isFile);
return Stream.of(files).map(this::getInfo).toList();
}
}
Loading…
Cancel
Save