瀏覽代碼

使用 enum 表示对象类型

reghao 2 年之前
父節點
當前提交
8aaffdd6f9

+ 11 - 8
dfs-store/src/main/java/cn/reghao/dfs/store/task/FileProcessor.java

@@ -2,6 +2,7 @@ package cn.reghao.dfs.store.task;
 
 import cn.reghao.dfs.store.model.vo.ObjectResult;
 import cn.reghao.jutil.jdk.thread.ThreadPoolWrapper;
+import cn.reghao.oss.api.constant.ObjectType;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
 
@@ -26,20 +27,22 @@ public class FileProcessor {
     public void process(ObjectResult objectResult) {
         String objectName = objectResult.getObjectName();
         String objectId = objectResult.getObjectId();
-        int fileType = objectResult.getFileType();
         String absolutePath = objectResult.getAbsolutePath();
-        switch (fileType) {
-            case 1001:
+        
+        int fileType = objectResult.getFileType();
+        ObjectType objectType = ObjectType.getByCode(fileType);
+        switch (objectType) {
+            case Image:
                 imageFileProcessor.process(objectName, objectId, absolutePath);
                 break;
-            case 1002:
+            case Video:
                 videoFileProcessor.process(objectName, objectId, absolutePath);
                 break;
-            case 1003:
-                break;
-            case 1004:
-                break;
+            case Audio:
+            case Text:
+            case Other:
             default:
+                log.info("{} 类型的 {} 文件暂时无法处理", objectType.name(), objectName);
         }
     }
 }

+ 1 - 1
dfs-store/src/main/java/cn/reghao/dfs/store/task/VideoFileProcessor.java

@@ -65,7 +65,7 @@ public class VideoFileProcessor {
         int height = videoProps.getCodedHeight().intValue();
         boolean horizontal = width>height;
         int duration = videoProps.getDuration().intValue();
-        VideoFile videoFile = new VideoFile(videoFileId, objectName, horizontal, duration);
+        VideoFile videoFile = new VideoFile(videoFileId, horizontal, duration);
 
         AudioProps audioProps = mediaProps.getAudioProps();
         if (audioProps == null) {

+ 6 - 5
dfs-store/src/main/java/cn/reghao/dfs/store/util/FileType.java

@@ -1,6 +1,7 @@
 package cn.reghao.dfs.store.util;
 
 import cn.reghao.jutil.jdk.shell.Shell;
+import cn.reghao.oss.api.constant.ObjectType;
 import cn.reghao.oss.api.constant.VideoUrlType;
 
 /**
@@ -14,17 +15,17 @@ public class FileType {
     }
 
     public static int getFileType(String contentType) {
-        int fileType = 1005;
+        int fileType = ObjectType.Other.getCode();
         if (contentType == null) {
             return fileType;
         } else if (contentType.startsWith("image")) {
-            fileType = 1001;
+            fileType = ObjectType.Image.getCode();
         } else if (contentType.startsWith("video")) {
-            fileType = 1002;
+            fileType = ObjectType.Video.getCode();
         } else if (contentType.startsWith("audio")) {
-            fileType = 1003;
+            fileType = ObjectType.Audio.getCode();
         } else if (contentType.startsWith("text")) {
-            fileType = 1004;
+            fileType = ObjectType.Text.getCode();
         }
         return fileType;
     }

+ 37 - 0
oss-api/src/main/java/cn/reghao/oss/api/constant/ObjectType.java

@@ -0,0 +1,37 @@
+package cn.reghao.oss.api.constant;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author reghao
+ * @date 2023-06-13 15:09:09
+ */
+public enum ObjectType {
+    Dir(1000),
+    Image(1001),
+    Video(1002),
+    Audio(1003),
+    Text(1004),
+    Other(1005);
+
+    private final int code;
+    ObjectType(int code) {
+        this.code = code;
+    }
+
+    private static Map<Integer, ObjectType> map = new HashMap<>();
+    static {
+        for (ObjectType type : ObjectType.values()) {
+            map.put(type.code, type);
+        }
+    }
+
+    public static ObjectType getByCode(int code) {
+        return map.get(code);
+    }
+
+    public int getCode() {
+        return code;
+    }
+}