use of com.qiwenshare.file.util.TreeNode in project qiwen-file by qiwenshare.
the class FileDealComp method insertTreeNode.
/**
* 组织一个树目录节点,文件移动的时候使用
* @param treeNode
* @param id
* @param filePath
* @param nodeNameQueue
* @return
*/
public TreeNode insertTreeNode(TreeNode treeNode, long id, String filePath, Queue<String> nodeNameQueue) {
List<TreeNode> childrenTreeNodes = treeNode.getChildren();
String currentNodeName = nodeNameQueue.peek();
if (currentNodeName == null) {
return treeNode;
}
filePath = filePath + currentNodeName + "/";
if (!isExistPath(childrenTreeNodes, currentNodeName)) {
// 1、判断有没有该子节点,如果没有则插入
// 插入
TreeNode resultTreeNode = new TreeNode();
resultTreeNode.setFilePath(filePath);
resultTreeNode.setLabel(nodeNameQueue.poll());
resultTreeNode.setId(++id);
childrenTreeNodes.add(resultTreeNode);
} else {
// 2、如果有,则跳过
nodeNameQueue.poll();
}
if (nodeNameQueue.size() != 0) {
for (int i = 0; i < childrenTreeNodes.size(); i++) {
TreeNode childrenTreeNode = childrenTreeNodes.get(i);
if (currentNodeName.equals(childrenTreeNode.getLabel())) {
childrenTreeNode = insertTreeNode(childrenTreeNode, id * 10, filePath, nodeNameQueue);
childrenTreeNodes.remove(i);
childrenTreeNodes.add(childrenTreeNode);
treeNode.setChildren(childrenTreeNodes);
}
}
} else {
treeNode.setChildren(childrenTreeNodes);
}
return treeNode;
}
use of com.qiwenshare.file.util.TreeNode in project qiwen-file by qiwenshare.
the class FileController method getFileTree.
@Operation(summary = "获取文件树", description = "文件移动的时候需要用到该接口,用来展示目录树", tags = { "file" })
@RequestMapping(value = "/getfiletree", method = RequestMethod.GET)
@ResponseBody
public RestResult<TreeNode> getFileTree() {
RestResult<TreeNode> result = new RestResult<TreeNode>();
JwtUser sessionUserBean = SessionUtil.getSession();
List<UserFile> userFileList = userFileService.selectFilePathTreeByUserId(sessionUserBean.getUserId());
TreeNode resultTreeNode = new TreeNode();
resultTreeNode.setLabel("/");
resultTreeNode.setId(0L);
long id = 1;
for (int i = 0; i < userFileList.size(); i++) {
UserFile userFile = userFileList.get(i);
String filePath = userFile.getFilePath() + userFile.getFileName() + "/";
Queue<String> queue = new LinkedList<>();
String[] strArr = filePath.split("/");
for (int j = 0; j < strArr.length; j++) {
if (!"".equals(strArr[j]) && strArr[j] != null) {
queue.add(strArr[j]);
}
}
if (queue.size() == 0) {
continue;
}
resultTreeNode = fileDealComp.insertTreeNode(resultTreeNode, id++, "/", queue);
}
List<TreeNode> treeNodeList = resultTreeNode.getChildren();
Collections.sort(treeNodeList, new Comparator<TreeNode>() {
@Override
public int compare(TreeNode o1, TreeNode o2) {
long i = o1.getId() - o2.getId();
return (int) i;
}
});
result.setSuccess(true);
result.setData(resultTreeNode);
return result;
}
Aggregations