mirror of
https://codeup.aliyun.com/64f7d6b8ce01efaafef1e678/coal/coal.git
synced 2026-01-25 07:46:40 +08:00
支持批量移动文件/文件夹
This commit is contained in:
@@ -100,7 +100,7 @@ public class AuthFilter extends OncePerRequestFilter {
|
||||
return;
|
||||
}
|
||||
|
||||
String sessionId = request.getHeader(Constants.HTTP_HEADER_TOKEN);
|
||||
String sessionId = StringUtils.defaultIfEmpty(request.getHeader(Constants.HTTP_HEADER_TOKEN), request.getParameter(Constants.HTTP_HEADER_TOKEN));
|
||||
|
||||
Optional<ResourceDto> resource =
|
||||
resourceService.findUrlFromCache(getRequestURI(request));
|
||||
|
||||
@@ -7,8 +7,7 @@ import java.util.*;
|
||||
@Data
|
||||
public class MoveDto {
|
||||
|
||||
private String srcId;
|
||||
private String newName;
|
||||
private List<String> srcIds;
|
||||
private String targetId;
|
||||
|
||||
|
||||
|
||||
@@ -19,4 +19,6 @@ public class PreCreateFileDto {
|
||||
private Long sliceSize;
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package cn.lihongjie.coal.netDisk.service;
|
||||
|
||||
import cn.lihongjie.coal.base.dto.CommonQuery;
|
||||
import cn.lihongjie.coal.base.dto.IdRequest;
|
||||
import cn.lihongjie.coal.base.mapper.CommonMapper;
|
||||
import cn.lihongjie.coal.base.service.BaseService;
|
||||
import cn.lihongjie.coal.common.Ctx;
|
||||
import cn.lihongjie.coal.common.TreeUtils;
|
||||
@@ -28,6 +29,7 @@ import lombok.Cleanup;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
|
||||
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
@@ -52,12 +54,15 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
@Transactional
|
||||
public class NetDiskService extends BaseService<NetDiskEntity, NetDiskRepository> {
|
||||
@Autowired private CommonMapper commonMapper;
|
||||
public static final int DEFAULT_SLICE_SIZE = 10 * 1024 * 1024;
|
||||
@Autowired ObjectMapper objectMapper;
|
||||
@Autowired OSSClient ossClient;
|
||||
@@ -279,6 +284,36 @@ public class NetDiskService extends BaseService<NetDiskEntity, NetDiskRepository
|
||||
}
|
||||
}
|
||||
|
||||
private String createNewFileName(String parent, String name) {
|
||||
|
||||
NetDiskEntity disk = get(parent);
|
||||
|
||||
if (disk.getChildren().stream().anyMatch(x -> x.getName().equals(name))) {
|
||||
|
||||
Pattern pattern = Pattern.compile(Pattern.quote(name) + "\\((\\d+)\\)");
|
||||
|
||||
int index =
|
||||
disk.getChildren().stream()
|
||||
.map(x -> x.getName())
|
||||
.mapToInt(
|
||||
x -> {
|
||||
Matcher matcher = pattern.matcher(x);
|
||||
|
||||
if (matcher.find()) {
|
||||
return commonMapper.toInt(matcher.group(1));
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
})
|
||||
.max()
|
||||
.orElse(1);
|
||||
|
||||
return name + "(" + (index + 1) + ")";
|
||||
}
|
||||
|
||||
return name;
|
||||
}
|
||||
|
||||
private void validateDirName(String name) {
|
||||
|
||||
if (StringUtils.isEmpty(name) || StringUtils.isBlank(name)) {
|
||||
@@ -294,7 +329,7 @@ public class NetDiskService extends BaseService<NetDiskEntity, NetDiskRepository
|
||||
public NetDiskDto createFile(CreateFileDto request) {
|
||||
|
||||
validateFileName(request.getName());
|
||||
checkParentWithEntryName(request.getParent(), request.getName());
|
||||
request.setName(createNewFileName(request.getParent(), request.getName()));
|
||||
|
||||
if (StringUtils.isNotBlank(request.getOssKey())) {
|
||||
|
||||
@@ -541,48 +576,52 @@ public class NetDiskService extends BaseService<NetDiskEntity, NetDiskRepository
|
||||
}
|
||||
|
||||
public NetDiskDto move(MoveDto request) {
|
||||
|
||||
NetDiskEntity src = get(request.getSrcId());
|
||||
NetDiskEntity target = get(request.getTargetId());
|
||||
if (StringUtils.isNotEmpty(request.getNewName())) {
|
||||
src.setName(request.getNewName());
|
||||
}
|
||||
|
||||
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() != null
|
||||
&& target.getChildren().stream()
|
||||
.filter(x -> x.getName().equals(src.getName()))
|
||||
.count()
|
||||
> 0) {
|
||||
throw new BizException("目标目录已经存在同名文件/文件夹");
|
||||
var srcParentIds = new HashSet<String>();
|
||||
for (String srcId : request.getSrcIds()) {
|
||||
|
||||
NetDiskEntity src = get(srcId);
|
||||
|
||||
TreeUtils.allChildren(src, NetDiskEntity::getChildren)
|
||||
.forEach(
|
||||
x -> {
|
||||
if (x.getId().equals(target.getId())) {
|
||||
throw new BizException("不能移动到自己的子目录");
|
||||
}
|
||||
});
|
||||
|
||||
if (target.getChildren() != null
|
||||
&& target.getChildren().stream()
|
||||
.filter(x -> x.getName().equals(src.getName()))
|
||||
.count()
|
||||
> 0) {
|
||||
throw new BizException("目标目录已经存在同名文件/文件夹");
|
||||
}
|
||||
|
||||
src.setParent(target);
|
||||
|
||||
this.save(src);
|
||||
|
||||
if (src.getParent() != null) {
|
||||
|
||||
srcParentIds.add(src.getParent().getId());
|
||||
}
|
||||
}
|
||||
|
||||
src.setParent(target);
|
||||
if (CollectionUtils.isNotEmpty(srcParentIds)) {
|
||||
|
||||
this.save(src);
|
||||
|
||||
if (src.getParent() != null) {
|
||||
|
||||
updateDirSize(src.getParent().getId());
|
||||
srcParentIds.forEach(this::updateDirSize);
|
||||
}
|
||||
|
||||
if (target.getParent() != null) {
|
||||
|
||||
updateDirSize(target.getParent().getId());
|
||||
}
|
||||
|
||||
return this.mapper.toDto(src);
|
||||
return null;
|
||||
}
|
||||
|
||||
public NetDiskDto copy(CopyDto request) {
|
||||
@@ -784,7 +823,7 @@ public class NetDiskService extends BaseService<NetDiskEntity, NetDiskRepository
|
||||
PreCreateFileDto dto = new PreCreateFileDto();
|
||||
validateFileName(request.getName());
|
||||
|
||||
checkParentWithEntryName(request.getParent(), request.getName());
|
||||
// checkParentWithEntryName(request.getParent(), request.getName());
|
||||
|
||||
List<NetDiskEntity> sha256 = repository.findAllBySha256(request.getSha256());
|
||||
|
||||
@@ -910,16 +949,15 @@ public class NetDiskService extends BaseService<NetDiskEntity, NetDiskRepository
|
||||
|
||||
validateEntityName(entity);
|
||||
|
||||
|
||||
save(entity);
|
||||
}
|
||||
|
||||
private void validateEntityName(NetDiskEntity entity) {
|
||||
if (StringUtils.equalsIgnoreCase(entity.getEntryType(), "0")){
|
||||
if (StringUtils.equalsIgnoreCase(entity.getEntryType(), "0")) {
|
||||
|
||||
validateDirName(entity.getName());
|
||||
|
||||
}else if (StringUtils.equalsIgnoreCase(entity.getEntryType(), "1")){
|
||||
} else if (StringUtils.equalsIgnoreCase(entity.getEntryType(), "1")) {
|
||||
|
||||
validateFileName(entity.getName());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user