文件模块已完成
This commit is contained in:
parent
e1458e937b
commit
ef88f4be1e
|
@ -0,0 +1,20 @@
|
||||||
|
package com.greenorange.promotion.annotation;
|
||||||
|
|
||||||
|
import com.greenorange.promotion.model.enums.FileUploadBizEnum;
|
||||||
|
import jakarta.validation.ConstraintValidator;
|
||||||
|
import jakarta.validation.ConstraintValidatorContext;
|
||||||
|
|
||||||
|
// 枚举校验器
|
||||||
|
public class FileEnumValidator implements ConstraintValidator<UserEnumValue, String> {
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void initialize(UserEnumValue constraintAnnotation) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isValid(String value, ConstraintValidatorContext context) {
|
||||||
|
return FileUploadBizEnum.getEnumByValue(value) != null;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,20 @@
|
||||||
|
package com.greenorange.promotion.annotation;
|
||||||
|
|
||||||
|
import jakarta.validation.Constraint;
|
||||||
|
import jakarta.validation.Payload;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
// 自定义校验注解
|
||||||
|
@Constraint(validatedBy = UserEnumValidator.class)
|
||||||
|
@Target({ ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD })
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
public @interface FileEnumValue {
|
||||||
|
String message() default "文件业务类型错误"; // 错误信息
|
||||||
|
Class<?>[] groups() default {}; // 组别
|
||||||
|
Class<? extends Payload>[] payload() default {}; // 负载
|
||||||
|
Class<? extends Enum<?>> enumClass(); // 枚举类类型
|
||||||
|
}
|
|
@ -7,16 +7,16 @@ import jakarta.validation.ConstraintValidatorContext;
|
||||||
|
|
||||||
|
|
||||||
// 枚举校验器
|
// 枚举校验器
|
||||||
public class EnumValidator implements ConstraintValidator<EnumValue, String> {
|
public class UserEnumValidator implements ConstraintValidator<UserEnumValue, String> {
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void initialize(EnumValue constraintAnnotation) {
|
public void initialize(UserEnumValue constraintAnnotation) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isValid(String value, ConstraintValidatorContext context) {
|
public boolean isValid(String value, ConstraintValidatorContext context) {
|
||||||
return UserRoleEnum.getEnumByValues(value) != null;
|
return UserRoleEnum.getEnumByValue(value) != null;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -9,10 +9,10 @@ import java.lang.annotation.RetentionPolicy;
|
||||||
import java.lang.annotation.Target;
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
// 自定义校验注解
|
// 自定义校验注解
|
||||||
@Constraint(validatedBy = EnumValidator.class)
|
@Constraint(validatedBy = UserEnumValidator.class)
|
||||||
@Target({ ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD })
|
@Target({ ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD })
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
public @interface EnumValue {
|
public @interface UserEnumValue {
|
||||||
String message() default "无效的用户角色"; // 错误信息
|
String message() default "无效的用户角色"; // 错误信息
|
||||||
Class<?>[] groups() default {}; // 组别
|
Class<?>[] groups() default {}; // 组别
|
||||||
Class<? extends Payload>[] payload() default {}; // 负载
|
Class<? extends Payload>[] payload() default {}; // 负载
|
|
@ -57,7 +57,7 @@ public class PermissionCheck {
|
||||||
// 接口的权限
|
// 接口的权限
|
||||||
String mustRole = requiresPermission.mustRole();
|
String mustRole = requiresPermission.mustRole();
|
||||||
// 获取接口权限的枚举类
|
// 获取接口权限的枚举类
|
||||||
UserRoleEnum mustUserRoleEnum = UserRoleEnum.getEnumByValues(mustRole);
|
UserRoleEnum mustUserRoleEnum = UserRoleEnum.getEnumByValue(mustRole);
|
||||||
ThrowUtils.throwIf(mustUserRoleEnum == null, ErrorCode.NO_AUTH_ERROR);
|
ThrowUtils.throwIf(mustUserRoleEnum == null, ErrorCode.NO_AUTH_ERROR);
|
||||||
// 获取用户权限
|
// 获取用户权限
|
||||||
String token = request.getHeader("Authorization");
|
String token = request.getHeader("Authorization");
|
||||||
|
@ -79,7 +79,7 @@ public class PermissionCheck {
|
||||||
|
|
||||||
// 获取用户权限的枚举类
|
// 获取用户权限的枚举类
|
||||||
String userRole = userInfo.getUserRole();
|
String userRole = userInfo.getUserRole();
|
||||||
UserRoleEnum userRoleEnum = UserRoleEnum.getEnumByValues(userRole);
|
UserRoleEnum userRoleEnum = UserRoleEnum.getEnumByValue(userRole);
|
||||||
|
|
||||||
// 接口权限只能是 ADMIN 或者 BOSS,用户权限是 ADMIN 或者 BOSS,USER,BAN
|
// 接口权限只能是 ADMIN 或者 BOSS,用户权限是 ADMIN 或者 BOSS,USER,BAN
|
||||||
// 校验角色
|
// 校验角色
|
||||||
|
|
|
@ -42,7 +42,7 @@ import java.util.List;
|
||||||
* 文件 控制器
|
* 文件 控制器
|
||||||
*/
|
*/
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("fileInfo")
|
@RequestMapping("file")
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Tag(name = "文件管理")
|
@Tag(name = "文件管理")
|
||||||
public class FileInfoController {
|
public class FileInfoController {
|
||||||
|
@ -63,9 +63,9 @@ public class FileInfoController {
|
||||||
@PostMapping("upload")
|
@PostMapping("upload")
|
||||||
@Operation(summary = "Web端文件上传(服务器)", description = "参数:文件添加请求体,权限:管理员,方法名:addFileInfo")
|
@Operation(summary = "Web端文件上传(服务器)", description = "参数:文件添加请求体,权限:管理员,方法名:addFileInfo")
|
||||||
@SysLog(title = "文件管理", content = "Web端文件上传(服务器)")
|
@SysLog(title = "文件管理", content = "Web端文件上传(服务器)")
|
||||||
public BaseResponse<String> uploadFile(@RequestPart("file") MultipartFile multipartFile, UploadFileRequest uploadFileRequest) {
|
public BaseResponse<String> uploadFile(@RequestPart("file") MultipartFile multipartFile, UploadFileRequest uploadFileRequest) throws IOException{
|
||||||
// 校验文件
|
// 校验文件
|
||||||
fileInfoService.validFile(multipartFile);
|
fileInfoService.validFile(multipartFile, uploadFileRequest);
|
||||||
// 文件上传
|
// 文件上传
|
||||||
String view = fileInfoService.uploadFile(multipartFile, uploadFileRequest);
|
String view = fileInfoService.uploadFile(multipartFile, uploadFileRequest);
|
||||||
return ResultUtils.success(view, "上传成功");
|
return ResultUtils.success(view, "上传成功");
|
||||||
|
@ -77,13 +77,13 @@ public class FileInfoController {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 文件下载(服务器)
|
* 文件下载(服务器)
|
||||||
* @param filename 文件名
|
* @param fileName 文件名
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
@GetMapping("/download")
|
@GetMapping("/download/{fileName}")
|
||||||
@Operation(summary = "文件下载(服务器)", description = "参数:文件名,权限:所有人,方法名:downloadFile")
|
@Operation(summary = "文件下载(服务器)", description = "参数:文件名,权限:所有人,方法名:downloadFile")
|
||||||
public void downloadFile(@RequestParam @NotBlank String filename, HttpServletResponse response) throws IOException {
|
public void downloadFile(@PathVariable @NotBlank String fileName, HttpServletResponse response) throws IOException {
|
||||||
fileInfoService.downloadFile(filename, response);
|
fileInfoService.downloadFile(fileName, response);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,10 +1,9 @@
|
||||||
package com.greenorange.promotion.model.dto.fileInfo;
|
package com.greenorange.promotion.model.dto.fileInfo;
|
||||||
|
|
||||||
import com.greenorange.promotion.annotation.EnumValue;
|
import com.greenorange.promotion.annotation.FileEnumValue;
|
||||||
import com.greenorange.promotion.model.enums.FileUploadBizEnum;
|
import com.greenorange.promotion.model.enums.FileUploadBizEnum;
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
import jakarta.validation.constraints.NotBlank;
|
import jakarta.validation.constraints.NotBlank;
|
||||||
import jakarta.validation.constraints.Min;
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
@ -67,10 +66,18 @@ public class FileInfoAddRequest implements Serializable {
|
||||||
/**
|
/**
|
||||||
* 文件业务类型(头像,项目,富文本,默认)
|
* 文件业务类型(头像,项目,富文本,默认)
|
||||||
*/
|
*/
|
||||||
@EnumValue(enumClass = FileUploadBizEnum.class)
|
@FileEnumValue(enumClass = FileUploadBizEnum.class)
|
||||||
@Schema(description = "文件业务类型(头像,项目,富文本,默认)", example = "avatar")
|
@Schema(description = "文件业务类型(头像,项目,富文本,默认)", example = "avatar")
|
||||||
private String biz;
|
private String biz;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件hash值
|
||||||
|
*/
|
||||||
|
@NotBlank(message = "文件hash值不能为空")
|
||||||
|
@Schema(description = "文件hash值", example = "3E8U2AM8")
|
||||||
|
private String hashValue;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Serial
|
@Serial
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
|
@ -1,9 +1,6 @@
|
||||||
package com.greenorange.promotion.model.dto.fileInfo;
|
package com.greenorange.promotion.model.dto.fileInfo;
|
||||||
|
|
||||||
import com.greenorange.promotion.annotation.EnumValue;
|
|
||||||
import com.greenorange.promotion.model.enums.FileUploadBizEnum;
|
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
import jakarta.validation.constraints.NotBlank;
|
|
||||||
import jakarta.validation.constraints.Min;
|
import jakarta.validation.constraints.Min;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
|
@ -29,7 +26,6 @@ public class FileInfoQueryRequest extends PageRequest implements Serializable {
|
||||||
/**
|
/**
|
||||||
* 文件view值
|
* 文件view值
|
||||||
*/
|
*/
|
||||||
@EnumValue(enumClass = FileUploadBizEnum.class)
|
|
||||||
@Schema(description = "文件view值", example = "3E8U2AM8")
|
@Schema(description = "文件view值", example = "3E8U2AM8")
|
||||||
private String fileView;
|
private String fileView;
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
package com.greenorange.promotion.model.dto.fileInfo;
|
package com.greenorange.promotion.model.dto.fileInfo;
|
||||||
|
|
||||||
import com.greenorange.promotion.annotation.EnumValue;
|
import com.greenorange.promotion.annotation.FileEnumValue;
|
||||||
import com.greenorange.promotion.model.enums.FileUploadBizEnum;
|
import com.greenorange.promotion.model.enums.FileUploadBizEnum;
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
import jakarta.validation.constraints.NotBlank;
|
import jakarta.validation.constraints.NotBlank;
|
||||||
|
@ -70,10 +70,17 @@ public class FileInfoUpdateRequest implements Serializable {
|
||||||
/**
|
/**
|
||||||
* 文件业务类型(头像,项目,富文本,默认)
|
* 文件业务类型(头像,项目,富文本,默认)
|
||||||
*/
|
*/
|
||||||
@EnumValue(enumClass = FileUploadBizEnum.class)
|
@FileEnumValue(enumClass = FileUploadBizEnum.class)
|
||||||
@Schema(description = "文件业务类型(头像,项目,富文本,默认)", example = "avatar")
|
@Schema(description = "文件业务类型(头像,项目,富文本,默认)", example = "avatar")
|
||||||
private String biz;
|
private String biz;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件hash值
|
||||||
|
*/
|
||||||
|
@NotBlank(message = "文件hash值不能为空")
|
||||||
|
@Schema(description = "文件hash值", example = "3E8U2AM8")
|
||||||
|
private String hashValue;
|
||||||
|
|
||||||
|
|
||||||
@Serial
|
@Serial
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
package com.greenorange.promotion.model.dto.fileInfo;
|
package com.greenorange.promotion.model.dto.fileInfo;
|
||||||
|
|
||||||
import com.greenorange.promotion.annotation.EnumValue;
|
import com.greenorange.promotion.annotation.FileEnumValue;
|
||||||
import com.greenorange.promotion.model.enums.FileUploadBizEnum;
|
import com.greenorange.promotion.model.enums.FileUploadBizEnum;
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
@ -15,7 +15,7 @@ public class UploadFileRequest implements Serializable {
|
||||||
/**
|
/**
|
||||||
* 文件业务类型(头像,项目,富文本,默认)
|
* 文件业务类型(头像,项目,富文本,默认)
|
||||||
*/
|
*/
|
||||||
@EnumValue(enumClass = FileUploadBizEnum.class)
|
@FileEnumValue(enumClass = FileUploadBizEnum.class)
|
||||||
@Schema(description = "文件业务类型(头像,项目,富文本,默认)", example = "avatar")
|
@Schema(description = "文件业务类型(头像,项目,富文本,默认)", example = "avatar")
|
||||||
private String biz;
|
private String biz;
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
package com.greenorange.promotion.model.dto.project;
|
package com.greenorange.promotion.model.dto.project;
|
||||||
|
|
||||||
import com.greenorange.promotion.annotation.EnumValue;
|
import com.greenorange.promotion.annotation.UserEnumValue;
|
||||||
import com.greenorange.promotion.model.enums.ProjectStatusEnum;
|
import com.greenorange.promotion.model.enums.ProjectStatusEnum;
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
import jakarta.validation.constraints.DecimalMin;
|
import jakarta.validation.constraints.DecimalMin;
|
||||||
|
@ -121,7 +121,7 @@ public class ProjectAddRequest implements Serializable {
|
||||||
/**
|
/**
|
||||||
* 项目状态(项目运行|人数已满|项目暂停)
|
* 项目状态(项目运行|人数已满|项目暂停)
|
||||||
*/
|
*/
|
||||||
@EnumValue(enumClass = ProjectStatusEnum.class)
|
@UserEnumValue(enumClass = ProjectStatusEnum.class)
|
||||||
@Schema(description = "项目状态", example = "项目运行")
|
@Schema(description = "项目状态", example = "项目运行")
|
||||||
private String projectStatus;
|
private String projectStatus;
|
||||||
|
|
||||||
|
|
|
@ -1,12 +1,11 @@
|
||||||
package com.greenorange.promotion.model.dto.user;
|
package com.greenorange.promotion.model.dto.user;
|
||||||
|
|
||||||
import com.greenorange.promotion.annotation.EnumValue;
|
import com.greenorange.promotion.annotation.UserEnumValue;
|
||||||
import com.greenorange.promotion.model.enums.UserRoleEnum;
|
import com.greenorange.promotion.model.enums.UserRoleEnum;
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
import jakarta.validation.constraints.NotBlank;
|
import jakarta.validation.constraints.NotBlank;
|
||||||
import jakarta.validation.constraints.Size;
|
import jakarta.validation.constraints.Size;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import org.hibernate.validator.constraints.Length;
|
|
||||||
|
|
||||||
import java.io.Serial;
|
import java.io.Serial;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
@ -65,7 +64,7 @@ public class UserInfoAddRequest implements Serializable {
|
||||||
/**
|
/**
|
||||||
* 用户角色
|
* 用户角色
|
||||||
*/
|
*/
|
||||||
@EnumValue(enumClass = UserRoleEnum.class)
|
@UserEnumValue(enumClass = UserRoleEnum.class)
|
||||||
@Schema(description = "用户角色", example = "user")
|
@Schema(description = "用户角色", example = "user")
|
||||||
private String userRole;
|
private String userRole;
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
package com.greenorange.promotion.model.dto.user;
|
package com.greenorange.promotion.model.dto.user;
|
||||||
|
|
||||||
import com.greenorange.promotion.annotation.EnumValue;
|
import com.greenorange.promotion.annotation.UserEnumValue;
|
||||||
import com.greenorange.promotion.model.enums.UserRoleEnum;
|
import com.greenorange.promotion.model.enums.UserRoleEnum;
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
import jakarta.validation.constraints.Min;
|
import jakarta.validation.constraints.Min;
|
||||||
|
@ -74,7 +74,7 @@ public class UserInfoUpdateRequest implements Serializable {
|
||||||
/**
|
/**
|
||||||
* 用户角色
|
* 用户角色
|
||||||
*/
|
*/
|
||||||
@EnumValue(enumClass = UserRoleEnum.class)
|
@UserEnumValue(enumClass = UserRoleEnum.class)
|
||||||
@Schema(description = "用户角色", example = "user")
|
@Schema(description = "用户角色", example = "user")
|
||||||
private String userRole;
|
private String userRole;
|
||||||
|
|
||||||
|
|
|
@ -51,6 +51,11 @@ public class FileInfo implements Serializable {
|
||||||
*/
|
*/
|
||||||
private String biz;
|
private String biz;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件hash值
|
||||||
|
*/
|
||||||
|
private String hashValue;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 是否删除
|
* 是否删除
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -39,7 +39,7 @@ public enum UserRoleEnum {
|
||||||
/**
|
/**
|
||||||
* 获取值列表
|
* 获取值列表
|
||||||
*/
|
*/
|
||||||
public static UserRoleEnum getEnumByValues(String value) {
|
public static UserRoleEnum getEnumByValue(String value) {
|
||||||
if (StringUtils.isBlank(value)) {
|
if (StringUtils.isBlank(value)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,6 +56,12 @@ public class FileInfoVO implements Serializable {
|
||||||
@Schema(description = "文件业务类型(头像,项目,富文本,默认)", example = "user_avatar")
|
@Schema(description = "文件业务类型(头像,项目,富文本,默认)", example = "user_avatar")
|
||||||
private String biz;
|
private String biz;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件hash值
|
||||||
|
*/
|
||||||
|
@Schema(description = "文件hash值", example = "3E8U3A")
|
||||||
|
private String hashValue;
|
||||||
|
|
||||||
|
|
||||||
@Serial
|
@Serial
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
|
@ -19,13 +19,13 @@ public interface FileInfoService extends IService<FileInfo> {
|
||||||
/**
|
/**
|
||||||
* 校验文件
|
* 校验文件
|
||||||
*/
|
*/
|
||||||
void validFile(MultipartFile multipartFile);
|
void validFile(MultipartFile multipartFile, UploadFileRequest uploadFileRequest);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取文件保存路径
|
* 获取文件保存路径
|
||||||
*/
|
*/
|
||||||
String uploadFile(MultipartFile multipartFile, UploadFileRequest uploadFileRequest);
|
String uploadFile(MultipartFile multipartFile, UploadFileRequest uploadFileRequest) throws IOException;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -16,7 +16,8 @@ import com.greenorange.promotion.service.file.FileInfoService;
|
||||||
import com.greenorange.promotion.mapper.FileInfoMapper;
|
import com.greenorange.promotion.mapper.FileInfoMapper;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import org.apache.commons.lang3.RandomStringUtils;
|
import org.apache.commons.codec.digest.DigestUtils;
|
||||||
|
import org.apache.commons.lang.RandomStringUtils;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
@ -51,7 +52,11 @@ public class FileInfoServiceImpl extends ServiceImpl<FileInfoMapper, FileInfo>
|
||||||
* 校验文件
|
* 校验文件
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void validFile(MultipartFile multipartFile) {
|
public void validFile(MultipartFile multipartFile, UploadFileRequest uploadFileRequest) {
|
||||||
|
// 获取业务名称
|
||||||
|
String biz = uploadFileRequest.getBiz();
|
||||||
|
FileUploadBizEnum fileUploadBizEnum = FileUploadBizEnum.getEnumByValue(biz);
|
||||||
|
ThrowUtils.throwIf(fileUploadBizEnum == null, ErrorCode.PARAMS_ERROR, "业务名称错误");
|
||||||
// 文件大小
|
// 文件大小
|
||||||
long fileSize = multipartFile.getSize();
|
long fileSize = multipartFile.getSize();
|
||||||
// 文件后缀
|
// 文件后缀
|
||||||
|
@ -67,7 +72,7 @@ public class FileInfoServiceImpl extends ServiceImpl<FileInfoMapper, FileInfo>
|
||||||
* 获取文件保存路径
|
* 获取文件保存路径
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String uploadFile(MultipartFile multipartFile, UploadFileRequest uploadFileRequest) {
|
public String uploadFile(MultipartFile multipartFile, UploadFileRequest uploadFileRequest) throws IOException {
|
||||||
// 获取业务名称
|
// 获取业务名称
|
||||||
String biz = uploadFileRequest.getBiz();
|
String biz = uploadFileRequest.getBiz();
|
||||||
// 获取文件名
|
// 获取文件名
|
||||||
|
@ -76,11 +81,20 @@ public class FileInfoServiceImpl extends ServiceImpl<FileInfoMapper, FileInfo>
|
||||||
String fileType = FileUtil.getSuffix(fileName);
|
String fileType = FileUtil.getSuffix(fileName);
|
||||||
// 获取文件路径
|
// 获取文件路径
|
||||||
// 获取view值
|
// 获取view值
|
||||||
String view = RandomStringUtils.random(8);
|
String view = RandomStringUtils.random(8, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
|
||||||
// 获取文件路径
|
// 获取文件路径
|
||||||
String filePath = String.format("%s/%s", biz, fileName);
|
String filePath = String.format("%s-%s", biz, fileName);
|
||||||
// 获取文件大小
|
// 获取文件大小
|
||||||
Double fileSize = multipartFile.getSize() / 1024.0;
|
Double fileSize = multipartFile.getSize() / 1024.0;
|
||||||
|
fileSize = Double.valueOf(String.format("%.2f", fileSize));
|
||||||
|
// 获取文件哈希值
|
||||||
|
InputStream inputStream = multipartFile.getInputStream();
|
||||||
|
String hashValue = DigestUtils.sha256Hex(inputStream);
|
||||||
|
// 判断是否存在哈希值相同的图片
|
||||||
|
LambdaQueryWrapper<FileInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
|
||||||
|
lambdaQueryWrapper.eq(FileInfo::getHashValue, hashValue);
|
||||||
|
FileInfo fileInfo = this.getOne(lambdaQueryWrapper);
|
||||||
|
if (fileInfo != null) return fileInfo.getFileView();
|
||||||
// 保存文件
|
// 保存文件
|
||||||
FileInfoAddRequest fileInfoAddRequest = FileInfoAddRequest.builder()
|
FileInfoAddRequest fileInfoAddRequest = FileInfoAddRequest.builder()
|
||||||
.name(fileName)
|
.name(fileName)
|
||||||
|
@ -89,8 +103,9 @@ public class FileInfoServiceImpl extends ServiceImpl<FileInfoMapper, FileInfo>
|
||||||
.size(fileSize)
|
.size(fileSize)
|
||||||
.fileView(view)
|
.fileView(view)
|
||||||
.biz(biz)
|
.biz(biz)
|
||||||
|
.hashValue(hashValue)
|
||||||
.build();
|
.build();
|
||||||
FileInfo fileInfo = commonService.copyProperties(fileInfoAddRequest, FileInfo.class);
|
fileInfo = commonService.copyProperties(fileInfoAddRequest, FileInfo.class);
|
||||||
this.save(fileInfo);
|
this.save(fileInfo);
|
||||||
// 创建上传目录,如果不存在
|
// 创建上传目录,如果不存在
|
||||||
File file = new File(UPLOAD_DIR + filePath);
|
File file = new File(UPLOAD_DIR + filePath);
|
||||||
|
@ -107,6 +122,7 @@ public class FileInfoServiceImpl extends ServiceImpl<FileInfoMapper, FileInfo>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 文件下载
|
* 文件下载
|
||||||
* @param filename 业务名称/view值
|
* @param filename 业务名称/view值
|
||||||
|
@ -114,8 +130,8 @@ public class FileInfoServiceImpl extends ServiceImpl<FileInfoMapper, FileInfo>
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void downloadFile(String filename, HttpServletResponse response) throws IOException{
|
public void downloadFile(String filename, HttpServletResponse response) throws IOException{
|
||||||
ThrowUtils.throwIf(!filename.contains("/"), ErrorCode.PARAMS_ERROR);
|
ThrowUtils.throwIf(!filename.contains("-"), ErrorCode.PARAMS_ERROR);
|
||||||
String[] split = filename.split("/");
|
String[] split = filename.split("-");
|
||||||
FileUploadBizEnum fileUploadBizEnum = FileUploadBizEnum.getEnumByValue(split[0]);
|
FileUploadBizEnum fileUploadBizEnum = FileUploadBizEnum.getEnumByValue(split[0]);
|
||||||
ThrowUtils.throwIf(fileUploadBizEnum == null, ErrorCode.PARAMS_ERROR, "业务类型错误");
|
ThrowUtils.throwIf(fileUploadBizEnum == null, ErrorCode.PARAMS_ERROR, "业务类型错误");
|
||||||
LambdaQueryWrapper<FileInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
|
LambdaQueryWrapper<FileInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
|
||||||
|
@ -126,7 +142,7 @@ public class FileInfoServiceImpl extends ServiceImpl<FileInfoMapper, FileInfo>
|
||||||
File file = new File(UPLOAD_DIR + fileInfo.getPath());
|
File file = new File(UPLOAD_DIR + fileInfo.getPath());
|
||||||
// // 设置response的Header
|
// // 设置response的Header
|
||||||
response.setContentType("application/octet-stream");
|
response.setContentType("application/octet-stream");
|
||||||
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(file.getName(), StandardCharsets.UTF_8));
|
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileInfo.getName(), StandardCharsets.UTF_8));
|
||||||
response.setContentLengthLong(file.length()); // 使用 setContentLengthLong 适应大文件
|
response.setContentLengthLong(file.length()); // 使用 setContentLengthLong 适应大文件
|
||||||
|
|
||||||
// 设置缓存相关的 HTTP 头
|
// 设置缓存相关的 HTTP 头
|
||||||
|
|
Loading…
Reference in New Issue
Block a user