get tree structure to display, no collapsing yet

pull/34/head
Zlatin Balevsky 2019-12-07 09:31:56 +00:00
parent 8224dda3fd
commit 8ae735e5c0
4 changed files with 112 additions and 25 deletions

View File

@ -26,6 +26,7 @@ public class FileManager {
private final FileTree<SharedFile> fileTree = new FileTree<>(); private final FileTree<SharedFile> fileTree = new FileTree<>();
private volatile String hashingFile; private volatile String hashingFile;
private volatile long revision;
public FileManager(Core core) { public FileManager(Core core) {
this.core = core; this.core = core;
@ -33,11 +34,13 @@ public class FileManager {
public void onFileLoadedEvent(FileLoadedEvent e) { public void onFileLoadedEvent(FileLoadedEvent e) {
fileTree.add(e.getLoadedFile().getFile(), e.getLoadedFile()); fileTree.add(e.getLoadedFile().getFile(), e.getLoadedFile());
revision++;
} }
public void onFileHashedEvent(FileHashedEvent e) { public void onFileHashedEvent(FileHashedEvent e) {
hashingFile = null; hashingFile = null;
fileTree.add(e.getSharedFile().getFile(), e.getSharedFile()); fileTree.add(e.getSharedFile().getFile(), e.getSharedFile());
revision++;
} }
public void onFileHashingEvent(FileHashingEvent e) { public void onFileHashingEvent(FileHashingEvent e) {
@ -45,8 +48,10 @@ public class FileManager {
} }
public void onFileDownloadedEvent(FileDownloadedEvent e) { public void onFileDownloadedEvent(FileDownloadedEvent e) {
if (core.getMuOptions().getShareDownloadedFiles()) if (core.getMuOptions().getShareDownloadedFiles()) {
fileTree.add(e.getDownloadedFile().getFile(), e.getDownloadedFile()); fileTree.add(e.getDownloadedFile().getFile(), e.getDownloadedFile());
revision++;
}
} }
void list(File parent, FileListCallback<SharedFile> callback) { void list(File parent, FileListCallback<SharedFile> callback) {
@ -61,6 +66,10 @@ public class FileManager {
return core.getFileManager().getFileToSharedFile().size(); return core.getFileManager().getFileToSharedFile().size();
} }
long getRevision() {
return revision;
}
void share(String filePath) { void share(String filePath) {
File file = new File(filePath); File file = new File(filePath);
FileSharedEvent event = new FileSharedEvent(); FileSharedEvent event = new FileSharedEvent();
@ -84,6 +93,7 @@ public class FileManager {
return; return;
fileTree.remove(file); fileTree.remove(file);
revision++;
FileUnsharedEvent event = new FileUnsharedEvent(); FileUnsharedEvent event = new FileUnsharedEvent();
event.setUnsharedFile(sf); event.setUnsharedFile(sf);
core.getEventBus().publish(event); core.getEventBus().publish(event);

View File

@ -31,16 +31,19 @@ public class FilesServlet extends HttpServlet {
if (section.equals("status")) { if (section.equals("status")) {
sb.append("<Status>"); sb.append("<Status>");
sb.append("<Count>").append(fileManager.numSharedFiles()).append("</Count>"); sb.append("<Count>").append(fileManager.numSharedFiles()).append("</Count>");
sb.append("<Revision>").append(fileManager.getRevision()).append("</Revision>");
String hashingFile = fileManager.getHashingFile(); String hashingFile = fileManager.getHashingFile();
if (hashingFile != null) if (hashingFile != null)
sb.append("<Hashing>").append(Util.escapeHTMLinXML(hashingFile)).append("</Hashing>"); sb.append("<Hashing>").append(Util.escapeHTMLinXML(hashingFile)).append("</Hashing>");
sb.append("</Status>"); sb.append("</Status>");
} else if (section.equals("files")) { } else if (section.equals("files")) {
sb.append("<Files>"); sb.append("<Files>");
sb.append("<Revision>").append(fileManager.getRevision()).append("</Revision>");
ListCallback cb = new ListCallback(sb); ListCallback cb = new ListCallback(sb);
String encodedPath = req.getParameter("path"); String encodedPath = req.getParameter("path");
File current = null; File current = null;
if (encodedPath != null) { if (encodedPath != null && encodedPath.length() > 0) {
String[] split = DataHelper.split(encodedPath, ","); String[] split = DataHelper.split(encodedPath, ",");
for (String element : split) { for (String element : split) {
element = Base64.decodeToString(element); element = Base64.decodeToString(element);
@ -88,7 +91,8 @@ public class FilesServlet extends HttpServlet {
} }
@Override @Override
public void onDirectory(File f) { public void onDirectory(File f) {
sb.append("<Directory>").append(Util.escapeHTMLinXML(f.getName())).append("</Directory>"); String name = f.getName().isEmpty() ? f.toString() : f.getName();
sb.append("<Directory>").append(Util.escapeHTMLinXML(name)).append("</Directory>");
} }
} }

View File

@ -1,4 +1,38 @@
class Node {
constructor(nodeId, parent, leaf, path, size, revision) {
this.nodeId = nodeId
this.parent = parent
this.leaf = leaf
this.children = []
this.path = path
this.size = size
this.revision = revision
}
updateDiv() {
var div = document.getElementById(this.nodeId)
if (this.leaf) {
div.innerHTML = "<li>"+this.path+"</li>"
} else {
if (this.children.length == 0) {
div.innerHTML = "<li><span><a href='#' onclick='window.expand(\"" + this.nodeId + "\");return false'>" +
this.path + "</a></span></li>"
} else {
var l = "<li>Collapse "+this.path+"<ul>"
var i
for (i = 0; i < this.children.length; i++) {
l += "<li>"
l += "<div id='" + this.children[i].nodeId+"'></div>"
l += "</li>"
}
l += "</ul></li>"
div.innerHTML = l
}
}
}
}
/** /**
* *
* Base64 encode / decode * Base64 encode / decode
@ -141,11 +175,6 @@ _utf8_decode : function (utftext) {
} }
function initFiles() {
setInterval(refreshStatus, 3000)
setTimeout(refreshStatus, 1)
}
function refreshStatus() { function refreshStatus() {
var xmlhttp = new XMLHttpRequest(); var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() { xmlhttp.onreadystatechange = function() {
@ -162,32 +191,74 @@ function refreshStatus() {
hashingSpan.innerHTML = "Hashing "+hashing[0].childNodes[0].nodeValue hashingSpan.innerHTML = "Hashing "+hashing[0].childNodes[0].nodeValue
} else } else
hashingSpan.innerHTML = ""; hashingSpan.innerHTML = "";
var newRevision = xmlDoc.getElementsByTagName("Revision")[0].childNodes[0].nodeValue
if (newRevision > treeRevision) {
// TODO: update expanded nodes
treeRevision = newRevision
}
} }
} }
xmlhttp.open("GET", "/MuWire/Files?section=status", true) xmlhttp.open("GET", "/MuWire/Files?section=status", true)
xmlhttp.send(); xmlhttp.send();
} }
var root = new Node("sharedTree",null,false) var treeRevision = -1
var root = new Node("root",null,false,"Shared Files", -1, -1)
var nodesById = new Map() var nodesById = new Map()
class Node { function initFiles() {
constructor(nodeId, parent, leaf, path) { setInterval(refreshStatus, 3000)
this.nodeId = nodeId setTimeout(refreshStatus, 1)
this.parent = parent
this.leaf = leaf
this.children = []
this.path = path
}
function updateDiv() { nodesById.set("root",root)
var div = document.getElementById(this.nodeId) root.updateDiv()
if (this.leaf) { }
div.innerHTML = "<li>"+this.path+</li>
} else { function expand(nodeId) {
if (children.length == 0) { var node = nodesById.get(nodeId)
div.innerHTML = "<li><span class='caret'>" + this.path + "</span></li>" var pathElements = []
var tmpNode = node
while(tmpNode.parent != null) {
pathElements.push(Base64.encode(tmpNode.path))
tmpNode = tmpNode.parent
}
var reversedPath = []
while(pathElements.length > 0)
reversedPath.push(pathElements.pop())
var encodedPath = reversedPath.join(",")
var xmlhttp = new XMLHttpRequest()
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var xmlDoc = this.responseXML
var revision = xmlDoc.getElementsByTagName("Revision")[0].childNodes[0].nodeValue
var fileElements = xmlDoc.getElementsByTagName("File")
var i
for (i = 0; i < fileElements.length; i++) {
var fileName = fileElements[i].getElementsByTagName("Name")[0].childNodes[0].nodeValue
var size = fileElements[i].getElementsByTagName("Size")[0].childNodes[0].nodeValue
var nodeId = node.nodeId + "_"+ fileName
var newFileNode = new Node(nodeId, node, true, fileName, size, revision)
nodesById.set(nodeId, newFileNode)
node.children.push(newFileNode)
}
var dirElements = xmlDoc.getElementsByTagName("Directory")
for (i = 0; i < dirElements.length; i++) {
var dirName = dirElements[i].childNodes[0].nodeValue
var nodeId = node.nodeId + "_"+ dirName
var newDirNode = new Node(nodeId, node, false, dirName, -1, revision)
nodesById.set(nodeId, newDirNode)
node.children.push(newDirNode)
}
node.updateDiv()
for (i = 0; i < node.children.length; i++) {
node.children[i].updateDiv()
} }
} }
} }
xmlhttp.open("GET", "/MuWire/Files?section=files&path="+encodedPath, true)
xmlhttp.send()
} }

View File

@ -14,7 +14,9 @@
<p>Shared Files <span id="count">0</span></p> <p>Shared Files <span id="count">0</span></p>
<p><span id="hashing"></span></p> <p><span id="hashing"></span></p>
<hr/> <hr/>
<div id="sharedTree"></div> <ul>
<div id="root"></div>
</ul>
<hr/> <hr/>
<form action="/MuWire/Files" method="post"> <form action="/MuWire/Files" method="post">
<input type="text" name="file"> <input type="text" name="file">