diff --git a/im-platform/src/main/java/com/bx/implatform/contant/Constant.java b/im-platform/src/main/java/com/bx/implatform/contant/Constant.java index 3884cd7..d255ba3 100644 --- a/im-platform/src/main/java/com/bx/implatform/contant/Constant.java +++ b/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; + /** * 大群人数上限 */ diff --git a/im-platform/src/main/java/com/bx/implatform/service/impl/FileServiceImpl.java b/im-platform/src/main/java/com/bx/implatform/service/impl/FileServiceImpl.java index 761f2dc..9eb4ce5 100644 --- a/im-platform/src/main/java/com/bx/implatform/service/impl/FileServiceImpl.java +++ b/im-platform/src/main/java/com/bx/implatform/service/impl/FileServiceImpl.java @@ -60,6 +60,8 @@ public class FileServiceImpl extends ServiceImpl 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 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 imple LambdaQueryWrapper 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 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); + } + } }