From 8bff987d30f5ab3625f7956ef8e6d3e83a9fe5ef Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Sat, 7 Dec 2019 17:19:13 +0000 Subject: [PATCH] implement adding comments to files --- .../java/com/muwire/webui/FileManager.java | 23 +++++++ .../java/com/muwire/webui/FilesServlet.java | 49 ++++++++++---- webui/src/main/js/files.js | 67 +++++++++++++++++-- 3 files changed, 120 insertions(+), 19 deletions(-) diff --git a/webui/src/main/java/com/muwire/webui/FileManager.java b/webui/src/main/java/com/muwire/webui/FileManager.java index 6e8ecbf7..2440a9ce 100644 --- a/webui/src/main/java/com/muwire/webui/FileManager.java +++ b/webui/src/main/java/com/muwire/webui/FileManager.java @@ -20,6 +20,7 @@ import com.muwire.core.files.FileSharedEvent; import com.muwire.core.files.FileTree; import com.muwire.core.files.FileTreeCallback; import com.muwire.core.files.FileUnsharedEvent; +import com.muwire.core.files.UICommentEvent; import net.i2p.data.Base64; @@ -113,6 +114,28 @@ public class FileManager { } } + void comment(File file, String comment) { + + if (file.isFile()) { + SharedFile sf = core.getFileManager().getFileToSharedFile().get(file); + if (sf == null) + return; + UICommentEvent e = new UICommentEvent(); + e.setOldComment(sf.getComment()); + sf.setComment(comment); + e.setSharedFile(sf); + revision++; + core.getEventBus().publish(e); + } else { + TraverseCallback cb = new TraverseCallback(); + fileTree.traverse(file, cb); + + for (SharedFile found : cb.found) { + comment(found.getFile(), comment); + } + } + } + private static class TraverseCallback implements FileTreeCallback { private final List found = new ArrayList<>(); diff --git a/webui/src/main/java/com/muwire/webui/FilesServlet.java b/webui/src/main/java/com/muwire/webui/FilesServlet.java index c6effd39..380d2512 100644 --- a/webui/src/main/java/com/muwire/webui/FilesServlet.java +++ b/webui/src/main/java/com/muwire/webui/FilesServlet.java @@ -89,6 +89,8 @@ public class FilesServlet extends HttpServlet { sb.append(""); sb.append("").append(Util.escapeHTMLinXML(f.getName())).append(""); sb.append("").append(DataHelper.formatSize2Decimal(value.getCachedLength())).append("B").append(""); + if (value.getComment() != null) + sb.append("").append(Util.escapeHTMLinXML(value.getComment())).append(""); // TODO: other stuff sb.append(""); } @@ -120,19 +122,42 @@ public class FilesServlet extends HttpServlet { resp.sendError(403,"Bad param"); return; } - File current = null; - for (String element : DataHelper.split(pathElements,",")) { - element = Util.unescapeHTMLinXML(Base64.decodeToString(element)); - if (element == null) { - resp.sendError(403,"Bad param"); - return; - } - if (current == null) - current = new File(element); - else - current = new File(current, element); + File file = getFromPathElements(pathElements); + if (file == null) { + resp.sendError(403, "Bad param"); + return; } - fileManager.unshareFile(current); + fileManager.unshareFile(file); + } else if (action.equals("comment")) { + String pathElements = req.getParameter("path"); + if (pathElements == null) { + resp.sendError(403,"Bad param"); + return; + } + File file = getFromPathElements(pathElements); + if (file == null) { + resp.sendError(403, "Bad param"); + return; + } + String comment = req.getParameter("comment"); // null is ok + if (comment.isEmpty()) + comment = null; + fileManager.comment(file, comment); } } + + private static File getFromPathElements(String pathElements) { + File current = null; + for (String element : DataHelper.split(pathElements,",")) { + element = Util.unescapeHTMLinXML(Base64.decodeToString(element)); + if (element == null) { + return null; + } + if (current == null) + current = new File(element); + else + current = new File(current, element); + } + return current; + } } diff --git a/webui/src/main/js/files.js b/webui/src/main/js/files.js index 243d925b..c724352e 100644 --- a/webui/src/main/js/files.js +++ b/webui/src/main/js/files.js @@ -1,26 +1,31 @@ class Node { - constructor(nodeId, parent, leaf, path, size, revision) { + constructor(nodeId, parent, leaf, path, size, comment, revision) { this.nodeId = nodeId this.parent = parent this.leaf = leaf this.children = [] this.path = path this.size = size + this.comment = comment this.revision = revision } updateDiv() { var div = document.getElementById(this.nodeId) var unshareLink = "Unshare" + var commentLink = "Comment"; if (this.leaf) { - div.innerHTML = "
  • "+this.path+"
    "+ unshareLink + "
  • " + div.innerHTML = "
  • "+this.path+"
    "+ unshareLink + " " + commentLink + "
  • " } else { if (this.children.length == 0) { div.innerHTML = "
  • " + - this.path + " " + unshareLink + "
  • " + this.path + " " + unshareLink + "" + commentLink + "
    " } else { - var l = "
  • "+this.path+" " + unshareLink + "
      " + var l = "
    • "+this.path+" " + unshareLink + l += " " + commentLink+"
      " + + l += "
        " var i for (i = 0; i < this.children.length; i++) { l += "
      • " @@ -205,7 +210,7 @@ function refreshStatus() { } var treeRevision = -1 -var root = new Node("root",null,false,"Shared Files", -1, -1) +var root = new Node("root",null,false,"Shared Files", -1, null, -1) var nodesById = new Map() function initFiles() { @@ -243,8 +248,14 @@ function expand(nodeId) { 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 comment = fileElements[i].getElementsByTagName("Comment") + if (comment != null && comment.length == 1) + comment = comment[0].childNodes[0].nodeValue + else + comment = null + var nodeId = node.nodeId + "_"+ Base64.encode(fileName) - var newFileNode = new Node(nodeId, node, true, fileName, size, revision) + var newFileNode = new Node(nodeId, node, true, fileName, size, comment, revision) nodesById.set(nodeId, newFileNode) node.children.push(newFileNode) } @@ -253,7 +264,7 @@ function expand(nodeId) { for (i = 0; i < dirElements.length; i++) { var dirName = dirElements[i].childNodes[0].nodeValue var nodeId = node.nodeId + "_"+ Base64.encode(dirName) - var newDirNode = new Node(nodeId, node, false, dirName, -1, revision) + var newDirNode = new Node(nodeId, node, false, dirName, -1, null, revision) nodesById.set(nodeId, newDirNode) node.children.push(newDirNode) } @@ -288,4 +299,46 @@ function unshare(nodeId) { xmlhttp.open("POST", "/MuWire/Files", true) xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); xmlhttp.send("action=unshare&path="+encodedPath) +} + +function showCommentForm(nodeId) { + var linkSpan = document.getElementById("comment-link-"+nodeId) + linkSpan.innerHTML="" + var commentDiv = document.getElementById("comment-"+nodeId) + + var node = nodesById.get(nodeId) + var existingComment = node.comment == null ? "" : node.comment + + var textArea = "" + var saveCommentLink = "Save" + var cancelCommentLink = "Cancel" + + var html = textArea + "
        " + saveCommentLink + " " + cancelCommentLink + + commentDiv.innerHTML = html +} + +function cancelComment(nodeId) { + var commentDiv = document.getElementById("comment-"+nodeId) + commentDiv.innerHTML = "" + + var node = nodesById.get(nodeId) + node.updateDiv() +} + +function saveComment(nodeId) { + var comment = document.getElementById("comment-text-"+nodeId).value + var node = nodesById.get(nodeId) + var encodedPath = encodedPathToRoot(node) + var xmlhttp = new XMLHttpRequest() + xmlhttp.onreadystatechange = function() { + if (this.readyState == 4 && this.status == 200) { + cancelComment(nodeId) + collapse(node.parent.nodeId) + expand(node.parent.nodeId) // this can probably be done smarter + } + } + xmlhttp.open("POST", "/MuWire/Files", true) + xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); + xmlhttp.send(encodeURI("action=comment&path="+encodedPath+ "&comment="+comment)) } \ No newline at end of file