reghao 3 years ago
parent
commit
cc0ab6a85b

+ 7 - 4
pom.xml

@@ -226,10 +226,13 @@
             <plugin>
             <plugin>
                 <groupId>org.springframework.boot</groupId>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-maven-plugin</artifactId>
                 <artifactId>spring-boot-maven-plugin</artifactId>
-                <configuration>
-                    <executable>true</executable>
-                    <fork>true</fork>
-                </configuration>
+                <executions>
+                    <execution>
+                        <goals>
+                            <goal>repackage</goal>
+                        </goals>
+                    </execution>
+                </executions>
             </plugin>
             </plugin>
         </plugins>
         </plugins>
     </build>
     </build>

+ 1 - 0
src/main/java/cn/reghao/tnb/file/app/config/PathUrl.java

@@ -10,6 +10,7 @@ import lombok.Getter;
 @AllArgsConstructor
 @AllArgsConstructor
 @Getter
 @Getter
 public class PathUrl {
 public class PathUrl {
+    private String blockId;
     // 本地文件路径
     // 本地文件路径
     private String filePath;
     private String filePath;
     private String url;
     private String url;

+ 6 - 0
src/main/java/cn/reghao/tnb/file/app/controller/VideoFileController.java

@@ -28,6 +28,12 @@ public class VideoFileController {
         this.fileUrlMapper = fileUrlMapper;
         this.fileUrlMapper = fileUrlMapper;
     }
     }
 
 
+    @GetMapping("/video/{uploadId}")
+    public void videoFile(@PathVariable("uploadId") String uploadId,
+                          @RequestHeader(required = false) String range,
+                          HttpServletResponse response) throws Exception {
+    }
+
     @GetMapping("/video/playback")
     @GetMapping("/video/playback")
     public void videoPlayer(@RequestParam("uploadId") String uploadId,
     public void videoPlayer(@RequestParam("uploadId") String uploadId,
                             @RequestHeader(required = false) String range,
                             @RequestHeader(required = false) String range,

+ 1 - 0
src/main/java/cn/reghao/tnb/file/app/db/mapper/FileInfoMapper.java

@@ -5,6 +5,7 @@ import cn.reghao.tnb.file.api.dto.FileInfoDto;
 import cn.reghao.tnb.file.app.model.dto.FileDto;
 import cn.reghao.tnb.file.app.model.dto.FileDto;
 import cn.reghao.tnb.file.app.model.po.FileInfo;
 import cn.reghao.tnb.file.app.model.po.FileInfo;
 import org.apache.ibatis.annotations.Mapper;
 import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
 
 
 /**
 /**
  * @author reghao
  * @author reghao

+ 6 - 0
src/main/java/cn/reghao/tnb/file/app/db/mapper/FileUrlMapper.java

@@ -4,6 +4,7 @@ import cn.reghao.jutil.jdk.db.BaseMapper;
 import cn.reghao.tnb.file.app.model.dto.FileUrlDto;
 import cn.reghao.tnb.file.app.model.dto.FileUrlDto;
 import cn.reghao.tnb.file.app.model.po.FileUrl;
 import cn.reghao.tnb.file.app.model.po.FileUrl;
 import org.apache.ibatis.annotations.Mapper;
 import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
 
 
 import java.util.List;
 import java.util.List;
 
 
@@ -13,6 +14,11 @@ import java.util.List;
  */
  */
 @Mapper
 @Mapper
 public interface FileUrlMapper extends BaseMapper<FileUrl> {
 public interface FileUrlMapper extends BaseMapper<FileUrl> {
+    @Deprecated
+    void updateSetFileUrl(@Param("fileId") String fileId,
+                          @Param("blockId") String blockId,
+                          @Param("filePath") String filePath);
+
     FileUrlDto findByUploadId(String uploadId);
     FileUrlDto findByUploadId(String uploadId);
     List<FileUrl> findByFileId(String fileId);
     List<FileUrl> findByFileId(String fileId);
 }
 }

+ 4 - 0
src/main/java/cn/reghao/tnb/file/app/db/mapper/FileUserMapper.java

@@ -4,8 +4,11 @@ import cn.reghao.jutil.jdk.db.BaseMapper;
 import cn.reghao.tnb.file.api.dto.FileInfoDto;
 import cn.reghao.tnb.file.api.dto.FileInfoDto;
 import cn.reghao.tnb.file.app.model.po.FileInfo;
 import cn.reghao.tnb.file.app.model.po.FileInfo;
 import cn.reghao.tnb.file.app.model.po.FileUser;
 import cn.reghao.tnb.file.app.model.po.FileUser;
+import cn.reghao.tnb.file.app.model.vo.TmpFile;
 import org.apache.ibatis.annotations.Mapper;
 import org.apache.ibatis.annotations.Mapper;
 
 
+import java.util.List;
+
 /**
 /**
  * @author reghao
  * @author reghao
  * @date 2022-04-26 20:05:30
  * @date 2022-04-26 20:05:30
@@ -15,4 +18,5 @@ public interface FileUserMapper extends BaseMapper<FileUser> {
     FileInfoDto findByUploadId(String uploadId);
     FileInfoDto findByUploadId(String uploadId);
     FileUser findByFileAndUserId(String fileId, long userId);
     FileUser findByFileAndUserId(String fileId, long userId);
     FileInfo findFileInfoByUploadId(String uploadId);
     FileInfo findFileInfoByUploadId(String uploadId);
+    List<TmpFile> findTmpFile();
 }
 }

+ 18 - 0
src/main/java/cn/reghao/tnb/file/app/model/vo/TmpFile.java

@@ -0,0 +1,18 @@
+package cn.reghao.tnb.file.app.model.vo;
+
+import lombok.Getter;
+import lombok.Setter;
+
+/**
+ * @author reghao
+ * @date 2022-05-23 22:57:30
+ */
+@Getter
+@Setter
+public class TmpFile {
+    private String uploadId;
+    private String fileId;
+    private String sha256sum;
+    private String suffix;
+    private Long size;
+}

+ 20 - 10
src/main/java/cn/reghao/tnb/file/app/service/FileUrlService.java

@@ -3,6 +3,7 @@ package cn.reghao.tnb.file.app.service;
 import cn.reghao.tnb.file.app.config.DfsProperties;
 import cn.reghao.tnb.file.app.config.DfsProperties;
 import cn.reghao.tnb.file.app.config.PathUrl;
 import cn.reghao.tnb.file.app.config.PathUrl;
 import cn.reghao.tnb.file.app.util.LoadBalancer;
 import cn.reghao.tnb.file.app.util.LoadBalancer;
+import cn.reghao.tnb.file.app.util.StoreDir;
 import org.springframework.stereotype.Service;
 import org.springframework.stereotype.Service;
 
 
 import java.io.IOException;
 import java.io.IOException;
@@ -25,28 +26,37 @@ public class FileUrlService {
     @Deprecated
     @Deprecated
     public PathUrl genPathAndUrl(String sha256sum, String fileId, String suffix)
     public PathUrl genPathAndUrl(String sha256sum, String fileId, String suffix)
             throws IOException, NoSuchAlgorithmException {
             throws IOException, NoSuchAlgorithmException {
-        String storeDir = loadBalancer.getStoreDir(0, sha256sum);
-        String filePath = String.format("%s/%s.%s", storeDir, fileId, suffix);
+        StoreDir storeDir = loadBalancer.getStoreDir(0, sha256sum);
+        String blockId = storeDir.getBlockId();
+        String fileDir = storeDir.getFileDir();
+
+        String filePath = String.format("%s/%s.%s", fileDir, fileId, suffix);
         String path = String.format("video/%s.%s", fileId, suffix);
         String path = String.format("video/%s.%s", fileId, suffix);
         String url = String.format("//%s/video/%s.%s", domain, fileId, suffix);
         String url = String.format("//%s/video/%s.%s", domain, fileId, suffix);
-        return new PathUrl(filePath, url, path);
+        return new PathUrl(blockId, filePath, url, path);
     }
     }
 
 
     public PathUrl genVideoPathAndUrl(String sha256sum, String uploadId, long fileSize, String fileId, String suffix)
     public PathUrl genVideoPathAndUrl(String sha256sum, String uploadId, long fileSize, String fileId, String suffix)
             throws IOException, NoSuchAlgorithmException {
             throws IOException, NoSuchAlgorithmException {
-        String storeDir = loadBalancer.getStoreDir(fileSize, sha256sum);
-        String filePath = String.format("%s/%s.%s", storeDir, fileId, suffix);
-        String path = String.format("video/%s.%s", uploadId, suffix);
+        StoreDir storeDir = loadBalancer.getStoreDir(fileSize, sha256sum);
+        String blockId = storeDir.getBlockId();
+        String fileDir = storeDir.getFileDir();
+
+        String filePath = String.format("%s%s.%s", fileDir, fileId, suffix);
         String url = String.format("//%s/video/%s.%s", domain, uploadId, suffix);
         String url = String.format("//%s/video/%s.%s", domain, uploadId, suffix);
-        return new PathUrl(filePath, url, path);
+        String path = String.format("video/%s.%s", uploadId, suffix);
+        return new PathUrl(blockId, filePath, url, path);
     }
     }
 
 
     public PathUrl getImagePathAndUrl(String sha256sum, String uploadId, long fileSize, String fileId, String suffix,
     public PathUrl getImagePathAndUrl(String sha256sum, String uploadId, long fileSize, String fileId, String suffix,
                                       int width, int height) throws IOException, NoSuchAlgorithmException {
                                       int width, int height) throws IOException, NoSuchAlgorithmException {
-        String storeDir = loadBalancer.getStoreDir(fileSize, sha256sum);
-        String filePath = String.format("%s/%s_%sx%s.%s", storeDir, fileId, width, height, suffix);
+        StoreDir storeDir = loadBalancer.getStoreDir(fileSize, sha256sum);
+        String blockId = storeDir.getBlockId();
+        String fileDir = storeDir.getFileDir();
+
+        String filePath = String.format("%s/%s_%sx%s.%s", fileDir, fileId, width, height, suffix);
         String path = String.format("video/%s.%s", uploadId, suffix);
         String path = String.format("video/%s.%s", uploadId, suffix);
         String url = String.format("//%s/image/%s.%s", domain, uploadId, suffix);
         String url = String.format("//%s/image/%s.%s", domain, uploadId, suffix);
-        return new PathUrl(filePath, url, path);
+        return new PathUrl(blockId, filePath, url, path);
     }
     }
 }
 }

+ 102 - 0
src/main/java/cn/reghao/tnb/file/app/service/TmpFileUrlService.java

@@ -0,0 +1,102 @@
+package cn.reghao.tnb.file.app.service;
+
+import cn.reghao.tnb.file.app.config.PathUrl;
+import cn.reghao.tnb.file.app.db.mapper.FileUrlMapper;
+import cn.reghao.tnb.file.app.db.mapper.FileUserMapper;
+import cn.reghao.tnb.file.app.model.po.FileUrl;
+import cn.reghao.tnb.file.app.model.vo.TmpFile;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.io.FileUtils;
+import org.springframework.stereotype.Service;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.*;
+import java.nio.file.attribute.BasicFileAttributes;
+import java.util.List;
+
+/**
+ * @author reghao
+ * @date 2022-05-23 23:42:06
+ */
+@Slf4j
+@Service
+@Deprecated
+public class TmpFileUrlService {
+    private final FileUserMapper fileUserMapper;
+    private final FileUrlMapper fileUrlMapper;
+    private final FileUrlService fileUrlService;
+
+    public TmpFileUrlService(FileUserMapper fileUserMapper, FileUrlMapper fileUrlMapper, FileUrlService fileUrlService) {
+        this.fileUserMapper = fileUserMapper;
+        this.fileUrlMapper = fileUrlMapper;
+        this.fileUrlService = fileUrlService;
+    }
+
+    public void start() {
+        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();
+
+            try {
+                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();
+
+                List<FileUrl> list1 = fileUrlMapper.findByFileId(fileId);
+                if (list1.isEmpty()) {
+                    continue;
+                }
+
+                FileUrl fileUrl = list1.get(0);
+                String url1 = fileUrl.getUrl();
+                String filePath1;
+                if (url.contains("group")) {
+                    filePath1 = url1.replace("//static.reghao.cn/group0/node0", "/root/mnt");
+                } else {
+                    filePath1 = url1.replace("//static.reghao.cn", "/root/mnt");
+                }
+
+                File source = new File(filePath1);
+                File dest = new File(filePath);
+                if (source.exists()) {
+                    FileUtils.copyFile(source, new File(filePath), true);
+                    fileUrlMapper.updateSetFileUrl(fileId, blockId, filePath);
+                } else {
+                    log.error("source file {} not exist", filePath);
+                }
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        }
+
+        log.info("start 执行完成...");
+    }
+
+    public void start1() {
+        fileUrlMapper.findAll().forEach(fileUrl -> {
+            String url = fileUrl.getUrl();
+            String filePath;
+            if (url.contains("group")) {
+                filePath = url.replace("//static.reghao.cn/group0/node0", "/root/mnt");
+            } else {
+                filePath = url.replace("//static.reghao.cn", "/root/mnt");
+            }
+
+            File file = new File(filePath);
+            if (!file.exists()) {
+                log.error("{} not exist", filePath);
+            } else {
+                log.info("{} exist", filePath);
+            }
+        });
+
+        log.info("start1 执行完成...");
+    }
+}

+ 6 - 2
src/main/java/cn/reghao/tnb/file/app/util/FileLifecycle.java

@@ -1,6 +1,7 @@
 package cn.reghao.tnb.file.app.util;
 package cn.reghao.tnb.file.app.util;
 
 
 import cn.reghao.tnb.file.app.config.DfsProperties;
 import cn.reghao.tnb.file.app.config.DfsProperties;
+import cn.reghao.tnb.file.app.service.TmpFileUrlService;
 import org.springframework.beans.factory.DisposableBean;
 import org.springframework.beans.factory.DisposableBean;
 import org.springframework.boot.ApplicationArguments;
 import org.springframework.boot.ApplicationArguments;
 import org.springframework.boot.ApplicationRunner;
 import org.springframework.boot.ApplicationRunner;
@@ -15,14 +16,17 @@ import java.io.IOException;
 @Component
 @Component
 public class FileLifecycle implements ApplicationRunner, DisposableBean {
 public class FileLifecycle implements ApplicationRunner, DisposableBean {
     private final DfsProperties dfsProperties;
     private final DfsProperties dfsProperties;
+    private final TmpFileUrlService tmpFileUrlService;
 
 
-    public FileLifecycle(DfsProperties dfsProperties) {
+    public FileLifecycle(DfsProperties dfsProperties, TmpFileUrlService tmpFileUrlService) {
         this.dfsProperties = dfsProperties;
         this.dfsProperties = dfsProperties;
+        this.tmpFileUrlService = tmpFileUrlService;
     }
     }
 
 
     @Override
     @Override
-    public void run(ApplicationArguments args) throws IOException {
+    public void run(ApplicationArguments args) throws Exception {
         LocalStores.init(dfsProperties);
         LocalStores.init(dfsProperties);
+        tmpFileUrlService.start();
     }
     }
 
 
     @Override
     @Override

+ 6 - 4
src/main/java/cn/reghao/tnb/file/app/util/LoadBalancer.java

@@ -19,12 +19,14 @@ import java.util.*;
 public class LoadBalancer {
 public class LoadBalancer {
     private final HashFunction hashFunction = Hashing.murmur3_32_fixed();
     private final HashFunction hashFunction = Hashing.murmur3_32_fixed();
 
 
-    public String getStoreDir(long fileSize, String sha256sum) throws IOException, NoSuchAlgorithmException {
-        String store = LocalStores.getMaxStore(fileSize);
-        List<String> subDirs = LocalStores.getSubDirs(store);
+    public StoreDir getStoreDir(long fileSize, String sha256sum) throws IOException, NoSuchAlgorithmException {
+        LocalStore localStore = LocalStores.getMaxStore(fileSize);
+        String blockId = localStore.getBlockId();
+        List<String> subDirs = LocalStores.getSubDirs(localStore.getBaseDir());
         int hash = hash(sha256sum);
         int hash = hash(sha256sum);
         int nodeIdx = hash % subDirs.size();
         int nodeIdx = hash % subDirs.size();
-        return subDirs.get(nodeIdx);
+        String dir = subDirs.get(nodeIdx);
+        return new StoreDir(blockId, dir);
     }
     }
 
 
     private int hash(Object object) throws IOException, NoSuchAlgorithmException {
     private int hash(Object object) throws IOException, NoSuchAlgorithmException {

+ 2 - 0
src/main/java/cn/reghao/tnb/file/app/util/LocalStore.java

@@ -20,6 +20,7 @@ public class LocalStore {
     private final String fsType;
     private final String fsType;
     private final String mountedOn;
     private final String mountedOn;
     private final long total;
     private final long total;
+    private volatile long available;
     private final long max;
     private final long max;
 
 
     public LocalStore(String baseDir, FileStore fileStore, String blockId, double maxPercent) throws IOException {
     public LocalStore(String baseDir, FileStore fileStore, String blockId, double maxPercent) throws IOException {
@@ -30,6 +31,7 @@ public class LocalStore {
         this.fsType = fileStore.type();
         this.fsType = fileStore.type();
         this.mountedOn = fileStore.toString().replace(fs, "").replace(" ()", "");
         this.mountedOn = fileStore.toString().replace(fs, "").replace(" ()", "");
         this.total = fileStore.getTotalSpace();
         this.total = fileStore.getTotalSpace();
+        this.available = fileStore.getUsableSpace();
 
 
         BigDecimal bigDecimal1 = new BigDecimal(total*10);
         BigDecimal bigDecimal1 = new BigDecimal(total*10);
         BigDecimal bigDecimal2 = new BigDecimal(maxPercent*10);
         BigDecimal bigDecimal2 = new BigDecimal(maxPercent*10);

+ 16 - 11
src/main/java/cn/reghao/tnb/file/app/util/LocalStores.java

@@ -61,7 +61,7 @@ public class LocalStores {
         int total = 128;
         int total = 128;
         for (int i = 0; i < total; i++) {
         for (int i = 0; i < total; i++) {
             for (int j = 0; j < total; j++) {
             for (int j = 0; j < total; j++) {
-                String dirPath = String.format("%s/%s/%s", baseDir, i, j);
+                String dirPath = String.format("%s%s/%s/", baseDir, i, j);
                 File dir = new File(dirPath);
                 File dir = new File(dirPath);
                 if (!dir.exists()) {
                 if (!dir.exists()) {
                     FileUtils.forceMkdir(dir);
                     FileUtils.forceMkdir(dir);
@@ -72,25 +72,30 @@ public class LocalStores {
     }
     }
 
 
     // TODO 优化算法, 处理异常
     // TODO 优化算法, 处理异常
-    public static String getMaxStore(long size) {
+    public static LocalStore getMaxStore(long size) throws IOException {
         Map<String, Long> map = new HashMap<>();
         Map<String, Long> map = new HashMap<>();
         for (Map.Entry<String, LocalStore> entry : storeMap.entrySet()) {
         for (Map.Entry<String, LocalStore> entry : storeMap.entrySet()) {
             String storePath = entry.getKey();
             String storePath = entry.getKey();
-            long availSize = 0L;
-            try {
-                LocalStore localStore = entry.getValue();
-                availSize = localStore.availableSize(size);
-            } catch (IOException ignored) {
-            } finally {
-                map.put(storePath, availSize);
-            }
+            LocalStore localStore = entry.getValue();
+            long availSize = localStore.getAvailable() - size;
+            map.put(storePath, availSize);
         }
         }
 
 
         List<String> result = new ArrayList<>();
         List<String> result = new ArrayList<>();
+        // 降序排列
         map.entrySet().stream()
         map.entrySet().stream()
                 .sorted(Map.Entry.comparingByValue())
                 .sorted(Map.Entry.comparingByValue())
                 .forEachOrdered(b -> result.add(b.getKey()));
                 .forEachOrdered(b -> result.add(b.getKey()));
-        return result.get(result.size()-1);
+
+        String store = result.get(0);
+        LocalStore localStore = storeMap.get(store);
+
+
+        if (localStore != null) {
+            return localStore;
+        } else {
+            throw new IOException("");
+        }
     }
     }
 
 
     public static List<String> getSubDirs(String store) {
     public static List<String> getSubDirs(String store) {

+ 15 - 0
src/main/java/cn/reghao/tnb/file/app/util/StoreDir.java

@@ -0,0 +1,15 @@
+package cn.reghao.tnb.file.app.util;
+
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+
+/**
+ * @author reghao
+ * @date 2022-05-23 23:31:21
+ */
+@AllArgsConstructor
+@Getter
+public class StoreDir {
+    private String blockId;
+    private String fileDir;
+}

+ 4 - 4
src/main/resources/application-dev.yml

@@ -5,9 +5,9 @@ spring:
     port: 6379
     port: 6379
     password: Dev@123456
     password: Dev@123456
   datasource:
   datasource:
-    url: jdbc:mysql://localhost:3306/reghao_tnb_rdb?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2b8
-    username: dev
-    password: Dev@123456
+    url: jdbc:mysql://192.168.0.50:3306/reghao_tnb_tdb?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2b8
+    username: test
+    password: Test@123456
 dubbo:
 dubbo:
   registry:
   registry:
     address: zookeeper://localhost:2181
     address: zookeeper://localhost:2181
@@ -15,4 +15,4 @@ dfs:
   domain: file.reghao.cn
   domain: file.reghao.cn
   baseDirs:
   baseDirs:
     - /home/reghao/opt/file/group0/
     - /home/reghao/opt/file/group0/
-    - /home/reghao/mnt/file/
+    - /media/reghao/52f23cbd-9a85-4fe4-a184-56110d4bd34e/file/

+ 3 - 10
src/main/resources/application-dev1.yml

@@ -13,13 +13,6 @@ dubbo:
     address: zookeeper://localhost:2181
     address: zookeeper://localhost:2181
 dfs:
 dfs:
   domain: file.reghao.cn
   domain: file.reghao.cn
-  dfsConfigs:
-    - baseDir: /home/reghao/opt/file/group0/
-      group: group0
-      node: node0
-    - baseDir: /home/reghao/opt/file/group1/
-      group: group1
-      node: node0
-    - baseDir: /home/reghao/opt/file/group2/
-      group: group2
-      node: node0
+  baseDirs:
+    - /home/reghao/opt/file/group0/
+    - /media/reghao/52f23cbd-9a85-4fe4-a184-56110d4bd34e/file/

+ 25 - 0
src/main/resources/application-dev2.yml

@@ -0,0 +1,25 @@
+spring:
+  redis:
+    database: 0
+    host: localhost
+    port: 6379
+    password: Dev@123456
+  datasource:
+    url: jdbc:mysql://localhost:3306/reghao_tnb_rdb?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2b8
+    username: dev
+    password: Dev@123456
+dubbo:
+  registry:
+    address: zookeeper://localhost:2181
+dfs:
+  domain: file.reghao.cn
+  dfsConfigs:
+    - baseDir: /home/reghao/opt/file/group0/
+      group: group0
+      node: node0
+    - baseDir: /home/reghao/opt/file/group1/
+      group: group1
+      node: node0
+    - baseDir: /home/reghao/opt/file/group2/
+      group: group2
+      node: node0

+ 4 - 10
src/main/resources/application-test.yml

@@ -13,13 +13,7 @@ dubbo:
     address: zookeeper://192.168.0.50:2181
     address: zookeeper://192.168.0.50:2181
 dfs:
 dfs:
   domain: file.reghao.cn
   domain: file.reghao.cn
-  dfsConfigs:
-    - baseDir: /home/reghao/opt/file/group0/
-      group: group0
-      node: node0
-    - baseDir: /home/reghao/opt/file/group1/
-      group: group1
-      node: node0
-    - baseDir: /home/reghao/opt/file/group2/
-      group: group2
-      node: node0
+  baseDirs:
+    - /opt/file/disk0/
+    - /opt/file/disk1/
+    - /opt/file/disk2/

+ 8 - 0
src/main/resources/mapper/FileUrlMapper.xml

@@ -9,6 +9,14 @@
         (#{id},#{deleted},#{createTime},#{updateTime},#{fileId},#{filePath},#{url},#{path},#{dcId},#{nodeId})
         (#{id},#{deleted},#{createTime},#{updateTime},#{fileId},#{filePath},#{url},#{path},#{dcId},#{nodeId})
     </insert>
     </insert>
 
 
+    <update id="updateSetFileUrl">
+        update file_url set update_time=now(),block_id=#{blockId},file_path=#{filePath}
+        where file_id=#{fileId}
+    </update>
+
+    <select id="findAll" resultType="cn.reghao.tnb.file.app.model.po.FileUrl">
+        select * from file_url
+    </select>
     <select id="findByUploadId" resultType="cn.reghao.tnb.file.app.model.dto.FileUrlDto">
     <select id="findByUploadId" resultType="cn.reghao.tnb.file.app.model.dto.FileUrlDto">
         select info.id,info.filename,url.path,url.url from file_url url
         select info.id,info.filename,url.path,url.url from file_url url
         inner join file_user fileUser
         inner join file_user fileUser

+ 6 - 0
src/main/resources/mapper/FileUserMapper.xml

@@ -25,4 +25,10 @@
         inner join file_user fileUser
         inner join file_user fileUser
         on fileUser.file_id=fileInfo.file_id and fileUser.upload_id=#{uploadId}
         on fileUser.file_id=fileInfo.file_id and fileUser.upload_id=#{uploadId}
     </select>
     </select>
+    <select id="findTmpFile" resultType="cn.reghao.tnb.file.app.model.vo.TmpFile">
+        select fileUser.upload_id,fileInfo.file_id,fileInfo.size,fileInfo.sha256sum,fileInfo.suffix
+        from file_user fileUser
+        inner join file_info fileInfo
+        on fileUser.file_id=fileInfo.file_id
+    </select>
 </mapper>
 </mapper>

+ 52 - 7
src/test/java/FileTest.java

@@ -2,11 +2,16 @@ import cn.reghao.jutil.jdk.security.DigestUtil;
 import cn.reghao.jutil.tool.id.IdGenerator;
 import cn.reghao.jutil.tool.id.IdGenerator;
 import cn.reghao.tnb.file.app.FileApplication;
 import cn.reghao.tnb.file.app.FileApplication;
 import cn.reghao.tnb.file.app.config.DfsProperties;
 import cn.reghao.tnb.file.app.config.DfsProperties;
+import cn.reghao.tnb.file.app.config.PathUrl;
 import cn.reghao.tnb.file.app.db.mapper.*;
 import cn.reghao.tnb.file.app.db.mapper.*;
 import cn.reghao.tnb.file.app.model.po.FileInfo;
 import cn.reghao.tnb.file.app.model.po.FileInfo;
 import cn.reghao.tnb.file.app.model.po.FileUrl;
 import cn.reghao.tnb.file.app.model.po.FileUrl;
+import cn.reghao.tnb.file.app.model.vo.TmpFile;
+import cn.reghao.tnb.file.app.service.FileUrlService;
+import cn.reghao.tnb.file.app.service.TmpFileUrlService;
 import cn.reghao.tnb.file.app.util.LoadBalancer;
 import cn.reghao.tnb.file.app.util.LoadBalancer;
 import cn.reghao.tnb.file.app.util.LocalStores;
 import cn.reghao.tnb.file.app.util.LocalStores;
+import cn.reghao.tnb.file.app.util.StoreDir;
 import org.junit.Test;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.junit.runner.RunWith;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -45,9 +50,15 @@ public class FileTest {
         int page = 1;
         int page = 1;
         int size = 100;
         int size = 100;
 
 
-        List<FileInfo> list = fileInfoMapper.findAllByPage(page, size);
-        list.forEach(fileInfo -> {
-            String fileId = fileInfo.getFileId();
+        List<TmpFile> list = fileUserMapper.findTmpFile();
+
+        List<FileInfo> list1 = fileInfoMapper.findAllByPage(page, size);
+        list1.forEach(fileInfo -> {
+            String suffix1 = fileInfo.getSuffix().replace(".", "");
+            //fileInfoMapper.updateSetSuffix(fileInfo.getFileId(), suffix1);
+            //System.out.println();
+
+            /*String fileId = fileInfo.getFileId();
             List<FileUrl> fileUrls = fileUrlMapper.findByFileId(fileId);
             List<FileUrl> fileUrls = fileUrlMapper.findByFileId(fileId);
             FileUrl fileUrl = fileUrls.get(0);
             FileUrl fileUrl = fileUrls.get(0);
             String url = fileUrl.getUrl();
             String url = fileUrl.getUrl();
@@ -59,7 +70,7 @@ public class FileTest {
             fileUrl.setFilePath(filePath);
             fileUrl.setFilePath(filePath);
             fileUrl.setPath(path);
             fileUrl.setPath(path);
             fileUrl.setUrl(url1);
             fileUrl.setUrl(url1);
-            System.out.println();
+            System.out.println();*/
         });
         });
     }
     }
 
 
@@ -71,7 +82,7 @@ public class FileTest {
     public void test1() throws IOException, NoSuchAlgorithmException {
     public void test1() throws IOException, NoSuchAlgorithmException {
         LocalStores.init(dfsProperties);
         LocalStores.init(dfsProperties);
 
 
-        String dirPath = "/home/reghao/Downloads";
+        String dirPath = "/media/reghao/52f23cbd-9a85-4fe4-a184-56110d4bd34e/工具/过人脸教程+控制器(0_0)/0/";
         File dir = new File(dirPath);
         File dir = new File(dirPath);
         for (File file : Objects.requireNonNull(dir.listFiles())) {
         for (File file : Objects.requireNonNull(dir.listFiles())) {
             if (file.isDirectory()) {
             if (file.isDirectory()) {
@@ -81,8 +92,42 @@ public class FileTest {
             long len = file.length();
             long len = file.length();
             FileInputStream fis = new FileInputStream(file);
             FileInputStream fis = new FileInputStream(file);
             String sha256sum = DigestUtil.sha256sum(fis);
             String sha256sum = DigestUtil.sha256sum(fis);
-            String storeDir = loadBalancer.getStoreDir(len, sha256sum);
-            System.out.println(storeDir);
+            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();
+            }
         }
         }
     }
     }
+
+    @Autowired
+    TmpFileUrlService tmpFileUrlService;
+    @Test
+    public void test3() {
+        tmpFileUrlService.start1();
+    }
 }
 }