上传文件添加md5

This commit is contained in:
cjw 2024-06-04 15:56:26 +08:00
parent e095e4e5a2
commit 54a1c30e3d
3 changed files with 22 additions and 8 deletions

View File

@ -38,8 +38,8 @@ public class SysOssResourceController extends BaseController {
/**
* 查询请填写功能名称列表
*/
@SaCheckPermission("file:resource:list")
@GetMapping("/list")
@SaCheckPermission("file:resource:pageList")
@GetMapping("/pageList")
public TableDataInfo<SysOssResourceVo> list(SysOssResourceBo bo, PageQuery pageQuery) {
return sysOssResourceService.queryPageList(bo, pageQuery);
}

View File

@ -76,6 +76,7 @@ public class SysOssResourceServiceImpl implements ISysOssResourceService {
private LambdaQueryWrapper<SysOssResource> buildQueryWrapper(SysOssResourceBo bo) {
Map<String, Object> params = bo.getParams();
LambdaQueryWrapper<SysOssResource> lqw = Wrappers.lambdaQuery();
return lqw;
}

View File

@ -36,6 +36,7 @@ import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
@ -193,18 +194,19 @@ public class SysOssServiceImpl implements ISysOssService, OssService {
String suffix = StringUtils.substring(originalfileName, originalfileName.lastIndexOf("."), originalfileName.length());
OssClient storage = OssFactory.instance();
UploadResult uploadResult;
String md5;
try {
String md5 = DigestUtils.md5Hex(file.getInputStream());
md5 = DigestUtils.md5Hex(file.getInputStream());
Long ossId = this.getIdByMd5(md5);
if(ObjectUtil.isNotNull(ossId)){
return SpringUtils.getAopProxy(this).getById(ossId);
if (ObjectUtil.isNotNull(ossId)) {
return SpringUtils.getAopProxy(this).getById(ossId);
}
uploadResult = storage.uploadSuffix(file.getBytes(), suffix);
} catch (IOException e) {
throw new ServiceException(e.getMessage());
}
// 保存文件信息
return buildResultEntity(originalfileName, suffix, storage.getConfigKey(), uploadResult);
return buildResultEntity(originalfileName, md5, suffix, storage.getConfigKey(), uploadResult);
}
/**
@ -218,19 +220,30 @@ public class SysOssServiceImpl implements ISysOssService, OssService {
String originalfileName = file.getName();
String suffix = StringUtils.substring(originalfileName, originalfileName.lastIndexOf("."), originalfileName.length());
OssClient storage = OssFactory.instance();
String md5;
try (FileInputStream fileInputStream = new FileInputStream(file)) {
md5 = DigestUtils.md5Hex(fileInputStream);
Long ossId = this.getIdByMd5(md5);
if (ObjectUtil.isNotNull(ossId)) {
return SpringUtils.getAopProxy(this).getById(ossId);
}
} catch (IOException e) {
throw new ServiceException(e.getMessage());
}
UploadResult uploadResult = storage.uploadSuffix(file, suffix);
// 保存文件信息
return buildResultEntity(originalfileName, suffix, storage.getConfigKey(), uploadResult);
return buildResultEntity(originalfileName, md5, suffix, storage.getConfigKey(), uploadResult);
}
@NotNull
private SysOssVo buildResultEntity(String originalfileName, String suffix, String configKey, UploadResult uploadResult) {
private SysOssVo buildResultEntity(String originalfileName, String md5, String suffix, String configKey, UploadResult uploadResult) {
SysOss oss = new SysOss();
oss.setUrl(uploadResult.getUrl());
oss.setFileSuffix(suffix);
oss.setFileName(uploadResult.getFilename());
oss.setOriginalName(originalfileName);
oss.setService(configKey);
oss.setFileMd5(md5);
baseMapper.insert(oss);
SysOssVo sysOssVo = MapstructUtils.convert(oss, SysOssVo.class);
return this.matchingUrl(sysOssVo);