|
|
@@ -12,6 +12,8 @@ import cn.reghao.tnb.file.app.service.TmpFileUrlService;
|
|
|
import cn.reghao.tnb.file.app.util.LoadBalancer;
|
|
|
import cn.reghao.tnb.file.app.util.LocalStores;
|
|
|
import cn.reghao.tnb.file.app.util.StoreDir;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.io.FileUtils;
|
|
|
import org.junit.Test;
|
|
|
import org.junit.runner.RunWith;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
@@ -22,13 +24,17 @@ import org.springframework.test.context.junit4.SpringRunner;
|
|
|
import java.io.File;
|
|
|
import java.io.FileInputStream;
|
|
|
import java.io.IOException;
|
|
|
+import java.nio.file.*;
|
|
|
+import java.nio.file.attribute.BasicFileAttributes;
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* @author reghao
|
|
|
* @date 2022-04-23 17:17:50
|
|
|
*/
|
|
|
+@Slf4j
|
|
|
@ActiveProfiles("dev")
|
|
|
@SpringBootTest(classes = FileApplication.class)
|
|
|
@RunWith(SpringRunner.class)
|
|
|
@@ -39,95 +45,68 @@ public class FileTest {
|
|
|
FileUrlMapper fileUrlMapper;
|
|
|
@Autowired
|
|
|
FileUserMapper fileUserMapper;
|
|
|
- @Autowired
|
|
|
- VideoFileMapper videoFileMapper;
|
|
|
- @Autowired
|
|
|
- ImageFileMapper imageFileMapper;
|
|
|
-
|
|
|
- IdGenerator idGenerator = new IdGenerator("upload-id");
|
|
|
- @Test
|
|
|
- public void test() {
|
|
|
- int page = 1;
|
|
|
- int size = 100;
|
|
|
|
|
|
- List<TmpFile> list = fileUserMapper.findTmpFile();
|
|
|
+ private void walkDir(String dirpath) throws IOException {
|
|
|
+ Path path = Paths.get(dirpath);
|
|
|
+ Files.walkFileTree(path, new FileVisitor<>() {
|
|
|
+ @Override
|
|
|
+ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
|
|
|
+ return FileVisitResult.CONTINUE;
|
|
|
+ }
|
|
|
|
|
|
- List<FileInfo> list1 = fileInfoMapper.findAllByPage(page, size);
|
|
|
- list1.forEach(fileInfo -> {
|
|
|
- String suffix1 = fileInfo.getSuffix().replace(".", "");
|
|
|
- //fileInfoMapper.updateSetSuffix(fileInfo.getFileId(), suffix1);
|
|
|
- //System.out.println();
|
|
|
+ @Override
|
|
|
+ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
|
|
|
+ File file1 = file.toFile();
|
|
|
+ if (file1.isFile()) {
|
|
|
+ String name = file1.getName();
|
|
|
+ String fileId = name;
|
|
|
+ if (name.contains(".")) {
|
|
|
+ fileId = name.split("\\.")[0];
|
|
|
+ }
|
|
|
+ map.put(fileId, file1.getAbsolutePath());
|
|
|
+ }
|
|
|
|
|
|
- /*String fileId = fileInfo.getFileId();
|
|
|
- List<FileUrl> fileUrls = fileUrlMapper.findByFileId(fileId);
|
|
|
- FileUrl fileUrl = fileUrls.get(0);
|
|
|
- String url = fileUrl.getUrl();
|
|
|
+ return FileVisitResult.CONTINUE;
|
|
|
+ }
|
|
|
|
|
|
- String filePath = url.replace("//static.reghao.cn", "");
|
|
|
- String path = url.replace("//static.reghao.cn", "");
|
|
|
- String url1 = url.replace("static.reghao.cn", "file.reghao.cn");
|
|
|
+ @Override
|
|
|
+ public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
|
|
|
+ return FileVisitResult.CONTINUE;
|
|
|
+ }
|
|
|
|
|
|
- fileUrl.setFilePath(filePath);
|
|
|
- fileUrl.setPath(path);
|
|
|
- fileUrl.setUrl(url1);
|
|
|
- System.out.println();*/
|
|
|
+ @Override
|
|
|
+ public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
|
|
|
+ return FileVisitResult.CONTINUE;
|
|
|
+ }
|
|
|
});
|
|
|
}
|
|
|
|
|
|
@Autowired
|
|
|
- DfsProperties dfsProperties;
|
|
|
- @Autowired
|
|
|
- LoadBalancer loadBalancer;
|
|
|
+ TmpFileUrlService tmpFileUrlService;
|
|
|
+ Map<String, String> map = new HashMap<>();
|
|
|
@Test
|
|
|
- public void test1() throws IOException, NoSuchAlgorithmException {
|
|
|
- LocalStores.init(dfsProperties);
|
|
|
+ public void start() throws IOException {
|
|
|
+ String dirPath = "/home/reghao/mnt/hertube/";
|
|
|
+ walkDir(dirPath);
|
|
|
|
|
|
- String dirPath = "/media/reghao/52f23cbd-9a85-4fe4-a184-56110d4bd34e/工具/过人脸教程+控制器(0_0)/0/";
|
|
|
- File dir = new File(dirPath);
|
|
|
- for (File file : Objects.requireNonNull(dir.listFiles())) {
|
|
|
- if (file.isDirectory()) {
|
|
|
- continue;
|
|
|
+ List<FileInfo> list = fileInfoMapper.findAllByUploaded();
|
|
|
+ list.forEach(fileInfo -> {
|
|
|
+ String fileId = fileInfo.getFileId();
|
|
|
+ List<FileUrl> fileUrls = fileUrlMapper.findByFileId(fileId);
|
|
|
+ if (fileUrls.isEmpty()) {
|
|
|
+ log.error("{} 在 file_url 中不存在", fileId);
|
|
|
+ return;
|
|
|
}
|
|
|
|
|
|
- long len = file.length();
|
|
|
- FileInputStream fis = new FileInputStream(file);
|
|
|
- String sha256sum = DigestUtil.sha256sum(fis);
|
|
|
- StoreDir storeDir = loadBalancer.getStoreDir(len, sha256sum);
|
|
|
- System.out.println(storeDir.getFileDir());
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @Autowired
|
|
|
- FileUrlService fileUrlService;
|
|
|
- @Test
|
|
|
- public void test2() throws IOException, NoSuchAlgorithmException {
|
|
|
- int page = 1;
|
|
|
- int size = 100;
|
|
|
-
|
|
|
- List<TmpFile> list = fileUserMapper.findTmpFile();
|
|
|
- for (TmpFile tmpFile : list) {
|
|
|
- String sha256sum = tmpFile.getSha256sum();
|
|
|
- long fileSize = tmpFile.getSize();
|
|
|
- String uploadId = tmpFile.getUploadId();
|
|
|
- String suffix = tmpFile.getSuffix();
|
|
|
- String fileId = tmpFile.getFileId();
|
|
|
-
|
|
|
- if (!suffix.equals("jpg")) {
|
|
|
- PathUrl pathUrl = fileUrlService.genVideoPathAndUrl(sha256sum, uploadId, fileSize, fileId, suffix);
|
|
|
- String blockId = pathUrl.getBlockId();
|
|
|
- String filePath = pathUrl.getFilePath();
|
|
|
- String url = pathUrl.getUrl();
|
|
|
- String path = pathUrl.getPath();
|
|
|
- fileUrlMapper.updateSetFileUrl(fileId, blockId, filePath);
|
|
|
- System.out.println();
|
|
|
+ FileUrl fileUrl = fileUrls.get(0);
|
|
|
+ String blockId = fileUrl.getBlockId();
|
|
|
+ if (blockId == null) {
|
|
|
+ String sourcePath = map.get(fileId);
|
|
|
+ TmpFile tmpFile = fileUserMapper.findTmpFileByFileId(fileId);
|
|
|
+ if (tmpFile != null && sourcePath != null) {
|
|
|
+ tmpFileUrlService.mvFile(tmpFile, sourcePath);
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @Autowired
|
|
|
- TmpFileUrlService tmpFileUrlService;
|
|
|
- @Test
|
|
|
- public void test3() {
|
|
|
- tmpFileUrlService.start1();
|
|
|
+ });
|
|
|
}
|
|
|
}
|