完善文件服务

This commit is contained in:
2024-01-10 10:21:53 +08:00
parent 9ee4672036
commit cd5b14ab89
6 changed files with 194 additions and 33 deletions

View File

@@ -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 <T> Iterable<T> allChildren(T src, Function<T, Iterable<T>> childrenGetter) {
ArrayList<T> ans = new ArrayList<>();
var seen = new HashSet<>();
Stack<T> 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<T> children = childrenGetter.apply(pop);
if (children != null) {
for (T child : children) {
stack.push(child);
}
}
}
return ans;
}
}

View File

@@ -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<NetDiskDto> 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<String> idToPath(@RequestBody IdRequest request) {
return R.success(this.service.idToPath(request));
}
/**
* id转路径, 同时返回所有父级详细信息
*
* @param request
* @return
*/
@PostMapping("/idToPath2")
public R<List<NetDiskDto>> 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<String> 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);
}
/**
* 创建文件夹
*

View File

@@ -30,7 +30,7 @@ public class NetDiskDto extends OrgCommonDto {
@Comment("文件/文件夹 大小, 单位 byte")
private Long size;
private Long sizeHumanReadable;
private String sizeHumanReadable;
@Comment("文件 sha256 值, 文件夹没有")

View File

@@ -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;
}
}
}

View File

@@ -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<String> 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

View File

@@ -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<NetDiskEntity, NetDiskRepository
public String idToPath(IdRequest request) {
String id = request.getId();
String ans = idToPath(id);
return StringUtils.startsWith(ans, "//")? StringUtils.substring(ans, 1): ans;
}
private String idToPath(String id) {
return this.repository.idToPath(id);
String ans = this.repository.idToPath(id);
return StringUtils.startsWith(ans, "//") ? StringUtils.substring(ans, 1) : ans;
}
public String pathToId(IdRequest request) {
String id = this.repository.pathToId(request.getId(), Ctx.currentUser().getOrganizationId());
String id =
this.repository.pathToId(request.getId(), Ctx.currentUser().getOrganizationId());
if (StringUtils.isEmpty(id)) {
if (StringUtils.equalsIgnoreCase(request.getId(), "/")) {
return ensureRoot().getId();
}
throw new BizException("路径不存在");
}
return id;
}
private NetDiskEntity ensureRoot() {
NetDiskEntity entity = new NetDiskEntity();
entity.setEntryType("0");
entity.setName("/");
entity.setOrganizationId(Ctx.currentUser().getOrganizationId());
save(entity);
return entity;
}
private NetDiskEntity findRoot() {
try {
@@ -141,6 +158,8 @@ public class NetDiskService extends BaseService<NetDiskEntity, NetDiskRepository
handlerRootDir(request);
transPathToId(request::getId, request::setId);
if (StringUtils.isEmpty(request.getId())) {
return this.mapper.toDto(
repository.findALlByParentNullAndOrganizationId(
@@ -149,7 +168,14 @@ public class NetDiskService extends BaseService<NetDiskEntity, NetDiskRepository
NetDiskEntity entity = get(request.getId());
return this.mapper.toDto(entity.getChildren());
ArrayList<NetDiskEntity> ans = new ArrayList<>();
if (entity.getChildren() != null) {
ans.addAll(entity.getChildren());
}
return this.mapper.toDto(ans);
}
public List<NetDiskDto> batchCreateDir(BatchCreateDirDto request) {
@@ -219,6 +245,19 @@ public class NetDiskService extends BaseService<NetDiskEntity, NetDiskRepository
return this.mapper.toDto(entity);
}
public void transPathToId(Supplier<String> path, Consumer<String> 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<NetDiskEntity, NetDiskRepository
src.setName(request.getNewName());
}
if (src.getParent().getId().equals(target.getId())) {
throw new BizException("不能移动到自己的子目录");
}
TreeUtils.allChildren(src, NetDiskEntity::getChildren)
.forEach(
x -> {
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<NetDiskEntity, NetDiskRepository
src.setName(request.getNewName());
}
if (src.getParent().getId().equals(target.getId())) {
throw new BizException("不能复制到自己的子目录");
}
// TreeUtils.allChildren(src, NetDiskEntity::getChildren)
// .forEach(
// x -> {
// 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<NetDiskEntity, NetDiskRepository
public void updateDirSize(String id) {
NetDiskEntity entity = get(id);
entity.setSize(entity.getChildren().stream().mapToLong(NetDiskEntity::getSize).sum());
entity.setSize(
entity.getChildren() == null
? 0
: entity.getChildren().stream().mapToLong(NetDiskEntity::getSize).sum());
save(entity);
while (entity.getParent() != null) {
entity = entity.getParent();
entity.setSize(entity.getChildren().stream().mapToLong(NetDiskEntity::getSize).sum());
entity.setSize(
entity.getChildren() == null
? 0
: entity.getChildren().stream()
.mapToLong(NetDiskEntity::getSize)
.sum());
save(entity);
}
}
@@ -822,7 +880,15 @@ public class NetDiskService extends BaseService<NetDiskEntity, NetDiskRepository
NetDiskEntity entity = get(request.getId());
return this.mapper.toTreeDto(entity);
}
public List<NetDiskDto> idToPath2(IdRequest request) {
String id = request.getId();
List<String> ans = this.repository.idToPath2(id);
List<NetDiskEntity> allById = this.repository.findAllById(ans).stream().sorted( Comparator.comparing(x -> ans.indexOf(x.getId()))).collect(Collectors.toList());
return this.mapper.toDto(allById);
}
}