diff --git a/nxhs-service/src/main/java/cc/yunxi/controller/FileController.java b/nxhs-service/src/main/java/cc/yunxi/controller/FileController.java index 291fa7b..4feb2ef 100644 --- a/nxhs-service/src/main/java/cc/yunxi/controller/FileController.java +++ b/nxhs-service/src/main/java/cc/yunxi/controller/FileController.java @@ -3,9 +3,12 @@ package cc.yunxi.controller; import cc.yunxi.common.domain.CommonResult; import cc.yunxi.common.exception.BizIllegalException; import cc.yunxi.common.utils.FileUtils; +import cc.yunxi.domain.dto.UserDTO; import cc.yunxi.domain.vo.file.FileUploadReqVO; import cc.yunxi.domain.vo.file.FileUploadRespVO; import cc.yunxi.service.IFileService; +import cc.yunxi.utils.UserContext; +import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; diff --git a/nxhs-service/src/main/java/cc/yunxi/domain/po/File.java b/nxhs-service/src/main/java/cc/yunxi/domain/po/File.java new file mode 100644 index 0000000..d1caa78 --- /dev/null +++ b/nxhs-service/src/main/java/cc/yunxi/domain/po/File.java @@ -0,0 +1,61 @@ +package cc.yunxi.domain.po; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; + +import java.time.LocalDateTime; +import java.util.Date; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.*; +import lombok.experimental.Accessors; + +/** + *

+ * 文件管理表 + *

+ * + * @author ccongli + * @since 2024-03-12 10:12:38 + */ +@Data +@TableName("nx_file") +@Accessors(chain = true) +@ApiModel(value = "File对象", description = "文件管理表") +public class File { + + @ApiModelProperty("主键id") + @TableId(value = "id", type = IdType.ASSIGN_ID) + private String id; + + @ApiModelProperty("文件名称") + @TableField("filename") + private String filename; + + @ApiModelProperty("访问url") + @TableField("url") + private String url; + + @ApiModelProperty("文件大小") + @TableField("size") + private String size; + + @ApiModelProperty("文件散列名") + @TableField("hash_val") + private String hashVal; + + @ApiModelProperty("来源 1 app用户 2 pc系统") + @TableField("source") + private Integer source; + + @ApiModelProperty("创建时间") + @TableField("f_creator_time") + private LocalDateTime creatorTime; + + @ApiModelProperty("创建用户") + @TableField("f_creator_user_id") + private String creatorUserId; + +} diff --git a/nxhs-service/src/main/java/cc/yunxi/domain/vo/file/FileUploadReqVO.java b/nxhs-service/src/main/java/cc/yunxi/domain/vo/file/FileUploadReqVO.java index 321f8b2..9aab8a8 100644 --- a/nxhs-service/src/main/java/cc/yunxi/domain/vo/file/FileUploadReqVO.java +++ b/nxhs-service/src/main/java/cc/yunxi/domain/vo/file/FileUploadReqVO.java @@ -15,9 +15,9 @@ public class FileUploadReqVO { @NotNull(message = "未上传资源") private MultipartFile file; -// @ApiModelProperty(value = "业务类型", required = true) -// @NotNull(message = "文件附件不能为空") -// private String fileBusinessType; +// @ApiModelProperty(value = "上传密钥", required = true, hidden = true, example = "8bd2aa89033ead51c505e44994e42189") +// @NotNull(message = "上传密钥") +// private String secretKey; // @ApiModelProperty(value = "自定义文件路径", hidden = true, example = "/xxx/yyyy/zzz") // @NotNull(message = "文件路径不能为空") diff --git a/nxhs-service/src/main/java/cc/yunxi/interceptor/LoginInterceptor.java b/nxhs-service/src/main/java/cc/yunxi/interceptor/LoginInterceptor.java index 4e65a17..0cca125 100644 --- a/nxhs-service/src/main/java/cc/yunxi/interceptor/LoginInterceptor.java +++ b/nxhs-service/src/main/java/cc/yunxi/interceptor/LoginInterceptor.java @@ -10,6 +10,7 @@ import cc.yunxi.utils.UserContext; import cn.hutool.core.util.ObjectUtil; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerInterceptor; @@ -25,11 +26,17 @@ public class LoginInterceptor implements HandlerInterceptor { private final RedisTool redisTool; + @Value("${nxhs.adminKey}") + private String adminKey; + @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // 1.获取请求头中的 token String token = request.getHeader("authorization"); log.info("request token = {}", token); + if (isAdmin(token)) { + return true; + } if (ObjectUtil.isEmpty(token)) { throw new UnauthorizedException("请登录后访问!"); } @@ -53,4 +60,9 @@ public class LoginInterceptor implements HandlerInterceptor { // 清理用户 UserContext.removeUser(); } + + + private boolean isAdmin(String token) { + return adminKey.equals(token); + } } diff --git a/nxhs-service/src/main/java/cc/yunxi/mapper/FileMapper.java b/nxhs-service/src/main/java/cc/yunxi/mapper/FileMapper.java new file mode 100644 index 0000000..5bb842a --- /dev/null +++ b/nxhs-service/src/main/java/cc/yunxi/mapper/FileMapper.java @@ -0,0 +1,18 @@ +package cc.yunxi.mapper; + +import cc.yunxi.domain.po.File; +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import org.apache.ibatis.annotations.Mapper; + +/** + *

+ * 文件管理表 Mapper 接口 + *

+ * + * @author ccongli + * @since 2024-03-12 10:12:38 + */ +@Mapper +public interface FileMapper extends BaseMapper { + +} diff --git a/nxhs-service/src/main/java/cc/yunxi/service/IFileService.java b/nxhs-service/src/main/java/cc/yunxi/service/IFileService.java index 43d70ef..4143a26 100644 --- a/nxhs-service/src/main/java/cc/yunxi/service/IFileService.java +++ b/nxhs-service/src/main/java/cc/yunxi/service/IFileService.java @@ -1,8 +1,10 @@ package cc.yunxi.service; +import cc.yunxi.domain.po.File; import cc.yunxi.domain.vo.file.FileUploadReqVO; import cc.yunxi.domain.vo.file.FileUploadRespVO; +import com.baomidou.mybatisplus.extension.service.IService; import javax.validation.Valid; @@ -15,8 +17,7 @@ import javax.validation.Valid; * @author ccongli * @since 2024-02-28 06:08:09 */ -public interface IFileService { - +public interface IFileService extends IService { /** * 上传文件 diff --git a/nxhs-service/src/main/java/cc/yunxi/service/impl/FileServiceImpl.java b/nxhs-service/src/main/java/cc/yunxi/service/impl/FileServiceImpl.java index 02a3d45..2501998 100644 --- a/nxhs-service/src/main/java/cc/yunxi/service/impl/FileServiceImpl.java +++ b/nxhs-service/src/main/java/cc/yunxi/service/impl/FileServiceImpl.java @@ -2,15 +2,21 @@ package cc.yunxi.service.impl; import cc.yunxi.common.exception.BizIllegalException; import cc.yunxi.common.utils.FileUtils; +import cc.yunxi.domain.po.File; import cc.yunxi.domain.vo.file.FileUploadReqVO; import cc.yunxi.domain.vo.file.FileUploadRespVO; import cc.yunxi.enums.FileStyleEnum; +import cc.yunxi.mapper.FileMapper; import cc.yunxi.service.IFileService; +import cc.yunxi.utils.UserContext; import cn.hutool.core.io.FileTypeUtil; import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.unit.DataSizeUtil; +import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; +import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; @@ -19,12 +25,13 @@ import org.springframework.stereotype.Service; import org.springframework.validation.annotation.Validated; import org.springframework.web.multipart.MultipartFile; +import javax.annotation.Resource; import java.io.BufferedOutputStream; -import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.InetAddress; import java.net.UnknownHostException; +import java.time.LocalDateTime; import java.util.List; import java.util.Optional; @@ -39,7 +46,7 @@ import java.util.Optional; @Service @Validated @Slf4j -public class FileServiceImpl implements IFileService { +public class FileServiceImpl extends ServiceImpl implements IFileService { @Value("${nxhs.upload.basePath}") private String basePath; @@ -58,6 +65,18 @@ public class FileServiceImpl implements IFileService { @Autowired private Environment environment; + @Resource + private FileMapper fileMapper; + + /** + * 通过hash值查找File + * @param hashName + * @return File + */ + private File getFileInfoByHashVal(String hashName) { + return new LambdaQueryChainWrapper<>(fileMapper).eq(File::getHashVal, hashName).one(); + } + @Override public FileUploadRespVO uploadFile(FileUploadReqVO uploadReqVO) { return upload(uploadReqVO.getFile(), FileStyleEnum.DOC); @@ -73,7 +92,7 @@ public class FileServiceImpl implements IFileService { @Override public byte[] download(String url) { // 默认为本地存储拉取 todo - String filePath = basePath + File.separator + url; + String filePath = basePath + java.io.File.separator + url; if (!FileUtil.exist(filePath)) { throw new BizIllegalException("下载资源不存在"); } @@ -106,21 +125,38 @@ public class FileServiceImpl implements IFileService { } } // 生成md5文件名 - String fileName = FileUtils.generateName(content, originalName); - // 判断文件是否存在,如果存在直接返回 todo -// path = Optional.ofNullable(path).orElse(basePath) + File.separator; - // 确定上传路径 - String URI = basePath + fileStyleEnum.toString().toLowerCase() + "/" + fileName; - String fullPath = System.getProperty("user.dir") + URI; - // 上传到文件存储位置 - FileUtil.writeBytes(content, fullPath); + String hashName = FileUtils.generateName(content, originalName); + // 判断文件是否存在,如果存在直接返回 FileUploadRespVO uploadRespVO = new FileUploadRespVO(); - uploadRespVO.setName(originalName); - uploadRespVO.setField(fileName); -// uploadRespVO.setUrl(getUrl(URI)); - uploadRespVO.setUrl(URI); + File fileInfo = getFileInfoByHashVal(hashName); + if (fileInfo != null) { + uploadRespVO.setName(fileInfo.getFilename()); + uploadRespVO.setUrl(fileInfo.getUrl()); + uploadRespVO.setField(fileInfo.getHashVal()); + } else { + // 确定上传路径 + String URI = basePath + fileStyleEnum.toString().toLowerCase() + "/" + hashName; + String fullPath = System.getProperty("user.dir") + URI; + // 上传到文件存储位置 + FileUtil.writeBytes(content, fullPath); + uploadRespVO.setName(originalName); + uploadRespVO.setField(hashName); +// uploadRespVO.setUrl(getUrl(URI)); + uploadRespVO.setUrl(URI); + // 判断文件来源 + String userId = UserContext.getUser().getId(); + Integer source = StrUtil.isNotEmpty(userId) ? 1 : 2; + // 入库操作 + fileInfo = new File().setUrl(URI) + .setFilename(originalName) + .setHashVal(hashName) + .setSize(DataSizeUtil.format(file.getSize())) + .setSource(source) + .setCreatorTime(LocalDateTime.now()) + .setCreatorUserId(userId); + this.save(fileInfo); + } return uploadRespVO; - // 入库操作 todo } catch (IOException exception) { throw new BizIllegalException("上传失败"); } diff --git a/nxhs-service/src/main/resources/application.yml b/nxhs-service/src/main/resources/application.yml index 8ac8b90..35b897a 100644 --- a/nxhs-service/src/main/resources/application.yml +++ b/nxhs-service/src/main/resources/application.yml @@ -74,6 +74,7 @@ nxhs: - /api/common/shlogin - /api/test/** - /api/file/download + adminKey: 8bd2aa89033ead51c505e44994e42189 # 后台接口访问Key upload: basePath: /upload/ file: diff --git a/nxhs-service/src/main/resources/mapper/FileMapper.xml b/nxhs-service/src/main/resources/mapper/FileMapper.xml new file mode 100644 index 0000000..745cb9a --- /dev/null +++ b/nxhs-service/src/main/resources/mapper/FileMapper.xml @@ -0,0 +1,5 @@ + + + + +