fix(回收站): 文件还原代码优化

This commit is contained in:
马超 2023-04-06 18:53:21 +08:00
parent e15176c48c
commit 4c8898390a
4 changed files with 55 additions and 50 deletions

View File

@ -25,7 +25,6 @@ import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
@ -40,8 +39,6 @@ import java.util.concurrent.Future;
@Component
@Async("asyncTaskExecutor")
public class AsyncTaskComp {
@Resource
IUserFileService userFileService;
@Resource
IRecoveryFileService recoveryFileService;
@ -62,16 +59,15 @@ public class AsyncTaskComp {
public Long getFilePointCount(String fileId) {
LambdaQueryWrapper<UserFile> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(UserFile::getFileId, fileId);
long count = userFileMapper.selectCount(lambdaQueryWrapper);
return count;
return userFileMapper.selectCount(lambdaQueryWrapper);
}
public Future<String> deleteUserFile(String userFileId) {
UserFile userFile = userFileService.getById(userFileId);
UserFile userFile = userFileMapper.selectById(userFileId);
if (userFile.getIsDir() == 1) {
LambdaQueryWrapper<UserFile> userFileLambdaQueryWrapper = new LambdaQueryWrapper<>();
userFileLambdaQueryWrapper.eq(UserFile::getDeleteBatchNum, userFile.getDeleteBatchNum());
List<UserFile> list = userFileService.list(userFileLambdaQueryWrapper);
List<UserFile> list = userFileMapper.selectList(userFileLambdaQueryWrapper);
recoveryFileService.deleteUserFileByDeleteBatchNum(userFile.getDeleteBatchNum());
for (UserFile userFileItem : list) {
@ -103,7 +99,7 @@ public class AsyncTaskComp {
}
}
return new AsyncResult<String>("deleteUserFile");
return new AsyncResult<>("deleteUserFile");
}
public Future<String> checkESUserFileId(String userFileId) {
@ -111,7 +107,7 @@ public class AsyncTaskComp {
if (userFile == null) {
fileDealComp.deleteESByUserFileId(userFileId);
}
return new AsyncResult<String>("checkUserFileId");
return new AsyncResult<>("checkUserFileId");
}
@ -128,8 +124,6 @@ public class AsyncTaskComp {
try {
fis = new FileInputStream(currentFile);
md5Str = DigestUtils.md5Hex(fis);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
@ -192,7 +186,7 @@ public class AsyncTaskComp {
}
fileDealComp.restoreParentFilePath(qiwenFile, userFile.getUserId());
return new AsyncResult<String>("saveUnzipFile");
return new AsyncResult<>("saveUnzipFile");
}

View File

@ -28,6 +28,7 @@ import com.qiwenshare.ufop.operation.write.domain.WriteFile;
import com.qiwenshare.ufop.util.UFOPUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.jaudiotagger.audio.AudioFile;
@ -80,7 +81,7 @@ public class FileDealComp {
/**
* 获取重复文件名
*
* <p>
* 场景1: 文件还原时 savefilePath 路径下保存 测试.txt 文件重名则会生成 测试(1).txt
* 场景2 上传文件时 savefilePath 路径下保存 测试.txt 文件重名则会生成 测试(1).txt
*
@ -91,39 +92,38 @@ public class FileDealComp {
public String getRepeatFileName(UserFile userFile, String savefilePath) {
String fileName = userFile.getFileName();
String extendName = userFile.getExtendName();
Integer deleteFlag = userFile.getDeleteFlag();
String userId = userFile.getUserId();
int isDir = userFile.getIsDir();
LambdaQueryWrapper<UserFile> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(UserFile::getFilePath, savefilePath)
.eq(UserFile::getDeleteFlag, deleteFlag)
.eq(UserFile::getDeleteFlag, 0)
.eq(UserFile::getUserId, userId)
.eq(UserFile::getFileName, fileName)
.eq(UserFile::getIsDir, isDir);
if (userFile.getIsDir() == 0) {
if (userFile.isFile()) {
lambdaQueryWrapper.eq(UserFile::getExtendName, extendName);
}
List<UserFile> list = userFileMapper.selectList(lambdaQueryWrapper);
if (list == null) {
return fileName;
}
if (list.isEmpty()) {
if (CollectionUtils.isEmpty(list)) {
return fileName;
}
int i = 0;
while (list != null && !list.isEmpty()) {
while (!CollectionUtils.isEmpty(list)) {
i++;
LambdaQueryWrapper<UserFile> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
lambdaQueryWrapper1.eq(UserFile::getFilePath, savefilePath)
.eq(UserFile::getDeleteFlag, deleteFlag)
.eq(UserFile::getDeleteFlag, 0)
.eq(UserFile::getUserId, userId)
.eq(UserFile::getFileName, fileName + "(" + i + ")")
.eq(UserFile::getIsDir, isDir);
if (userFile.getIsDir() == 0) {
if (userFile.isFile()) {
lambdaQueryWrapper1.eq(UserFile::getExtendName, extendName);
}
list = userFileMapper.selectList(lambdaQueryWrapper1);
}
return fileName + "(" + i + ")";
@ -132,7 +132,7 @@ public class FileDealComp {
/**
* 还原父文件路径
*
* <p>
* 1回收站文件还原操作会将文件恢复到原来的路径下,当还原文件的时候如果父目录已经不存在了则需要把父母录给还原
* 2上传目录
*
@ -143,7 +143,7 @@ public class FileDealComp {
if (qiwenFile.isFile()) {
qiwenFile = qiwenFile.getParentFile();
}
while(qiwenFile.getParent() != null) {
while (qiwenFile.getParent() != null) {
String fileName = qiwenFile.getName();
String parentFilePath = qiwenFile.getParent();
@ -173,13 +173,14 @@ public class FileDealComp {
/**
* 删除重复的子目录文件
*
* <p>
* 当还原目录的时候如果其子目录在文件系统中已存在则还原之后进行去重操作
*
* @param filePath
* @param sessionUserId
*/
public void deleteRepeatSubDirFile(String filePath, String sessionUserId) {
log.debug("删除子目录:"+filePath);
log.debug("删除子目录:" + filePath);
LambdaQueryWrapper<UserFile> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.select(UserFile::getFileName, UserFile::getFilePath)
@ -197,7 +198,7 @@ public class FileDealComp {
.eq(UserFile::getFileName, userFile.getFileName())
.eq(UserFile::getDeleteFlag, "0");
List<UserFile> userFiles = userFileMapper.selectList(lambdaQueryWrapper1);
for (int i = 0; i < userFiles.size() - 1; i ++) {
for (int i = 0; i < userFiles.size() - 1; i++) {
userFileMapper.deleteById(userFiles.get(i).getUserFileId());
}
}
@ -205,6 +206,7 @@ public class FileDealComp {
/**
* 组织一个树目录节点文件移动的时候使用
*
* @param treeNode
* @param id
* @param filePath
@ -258,20 +260,21 @@ public class FileDealComp {
/**
* 判断该路径在树节点中是否已经存在
*
* @param childrenTreeNodes
* @param path
* @return
*/
public boolean isExistPath(List<TreeNode> childrenTreeNodes, String path){
public boolean isExistPath(List<TreeNode> childrenTreeNodes, String path) {
boolean isExistPath = false;
try {
for (int i = 0; i < childrenTreeNodes.size(); i++){
if (path.equals(childrenTreeNodes.get(i).getLabel())){
for (TreeNode childrenTreeNode : childrenTreeNodes) {
if (path.equals(childrenTreeNode.getLabel())) {
isExistPath = true;
}
}
}catch (Exception e){
} catch (Exception e) {
e.printStackTrace();
}
@ -409,8 +412,7 @@ public class FileDealComp {
DownloadFile downloadFile = new DownloadFile();
downloadFile.setFileUrl(fileUrl);
InputStream inputStream = ufopFactory.getDownloader(storageType).getInputStream(downloadFile);
String md5Str = DigestUtils.md5Hex(inputStream);
return md5Str;
return DigestUtils.md5Hex(inputStream);
}
public void saveFileInputStream(int storageType, String fileUrl, InputStream inputStream) throws IOException {
@ -422,7 +424,7 @@ public class FileDealComp {
writer1.write(inputStream, writeFile);
}
public boolean isDirExist(String fileName, String filePath, String userId){
public boolean isDirExist(String fileName, String filePath, String userId) {
LambdaQueryWrapper<UserFile> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(UserFile::getFileName, fileName)
.eq(UserFile::getFilePath, QiwenFile.formatPath(filePath))
@ -524,7 +526,7 @@ public class FileDealComp {
if (StringUtils.isEmpty(music.getLyrics())) {
try {
String lyc = MusicUtils.getLyc(music.getArtist(), music.getTitle(), music.getAlbum());
music.setLyrics(lyc);
} catch (Exception e) {

View File

@ -29,8 +29,7 @@ public class JwtComp {
// 本地的密码解码
byte[] encodedKey = Base64.decodeBase64(jwtProperties.getSecret());
// 根据给定的字节数组使用AES加密算法构造一个密钥
SecretKey key = new SecretKeySpec(encodedKey, 0, encodedKey.length, "AES");
return key;
return new SecretKeySpec(encodedKey, 0, encodedKey.length, "AES");
}
// 创建jwt
@ -61,10 +60,9 @@ public class JwtComp {
// 解密jwt
public Claims parseJWT(String jwt) throws Exception {
SecretKey key = generalKey(); // 签名秘钥和生成的签名的秘钥一模一样
Claims claims = Jwts.parser() // 得到DefaultJwtParser
.setSigningKey(key) // 设置签名的秘钥
.parseClaimsJws(jwt).getBody(); // 设置需要解析的jwt
return claims;
return Jwts.parser() // 得到DefaultJwtParser
.setSigningKey(key) // 设置签名的秘钥
.parseClaimsJws(jwt).getBody();
}

View File

@ -1,6 +1,7 @@
package com.qiwenshare.file.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiwenshare.file.api.IRecoveryFileService;
@ -12,6 +13,7 @@ import com.qiwenshare.file.mapper.RecoveryFileMapper;
import com.qiwenshare.file.mapper.UserFileMapper;
import com.qiwenshare.file.vo.file.RecoveryFileListVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -45,17 +47,26 @@ public class RecoveryFileService extends ServiceImpl<RecoveryFileMapper, Recove
@Override
public void restorefile(String deleteBatchNum, String filePath, String sessionUserId) {
LambdaUpdateWrapper<UserFile> userFileLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
userFileLambdaUpdateWrapper.set(UserFile::getDeleteFlag, 0)
.set(UserFile::getDeleteBatchNum, "")
.eq(UserFile::getDeleteBatchNum, deleteBatchNum);
userFileMapper.update(null, userFileLambdaUpdateWrapper);
List<UserFile> restoreUserFileList = userFileMapper.selectList(new QueryWrapper<UserFile>().lambda().eq(UserFile::getDeleteBatchNum, deleteBatchNum));
for (UserFile restoreUserFile : restoreUserFileList) {
restoreUserFile.setDeleteFlag(0);
restoreUserFile.setDeleteBatchNum(deleteBatchNum);
String fileName = fileDealComp.getRepeatFileName(restoreUserFile, restoreUserFile.getFilePath());
if (restoreUserFile.isDirectory()) {
if (!StringUtils.equals(fileName, restoreUserFile.getFileName())) {
userFileMapper.deleteById(restoreUserFile);
} else {
userFileMapper.updateById(restoreUserFile);
}
} else if (restoreUserFile.isFile()) {
restoreUserFile.setFileName(fileName);
userFileMapper.updateById(restoreUserFile);
}
}
QiwenFile qiwenFile = new QiwenFile(filePath, true);
fileDealComp.restoreParentFilePath(qiwenFile, sessionUserId);
fileDealComp.deleteRepeatSubDirFile(filePath, sessionUserId);
// TODO 如果被还原的文件已存在暂未实现
LambdaQueryWrapper<RecoveryFile> recoveryFileServiceLambdaQueryWrapper = new LambdaQueryWrapper<>();
recoveryFileServiceLambdaQueryWrapper.eq(RecoveryFile::getDeleteBatchNum, deleteBatchNum);
recoveryFileMapper.delete(recoveryFileServiceLambdaQueryWrapper);