Skip to content

Commit

Permalink
Merge pull request #119 from jamebal/recycle
Browse files Browse the repository at this point in the history
perf: 优化删除文件功能
  • Loading branch information
jamebal authored Jul 7, 2024
2 parents c5735f6 + 853d872 commit 0d53ad9
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ public ResponseResult<Object> unFavorite(@RequestParam String[] fileIds) {
public ResponseResult<Object> delete(@RequestParam String username, @RequestParam String[] fileIds, @RequestParam String currentDirectory) {
if (fileIds != null && fileIds.length > 0) {
List<String> list = Arrays.asList(fileIds);
return fileService.delete(username, currentDirectory, list, userLoginHolder.getUsername());
return fileService.delete(username, currentDirectory, list, userLoginHolder.getUsername(), false);
} else {
throw new CommonException(ExceptionType.MISSING_PARAMETERS.getCode(), ExceptionType.MISSING_PARAMETERS.getMsg());
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/jmal/clouddisk/service/IFileService.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,10 @@ public interface IFileService {
* @param currentDirectory 当前目录
* @param fileIds 文件id
* @param operator 操作者
* @param sweep 是否彻底删除
* @return ResponseResult<Object>
*/
ResponseResult<Object> delete(String username, String currentDirectory, List<String> fileIds, String operator);
ResponseResult<Object> delete(String username, String currentDirectory, List<String> fileIds, String operator, boolean sweep);

/**
* 显示缩略图
Expand Down
36 changes: 24 additions & 12 deletions src/main/java/com/jmal/clouddisk/service/impl/FileServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,7 @@ public ResponseResult<Object> move(UploadApiParamDTO upload, List<String> froms,
getCopyResult(upload, froms, to, true);
String currentDirectory = getOssFileCurrentDirectory(upload, froms);
// 删除
delete(upload.getUsername(), currentDirectory, froms, upload.getUsername());
delete(upload.getUsername(), currentDirectory, froms, upload.getUsername(), true);
} catch (CommonException e) {
pushMessageOperationFileError(upload.getUsername(), Convert.toStr(e.getMsg(), Constants.UNKNOWN_ERROR), "移动");
} catch (Exception e) {
Expand Down Expand Up @@ -1855,7 +1855,7 @@ public ResponseResult<Object> unFavorite(List<String> fileIds) {
}

@Override
public ResponseResult<Object> delete(String username, String currentDirectory, List<String> fileIds, String operator) {
public ResponseResult<Object> delete(String username, String currentDirectory, List<String> fileIds, String operator, boolean sweep) {
FileDocument doc = getById(fileIds.get(0));
List<OperationPermission> operationPermissionList = null;
if (doc != null) {
Expand All @@ -1871,7 +1871,6 @@ public ResponseResult<Object> delete(String username, String currentDirectory, L
webOssService.delete(ossPath, fileIds);
return ResultUtil.success();
}

Query query = new Query();
query.addCriteria(Criteria.where("_id").in(fileIds));
List<FileDocument> fileDocuments = mongoTemplate.find(query, FileDocument.class, COLLECTION_NAME);
Expand All @@ -1883,8 +1882,15 @@ public ResponseResult<Object> delete(String username, String currentDirectory, L
if (CommonFileService.isLock(fileDocument)) {
throw new CommonException(ExceptionType.LOCKED_RESOURCES);
}
if (BooleanUtil.isFalse(fileDocument.getIsFolder())) {
isDel = true;
if (sweep) {
String currentDirectory1 = getUserDirectory(fileDocument.getPath());
String filePath = fileProperties.getRootDir() + File.separator + username + currentDirectory1 + fileDocument.getName();
File file = new File(filePath);
isDel = FileUtil.del(file);
} else {
if (BooleanUtil.isFalse(fileDocument.getIsFolder())) {
isDel = true;
}
}
if (Boolean.TRUE.equals(fileDocument.getIsFolder())) {
// 删除文件夹及其下的所有文件
Expand All @@ -1893,22 +1899,28 @@ public ResponseResult<Object> delete(String username, String currentDirectory, L
moveToTrash(delFileDocumentList, true);
// 提取出delFileDocumentList中文件id
List<String> delFileIds = delFileDocumentList.stream().map(FileDocument::getId).collect(Collectors.toList());
deleteDependencies(username, delFileIds, true);
deleteDependencies(username, delFileIds, !sweep);
isDel = true;
}
pushMessage(username, fileDocument, Constants.DELETE_FILE);
}
OperationTips operationTips = OperationTips.builder().operation("移动到回收站").build();
OperationTips operationTips = OperationTips.builder().operation(sweep ? "删除" : "移动到回收站").build();
if (isDel) {
List<FileDocument> delFileDocumentList = mongoTemplate.findAllAndRemove(query, FileDocument.class, COLLECTION_NAME);
// 移动到回收站
moveToTrash(delFileDocumentList, false);
deleteDependencies(username, fileIds, true);
if (sweep) {
mongoTemplate.remove(query, COLLECTION_NAME);
} else {
List<FileDocument> delFileDocumentList = mongoTemplate.findAllAndRemove(query, FileDocument.class, COLLECTION_NAME);
// 移动到回收站
moveToTrash(delFileDocumentList, false);
}
deleteDependencies(username, fileIds, !sweep);
operationTips.setSuccess(true);
} else {
operationTips.setSuccess(false);
}
pushMessage(username, operationTips, Constants.OPERATION_TIPS);
if (!sweep) {
pushMessage(username, operationTips, Constants.OPERATION_TIPS);
}
return ResultUtil.success();
}

Expand Down
14 changes: 14 additions & 0 deletions src/main/resources/db/menu.json
Original file line number Diff line number Diff line change
Expand Up @@ -753,5 +753,19 @@
"path": "/setting/taskProgress",
"sortNumber": 16,
"updateTime": 1717766722975
},
{
"authority": "",
"component": "/trash/index",
"createTime": 1719912060104,
"hide": false,
"icon": "huishouzhan",
"id": "6683c67cf66d83694bc9fe5c",
"menuType": 0,
"name": "回收站",
"parentId": "",
"path": "/trash",
"sortNumber": 8,
"updateTime": 1719912071511
}
]
16 changes: 11 additions & 5 deletions src/main/resources/db/role.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"5ffc37a19a2c787ef115f97f",
"5ffc37db9a2c787ef115f980",
"63b79129a7b6db0f8c1f2ad9",
"6683c67cf66d83694bc9fe5c",
"5ffb1b38eb9cf770a889b7ea",
"6020dec9cfb7a66f628397a8",
"5ffb1b57eb9cf770a889b7eb",
Expand Down Expand Up @@ -43,6 +44,7 @@
"5ffd707129d84649e3f02cda",
"5ffb1cf6eb9cf770a889b7f3",
"5ffd396829d8463da1186735",
"66630732fc290e06878516f0",
"600cf2ef194ba43988898423",
"5ffd395029d8463da1186734",
"5ffd38ed29d8463da1186731",
Expand All @@ -60,7 +62,7 @@
],
"name": "超级管理员",
"remarks": "超级管理员",
"updateTime": 1680744633638
"updateTime": 1720334128676
},
{
"code": "admin",
Expand All @@ -75,6 +77,7 @@
"5ffc37a19a2c787ef115f97f",
"5ffc37db9a2c787ef115f980",
"63b79129a7b6db0f8c1f2ad9",
"6683c67cf66d83694bc9fe5c",
"5ffb1b38eb9cf770a889b7ea",
"6020dec9cfb7a66f628397a8",
"5ffb1b57eb9cf770a889b7eb",
Expand Down Expand Up @@ -107,7 +110,7 @@
],
"name": "管理员",
"remarks": "所有菜单(只读)",
"updateTime": 1613718961613
"updateTime": 1720334138113
},
{
"code": "visitor-website",
Expand Down Expand Up @@ -145,6 +148,7 @@
"5ffc378b9a2c787ef115f97e",
"5ffc37a19a2c787ef115f97f",
"5ffc37db9a2c787ef115f980",
"6683c67cf66d83694bc9fe5c",
"5ffb1b38eb9cf770a889b7ea",
"5ffd3ecf29d8463da118673e",
"5ffd3f5f29d8463da1186740",
Expand Down Expand Up @@ -173,7 +177,7 @@
],
"name": "网站编辑者",
"remarks": "所有网站菜单可写",
"updateTime": 1611300975061
"updateTime": 1720334159599
},
{
"code": "admin-website",
Expand All @@ -188,6 +192,7 @@
"5ffc37a19a2c787ef115f97f",
"5ffc37db9a2c787ef115f980",
"63b79129a7b6db0f8c1f2ad9",
"6683c67cf66d83694bc9fe5c",
"5ffb1b38eb9cf770a889b7ea",
"5ffd3ecf29d8463da118673e",
"5ffd3dc829d8463da118673a",
Expand All @@ -210,7 +215,7 @@
],
"name": "网站管理员",
"remarks": "网站-所有菜单(只读)",
"updateTime": 1613719034018
"updateTime": 1720334165458
},
{
"code": "visitor",
Expand Down Expand Up @@ -291,6 +296,7 @@
"5ffc37a19a2c787ef115f97f",
"5ffc37db9a2c787ef115f980",
"63b79129a7b6db0f8c1f2ad9",
"6683c67cf66d83694bc9fe5c",
"5ffb1b38eb9cf770a889b7ea",
"6020dec9cfb7a66f628397a8",
"5ffd3ecf29d8463da118673e",
Expand All @@ -305,6 +311,6 @@
],
"name": "普通用户",
"remarks": "普通网盘用户",
"updateTime": 1613719060086
"updateTime": 1720334191182
}
]

0 comments on commit 0d53ad9

Please sign in to comment.