|
@@ -4,7 +4,9 @@ import cn.reghao.oss.api.dto.ServerInfo;
|
|
|
import cn.reghao.oss.sdk.OssClient;
|
|
import cn.reghao.oss.sdk.OssClient;
|
|
|
import cn.reghao.tnb.file.app.zdisk.model.vo.FileTree;
|
|
import cn.reghao.tnb.file.app.zdisk.model.vo.FileTree;
|
|
|
import cn.reghao.oss.api.constant.ObjectType;
|
|
import cn.reghao.oss.api.constant.ObjectType;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import net.sourceforge.tess4j.Tesseract;
|
|
import net.sourceforge.tess4j.Tesseract;
|
|
|
|
|
+import org.apache.tika.Tika;
|
|
|
import org.junit.jupiter.api.Test;
|
|
import org.junit.jupiter.api.Test;
|
|
|
import org.mockito.junit.MockitoJUnitRunner;
|
|
import org.mockito.junit.MockitoJUnitRunner;
|
|
|
|
|
|
|
@@ -18,57 +20,44 @@ import java.util.Map;
|
|
|
import java.util.TreeMap;
|
|
import java.util.TreeMap;
|
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
+@Slf4j
|
|
|
public class DiskUnitTest {
|
|
public class DiskUnitTest {
|
|
|
void walkDir(Path path) throws IOException {
|
|
void walkDir(Path path) throws IOException {
|
|
|
- Files.walkFileTree(path, new FileVisitor<Path>() {
|
|
|
|
|
- @Override
|
|
|
|
|
- public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
|
|
|
|
|
- File dir1 = dir.toFile();
|
|
|
|
|
- process(dir1);
|
|
|
|
|
- return FileVisitResult.CONTINUE;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
|
|
+ Files.walkFileTree(path, new SimpleFileVisitor<>() {
|
|
|
@Override
|
|
@Override
|
|
|
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
|
|
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
|
|
|
File file1 = file.toFile();
|
|
File file1 = file.toFile();
|
|
|
process(file1);
|
|
process(file1);
|
|
|
return FileVisitResult.CONTINUE;
|
|
return FileVisitResult.CONTINUE;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- @Override
|
|
|
|
|
- public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
|
|
|
|
|
- return FileVisitResult.CONTINUE;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- @Override
|
|
|
|
|
- public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
|
|
|
|
|
- return FileVisitResult.CONTINUE;
|
|
|
|
|
- }
|
|
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- List<FileTree> fileTreeList = new ArrayList<>();
|
|
|
|
|
private void process(File file) {
|
|
private void process(File file) {
|
|
|
- String basePath = dir.getAbsolutePath();
|
|
|
|
|
- String absolutePath = file.getAbsolutePath();
|
|
|
|
|
- String path = absolutePath.replace(basePath, "");
|
|
|
|
|
- String pid = file.getParent().replace(basePath, "");
|
|
|
|
|
- if (path.equals("")) {
|
|
|
|
|
- pid = "0";
|
|
|
|
|
- } else if (pid.equals("")) {
|
|
|
|
|
- pid = "/";
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ String contentType = getContentType(file);
|
|
|
|
|
+ log.info("{} -> {}", contentType, file.getName());
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- int fileType = ObjectType.Image.getCode();
|
|
|
|
|
- if (file.isDirectory()) {
|
|
|
|
|
- fileType = ObjectType.Folder.getCode();
|
|
|
|
|
|
|
+ Tika tika = new Tika();
|
|
|
|
|
+ private String getContentType(File file) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ // Tika 会根据文件头字节进行深度探测
|
|
|
|
|
+ return tika.detect(file);
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ log.error("Tika 探测文件类型失败: {}", file.getAbsolutePath(), e);
|
|
|
|
|
+ return "application/octet-stream";
|
|
|
}
|
|
}
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- String fileId = absolutePath.replace(basePath, "");
|
|
|
|
|
- FileTree fileTree = new FileTree(pid, fileId, fileType);
|
|
|
|
|
- fileTreeList.add(fileTree);
|
|
|
|
|
|
|
+ @Test
|
|
|
|
|
+ public void fileWalk() throws IOException {
|
|
|
|
|
+ String baseDir = "/disk/2/porn/bt";
|
|
|
|
|
+ Path path = Paths.get(baseDir);
|
|
|
|
|
+ walkDir(path);
|
|
|
|
|
+ System.out.println();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ List<FileTree> fileTreeList = new ArrayList<>();
|
|
|
String baseDir = "/home/reghao/Downloads/";
|
|
String baseDir = "/home/reghao/Downloads/";
|
|
|
File dir = new File(baseDir);
|
|
File dir = new File(baseDir);
|
|
|
|
|
|