Explorar o código

更新文件存储在本地磁盘的路径和通过 web 访问的 url

reghao %!s(int64=3) %!d(string=hai) anos
pai
achega
07e9288380

+ 7 - 4
src/main/java/cn/reghao/tnb/file/app/service/FileUploadService.java

@@ -1,6 +1,7 @@
 package cn.reghao.tnb.file.app.service;
 
 import cn.reghao.jutil.jdk.security.DigestUtil;
+import cn.reghao.tnb.file.app.util.redis.RedisKey;
 import cn.reghao.tnb.file.app.util.redis.ds.RedisSet;
 import cn.reghao.tnb.file.app.config.PathUrl;
 import cn.reghao.tnb.file.app.db.repository.FileRepository;
@@ -67,13 +68,14 @@ public class FileUploadService {
         String uploadId = uploadFile.getUploadId();
         FileInfo fileInfo = fileRepository.getFileInfoByUploadId(uploadId);
         if (!fileInfo.getUploaded()) {
-            put(uploadFile.getFile(), fileInfo);
+            put(uploadFile.getFile(), fileInfo, uploadId);
         }
     }
 
     @Transactional(rollbackFor = Exception.class)
-    public synchronized String put(MultipartFile multipartFile, FileInfo fileInfo) throws Exception {
+    public synchronized String put(MultipartFile multipartFile, FileInfo fileInfo, String uploadId) throws Exception {
         byte[] bytes = multipartFile.getBytes();
+        long size = multipartFile.getSize();
         String sha256sum = DigestUtil.sha256sum(bytes);
         if (!sha256sum.equals(fileInfo.getSha256sum())) {
             throw new Exception("uploadId 和 sha256sum 不匹配!");
@@ -85,9 +87,10 @@ public class FileUploadService {
         PathUrl pathUrl;
         if (contentType != null && contentType.startsWith("image")) {
             ImageOps.Size size1 = ImageOps.info(new ByteArrayInputStream(bytes));
-            pathUrl = fileUrlService.getImagePathAndUrl(sha256sum, fileId, suffix, size1.getWidth(), size1.getHeight());
+            pathUrl = fileUrlService.getImagePathAndUrl(sha256sum, uploadId, size, fileId, suffix,
+                    size1.getWidth(), size1.getHeight());
         } else {
-            pathUrl = fileUrlService.genPathAndUrl(sha256sum, fileId, suffix);
+            pathUrl = fileUrlService.genVideoPathAndUrl(sha256sum, uploadId, size, fileId, suffix);
         }
 
         FileContentType fileType = fileTypeService.getFileType(contentType, pathUrl.getFilePath());

+ 8 - 6
src/main/java/cn/reghao/tnb/file/app/service/FileUrlService.java

@@ -22,7 +22,9 @@ public class FileUrlService {
         this.domain = dfsProperties.getDomain();
     }
 
-    public PathUrl genPathAndUrl(String sha256sum, String fileId, String suffix) throws IOException, NoSuchAlgorithmException {
+    @Deprecated
+    public PathUrl genPathAndUrl(String sha256sum, String fileId, String suffix)
+            throws IOException, NoSuchAlgorithmException {
         String storeDir = loadBalancer.getStoreDir(0, sha256sum);
         String filePath = String.format("%s/%s.%s", storeDir, fileId, suffix);
         String path = String.format("video/%s.%s", fileId, suffix);
@@ -39,12 +41,12 @@ public class FileUrlService {
         return new PathUrl(filePath, url, path);
     }
 
-    public PathUrl getImagePathAndUrl(String sha256sum, String fileId, String suffix, int width, int height)
-            throws IOException, NoSuchAlgorithmException {
-        String storeDir = loadBalancer.getStoreDir(0, sha256sum);
+    public PathUrl getImagePathAndUrl(String sha256sum, String uploadId, long fileSize, String fileId, String suffix,
+                                      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);
-        String path = String.format("video/%s.%s", fileId, suffix);
-        String url = String.format("//%s/image/%s.%s", domain, fileId, suffix);
+        String path = String.format("video/%s.%s", uploadId, suffix);
+        String url = String.format("//%s/image/%s.%s", domain, uploadId, suffix);
         return new PathUrl(filePath, url, path);
     }
 }

+ 1 - 1
src/main/java/cn/reghao/tnb/file/app/service/RedisKey.java → src/main/java/cn/reghao/tnb/file/app/util/redis/RedisKey.java

@@ -1,4 +1,4 @@
-package cn.reghao.tnb.file.app.service;
+package cn.reghao.tnb.file.app.util.redis;
 
 /**
  * @author reghao

+ 0 - 19
src/main/java/cn/reghao/tnb/file/app/util/redis/RedisKeys.java

@@ -1,19 +0,0 @@
-package cn.reghao.tnb.file.app.util.redis;
-
-/**
- * @author reghao
- * @date 2021-12-11 19:20:25
- */
-public class RedisKeys {
-    public static String captchaCodeKey(String r) {
-        return "captchaCode.r:" + r;
-    }
-
-    public static String verifyCodeKey(String receiver) {
-        return "verifyCode.receiver:" + receiver;
-    }
-
-    public static String userTokenKey(String userId) {
-        return "user.token:" + userId;
-    }
-}