hook up the downloaded content servlet

This commit is contained in:
Zlatin Balevsky
2019-12-13 23:57:51 +00:00
parent e8dd7d710d
commit 5147cf21a0
4 changed files with 52 additions and 12 deletions

View File

@@ -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<SharedFile> sharedFiles = core.getFileManager().getRootToFiles().get(infoHash);
if (sharedFiles == null || sharedFiles.isEmpty())
return null;
return sharedFiles.iterator().next().getFile();
}
}

View File

@@ -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("<Name>").append(Util.escapeHTMLinXML(sf.getFile().getName())).append("</Name>");
sb.append("<Path>").append(Util.escapeHTMLinXML(sf.getCachedPath())).append("</Path>");
sb.append("<Size>").append(DataHelper.formatSize2Decimal(sf.getCachedLength())).append("B").append("</Size>");
sb.append("<InfoHash>").append(Base64.encode(sf.getInfoHash().getRoot())).append("</InfoHash>");
if (sf.getComment() != null) {
String comment = DataUtil.readi18nString(Base64.decode(sf.getComment()));
sb.append("<Comment>").append(Util.escapeHTMLinXML(comment)).append("</Comment>");
@@ -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("<File>");
sb.append("<Name>").append(Util.escapeHTMLinXML(name)).append("</Name>");
sb.append("<InfoHash>").append(Base64.encode(infoHash.getRoot())).append("</InfoHash>");
sb.append("<Path>").append(Util.escapeHTMLinXML(path)).append("</Path>");
sb.append("<Size>").append(DataHelper.formatSize2Decimal(size, false)).append("B").append("</Size>");
if (comment != null) {

View File

@@ -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 = "<li>"+this.path+"<br/>"+ unshareLink + " " + certifyLink + " " + certified + " " +
commentLink + "<div id='comment-" + this.nodeId+ "'></div></li>"
var fetchLink = "<a href='/MuWire/DownloadedContent/" + this.infoHash + "'>" + _t("Fetch") + "</a>"
var html = "<li>"+this.path+"<br/>"+ unshareLink + " " + fetchLink + " " + certifyLink + " " + certified + " " +
commentLink + "<div id='comment-" + this.nodeId+ "'></div>"
html += "</li>"
div.innerHTML = html
} else {
if (this.children.length == 0) {
div.innerHTML = "<li><span><a class='caret' href='#' onclick='window.expand(\"" + this.nodeId + "\");return false'>" +
@@ -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)
}

View File

@@ -46,6 +46,11 @@
<servlet-class>com.muwire.webui.CertificateServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>com.muwire.webui.DownloadedContentServlet</servlet-name>
<servlet-class>com.muwire.webui.DownloadedContentServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>com.muwire.webui.MuWireServlet</servlet-name>
<url-pattern>/index.jsp</url-pattern>
@@ -81,6 +86,11 @@
<url-pattern>/Certificate</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>com.muwire.webui.DownloadedContentServlet</servlet-name>
<url-pattern>/DownloadedContent/*</url-pattern>
</servlet-mapping>
__JASPER__
<!--