|
|
@@ -0,0 +1,645 @@
|
|
|
+package cn.reghao.tnb.content.app.disk.service;
|
|
|
+
|
|
|
+import cn.reghao.jutil.jdk.converter.DateTimeConverter;
|
|
|
+import cn.reghao.jutil.jdk.db.Page;
|
|
|
+import cn.reghao.jutil.jdk.result.Result;
|
|
|
+import cn.reghao.oss.sdk.model.constant.ObjectType;
|
|
|
+import cn.reghao.oss.sdk.model.dto.ObjectInfo;
|
|
|
+import cn.reghao.tnb.common.auth.UserContext;
|
|
|
+import cn.reghao.tnb.common.util.StringUtil;
|
|
|
+import cn.reghao.tnb.content.app.disk.db.mapper.NetDiskMapper;
|
|
|
+import cn.reghao.tnb.content.app.disk.model.dto.CreateDir;
|
|
|
+import cn.reghao.tnb.content.app.disk.model.dto.MoveFile;
|
|
|
+import cn.reghao.tnb.content.app.disk.model.po.NetDisk;
|
|
|
+import cn.reghao.tnb.content.app.disk.model.query.DiskQuery;
|
|
|
+import cn.reghao.tnb.content.app.disk.model.vo.NamePath;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author reghao
|
|
|
+ * @date 2023-11-10 23:48:48
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class NetDiskService {
|
|
|
+ private final NetDiskMapper netDiskMapper;
|
|
|
+
|
|
|
+ public NetDiskService(NetDiskMapper netDiskMapper) {
|
|
|
+ this.netDiskMapper = netDiskMapper;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String createFolder(String parentPath, String folderName, long owner) {
|
|
|
+ String pid = getFolderId(parentPath, owner, false);
|
|
|
+ if (pid == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ NamePath namePath = getUniqueNamePath(parentPath, folderName, owner);
|
|
|
+ String fileId = UUID.randomUUID().toString().replace("-", "");
|
|
|
+ NetDisk diskFile = new NetDisk(fileId, pid, namePath.getPath(), namePath.getFilename());
|
|
|
+ netDiskMapper.save(diskFile);
|
|
|
+ return fileId;
|
|
|
+ }
|
|
|
+
|
|
|
+ public synchronized String createDirs(String dirPath, long owner) {
|
|
|
+ String pid = "0";
|
|
|
+ if (dirPath.equals("/")) {
|
|
|
+ return pid;
|
|
|
+ }
|
|
|
+
|
|
|
+ DiskQuery diskQuery = new DiskQuery.Builder()
|
|
|
+ .path(dirPath)
|
|
|
+ .owner(owner)
|
|
|
+ .build();
|
|
|
+ Page page = new Page(1, 10);
|
|
|
+ List<NetDisk> netDiskList = netDiskMapper.findDiskQueryByPage(diskQuery, page);
|
|
|
+ if (!netDiskList.isEmpty()) {
|
|
|
+ return netDiskList.get(0).getFileId();
|
|
|
+ }
|
|
|
+
|
|
|
+ String savedDirPath = dirPath;
|
|
|
+ List<String> list = new ArrayList<>();
|
|
|
+ list.add(dirPath);
|
|
|
+
|
|
|
+ int idx = dirPath.lastIndexOf("/");
|
|
|
+ while (idx != 0) {
|
|
|
+ dirPath = dirPath.substring(0, idx);
|
|
|
+ list.add(dirPath);
|
|
|
+ idx = dirPath.lastIndexOf("/");
|
|
|
+ }
|
|
|
+ Collections.reverse(list);
|
|
|
+
|
|
|
+ for (String parent : list) {
|
|
|
+ diskQuery = new DiskQuery.Builder()
|
|
|
+ .path(parent)
|
|
|
+ .owner(owner)
|
|
|
+ .build();
|
|
|
+ netDiskList = netDiskMapper.findDiskQueryByPage(diskQuery, page);
|
|
|
+ if (!netDiskList.isEmpty()) {
|
|
|
+ pid = netDiskList.get(0).getFileId();
|
|
|
+ } else {
|
|
|
+ String fileId = UUID.randomUUID().toString().replace("-", "");
|
|
|
+ String path = parent;
|
|
|
+ String dirname = parent.substring(parent.lastIndexOf("/")+1);
|
|
|
+ netDiskMapper.save(new NetDisk(fileId, pid, path, dirname));
|
|
|
+ pid = fileId;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ diskQuery = new DiskQuery.Builder()
|
|
|
+ .path(savedDirPath)
|
|
|
+ .owner(owner)
|
|
|
+ .build();
|
|
|
+ netDiskList = netDiskMapper.findDiskQueryByPage(diskQuery, page);
|
|
|
+ return netDiskList.get(0).getFileId();
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean checkFile(CreateDir createDir) {
|
|
|
+ long owner = UserContext.getUserId();
|
|
|
+ String path = createDir.getPath();
|
|
|
+ String sha256sum = createDir.getSha256sum();
|
|
|
+
|
|
|
+ DiskQuery diskQuery = new DiskQuery.Builder()
|
|
|
+ .path(path)
|
|
|
+ .sha256sum(sha256sum)
|
|
|
+ .owner(owner)
|
|
|
+ .build();
|
|
|
+ Page page = new Page(1, 10);
|
|
|
+ List<NetDisk> netDiskList = netDiskMapper.findDiskQueryByPage(diskQuery, page);
|
|
|
+ return !netDiskList.isEmpty();
|
|
|
+ }
|
|
|
+
|
|
|
+ public Result addNetDisk(ObjectInfo diskFile, String path) {
|
|
|
+ long owner = UserContext.getUserId();
|
|
|
+ String uploadId = diskFile.getObjectId();
|
|
|
+ try {
|
|
|
+ String filename = diskFile.getFilename();
|
|
|
+ long size = diskFile.getSize();
|
|
|
+ int fileType = diskFile.getFileType();
|
|
|
+ String sha256sum = diskFile.getSha256sum();
|
|
|
+
|
|
|
+ String parentPath;
|
|
|
+ if (path.equals("/")) {
|
|
|
+ parentPath = "/";
|
|
|
+ } else {
|
|
|
+ File file = new File(path);
|
|
|
+ parentPath = file.getParent();
|
|
|
+ }
|
|
|
+ String pid = createDirs(parentPath, owner);
|
|
|
+ int channelCode = 111;
|
|
|
+ NetDisk netDisk = new NetDisk(channelCode, uploadId, pid, path, filename, fileType, sha256sum, size);
|
|
|
+ netDiskMapper.save(netDisk);
|
|
|
+ return Result.success();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return Result.fail("上传文件失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getFolderId(String parent, long owner, boolean isFileId) {
|
|
|
+ if ("/".equals(parent)) {
|
|
|
+ return "0";
|
|
|
+ }
|
|
|
+
|
|
|
+ NetDisk parentFile;
|
|
|
+ if (isFileId) {
|
|
|
+ DiskQuery diskQuery = new DiskQuery.Builder()
|
|
|
+ .path(parent)
|
|
|
+ .build();
|
|
|
+ Page page = new Page(1, 10);
|
|
|
+ List<NetDisk> netDiskList = netDiskMapper.findDiskQueryByPage(diskQuery, page);
|
|
|
+ parentFile = netDiskList.get(0);
|
|
|
+ } else {
|
|
|
+ DiskQuery diskQuery = new DiskQuery.Builder()
|
|
|
+ .path(parent)
|
|
|
+ .owner(owner)
|
|
|
+ .build();
|
|
|
+ Page page = new Page(1, 10);
|
|
|
+ List<NetDisk> netDiskList = netDiskMapper.findDiskQueryByPage(diskQuery, page);
|
|
|
+ parentFile = netDiskList.get(0);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (parentFile == null) {
|
|
|
+ String msg = String.format("目录 %s 不存在", parent);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (parentFile.getFileType() != ObjectType.Dir.getCode()) {
|
|
|
+ String msg = String.format("%s 不是目录", parent);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ return parentFile.getFileId();
|
|
|
+ }
|
|
|
+
|
|
|
+ public Result rename(String fileId, String newFilename, long owner) {
|
|
|
+ DiskQuery diskQuery = new DiskQuery.Builder()
|
|
|
+ .fileId(fileId)
|
|
|
+ .build();
|
|
|
+ Page page = new Page(1, 10);
|
|
|
+ List<NetDisk> netDiskList = netDiskMapper.findDiskQueryByPage(diskQuery, page);
|
|
|
+ NetDisk current = netDiskList.get(0);
|
|
|
+ if (current == null) {
|
|
|
+ String msg = String.format("file %s not exist", fileId);
|
|
|
+ return Result.fail(msg);
|
|
|
+ }
|
|
|
+
|
|
|
+ String parentPath = getParentPath(current.getPath());
|
|
|
+ String newPath = String.format("%s/%s", parentPath, newFilename);
|
|
|
+
|
|
|
+ diskQuery = new DiskQuery.Builder()
|
|
|
+ .owner(owner)
|
|
|
+ .path(newPath)
|
|
|
+ .build();
|
|
|
+ page = new Page(1, 10);
|
|
|
+ netDiskList = netDiskMapper.findDiskQueryByPage(diskQuery, page);
|
|
|
+ NetDisk netDisk = netDiskList.get(0);
|
|
|
+ if (netDisk != null) {
|
|
|
+ String msg = String.format("filename %s exist", newFilename);
|
|
|
+ return Result.fail(msg);
|
|
|
+ }
|
|
|
+
|
|
|
+ current.setPath(newPath);
|
|
|
+ current.setFilename(newFilename);
|
|
|
+ List<NetDisk> diskFileList = new ArrayList<>();
|
|
|
+ diskFileList.add(current);
|
|
|
+
|
|
|
+ if (current.getFileType() == ObjectType.Dir.getCode()) {
|
|
|
+ getChildren(current.getPath()).stream()
|
|
|
+ .filter(childFile -> !childFile.getFileId().equals(fileId))
|
|
|
+ .forEach(childFile -> {
|
|
|
+ String filename = childFile.getFilename();
|
|
|
+ NamePath namePath = getUniqueNamePath(newPath, filename, owner);
|
|
|
+
|
|
|
+ childFile.setPath(namePath.getPath());
|
|
|
+ childFile.setFilename(namePath.getFilename());
|
|
|
+ diskFileList.add(childFile);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!diskFileList.isEmpty()) {
|
|
|
+ //diskFile1Repository.updateFilenames(diskFileList);
|
|
|
+ }
|
|
|
+
|
|
|
+ return Result.success();
|
|
|
+ }
|
|
|
+
|
|
|
+ public Result move(MoveFile moveFile, long owner) {
|
|
|
+ String pid = moveFile.getPid();
|
|
|
+ List<String> fileIds = moveFile.getFileIds();
|
|
|
+ boolean copy = moveFile.getType() == 2;
|
|
|
+
|
|
|
+ DiskQuery diskQuery = new DiskQuery.Builder()
|
|
|
+ .fileId(pid)
|
|
|
+ .build();
|
|
|
+ Page page = new Page(1, 10);
|
|
|
+ List<NetDisk> netDiskList = netDiskMapper.findDiskQueryByPage(diskQuery, page);
|
|
|
+ NetDisk parentFile = netDiskList.get(0);
|
|
|
+ if (parentFile == null) {
|
|
|
+ String msg = "文件被移动/复制到的位置不存在";
|
|
|
+ return Result.fail(msg);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (parentFile.getFileType() != ObjectType.Dir.getCode()) {
|
|
|
+ String msg = "文件被移动/复制到的位置不是目录";
|
|
|
+ return Result.fail(msg);
|
|
|
+ }
|
|
|
+
|
|
|
+ Set<String> parents = new HashSet<>();
|
|
|
+ String pid1 = parentFile.getPid();
|
|
|
+ parents.add(pid1);
|
|
|
+ while (!pid1.equals("0")) {
|
|
|
+ diskQuery = new DiskQuery.Builder()
|
|
|
+ .fileId(pid1)
|
|
|
+ .build();
|
|
|
+ page = new Page(1, 10);
|
|
|
+ netDiskList = netDiskMapper.findDiskQueryByPage(diskQuery, page);
|
|
|
+ pid1 = netDiskList.get(0).getPid();
|
|
|
+ parents.add(pid1);
|
|
|
+ }
|
|
|
+
|
|
|
+ for (String fileId : fileIds) {
|
|
|
+ if (parents.contains(fileId)) {
|
|
|
+ String msg = "文件被移动/复制到的位置不能是其自身或子目录";
|
|
|
+ return Result.fail(msg);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ List<NetDisk> list = findByFileIdContains(fileIds);
|
|
|
+ if (list.isEmpty()) {
|
|
|
+ String msg = "被移动/复制的文件不存在";
|
|
|
+ return Result.fail(msg);
|
|
|
+ }
|
|
|
+
|
|
|
+ String currentPid = list.get(0).getPid();
|
|
|
+ for (int i = 1; i < list.size(); i++) {
|
|
|
+ if (!list.get(i).getPid().equals(currentPid)) {
|
|
|
+ String msg = "被移动/复制的文件必须来自同一目录";
|
|
|
+ return Result.fail(msg);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ List<NetDisk> copiedFiles = list.stream()
|
|
|
+ .filter(diskFile -> diskFile.getFileType() != ObjectType.Dir.getCode())
|
|
|
+ .peek(diskFile -> {
|
|
|
+ setRootPath(diskFile, parentFile, owner);
|
|
|
+ if (copy) {
|
|
|
+ reinitDiskFile(diskFile);
|
|
|
+ }
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+
|
|
|
+ List<NetDisk> list1 = new ArrayList<>();
|
|
|
+ list.stream()
|
|
|
+ .filter(diskFile -> diskFile.getFileType() == ObjectType.Dir.getCode())
|
|
|
+ .forEach(dirFile -> {
|
|
|
+ List<NetDisk> results = getChildren(dirFile.getPath());
|
|
|
+ NetDisk diskFile = list2tree(dirFile.getFileId(), results);
|
|
|
+
|
|
|
+ moveRoot(diskFile, parentFile, copy, owner);
|
|
|
+ List<NetDisk> list2 = new ArrayList<>();
|
|
|
+ tree2List(diskFile, list2);
|
|
|
+ list1.addAll(list2);
|
|
|
+ });
|
|
|
+
|
|
|
+ copiedFiles.addAll(list1);
|
|
|
+ if (!copiedFiles.isEmpty()) {
|
|
|
+ if (copy) {
|
|
|
+ //diskFile1Repository.saveAll(copiedFiles);
|
|
|
+ } else {
|
|
|
+ // diskFile1Repository.updateDiskFiles(copiedFiles);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return Result.success();
|
|
|
+ }
|
|
|
+
|
|
|
+ public Result move(String pid, List<String> fileIds, boolean copy, long owner) {
|
|
|
+ DiskQuery diskQuery = new DiskQuery.Builder()
|
|
|
+ .fileId(pid)
|
|
|
+ .build();
|
|
|
+ Page page = new Page(1, 10);
|
|
|
+ List<NetDisk> netDiskList = netDiskMapper.findDiskQueryByPage(diskQuery, page);
|
|
|
+ NetDisk parentFile = netDiskList.get(0);
|
|
|
+ if (parentFile == null) {
|
|
|
+ String msg = "文件被移动/复制到的位置不存在";
|
|
|
+ return Result.fail(msg);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (parentFile.getFileType() != ObjectType.Dir.getCode()) {
|
|
|
+ String msg = "文件被移动/复制到的位置不是目录";
|
|
|
+ return Result.fail(msg);
|
|
|
+ }
|
|
|
+
|
|
|
+ Set<String> parents = new HashSet<>();
|
|
|
+ String pid1 = parentFile.getPid();
|
|
|
+ parents.add(pid1);
|
|
|
+ while (!pid1.equals("0")) {
|
|
|
+ diskQuery = new DiskQuery.Builder()
|
|
|
+ .fileId(pid1)
|
|
|
+ .build();
|
|
|
+ page = new Page(1, 10);
|
|
|
+ netDiskList = netDiskMapper.findDiskQueryByPage(diskQuery, page);
|
|
|
+ pid1 = netDiskList.get(0).getPid();
|
|
|
+ parents.add(pid1);
|
|
|
+ }
|
|
|
+
|
|
|
+ for (String fileId : fileIds) {
|
|
|
+ if (parents.contains(fileId)) {
|
|
|
+ String msg = "文件被移动/复制到的位置不能是其自身或子目录";
|
|
|
+ return Result.fail(msg);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ List<NetDisk> list = findByFileIdContains(fileIds);
|
|
|
+ if (list.isEmpty()) {
|
|
|
+ String msg = "被移动/复制的文件不存在";
|
|
|
+ return Result.fail(msg);
|
|
|
+ }
|
|
|
+
|
|
|
+ String currentPid = list.get(0).getPid();
|
|
|
+ for (int i = 1; i < list.size(); i++) {
|
|
|
+ if (!list.get(i).getPid().equals(currentPid)) {
|
|
|
+ String msg = "被移动/复制的文件必须来自同一目录";
|
|
|
+ return Result.fail(msg);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ List<NetDisk> copiedFiles = list.stream()
|
|
|
+ .filter(diskFile -> diskFile.getFileType() != ObjectType.Dir.getCode())
|
|
|
+ .peek(diskFile -> {
|
|
|
+ setRootPath(diskFile, parentFile, owner);
|
|
|
+ if (copy) {
|
|
|
+ reinitDiskFile(diskFile);
|
|
|
+ }
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+
|
|
|
+ List<NetDisk> list1 = new ArrayList<>();
|
|
|
+ list.stream()
|
|
|
+ .filter(diskFile -> diskFile.getFileType() == ObjectType.Dir.getCode())
|
|
|
+ .forEach(dirFile -> {
|
|
|
+ List<NetDisk> results = getChildren(dirFile.getPath());
|
|
|
+ NetDisk diskFile = list2tree(dirFile.getFileId(), results);
|
|
|
+
|
|
|
+ moveRoot(diskFile, parentFile, copy, owner);
|
|
|
+ List<NetDisk> list2 = new ArrayList<>();
|
|
|
+ tree2List(diskFile, list2);
|
|
|
+ list1.addAll(list2);
|
|
|
+ });
|
|
|
+
|
|
|
+ copiedFiles.addAll(list1);
|
|
|
+ if (!copiedFiles.isEmpty()) {
|
|
|
+ if (copy) {
|
|
|
+ netDiskMapper.saveAll(copiedFiles);
|
|
|
+ } else {
|
|
|
+ //diskFile1Repository.updateDiskFiles(list1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return Result.success();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void moveRoot(NetDisk current, NetDisk parent, boolean reset, long owner) {
|
|
|
+ String oldParentPath1 = current.getPath();
|
|
|
+ setRootPath(current, parent, owner);
|
|
|
+ if (reset) {
|
|
|
+ reinitDiskFile(current);
|
|
|
+ }
|
|
|
+
|
|
|
+ List<NetDisk> list = current.getChildren();
|
|
|
+ if (!list.isEmpty()) {
|
|
|
+ for (NetDisk child : list) {
|
|
|
+ moveNode(child, current, oldParentPath1, reset);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void moveNode(NetDisk current, NetDisk parent, String oldParentPath, boolean reset) {
|
|
|
+ String oldParentPath1 = current.getPath();
|
|
|
+ setNodePath(current, parent, oldParentPath, reset);
|
|
|
+ if (reset) {
|
|
|
+ reinitDiskFile(current);
|
|
|
+ }
|
|
|
+
|
|
|
+ List<NetDisk> list = current.getChildren();
|
|
|
+ if (!list.isEmpty()) {
|
|
|
+ for (NetDisk child : list) {
|
|
|
+ moveNode(child, current, oldParentPath1, reset);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void setRootPath(NetDisk current, NetDisk parent, long owner) {
|
|
|
+ String pid = parent.getFileId();
|
|
|
+ String parentPath = parent.getPath();
|
|
|
+
|
|
|
+ String filename = current.getFilename();
|
|
|
+ NamePath namePath = getUniqueNamePath(parentPath, filename, owner);
|
|
|
+ current.setPid(pid);
|
|
|
+ current.setPath(namePath.getPath());
|
|
|
+ current.setFilename(namePath.getFilename());
|
|
|
+ }
|
|
|
+
|
|
|
+ private void setNodePath(NetDisk current, NetDisk parent, String oldParentPath, boolean resetPid) {
|
|
|
+ String pid = parent.getFileId();
|
|
|
+ String parentPath = parent.getPath();
|
|
|
+ String newPath = current.getPath().replace(oldParentPath, parentPath);
|
|
|
+ current.setPath(newPath);
|
|
|
+
|
|
|
+ if (resetPid) {
|
|
|
+ current.setPid(pid);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 重新初始化
|
|
|
+ *
|
|
|
+ * @param
|
|
|
+ * @return
|
|
|
+ * @date 2023-11-17 13:38:20
|
|
|
+ */
|
|
|
+ private void reinitDiskFile(NetDisk diskFile) {
|
|
|
+ String fileId = UUID.randomUUID().toString().replace("-", "");
|
|
|
+ diskFile.setFileId(fileId);
|
|
|
+ diskFile.setId(null);
|
|
|
+ diskFile.setCreateTime(null);
|
|
|
+ diskFile.setUpdateTime(null);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void delete(List<String> fileIds) {
|
|
|
+ List<NetDisk> list = findByFileIdContains(fileIds);
|
|
|
+ List<String> files = list.stream()
|
|
|
+ .filter(diskFile -> diskFile.getFileType() != ObjectType.Dir.getCode())
|
|
|
+ .map(NetDisk::getFileId)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ if (!files.isEmpty()) {
|
|
|
+ //diskFile1Repository.updateDeleteFiles(files);
|
|
|
+ }
|
|
|
+
|
|
|
+ List<String> files1 = list.stream()
|
|
|
+ .filter(diskFile -> diskFile.getFileType() == ObjectType.Dir.getCode())
|
|
|
+ .map(diskFile1 -> getChildren(diskFile1.getPath()))
|
|
|
+ .flatMap(Collection::stream)
|
|
|
+ .map(NetDisk::getFileId)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ if (!files1.isEmpty()) {
|
|
|
+ //diskFile1Repository.updateDeleteFiles(files1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void restore(List<String> fileIds) {
|
|
|
+ List<NetDisk> list = findByFileIdContains(fileIds);
|
|
|
+ List<String> files = list.stream()
|
|
|
+ .filter(diskFile -> diskFile.getFileType() != ObjectType.Dir.getCode())
|
|
|
+ .map(NetDisk::getFileId)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ list.stream()
|
|
|
+ .filter(diskFile -> diskFile.getFileType() == ObjectType.Dir.getCode())
|
|
|
+ .map(diskFile1 -> getChildren(diskFile1.getPath()))
|
|
|
+ .flatMap(Collection::stream)
|
|
|
+ .forEach(diskFile -> {
|
|
|
+ files.add(diskFile.getFileId());
|
|
|
+ });
|
|
|
+
|
|
|
+ if (!files.isEmpty()) {
|
|
|
+ //diskFile1Repository.updateRestoreFiles(files);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void erase(List<String> fileIds) {
|
|
|
+ List<NetDisk> list = findByFileIdContains(fileIds);
|
|
|
+ List<String> files = list.stream()
|
|
|
+ .filter(diskFile -> diskFile.getFileType() != ObjectType.Dir.getCode())
|
|
|
+ .map(NetDisk::getFileId)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ list.stream()
|
|
|
+ .filter(diskFile -> diskFile.getFileType() == ObjectType.Dir.getCode())
|
|
|
+ .map(diskFile -> findByPathPrefix(diskFile.getPath()))
|
|
|
+ .flatMap(Collection::stream)
|
|
|
+ .forEach(diskFile -> {
|
|
|
+ files.add(diskFile.getFileId());
|
|
|
+ });
|
|
|
+
|
|
|
+ if (!files.isEmpty()) {
|
|
|
+ //diskFile1Repository.deleteByFileIds(files);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<NetDisk> getChildren(String path) {
|
|
|
+ return findByPathPrefix(path);
|
|
|
+ }
|
|
|
+
|
|
|
+ public NetDisk list2tree(String rootId, List<NetDisk> list) {
|
|
|
+ Map<String, NetDisk> map = list.stream()
|
|
|
+ .collect(Collectors.toMap(NetDisk::getFileId, diskFile -> diskFile));
|
|
|
+ list.forEach(diskFile -> {
|
|
|
+ String pid = diskFile.getPid();
|
|
|
+ String fileId = diskFile.getFileId();
|
|
|
+ if (!fileId.equals(rootId)) {
|
|
|
+ NetDisk parent = map.get(pid);
|
|
|
+ parent.getChildren().add(diskFile);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ return map.get(rootId);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void tree2List(NetDisk root, List<NetDisk> list) {
|
|
|
+ list.add(root);
|
|
|
+ for (NetDisk child : root.getChildren()) {
|
|
|
+ tree2List(child, list);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public NamePath getUniqueNamePath(String parentPath, String filename, long owner) {
|
|
|
+ String path = getPath(parentPath, filename);
|
|
|
+ DiskQuery diskQuery = new DiskQuery.Builder()
|
|
|
+ .path(path)
|
|
|
+ .path(path)
|
|
|
+ .build();
|
|
|
+ Page page = new Page(1, 10);
|
|
|
+ List<NetDisk> netDiskList = netDiskMapper.findDiskQueryByPage(diskQuery, page);
|
|
|
+ NetDisk diskFile = netDiskList.get(0);
|
|
|
+ if (diskFile != null) {
|
|
|
+ filename = getNewFilename(filename);
|
|
|
+ path = getPath(parentPath, filename);
|
|
|
+ }
|
|
|
+
|
|
|
+ return new NamePath(filename, path);
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getNewFilename(String filename) {
|
|
|
+ String suffix = StringUtil.getSuffix(filename);
|
|
|
+ String prefix = filename.replace(suffix, "");
|
|
|
+ // yyyymmdd_hh:mm:ss 格式字符串
|
|
|
+ String dateTimeStr = DateTimeConverter.format(LocalDateTime.now())
|
|
|
+ .replace(" ", "_")
|
|
|
+ .replace("-", "")
|
|
|
+ .replace(":", "");;
|
|
|
+ return String.format("%s-%s", prefix, dateTimeStr + suffix);
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getPath(String parentPath, String filename) {
|
|
|
+ String path;
|
|
|
+ if (parentPath.equals("/")) {
|
|
|
+ path = parentPath + filename;
|
|
|
+ } else {
|
|
|
+ path = String.format("%s/%s" , parentPath, filename);
|
|
|
+ }
|
|
|
+
|
|
|
+ return path;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getParentPath(String path) {
|
|
|
+ int index = path.lastIndexOf("/");
|
|
|
+ return path.substring(0, index);
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<NetDisk> findByPathPrefix(String pathPrefix) {
|
|
|
+ /*Specification<NetDisk> specification = (root, query, cb) -> {
|
|
|
+ List<Predicate> predicates = new ArrayList<>();
|
|
|
+ predicates.add(cb.like(root.get("path"), "%" + pathPrefix + "%"));
|
|
|
+
|
|
|
+ // select * from app_building where app_id like '%test%' and app_name like '%测试%'
|
|
|
+ return cb.and(predicates.toArray(new Predicate[0]));
|
|
|
+ };*/
|
|
|
+
|
|
|
+ return netDiskMapper.findAll();
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<NetDisk> findByFileIdContains(List<String> fileIds) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<NamePath> getPathList(String path) {
|
|
|
+ List<NamePath> pathList = new ArrayList<>();
|
|
|
+ if (path.equals("/")) {
|
|
|
+ NamePath namePath = new NamePath("/", "/");
|
|
|
+ //pathList.add(namePath);
|
|
|
+ } else {
|
|
|
+ while (!path.equals("/")) {
|
|
|
+ int index = path.lastIndexOf("/");
|
|
|
+ String name = path.substring(index+1);
|
|
|
+ NamePath namePath = new NamePath(name, path);
|
|
|
+ pathList.add(namePath);
|
|
|
+
|
|
|
+ if (index == 0) {
|
|
|
+ path = path.substring(0, index+1);
|
|
|
+ } else {
|
|
|
+ path = path.substring(0, index);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ NamePath namePath = new NamePath("/", "/");
|
|
|
+ pathList.add(namePath);
|
|
|
+ }
|
|
|
+
|
|
|
+ Collections.reverse(pathList);
|
|
|
+ return pathList;
|
|
|
+ }
|
|
|
+}
|