Kaynağa Gözat

文件名长度限制128字符

xsx 4 ay önce
ebeveyn
işleme
8f355d5a73

+ 5 - 0
im-platform/src/main/java/com/bx/implatform/contant/Constant.java

@@ -15,6 +15,11 @@ public final class Constant {
      */
     public static final Long MAX_FILE_SIZE = 20 * 1024 * 1024L;
 
+    /**
+     * 最大文件名长度
+     */
+    public static final Long MAX_FILE_NAME_LENGTH = 128L;
+
     /**
      * 大群人数上限
      */

+ 10 - 0
im-platform/src/main/java/com/bx/implatform/service/impl/FileServiceImpl.java

@@ -60,6 +60,8 @@ public class FileServiceImpl extends ServiceImpl<FileInfoMapper, FileInfo> imple
     public String uploadFile(MultipartFile file) {
         try {
             Long userId = SessionContext.getSession().getUserId();
+            // 文件名长度校验
+            checkFileNameLength(file);
             // 大小校验
             if (file.getSize() > Constant.MAX_FILE_SIZE) {
                 throw new GlobalException(ResultCode.PROGRAM_ERROR, "文件大小不能超过20M");
@@ -95,6 +97,8 @@ public class FileServiceImpl extends ServiceImpl<FileInfoMapper, FileInfo> imple
     public UploadImageVO uploadImage(MultipartFile file, Boolean isPermanent,Long thumbSize) {
         try {
             Long userId = SessionContext.getSession().getUserId();
+            // 文件名长度校验
+            checkFileNameLength(file);
             // 大小校验
             if (file.getSize() > Constant.MAX_IMAGE_SIZE) {
                 throw new GlobalException(ResultCode.PROGRAM_ERROR, "图片大小不能超过20M");
@@ -170,6 +174,7 @@ public class FileServiceImpl extends ServiceImpl<FileInfoMapper, FileInfo> imple
         LambdaQueryWrapper<FileInfo> wrapper = Wrappers.lambdaQuery();
         wrapper.eq(FileInfo::getMd5, md5);
         wrapper.eq(FileInfo::getFileType, fileType);
+        wrapper.last("limit 1");
         return getOne(wrapper);
     }
 
@@ -199,4 +204,9 @@ public class FileServiceImpl extends ServiceImpl<FileInfoMapper, FileInfo> imple
         this.save(fileInfo);
     }
 
+    private void checkFileNameLength(MultipartFile file){
+        if(file.getOriginalFilename().length() > Constant.MAX_FILE_NAME_LENGTH){
+            throw new GlobalException("文件名长度不能超过" + Constant.MAX_FILE_NAME_LENGTH);
+        }
+    }
 }