Pārlūkot izejas kodu

oss-store 处理对象失败直接抛出异常

reghao 2 gadi atpakaļ
vecāks
revīzija
052dffffec

+ 4 - 5
oss-store/src/main/java/cn/reghao/oss/store/controller/ObjectUploadController.java

@@ -14,7 +14,6 @@ import cn.reghao.jutil.jdk.security.DigestUtil;
 import cn.reghao.jutil.web.ServletUtil;
 import cn.reghao.oss.api.dto.OssPayload;
 import cn.reghao.oss.api.rest.UploadFileRet;
-import cn.reghao.oss.store.util.StringUtil;
 import cn.reghao.oss.store.util.UserContext;
 import org.apache.commons.io.FileUtils;
 import org.springframework.http.HttpStatus;
@@ -107,17 +106,17 @@ public class ObjectUploadController {
         ObjectProp objectProp = objectNameService.getObjectProp(channelId, filename);
         ObjectResult objectResult = putObjectService.putObject(objectProp, contentId, savedFile, filename, sha256sum1);
         UploadFileRet uploadFileRet;
+        String errMsg;
         try {
             uploadFileRet = fileProcessor.process(objectResult, channelId);
-            if (uploadFileRet != null) {
-                return ResponseEntity.status(HttpStatus.OK).body(WebResult.success(uploadFileRet));
-            }
+            return ResponseEntity.status(HttpStatus.OK).body(WebResult.success(uploadFileRet));
         } catch (Exception e) {
             e.printStackTrace();
+            errMsg = e.getMessage();
         }
 
         putObjectService.deleteObject(objectResult.getObjectId());
-        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(WebResult.fail());
+        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errMsg);
     }
 
     @DeleteMapping(value = "/")

+ 4 - 3
oss-store/src/main/java/cn/reghao/oss/store/task/FileProcessor.java

@@ -39,7 +39,7 @@ public class FileProcessor {
         this.imageRepository = imageRepository;
     }
 
-    public UploadFileRet process(ObjectResult objectResult, int channelId) {
+    public UploadFileRet process(ObjectResult objectResult, int channelId) throws Exception {
         String objectName = objectResult.getObjectName();
         int fileType = objectResult.getFileType();
         ObjectType objectType = ObjectType.getByCode(fileType);
@@ -48,7 +48,8 @@ public class FileProcessor {
         UploadChannel channel = UploadChannel.getUploadChannel(channelId);
         switch (channel) {
             case disk:
-                uploadFileRet = processFile(objectResult, channelId);
+                uploadFileRet = diskFileProcessor.process(objectResult);
+                //uploadFileRet = processFile(objectResult, channelId);
                 break;
             case video:
                 uploadFileRet = videoFileProcessor.process(objectResult);
@@ -77,7 +78,7 @@ public class FileProcessor {
         uploadFileRet.setUrl(url);
     }
 
-    private UploadFileRet processFile(ObjectResult objectResult, int channelId) {
+    private UploadFileRet processFile(ObjectResult objectResult, int channelId) throws Exception {
         int fileType = objectResult.getFileType();
         UploadFileRet uploadFileRet;
         if (fileType == ObjectType.Image.getCode()) {

+ 1 - 1
oss-store/src/main/java/cn/reghao/oss/store/task/FileTask.java

@@ -24,7 +24,7 @@ public class FileTask {
     public void exec() {
     }
 
-    private void execTask() {
+    private void execTask() throws Exception {
         List<String> objectNames = List.of(
                 "audio/playback/vJPA6ZGzV2.mp3"
         );

+ 3 - 3
oss-store/src/main/java/cn/reghao/oss/store/task/processor/AudioFileProcessor.java

@@ -42,7 +42,7 @@ public class AudioFileProcessor {
         this.fileStoreService = fileStoreService;
     }
 
-    public UploadFileRet process(ObjectResult objectResult) {
+    public UploadFileRet process(ObjectResult objectResult) throws Exception {
         String objectName = objectResult.getObjectName();
         String objectId = objectResult.getObjectId();
         String audioFileId = objectId;
@@ -73,8 +73,8 @@ public class AudioFileProcessor {
         String absolutePath = objectResult.getAbsolutePath();
         MediaProps mediaProps = FFmpegWrapper.getMediaProps(absolutePath);
         if (mediaProps == null || mediaProps.getAudioProps() == null) {
-            log.error("{} 的 FFmpeg 音频信息为 null", objectName);
-            return null;
+            String errMsg = String.format("%s 的 FFmpeg 音频信息为 null", objectName);
+            throw new Exception(errMsg);
         }
 
         AudioProps audioProps = mediaProps.getAudioProps();

+ 33 - 37
oss-store/src/main/java/cn/reghao/oss/store/task/processor/ImageFileProcessor.java

@@ -40,7 +40,7 @@ public class ImageFileProcessor {
         this.putObjectService = putObjectService;
     }
 
-    public UploadFileRet processImage(ObjectResult objectResult, int channelId) {
+    public UploadFileRet processImage(ObjectResult objectResult, int channelId) throws Exception {
         String objectName = objectResult.getObjectName();
         String objectId = objectResult.getObjectId();
         String imageFileId = objectId;
@@ -59,28 +59,21 @@ public class ImageFileProcessor {
             return null;
         }
 
-        try {
-            ImageOps.Size size = ImageOps.info(new File(absolutePath));
-            int width = size.getWidth();
-            int height = size.getHeight();
-            List<ImageFile> list = new ArrayList<>();
-            list.add(new ImageFile(imageFileId, objectId, format, objectUrl, width, height));
-            if (channelId == UploadChannel.photo.getCode() || channelId == UploadChannel.disk.getCode()) {
-                ImageFile imageFile = getConvertedImageFile(objectResult, "webp", width, height);
-                if (imageFile != null) {
-                    list.add(imageFile);
-                }
-            }
-
-            imageRepository.saveImageFiles(list);
-            return new UploadFileRet(objectId, null);
-        } catch (Exception e) {
-            e.printStackTrace();
+        ImageOps.Size size = ImageOps.info(new File(absolutePath));
+        int width = size.getWidth();
+        int height = size.getHeight();
+        List<ImageFile> list = new ArrayList<>();
+        list.add(new ImageFile(imageFileId, objectId, format, objectUrl, width, height));
+        if (channelId == UploadChannel.photo.getCode() || channelId == UploadChannel.disk.getCode()) {
+            ImageFile imageFile = getConvertedImageFile(objectResult, "webp", width, height);
+            list.add(imageFile);
         }
-        return null;
+
+        imageRepository.saveImageFiles(list);
+        return new UploadFileRet(objectId, null);
     }
 
-    private UploadFileRet processDuplicate(ObjectResult objectResult, int channelId) {
+    private UploadFileRet processDuplicate(ObjectResult objectResult, int channelId) throws Exception {
         String objectName = objectResult.getObjectName();
         String objectId = objectResult.getObjectId();
         String imageFileId = objectId;
@@ -105,9 +98,7 @@ public class ImageFileProcessor {
                 int width = imageFile.getWidth();
                 int height = imageFile.getHeight();
                 ImageFile imageFile2 = getConvertedImageFile(objectResult, "webp", width, height);
-                if (imageFile2 != null) {
-                    list.add(imageFile2);
-                }
+                list.add(imageFile2);
             }
         }
 
@@ -115,16 +106,23 @@ public class ImageFileProcessor {
         return new UploadFileRet(objectId, null);
     }
 
-    private ImageFile getConvertedImageFile(ObjectResult objectResult, String format, int width, int height) {
+    private ImageFile getConvertedImageFile(ObjectResult objectResult, String format, int width, int height) throws Exception {
         String imageFileId = objectResult.getObjectId();
         String originalObjectName = objectResult.getObjectName();
         String absolutePath = objectResult.getAbsolutePath();
         File srcFile = new File(absolutePath);
-
         String contentId = UUID.randomUUID().toString().replace("-", "");
         String destPath = fileStoreService.genFilePath(contentId, srcFile.length(), "."+format);
         File destFile = new File(destPath);
-        try {
+
+        String srcFormat = ImageOps.getFormat(srcFile);
+        if (srcFormat.equals("png")) {
+            if ("jpeg".equals(format) || "webp".equals(format)) {
+                ImageOps.convertPng(srcFile, destFile, format);
+            } else {
+                ImageOps.convert2thumbnail(srcFile, destFile, width, height);
+            }
+        } else {
             if ("jpeg".equals(format)) {
                 ImageOps.convert2jpeg(srcFile, destFile);
             } else if ("webp".equals(format)) {
@@ -132,19 +130,17 @@ public class ImageFileProcessor {
             } else {
                 ImageOps.convert2thumbnail(srcFile, destFile, width, height);
             }
-
-            if (destFile.exists()) {
-                ObjectResult objectResult1 = saveImage(originalObjectName, contentId, "."+format, destFile);
-                String objectName1 = objectResult1.getObjectName();
-                String objectId1 = objectResult1.getObjectId();
-                String objectUrl1 = objectNameService.getObjectUrl(objectName1);
-                return new ImageFile(imageFileId, objectId1, format, objectUrl1, width, height);
-            }
-        } catch (Exception e) {
-            e.printStackTrace();
         }
 
-        return null;
+        if (destFile.exists()) {
+            ObjectResult objectResult1 = saveImage(originalObjectName, contentId, "."+format, destFile);
+            String objectName1 = objectResult1.getObjectName();
+            String objectId1 = objectResult1.getObjectId();
+            String objectUrl1 = objectNameService.getObjectUrl(objectName1);
+            return new ImageFile(imageFileId, objectId1, format, objectUrl1, width, height);
+        } else {
+            throw new Exception("image conversion failed");
+        }
     }
 
     private ImageFile getThumbnailFile(ObjectResult objectResult, int width, int height) {

+ 5 - 5
oss-store/src/main/java/cn/reghao/oss/store/task/processor/VideoFileProcessor.java

@@ -37,7 +37,7 @@ public class VideoFileProcessor {
         this.putObjectService = putObjectService;
     }
 
-    public UploadFileRet process(ObjectResult objectResult) {
+    public UploadFileRet process(ObjectResult objectResult) throws Exception {
         String objectName = objectResult.getObjectName();
         String objectId = objectResult.getObjectId();
         String videoFileId = objectId;
@@ -68,14 +68,14 @@ public class VideoFileProcessor {
         String absolutePath = objectResult.getAbsolutePath();
         MediaProps mediaProps = FFmpegWrapper.getMediaProps(absolutePath);
         if (mediaProps == null) {
-            log.error("{} 的 FFmpeg 媒体信息为 null", objectName);
-            return null;
+            String errMsg = String.format("%s 的 FFmpeg 媒体信息为 null", objectName);
+            throw new Exception(errMsg);
         }
 
         VideoProps videoProps = mediaProps.getVideoProps();
         if (videoProps == null) {
-            log.error("{} 的 FFmpeg 视频信息为 null", objectName);
-            return null;
+            String errMsg = String.format("%s 的 FFmpeg 视频信息为 null", objectName);
+            throw new Exception(errMsg);
         }
 
         String videoCodec = videoProps.getCodecName();