Browse Source

用户模块 上传头像接口增加文件类型大小判断(图片类型+2M限制)

songfuqiang 2 years ago
parent
commit
29e5493e9a

+ 33 - 0
backend-core/src/main/java/cn/kdan/pdf/backend/core/service/impl/AvatarServiceImpl.java

@@ -10,6 +10,8 @@ import cn.kdan.pdf.backend.core.pojo.app.UpTokenVo;
 import cn.kdan.pdf.backend.core.properties.OssProperties;
 import cn.kdan.pdf.backend.core.service.AvatarService;
 import cn.kdan.pdf.backend.core.service.MemberService;
+import cn.kdan.pdf.backend.core.utils.CommonBusinessUtils;
+import exception.BackendRuntimeException;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -18,6 +20,7 @@ import org.springframework.util.CollectionUtils;
 import org.springframework.web.multipart.MultipartFile;
 import utils.CommonUtils;
 
+import javax.activation.MimetypesFileTypeMap;
 import javax.annotation.Resource;
 import java.io.IOException;
 import java.util.Date;
@@ -40,6 +43,14 @@ public class AvatarServiceImpl implements AvatarService {
     @Autowired
     private OssProperties ossProperties;
 
+    // 图片文件类型
+    public static String IMG_TYPE_PNG = "PNG";
+    public static String IMG_TYPE_JPG = "JPG";
+    public static String IMG_TYPE_JPEG = "JPEG";
+    public static String IMG_TYPE_DMG = "BMP";
+    public static String IMG_TYPE_GIF = "GIF";
+    public static String IMG_TYPE_SVG = "SVG";
+
 
     @Override
     public List<Avatars> list() {
@@ -68,6 +79,7 @@ public class AvatarServiceImpl implements AvatarService {
     @Override
     public UpTokenVo picUpload(MultipartFile file) {
         UpTokenVo upTokenVo = new UpTokenVo();
+        checkFile(file);
         Members currentUser = memberService.getCurrentUser();
         String memberId = currentUser.getId();
 
@@ -117,4 +129,25 @@ public class AvatarServiceImpl implements AvatarService {
         return upTokenVo;
     }
 
+    /**
+     * 检查文件类型大小等
+     * @param file
+     */
+    private void checkFile(MultipartFile file) {
+        long size = file.getSize();
+        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
+        if(!(IMG_TYPE_DMG.equals(suffix.toUpperCase()) ||
+                IMG_TYPE_GIF.equals(suffix.toUpperCase()) ||
+                IMG_TYPE_JPEG.equals(suffix.toUpperCase()) ||
+                IMG_TYPE_JPG.equals(suffix.toUpperCase()) ||
+                IMG_TYPE_PNG.equals(suffix.toUpperCase()) ||
+                IMG_TYPE_SVG.equals(suffix.toUpperCase()))){
+            throw new BackendRuntimeException("请上传图片类型的文件!");
+        }
+        boolean flag = CommonBusinessUtils.checkFileSize(size, 2, "M");
+        if(!flag){
+            throw new BackendRuntimeException("上传图片太大!");
+        }
+    }
+
 }

+ 24 - 0
backend-core/src/main/java/cn/kdan/pdf/backend/core/utils/CommonBusinessUtils.java

@@ -54,4 +54,28 @@ public class CommonBusinessUtils {
         return false;
     }
 
+    /**
+     * 判断文件大小
+     * @param len 文件长度
+     * @param size 限制大小
+     * @param unit 限制单位(B,K,M,G)
+     * @return 是否结果大于
+     */
+    public static boolean checkFileSize(Long len, int size, String unit) {
+        double fileSize = 0;
+        if ("B".equals(unit.toUpperCase())) {
+            fileSize = (double) len;
+        } else if ("K".equals(unit.toUpperCase())) {
+            fileSize = (double) len / 1024;
+        } else if ("M".equals(unit.toUpperCase())) {
+            fileSize = (double) len / 1048576;
+        } else if ("G".equals(unit.toUpperCase())) {
+            fileSize = (double) len / 1073741824;
+        }
+        if (fileSize > size) {
+            return false;
+        }
+        return true;
+    }
+
 }