完善网盘

This commit is contained in:
2024-01-13 19:59:55 +08:00
parent 64cea704b2
commit 3269609091
2 changed files with 31 additions and 32 deletions

View File

@@ -305,7 +305,7 @@ public class NetDiskController {
* @param response * @param response
*/ */
@GetMapping("/downloadBatch") @GetMapping("/downloadBatch")
@SysLog(action = "downloadBatch", message = "ids") @SysLog(action = "downloadBatch")
@SignCheck(false) @SignCheck(false)
public void downloadDir(@RequestParam("ids") String ids, HttpServletResponse response) { public void downloadDir(@RequestParam("ids") String ids, HttpServletResponse response) {

View File

@@ -247,7 +247,7 @@ public class NetDiskService extends BaseService<NetDiskEntity, NetDiskRepository
entity.setOrganizationId(Ctx.currentUser().getOrganizationId()); entity.setOrganizationId(Ctx.currentUser().getOrganizationId());
entity.setParent(parent); entity.setParent(parent);
save(entity); save(entity);
if (parent.getChildren() == null){ if (parent.getChildren() == null) {
parent.setChildren(new ArrayList<>()); parent.setChildren(new ArrayList<>());
} }
parent.getChildren().add(entity); parent.getChildren().add(entity);
@@ -421,30 +421,33 @@ public class NetDiskService extends BaseService<NetDiskEntity, NetDiskRepository
private void resolveNameWithPath(CreateFileDto request) { private void resolveNameWithPath(CreateFileDto request) {
if (request.getName().contains("/")) {
if (request.getName().contains("/")){
String oldId = request.getParent(); String oldId = request.getParent();
String[] split = request.getName().split("/"); String[] split = request.getName().split("/");
request.setName(split[split.length - 1]); request.setName(split[split.length - 1]);
NetDiskEntity current = get(request.getParent()); NetDiskEntity current = get(request.getParent());
for(int i = 0; i < split.length - 1; i++) { for (int i = 0; i < split.length - 1; i++) {
String name = split[i]; String name = split[i];
if (StringUtils.isEmpty(name)){ if (StringUtils.isEmpty(name)) {
continue; continue;
} }
if (current.getChildren() == null || current.getChildren().stream().noneMatch(x -> x.getName().equals(name))){ if (current.getChildren() == null
|| current.getChildren().stream()
.noneMatch(x -> x.getName().equals(name))) {
throw new BizException("路径不存在: " + request.getName()); throw new BizException("路径不存在: " + request.getName());
}else { } else {
current = current.getChildren().stream().filter(x -> x.getName().equals(name)).findFirst().get(); current =
current.getChildren().stream()
.filter(x -> x.getName().equals(name))
.findFirst()
.get();
} }
} }
request.setParent(current.getId()); request.setParent(current.getId());
@@ -816,10 +819,10 @@ public class NetDiskService extends BaseService<NetDiskEntity, NetDiskRepository
response.setContentType("application/zip"); response.setContentType("application/zip");
// 设置响应头指定ZIP文件名 // 设置响应头指定ZIP文件名
String zipName = URLEncoder.encode("批量下载" + LocalDate.now() + ".zip", StandardCharsets.UTF_8); String zipName =
URLEncoder.encode("批量下载" + LocalDate.now() + ".zip", StandardCharsets.UTF_8);
response.setHeader("Content-Disposition", "attachment; filename=" + zipName); response.setHeader("Content-Disposition", "attachment; filename=" + zipName);
StopWatch stopWatch = new StopWatch("批量下载 "+ StringUtils.join(request.getIds(), ",")); StopWatch stopWatch = new StopWatch("批量下载 " + StringUtils.join(request.getIds(), ","));
@Cleanup("close") @Cleanup("close")
ZipArchiveOutputStream zipArchiveOutputStream = ZipArchiveOutputStream zipArchiveOutputStream =
@@ -829,16 +832,6 @@ public class NetDiskService extends BaseService<NetDiskEntity, NetDiskRepository
NetDiskEntity entity = get(id); NetDiskEntity entity = get(id);
stopWatch.start(
"下载文件夹/文件夹 "
+ entity.getName()
+ " "
+ entity.getEntryTypeName()
+ " "
+ entity.getId());
zipEntity(entity, zipArchiveOutputStream, entity, stopWatch); zipEntity(entity, zipArchiveOutputStream, entity, stopWatch);
log.info(stopWatch.prettyPrint()); log.info(stopWatch.prettyPrint());
@@ -858,6 +851,7 @@ public class NetDiskService extends BaseService<NetDiskEntity, NetDiskRepository
parts.addFirst(entry.getName()); parts.addFirst(entry.getName());
entry = entry.getParent(); entry = entry.getParent();
} }
parts.addFirst(root.getName());
return StringUtils.join(parts, "/") + (isDir ? "/" : ""); return StringUtils.join(parts, "/") + (isDir ? "/" : "");
} }
@@ -1004,17 +998,27 @@ public class NetDiskService extends BaseService<NetDiskEntity, NetDiskRepository
NetDiskEntity root, NetDiskEntity root,
StopWatch stopWatch) { StopWatch stopWatch) {
String fullName = getFullName(root, dir);
if (dir.getEntryType().equals("0")) { if (dir.getEntryType().equals("0")) {
stopWatch.start("创建目录 " + dir.getName() + " " + dir.getId()); stopWatch.start("创建目录 " + dir.getName() + " " + fullName + " " + dir.getId());
zipArchiveOutputStream.putArchiveEntry(new ZipArchiveEntry(getFullName(root, dir))); zipArchiveOutputStream.putArchiveEntry(new ZipArchiveEntry(fullName));
zipArchiveOutputStream.closeArchiveEntry(); zipArchiveOutputStream.closeArchiveEntry();
stopWatch.stop(); stopWatch.stop();
if (dir.getChildren() != null) {
for (NetDiskEntity child : dir.getChildren()) {
zipEntity(child, zipArchiveOutputStream, root, stopWatch);
}
}
} else { } else {
zipArchiveOutputStream.putArchiveEntry(new ZipArchiveEntry(getFullName(root, dir))); zipArchiveOutputStream.putArchiveEntry(new ZipArchiveEntry(fullName));
stopWatch.start("下载文件 " + dir.getName() + " " + dir.getId()); stopWatch.start("下载文件 " + dir.getName() + " " + dir.getId());
OSSObject ossObject = OSSObject ossObject =
@@ -1024,11 +1028,6 @@ public class NetDiskService extends BaseService<NetDiskEntity, NetDiskRepository
stopWatch.stop(); stopWatch.stop();
zipArchiveOutputStream.closeArchiveEntry(); zipArchiveOutputStream.closeArchiveEntry();
for (NetDiskEntity child : dir.getChildren()) {
zipEntity(child, zipArchiveOutputStream, root, stopWatch);
}
} }
} }