|
|
@@ -4,8 +4,10 @@ import cn.hutool.crypto.digest.DigestUtil;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.ylx.common.constant.Constants;
|
|
|
import com.ylx.common.config.RuoYiConfig;
|
|
|
import com.ylx.common.core.domain.AjaxResult;
|
|
|
+import com.ylx.common.utils.StringUtils;
|
|
|
import com.ylx.common.utils.file.FileUploadUtils;
|
|
|
import com.ylx.common.utils.file.FileUtils;
|
|
|
import com.ylx.framework.config.ServerConfig;
|
|
|
@@ -85,10 +87,10 @@ public class TbFileServiceImpl extends ServiceImpl<TbFileMapper, TbFile> impleme
|
|
|
* @return 如果找到匹配的文件记录则返回该记录,否则返回 null
|
|
|
*/
|
|
|
@Override
|
|
|
- public TbFile getByMd5(String md5) {
|
|
|
+ public List<TbFile> getByMd5(String md5) {
|
|
|
LambdaQueryWrapper<TbFile> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
- queryWrapper.eq(TbFile::getMd5, md5);
|
|
|
- return this.getOne(queryWrapper);
|
|
|
+ queryWrapper.eq(TbFile::getMd5, md5).orderByDesc(TbFile::getCreateTime);
|
|
|
+ return this.list(queryWrapper);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -114,14 +116,14 @@ public class TbFileServiceImpl extends ServiceImpl<TbFileMapper, TbFile> impleme
|
|
|
// 计算文件 MD5 值
|
|
|
String md5 = calculateMD5(file);
|
|
|
// 检查是否已存在相同文件(通过MD5去重)
|
|
|
- TbFile dbFile = this.getByMd5(md5);
|
|
|
- if (null != dbFile) {
|
|
|
+ List<TbFile> dbFiles = this.getByMd5(md5);
|
|
|
+ if (null != dbFiles && !dbFiles.isEmpty()) {
|
|
|
// 文件已存在,直接返回已有文件的访问信息(秒传)
|
|
|
- ajax.put("url", dbFile.getFileUrl());
|
|
|
- ajax.put("newFileName", FileUtils.getName(dbFile.getFileUrl()));
|
|
|
- ajax.put("originalFilename", dbFile.getFileName());
|
|
|
+ ajax.put("url", dbFiles.get(0).getFileUrl());
|
|
|
+ ajax.put("newFileName", FileUtils.getName(dbFiles.get(0).getFileUrl()));
|
|
|
+ ajax.put("originalFilename", dbFiles.get(0).getFileName());
|
|
|
// 封面图URL路径(针对视频文件)
|
|
|
- ajax.put("coverUrl", dbFile.getCoverUrl());
|
|
|
+ ajax.put("coverUrl", dbFiles.get(0).getCoverUrl());
|
|
|
return ajax;
|
|
|
}
|
|
|
|
|
|
@@ -191,14 +193,9 @@ public class TbFileServiceImpl extends ServiceImpl<TbFileMapper, TbFile> impleme
|
|
|
// 计算文件 MD5 值
|
|
|
String md5 = calculateMD5(file);
|
|
|
// 检查是否已存在相同文件(通过 MD5 去重)
|
|
|
- TbFile dbFile = this.getByMd5(md5);
|
|
|
- if (null != dbFile) {
|
|
|
- // 文件已存在,直接返回已有文件的访问信息(秒传)
|
|
|
- ajax.put("url", dbFile.getFileUrl());
|
|
|
- ajax.put("fileName", dbFile.getFileUrl());
|
|
|
- ajax.put("newFileName", FileUtils.getName(dbFile.getFileUrl()));
|
|
|
- ajax.put("originalFilename", dbFile.getFileName());
|
|
|
- return ajax;
|
|
|
+ List<TbFile> dbFiles = this.getByMd5(md5);
|
|
|
+ if (null != dbFiles && !dbFiles.isEmpty()) {
|
|
|
+ deleteExistingSameFile(dbFiles.get(0));
|
|
|
}
|
|
|
|
|
|
// 文件不存在,上传新文件
|
|
|
@@ -226,11 +223,40 @@ public class TbFileServiceImpl extends ServiceImpl<TbFileMapper, TbFile> impleme
|
|
|
this.save(tbFile);
|
|
|
return ajax;
|
|
|
} catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
// 上传失败,返回错误信息
|
|
|
return AjaxResult.error(e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private void deleteExistingSameFile(TbFile dbFile) {
|
|
|
+ deleteLocalProfileFile(dbFile.getFileUrl());
|
|
|
+ if (StringUtils.isNotEmpty(dbFile.getId())) {
|
|
|
+ this.removeById(dbFile.getId());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (StringUtils.isNotEmpty(dbFile.getMd5())) {
|
|
|
+ LambdaQueryWrapper<TbFile> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
+ queryWrapper.eq(TbFile::getMd5, dbFile.getMd5());
|
|
|
+ this.remove(queryWrapper);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void deleteLocalProfileFile(String fileUrl) {
|
|
|
+ if (StringUtils.isEmpty(fileUrl) || !fileUrl.startsWith(Constants.RESOURCE_PREFIX)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ String profile = RuoYiConfig.getProfile();
|
|
|
+ if (StringUtils.isEmpty(profile)) {
|
|
|
+ log.warn("文件根路径未配置,跳过删除旧文件,fileUrl={}", fileUrl);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ String localPath = profile + StringUtils.substringAfter(fileUrl, Constants.RESOURCE_PREFIX);
|
|
|
+ if (!FileUtils.deleteFile(localPath)) {
|
|
|
+ log.warn("旧文件不存在或删除失败,localPath={}", localPath);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 生成封面图片文件到指定路径
|
|
|
* <p>
|