فهرست منبع

更新对图片文件的处理

reghao 2 سال پیش
والد
کامیت
6d676f8685

+ 13 - 1
dfs-store/src/main/java/cn/reghao/dfs/store/model/po/ImageFile.java

@@ -27,12 +27,24 @@ public class ImageFile extends BaseObject<Integer> {
     private String thumbnailUrl;
 
     public ImageFile(String imageFileId, Integer width, Integer height, Boolean horizontal,
-                     String jpegUrl, String webpUrl) {
+                     String jpegObjectId, String jpegUrl) {
         this.imageFileId = imageFileId;
         this.width = width;
         this.height = height;
         this.horizontal = horizontal;
+        this.jpegObjectId = jpegObjectId;
         this.jpegUrl = jpegUrl;
+    }
+
+    public ImageFile(String imageFileId, Integer width, Integer height, Boolean horizontal,
+                     String jpegObjectId, String jpegUrl, String webpObjectId, String webpUrl) {
+        this.imageFileId = imageFileId;
+        this.width = width;
+        this.height = height;
+        this.horizontal = horizontal;
+        this.jpegObjectId = jpegObjectId;
+        this.jpegUrl = jpegUrl;
+        this.webpObjectId = webpObjectId;
         this.webpUrl = webpUrl;
     }
 }

+ 2 - 5
dfs-store/src/main/java/cn/reghao/dfs/store/task/FileProcessor.java

@@ -43,13 +43,10 @@ public class FileProcessor {
                 uploadFileRet = videoFileProcessor.process(objectResult);
                 break;
             case cover:
-                uploadFileRet = imageFileProcessor.processCover(objectResult);
-                break;
             case avatar:
-                uploadFileRet = imageFileProcessor.processAvatar(objectResult);
-                break;
             case photo:
-                uploadFileRet = imageFileProcessor.processPhoto(objectResult);
+            case image:
+                uploadFileRet = imageFileProcessor.process(objectResult);
                 break;
             case audio:
                 uploadFileRet = audioFileProcessor.process(objectResult);

+ 37 - 37
dfs-store/src/main/java/cn/reghao/dfs/store/task/ImageFileProcessor.java

@@ -38,18 +38,6 @@ public class ImageFileProcessor {
         this.putObjectService = putObjectService;
     }
 
-    public UploadFileRet processCover(ObjectResult objectResult) {
-        return null;
-    }
-
-    public UploadFileRet processAvatar(ObjectResult objectResult) {
-        return null;
-    }
-
-    public UploadFileRet processPhoto(ObjectResult objectResult) {
-        return null;
-    }
-
     public UploadFileRet process(ObjectResult objectResult) {
         String originalObjectId = objectResult.getObjectId();
         boolean duplicate = objectResult.isDuplicate();
@@ -57,80 +45,92 @@ public class ImageFileProcessor {
             String dupObjectId = objectResult.getDupObjectId();
             ImageFile imageFile = mediaRepository.findImageFile(dupObjectId);
 
-            String jpegUrl = imageFile.getJpegUrl();
-            String jpegObjectName = objectNameService.getObjectNameFromUrl(jpegUrl);
+            String jpegObjectName = objectNameService.getObjectNameFromUrl(imageFile.getJpegUrl());
             ObjectResult objectResult2 = putObjectService.copyObject(jpegObjectName, ".jpeg");
             String jpegObjectId = objectResult2.getObjectId();
-            String jpegUrl1 = objectNameService.getObjectUrl(objectResult2.getObjectName());
+            String jpegUrl = objectNameService.getObjectUrl(objectResult2.getObjectName());
 
-            String webpUrl = imageFile.getWebpUrl();
-            String webpObjectName = objectNameService.getObjectNameFromUrl(webpUrl);
+            String webpObjectName = objectNameService.getObjectNameFromUrl(imageFile.getWebpUrl());
             ObjectResult objectResult3 = putObjectService.copyObject(webpObjectName, ".webp");
             String webpObjectId = objectResult3.getObjectId();
-            String webpUrl1 = objectNameService.getObjectUrl(objectResult3.getObjectName());
+            String webpUrl = objectNameService.getObjectUrl(objectResult3.getObjectName());
 
             int width = imageFile.getWidth();
             int height = imageFile.getHeight();
             boolean horizontal = imageFile.getHorizontal();
 
-            ImageFile imageFile1 = new ImageFile(originalObjectId, width, height, horizontal, jpegUrl1, webpUrl1);
+            ImageFile imageFile1 = new ImageFile(originalObjectId, width, height, horizontal,
+                    jpegObjectId, jpegUrl, webpObjectId, webpUrl);
             mediaRepository.saveImageFile(imageFile1);
-            return new UploadFileRet(objectResult2.getObjectId(), jpegUrl1);
+            return new UploadFileRet(jpegObjectId, jpegUrl);
         }
 
         String originalObjectName = objectResult.getObjectName();
         String absolutePath = objectResult.getAbsolutePath();
         try {
             ObjectResult objectResult1;
+            String jpegObjectId;
             String jpegUrl;
+            String webpObjectId;
             String webpUrl;
             String format = ImageOps.getFormat(new File(absolutePath));
             if (imageFormats.contains(format)) {
-                objectResult1 = putObjectService.copyObject(originalObjectName, "."+format);
+                String jpegObjectName = objectResult.getObjectName();
+                objectResult1 = putObjectService.copyObject(jpegObjectName, "."+format);
+                jpegObjectId = objectResult1.getObjectId();
                 jpegUrl = objectNameService.getObjectUrl(objectResult1.getObjectName());
             } else {
-                jpegUrl = getJpegUrl(originalObjectName, absolutePath);
+                ObjectResult objectResult2 = getJpegObject(originalObjectName, absolutePath);
+                jpegObjectId = objectResult2.getObjectId();
+                jpegUrl = objectNameService.getObjectUrl(objectResult2.getObjectName());
             }
 
-            if (format.equalsIgnoreCase("webp")) {
-                objectResult1 = putObjectService.copyObject(originalObjectName, "."+format);
-                webpUrl = objectNameService.getObjectUrl(objectResult1.getObjectName());
+            if (format.equals("webp")) {
+                String webpObjectName = objectResult.getObjectName();
+                ObjectResult objectResult3 = putObjectService.copyObject(webpObjectName, ".webp");
+                webpObjectId = objectResult3.getObjectId();
+                webpUrl = objectNameService.getObjectUrl(objectResult3.getObjectName());
             } else {
-                webpUrl = getWebpUrl(originalObjectName, absolutePath);
+                ObjectResult objectResult3 = getWebpObject(originalObjectName, absolutePath);
+                webpObjectId = objectResult3.getObjectId();
+                webpUrl = objectNameService.getObjectUrl(objectResult3.getObjectName());
             }
 
             ImageOps.Size size = ImageOps.info(new File(absolutePath));
-            boolean horizontal = size.getWidth() > size.getHeight();
-            ImageFile imageFile =
-                    new ImageFile(originalObjectId, size.getWidth(), size.getHeight(), horizontal, jpegUrl, webpUrl);
+            int width = size.getWidth();
+            int height = size.getHeight();
+            boolean horizontal = width > height;
+            ImageFile imageFile = new ImageFile(originalObjectId, width, height, horizontal,
+                    jpegObjectId, jpegUrl, webpObjectId, webpUrl);
             mediaRepository.saveImageFile(imageFile);
-
-            return new UploadFileRet("objectResult1.getObjectId()", jpegUrl);
+            return new UploadFileRet(originalObjectId, jpegUrl);
         } catch (Exception e) {
             e.printStackTrace();
         }
-
         return null;
     }
 
-    private String getJpegUrl(String originalObjectName, String originalPath) throws Exception {
+    private ObjectResult getJpegObject(String originalObjectName, String originalPath) throws Exception {
         byte[] bytes = ImageOps.convert2jpg(new File(originalPath));
         return saveImage(originalObjectName, bytes, ".jpeg");
     }
 
-    private String getWebpUrl(String originalObjectName, String originalPath) throws Exception {
+    private ObjectResult getWebpObject(String originalObjectName, String originalPath) throws Exception {
         BufferedImage bi = ImageIO.read(new File(originalPath));
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         ImageIO.write(bi, "webp", baos);
         return saveImage(originalObjectName, baos.toByteArray(), ".webp");
     }
 
-    private String saveImage(String originalObjectName, byte[] bytes, String suffix) throws Exception {
+    private ObjectResult getThumbnailObject(String originalObjectName, String originalPath) throws Exception {
+        return null;
+    }
+
+    private ObjectResult saveImage(String originalObjectName, byte[] bytes, String suffix) throws Exception {
         ObjectProp objectProp = objectNameService.getObjectProp(originalObjectName, suffix);
         String contentId = UUID.randomUUID().toString().replace("-", "");
         File savedFile = fileStoreService.saveFile(bytes, contentId);
         String sha256sum = DigestUtil.sha256sum(savedFile.getAbsolutePath());
-        ObjectResult objectResult = putObjectService.putObject(objectProp, contentId, savedFile, "", sha256sum);
-        return objectNameService.getObjectUrl(objectResult.getObjectName());
+        return putObjectService.putObject(objectProp, contentId, savedFile, "", sha256sum);
     }
 }