优化文件上传配置和校验
This commit is contained in:
@@ -29,6 +29,13 @@ public interface FileBizBaseEnum extends BaseEnum {
|
||||
*/
|
||||
FilePermissionType permission();
|
||||
|
||||
/**
|
||||
* 文件大小限制
|
||||
*
|
||||
* @return 文件大小限制
|
||||
*/
|
||||
long sizeLimit();
|
||||
|
||||
/**
|
||||
* 文件过期时间(小时)
|
||||
*
|
||||
|
||||
@@ -14,7 +14,7 @@ package xtools.app.sys.file.enums;
|
||||
public enum FileBizEnum implements FileBizBaseEnum {
|
||||
|
||||
// 系统通知
|
||||
SYS_NOTICE(-10, "系统通知", "sys-notice-editor-img", FilePermissionType.PUBLIC, 12, "png", "jpg", "jpeg", "gif", "bmp", "webp");
|
||||
SYS_NOTICE(-10, "系统通知", "sys-notice-editor-img", FilePermissionType.PUBLIC, 1024 * 1024 * 5, 12, "png", "jpg", "jpeg", "gif", "bmp", "svg", "webp");
|
||||
|
||||
/**
|
||||
* 编码
|
||||
@@ -36,6 +36,11 @@ public enum FileBizEnum implements FileBizBaseEnum {
|
||||
*/
|
||||
private final FilePermissionType permission;
|
||||
|
||||
/**
|
||||
* 文件大小限制
|
||||
*/
|
||||
private final long sizeLimit;
|
||||
|
||||
/**
|
||||
* 文件过期时间(小时)
|
||||
*/
|
||||
@@ -53,14 +58,16 @@ public enum FileBizEnum implements FileBizBaseEnum {
|
||||
* @param desc 说明
|
||||
* @param bucket 存储桶
|
||||
* @param permission 文件权限
|
||||
* @param sizeLimit 文件大小限制
|
||||
* @param expireTime 文件过期时间(小时)
|
||||
* @param extArr 扩展名
|
||||
*/
|
||||
FileBizEnum(int code, String desc, String bucket, FilePermissionType permission, int expireTime, String... extArr) {
|
||||
FileBizEnum(int code, String desc, String bucket, FilePermissionType permission, long sizeLimit, int expireTime, String... extArr) {
|
||||
this.code = code;
|
||||
this.desc = desc;
|
||||
this.bucket = bucket;
|
||||
this.permission = permission;
|
||||
this.sizeLimit = sizeLimit;
|
||||
this.expireTime = expireTime;
|
||||
this.extArr = extArr;
|
||||
}
|
||||
@@ -115,6 +122,16 @@ public enum FileBizEnum implements FileBizBaseEnum {
|
||||
return permission;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件大小限制
|
||||
*
|
||||
* @return 文件大小限制
|
||||
*/
|
||||
@Override
|
||||
public long sizeLimit() {
|
||||
return sizeLimit;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件过期时间
|
||||
*
|
||||
|
||||
@@ -51,7 +51,7 @@ public interface SysFileOptService {
|
||||
* @return 文件ID
|
||||
*/
|
||||
Long upload(
|
||||
@NonNull String fileName,
|
||||
String fileName,
|
||||
@NonNull InputStream inputStream,
|
||||
long contentLength,
|
||||
@NonNull Long userId,
|
||||
|
||||
@@ -108,7 +108,7 @@ public class SysFileOptServiceImpl implements SysFileOptService, BaseParams {
|
||||
*/
|
||||
@Override
|
||||
public Long upload(
|
||||
@NonNull String fileName,
|
||||
String fileName,
|
||||
@NonNull InputStream inputStream,
|
||||
long contentLength,
|
||||
@NonNull Long userId,
|
||||
@@ -116,8 +116,24 @@ public class SysFileOptServiceImpl implements SysFileOptService, BaseParams {
|
||||
@NonNull FileBizBaseEnum bizType,
|
||||
Long bizId
|
||||
) {
|
||||
// 上传事件
|
||||
// 上传时间
|
||||
Instant uploadTime = Instant.now();
|
||||
|
||||
// 文件名校验
|
||||
if (StringUtils.isBlank(fileName)) {
|
||||
throw new BizError("文件名称不能为空");
|
||||
}
|
||||
// 文件格式校验
|
||||
String suffix = FileUtils.getSuffix(fileName);
|
||||
if (!CheckUtils.suffix(suffix, bizType.extArr())) {
|
||||
throw new BizError("文件格式错误");
|
||||
}
|
||||
// 文件大小限制
|
||||
long sizeLimit = bizType.sizeLimit();
|
||||
if (sizeLimit > CP_NEGATIVE1 && contentLength > sizeLimit) {
|
||||
throw new BizError("文件大小超限");
|
||||
}
|
||||
|
||||
// 计算文件码
|
||||
String md5 = null;
|
||||
String sm3 = null;
|
||||
@@ -127,7 +143,7 @@ public class SysFileOptServiceImpl implements SysFileOptService, BaseParams {
|
||||
byte[] bytes = inputStream.readAllBytes();
|
||||
inputStream = new ByteArrayInputStream(bytes);
|
||||
contentLength = bytes.length;
|
||||
if (contentLength <= 0) {
|
||||
if (contentLength <= CP_NUM0) {
|
||||
throw new BizError("文件长度错误");
|
||||
}
|
||||
byte[] md5Bytes = Md5Utils.encrypt(bytes);
|
||||
@@ -143,11 +159,7 @@ public class SysFileOptServiceImpl implements SysFileOptService, BaseParams {
|
||||
throw new BizError("计算文件码失败");
|
||||
}
|
||||
}
|
||||
// 获取文件后缀
|
||||
String suffix = FileUtils.getSuffix(fileName);
|
||||
if (StringUtils.isBlank(suffix)) {
|
||||
throw new BizError("文件格式错误");
|
||||
}
|
||||
|
||||
// 文件存储器的文件名
|
||||
String filePath = UuidUtils.get() + CP_LINE + fileName;
|
||||
// 获取存储桶
|
||||
|
||||
Reference in New Issue
Block a user