From 5147cf21a045e07a97494b82d48e9a7f42ff992b Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Fri, 13 Dec 2019 23:57:51 +0000 Subject: [PATCH] hook up the downloaded content servlet --- .../webui/DownloadedContentServlet.java | 22 +++++++++++++---- .../java/com/muwire/webui/FilesServlet.java | 8 ++++++- webui/src/main/js/files.js | 24 ++++++++++++++----- webui/templates/web.xml.template | 10 ++++++++ 4 files changed, 52 insertions(+), 12 deletions(-) diff --git a/webui/src/main/java/com/muwire/webui/DownloadedContentServlet.java b/webui/src/main/java/com/muwire/webui/DownloadedContentServlet.java index 48b0f23d..72cef9df 100644 --- a/webui/src/main/java/com/muwire/webui/DownloadedContentServlet.java +++ b/webui/src/main/java/com/muwire/webui/DownloadedContentServlet.java @@ -1,15 +1,25 @@ package com.muwire.webui; import java.io.File; +import java.util.Set; import javax.servlet.ServletConfig; import javax.servlet.ServletException; +import com.muwire.core.Core; +import com.muwire.core.InfoHash; +import com.muwire.core.SharedFile; + +import net.i2p.data.Base64; + public class DownloadedContentServlet extends BasicServlet { + private Core core; + @Override public void init(ServletConfig config) throws ServletException { super.init(config); + core = (Core) config.getServletContext().getAttribute("core"); loadMimeMap("com/muwire/webui/mime"); } @@ -20,10 +30,12 @@ public class DownloadedContentServlet extends BasicServlet { * @return file or null */ @Override - public File getResource(String pathInContext) - { - File r = null; - // TODO - return r; + public File getResource(String pathInContext) { + String infoHashB64 = pathInContext.substring("/DownloadedContent/".length()); + InfoHash infoHash = new InfoHash(Base64.decode(infoHashB64)); + Set sharedFiles = core.getFileManager().getRootToFiles().get(infoHash); + if (sharedFiles == null || sharedFiles.isEmpty()) + return null; + return sharedFiles.iterator().next().getFile(); } } diff --git a/webui/src/main/java/com/muwire/webui/FilesServlet.java b/webui/src/main/java/com/muwire/webui/FilesServlet.java index a1b5beda..763ad250 100644 --- a/webui/src/main/java/com/muwire/webui/FilesServlet.java +++ b/webui/src/main/java/com/muwire/webui/FilesServlet.java @@ -17,6 +17,7 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.muwire.core.Core; +import com.muwire.core.InfoHash; import com.muwire.core.SharedFile; import com.muwire.core.filecert.CertificateManager; import com.muwire.core.util.DataUtil; @@ -79,6 +80,7 @@ public class FilesServlet extends HttpServlet { if (sf.getComment() != null) comment = DataUtil.readi18nString(Base64.decode(sf.getComment())); FilesTableEntry entry = new FilesTableEntry(sf.getFile().getName(), + sf.getInfoHash(), sf.getCachedPath(), sf.getCachedLength(), comment, @@ -130,6 +132,7 @@ public class FilesServlet extends HttpServlet { sb.append("").append(Util.escapeHTMLinXML(sf.getFile().getName())).append(""); sb.append("").append(Util.escapeHTMLinXML(sf.getCachedPath())).append(""); sb.append("").append(DataHelper.formatSize2Decimal(sf.getCachedLength())).append("B").append(""); + sb.append("").append(Base64.encode(sf.getInfoHash().getRoot())).append(""); if (sf.getComment() != null) { String comment = DataUtil.readi18nString(Base64.decode(sf.getComment())); sb.append("").append(Util.escapeHTMLinXML(comment)).append(""); @@ -186,13 +189,15 @@ public class FilesServlet extends HttpServlet { private static class FilesTableEntry { private final String name; + private final InfoHash infoHash; private final String path; private final long size; private final String comment; private final boolean certified; - FilesTableEntry(String name, String path, long size, String comment, boolean certified) { + FilesTableEntry(String name, InfoHash infoHash, String path, long size, String comment, boolean certified) { this.name = name; + this.infoHash = infoHash; this.path = path; this.size = size; this.comment = comment; @@ -202,6 +207,7 @@ public class FilesServlet extends HttpServlet { void toXML(StringBuilder sb) { sb.append(""); sb.append("").append(Util.escapeHTMLinXML(name)).append(""); + sb.append("").append(Base64.encode(infoHash.getRoot())).append(""); sb.append("").append(Util.escapeHTMLinXML(path)).append(""); sb.append("").append(DataHelper.formatSize2Decimal(size, false)).append("B").append(""); if (comment != null) { diff --git a/webui/src/main/js/files.js b/webui/src/main/js/files.js index 98c47a2f..bb807c1f 100644 --- a/webui/src/main/js/files.js +++ b/webui/src/main/js/files.js @@ -1,9 +1,10 @@ class Node { - constructor(nodeId, parent, leaf, path, size, comment, certified, revision) { + constructor(nodeId, parent, leaf, infoHash, path, size, comment, certified, revision) { this.nodeId = nodeId this.parent = parent this.leaf = leaf + this.infoHash = infoHash this.children = [] this.path = path this.size = size @@ -28,8 +29,13 @@ class Node { if (this.certified == "true") certified = _t("Certified") - div.innerHTML = "
  • "+this.path+"
    "+ unshareLink + " " + certifyLink + " " + certified + " " + - commentLink + "
  • " + var fetchLink = "" + _t("Fetch") + "" + var html = "
  • "+this.path+"
    "+ unshareLink + " " + fetchLink + " " + certifyLink + " " + certified + " " + + commentLink + "
    " + + html += "
  • " + + div.innerHTML = html } else { if (this.children.length == 0) { div.innerHTML = "
  • " + @@ -53,6 +59,11 @@ class Node { } +function fetch(infoHash) { + var xmlhttp = new XMLHttpRequest() + xmlhttp.open("GET", "/MuWire/DownloadedContent/" + infoHash) + xmlhttp.send() +} function refreshStatus() { var xmlhttp = new XMLHttpRequest(); @@ -83,7 +94,7 @@ function refreshStatus() { } var treeRevision = -1 -var root = new Node("root",null,false,_t("Shared Files"), -1, null, false, -1) +var root = new Node("root",null,false, null, _t("Shared Files"), -1, null, false, -1) var nodesById = new Map() function initFiles() { @@ -120,6 +131,7 @@ function expand(nodeId) { var i for (i = 0; i < fileElements.length; i++) { var fileName = fileElements[i].getElementsByTagName("Name")[0].childNodes[0].nodeValue + var infoHash = fileElements[i].getElementsByTagName("InfoHash")[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) @@ -129,7 +141,7 @@ function expand(nodeId) { var certified = fileElements[i].getElementsByTagName("Certified")[0].childNodes[0].nodeValue var nodeId = node.nodeId + "_"+ Base64.encode(fileName) - var newFileNode = new Node(nodeId, node, true, fileName, size, comment, certified, revision) + var newFileNode = new Node(nodeId, node, true, infoHash, fileName, size, comment, certified, revision) nodesById.set(nodeId, newFileNode) node.children.push(newFileNode) } @@ -138,7 +150,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, null, false, revision) + var newDirNode = new Node(nodeId, node, false, null, dirName, -1, null, false, revision) nodesById.set(nodeId, newDirNode) node.children.push(newDirNode) } diff --git a/webui/templates/web.xml.template b/webui/templates/web.xml.template index d8774bf0..a55f252f 100644 --- a/webui/templates/web.xml.template +++ b/webui/templates/web.xml.template @@ -46,6 +46,11 @@ com.muwire.webui.CertificateServlet + + com.muwire.webui.DownloadedContentServlet + com.muwire.webui.DownloadedContentServlet + + com.muwire.webui.MuWireServlet /index.jsp @@ -81,6 +86,11 @@ /Certificate + + com.muwire.webui.DownloadedContentServlet + /DownloadedContent/* + + __JASPER__