Sfoglia il codice sorgente

MediaFileProcessor#getImageInfo 抛出的 javax.imageio.IIOException 异常在 dubbo 中会出错, 重新包装为 Exception 后再抛出

reghao 7 mesi fa
parent
commit
fe25ca743c

+ 23 - 17
oss-store/src/main/java/cn/reghao/oss/store/task/MediaFileProcessor.java

@@ -20,6 +20,7 @@ import cn.reghao.oss.store.util.FileType;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
 
+import javax.imageio.IIOException;
 import java.io.File;
 import java.time.LocalDateTime;
 import java.util.List;
@@ -101,24 +102,29 @@ public class MediaFileProcessor {
      * @date 2025-08-17 00:08:482
      */
     public ImageInfo getImageInfo(String imageFileId) throws Exception {
-        ObjectMeta objectMeta = objectRepository.getObjectMetaById(imageFileId);
-        if (objectMeta == null) {
-            String errMsg = String.format("%s not exist in oss-store", imageFileId);
-            throw new Exception(errMsg);
+        try {
+            ObjectMeta objectMeta = objectRepository.getObjectMetaById(imageFileId);
+            if (objectMeta == null) {
+                String errMsg = String.format("%s not exist in oss-store", imageFileId);
+                throw new Exception(errMsg);
+            }
+
+            String absolutePath = objectMeta.getAbsolutePath();
+            File file = new File(absolutePath);
+            String format = ImageOps.getFormat(file);
+            ImageOps.Size size = ImageOps.info(new File(absolutePath));
+            int width = size.getWidth();
+            int height = size.getHeight();
+            String objectId = imageFileId;
+            String objectName = objectMeta.getObjectName();
+            long size1 = objectMeta.getSize();
+
+            ImageInfo imageInfo = new ImageInfo(imageFileId, objectId, format, objectName, width, height, size1);
+            return imageInfo;
+        } catch (IIOException iioException) {
+            String msg = iioException.getMessage();
+            throw new Exception(msg);
         }
-
-        String absolutePath = objectMeta.getAbsolutePath();
-        File file = new File(absolutePath);
-        String format = ImageOps.getFormat(file);
-        ImageOps.Size size = ImageOps.info(new File(absolutePath));
-        int width = size.getWidth();
-        int height = size.getHeight();
-        String objectId = imageFileId;
-        String objectName = objectMeta.getObjectName();
-        long size1 = objectMeta.getSize();
-
-        ImageInfo imageInfo = new ImageInfo(imageFileId, objectId, format, objectName, width, height, size1);
-        return imageInfo;
     }
 
     public List<ConvertedImageInfo> getWebpInfos(List<String> imageFileIds) {