From cd5b14ab891286e3132dfadd52ffcdbd8c67c07e Mon Sep 17 00:00:00 2001 From: lihongjie0209 Date: Wed, 10 Jan 2024 10:21:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=96=87=E4=BB=B6=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/lihongjie/coal/common/TreeUtils.java | 43 ++++++++ .../netDisk/controller/NetDiskController.java | 37 +++++-- .../coal/netDisk/dto/NetDiskDto.java | 2 +- .../coal/netDisk/entity/NetDiskEntity.java | 14 +-- .../netDisk/repository/NetDiskRepository.java | 29 ++++- .../coal/netDisk/service/NetDiskService.java | 102 ++++++++++++++---- 6 files changed, 194 insertions(+), 33 deletions(-) create mode 100644 src/main/java/cn/lihongjie/coal/common/TreeUtils.java diff --git a/src/main/java/cn/lihongjie/coal/common/TreeUtils.java b/src/main/java/cn/lihongjie/coal/common/TreeUtils.java new file mode 100644 index 00000000..022d6d7a --- /dev/null +++ b/src/main/java/cn/lihongjie/coal/common/TreeUtils.java @@ -0,0 +1,43 @@ +package cn.lihongjie.coal.common; + +import cn.lihongjie.coal.exception.BizException; + +import lombok.experimental.UtilityClass; + +import org.apache.poi.ss.formula.functions.T; + +import java.util.*; +import java.util.function.Function; + +@UtilityClass +public class TreeUtils { + public static Iterable allChildren(T src, Function> childrenGetter) { + + ArrayList ans = new ArrayList<>(); + + var seen = new HashSet<>(); + + Stack stack = new Stack<>(); + + stack.push(src); + + while (!stack.isEmpty()) { + T pop = stack.pop(); + if (!seen.add(pop)) { + throw new BizException("循环引用 " + pop.toString()); + } + if (pop != src) { + + ans.add(pop); + } + Iterable children = childrenGetter.apply(pop); + if (children != null) { + + for (T child : children) { + stack.push(child); + } + } + } + return ans; + } +} diff --git a/src/main/java/cn/lihongjie/coal/netDisk/controller/NetDiskController.java b/src/main/java/cn/lihongjie/coal/netDisk/controller/NetDiskController.java index adefca36..881dc362 100644 --- a/src/main/java/cn/lihongjie/coal/netDisk/controller/NetDiskController.java +++ b/src/main/java/cn/lihongjie/coal/netDisk/controller/NetDiskController.java @@ -2,7 +2,9 @@ package cn.lihongjie.coal.netDisk.controller; import cn.lihongjie.coal.annotation.OrgScope; import cn.lihongjie.coal.annotation.SysLog; +import cn.lihongjie.coal.base.dto.CommonQuery; import cn.lihongjie.coal.base.dto.IdRequest; +import cn.lihongjie.coal.base.dto.R; import cn.lihongjie.coal.netDisk.dto.*; import cn.lihongjie.coal.netDisk.service.NetDiskService; @@ -11,6 +13,7 @@ import jakarta.servlet.http.HttpServletResponse; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @@ -26,6 +29,17 @@ import java.util.List; public class NetDiskController { @Autowired private NetDiskService service; + /** + * 文件搜索接口 + * + * @param request + * @return + */ + @PostMapping("/list") + public Page list(@RequestBody CommonQuery request) { + return this.service.list(request); + } + @PostMapping("/getById") public NetDiskDto getById(@RequestBody IdRequest request) { return this.service.getById(request.getId()); @@ -38,10 +52,23 @@ public class NetDiskController { * @return */ @PostMapping("/idToPath") - public String idToPath(@RequestBody IdRequest request) { - return this.service.idToPath(request); + public R idToPath(@RequestBody IdRequest request) { + return R.success(this.service.idToPath(request)); } + + /** + * id转路径, 同时返回所有父级详细信息 + * + * @param request + * @return + */ + @PostMapping("/idToPath2") + public R> idToPath2(@RequestBody IdRequest request) { + return R.success(this.service.idToPath2(request)); + } + + /** * 路径转id * @@ -49,8 +76,8 @@ public class NetDiskController { * @return */ @PostMapping("/pathToId") - public String pathToId(@RequestBody IdRequest request) { - return this.service.pathToId(request); + public R pathToId(@RequestBody IdRequest request) { + return R.success(this.service.pathToId(request)); } /** @@ -65,7 +92,6 @@ public class NetDiskController { return this.service.ls(request); } - /** * 列出文件树 * @@ -77,7 +103,6 @@ public class NetDiskController { return this.service.tree(request); } - /** * 创建文件夹 * diff --git a/src/main/java/cn/lihongjie/coal/netDisk/dto/NetDiskDto.java b/src/main/java/cn/lihongjie/coal/netDisk/dto/NetDiskDto.java index 512db06d..fca89403 100644 --- a/src/main/java/cn/lihongjie/coal/netDisk/dto/NetDiskDto.java +++ b/src/main/java/cn/lihongjie/coal/netDisk/dto/NetDiskDto.java @@ -30,7 +30,7 @@ public class NetDiskDto extends OrgCommonDto { @Comment("文件/文件夹 大小, 单位 byte") private Long size; - private Long sizeHumanReadable; + private String sizeHumanReadable; @Comment("文件 sha256 值, 文件夹没有") diff --git a/src/main/java/cn/lihongjie/coal/netDisk/entity/NetDiskEntity.java b/src/main/java/cn/lihongjie/coal/netDisk/entity/NetDiskEntity.java index cc0f33a9..4beb993c 100644 --- a/src/main/java/cn/lihongjie/coal/netDisk/entity/NetDiskEntity.java +++ b/src/main/java/cn/lihongjie/coal/netDisk/entity/NetDiskEntity.java @@ -43,7 +43,7 @@ public class NetDiskEntity extends OrgCommonEntity { @Formula("(pg_size_pretty(size))") - private Long sizeHumanReadable; + private String sizeHumanReadable; @Comment("文件 sha256 值, 文件夹没有") @@ -73,9 +73,11 @@ public class NetDiskEntity extends OrgCommonEntity { private String sliceTaskId; - - - - - + @Override + public void prePersist() { + super.prePersist(); + if (this.size == null){ + this.size = 0L; + } + } } diff --git a/src/main/java/cn/lihongjie/coal/netDisk/repository/NetDiskRepository.java b/src/main/java/cn/lihongjie/coal/netDisk/repository/NetDiskRepository.java index f13f2802..2953eb77 100644 --- a/src/main/java/cn/lihongjie/coal/netDisk/repository/NetDiskRepository.java +++ b/src/main/java/cn/lihongjie/coal/netDisk/repository/NetDiskRepository.java @@ -48,14 +48,39 @@ from tmp2 nativeQuery = true) String idToPath(@Param("id") String id); + + @Query( value = """ -with recursive tmp as (select regexp_split_to_array(:path, '/') as arr) +with recursive + tmp as (select t.*, 0 as level + from t_net_disk t + where t.id = :id + union all + + select p.*, t.level + 1 as level + from t_net_disk p + inner join tmp t on p.id = t.parent_id), + + tmp2 as (select * from tmp order by level desc) + + +select id +from tmp2 +""", + nativeQuery = true) + List idToPath2(@Param("id") String id); + + @Query( + value = + """ + +with recursive tmp as (select (regexp_split_to_array(:path, '/'))[2::] as arr) , tmp2 as (select tmp.arr, 1 as index, t.id as id, ARRAY_LENGTH(tmp.arr, 1) as len from tmp - inner join t_net_disk t on t.parent_id is null and t.organization_id = :organizationId + inner join t_net_disk t on t.parent_id is null and t.organization_id = :organizationId and t.name = '/' union all select tx.arr as arr, tx.index + 1 as index, t.id as id, tx.len diff --git a/src/main/java/cn/lihongjie/coal/netDisk/service/NetDiskService.java b/src/main/java/cn/lihongjie/coal/netDisk/service/NetDiskService.java index 537491a5..b93da20f 100644 --- a/src/main/java/cn/lihongjie/coal/netDisk/service/NetDiskService.java +++ b/src/main/java/cn/lihongjie/coal/netDisk/service/NetDiskService.java @@ -4,6 +4,7 @@ import cn.lihongjie.coal.base.dto.CommonQuery; import cn.lihongjie.coal.base.dto.IdRequest; import cn.lihongjie.coal.base.service.BaseService; import cn.lihongjie.coal.common.Ctx; +import cn.lihongjie.coal.common.TreeUtils; import cn.lihongjie.coal.exception.BizException; import cn.lihongjie.coal.netDisk.dto.*; import cn.lihongjie.coal.netDisk.entity.NetDiskEntity; @@ -49,6 +50,8 @@ import java.net.URL; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.util.*; +import java.util.function.Consumer; +import java.util.function.Supplier; import java.util.stream.Collectors; @Service @@ -66,26 +69,40 @@ public class NetDiskService extends BaseService ans = new ArrayList<>(); + + if (entity.getChildren() != null) { + + ans.addAll(entity.getChildren()); + } + + return this.mapper.toDto(ans); } public List batchCreateDir(BatchCreateDirDto request) { @@ -219,6 +245,19 @@ public class NetDiskService extends BaseService path, Consumer idConsumer) { + + String p = path.get(); + + if (StringUtils.isNotBlank(p) && p.startsWith("/")) { + + idConsumer.accept(pathToId(new IdRequest(p))); + + } else { + idConsumer.accept(p); + } + } + private void checkParentWithEntryName(String parent, String name) { if (StringUtils.isEmpty(parent)) { @@ -509,16 +548,23 @@ public class NetDiskService extends BaseService { + if (x.getId().equals(target.getId())) { + throw new BizException("不能移动到自己的子目录"); + } + }); if (!target.getEntryType().equals("0")) { throw new BizException("只能移动到目录"); } - if (target.getChildren().stream().filter(x -> x.getName().equals(src.getName())).count() - > 0) { + if (target.getChildren() != null + && target.getChildren().stream() + .filter(x -> x.getName().equals(src.getName())) + .count() + > 0) { throw new BizException("目标目录已经存在同名文件/文件夹"); } @@ -550,9 +596,13 @@ public class NetDiskService extends BaseService { +// if (x.getId().equals(src.getId())) { +// throw new BizException("不能复制到自己的子目录"); +// } +// }); if (!target.getEntryType().equals("0")) { throw new BizException("只能复制到目录"); @@ -770,12 +820,20 @@ public class NetDiskService extends BaseService idToPath2(IdRequest request) { + String id = request.getId(); + + List ans = this.repository.idToPath2(id); + + List allById = this.repository.findAllById(ans).stream().sorted( Comparator.comparing(x -> ans.indexOf(x.getId()))).collect(Collectors.toList()); + return this.mapper.toDto(allById); } }