mirror of https://github.com/zlatinb/muwire
download functionality
parent
c59e038c2a
commit
80a89a5ac0
|
@ -1,7 +1,11 @@
|
||||||
package com.muwire.webui;
|
package com.muwire.webui;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import javax.servlet.ServletConfig;
|
import javax.servlet.ServletConfig;
|
||||||
import javax.servlet.ServletException;
|
import javax.servlet.ServletException;
|
||||||
|
@ -9,7 +13,11 @@ import javax.servlet.http.HttpServlet;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
import com.muwire.core.Core;
|
||||||
|
import com.muwire.core.InfoHash;
|
||||||
import com.muwire.core.Persona;
|
import com.muwire.core.Persona;
|
||||||
|
import com.muwire.core.download.UIDownloadEvent;
|
||||||
|
import com.muwire.core.search.UIResultEvent;
|
||||||
import com.muwire.webui.BrowseManager.Browse;
|
import com.muwire.webui.BrowseManager.Browse;
|
||||||
|
|
||||||
import net.i2p.data.Base64;
|
import net.i2p.data.Base64;
|
||||||
|
@ -18,6 +26,8 @@ import net.i2p.data.DataHelper;
|
||||||
public class BrowseServlet extends HttpServlet {
|
public class BrowseServlet extends HttpServlet {
|
||||||
|
|
||||||
private BrowseManager browseManager;
|
private BrowseManager browseManager;
|
||||||
|
private DownloadManager downloadManager;
|
||||||
|
private Core core;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||||
|
@ -64,6 +74,7 @@ public class BrowseServlet extends HttpServlet {
|
||||||
browse.getResults().forEach(result -> {
|
browse.getResults().forEach(result -> {
|
||||||
sb.append("<Result>");
|
sb.append("<Result>");
|
||||||
sb.append("<Name>").append(Util.escapeHTMLinXML(result.getName())).append("</Name>");
|
sb.append("<Name>").append(Util.escapeHTMLinXML(result.getName())).append("</Name>");
|
||||||
|
sb.append("<Downloading>").append(downloadManager.isDownloading(result.getInfohash())).append("</Downloading>");
|
||||||
sb.append("<Size>").append(DataHelper.formatSize2Decimal(result.getSize(), false)).append("B").append("</Size>");
|
sb.append("<Size>").append(DataHelper.formatSize2Decimal(result.getSize(), false)).append("B").append("</Size>");
|
||||||
sb.append("<InfoHash>").append(Base64.encode(result.getInfohash().getRoot())).append("</InfoHash>");
|
sb.append("<InfoHash>").append(Base64.encode(result.getInfohash().getRoot())).append("</InfoHash>");
|
||||||
if (result.getComment() != null) {
|
if (result.getComment() != null) {
|
||||||
|
@ -106,12 +117,66 @@ public class BrowseServlet extends HttpServlet {
|
||||||
}
|
}
|
||||||
browseManager.browse(host);
|
browseManager.browse(host);
|
||||||
resp.sendRedirect("/MuWire/BrowseHost.jsp");
|
resp.sendRedirect("/MuWire/BrowseHost.jsp");
|
||||||
} // TODO: implement canceling of browse
|
} else if (action.equals("download")) {
|
||||||
|
if (core == null) {
|
||||||
|
resp.sendError(403, "Not initialized");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String personaB64 = req.getParameter("host");
|
||||||
|
if (personaB64 == null) {
|
||||||
|
resp.sendError(403,"Bad param");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Persona host;
|
||||||
|
try {
|
||||||
|
host = new Persona(new ByteArrayInputStream(Base64.decode(personaB64)));
|
||||||
|
} catch (Exception bad) {
|
||||||
|
resp.sendError(403,"Bad param");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String infoHashB64 = req.getParameter("infoHash");
|
||||||
|
if (infoHashB64 == null) {
|
||||||
|
resp.sendError(403, "Bad param");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
final InfoHash infoHash;
|
||||||
|
try {
|
||||||
|
infoHash = new InfoHash(Base64.decode(infoHashB64));
|
||||||
|
} catch (Exception bad) {
|
||||||
|
resp.sendError(403, "Bad param");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Browse browse = browseManager.getBrowses().get(host);
|
||||||
|
if (browse == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
Set<UIResultEvent> results = browse.getResults().stream().filter(e -> e.getInfohash().equals(infoHash)).
|
||||||
|
collect(Collectors.toSet());
|
||||||
|
|
||||||
|
if (results.isEmpty())
|
||||||
|
return;
|
||||||
|
|
||||||
|
UIDownloadEvent event = new UIDownloadEvent();
|
||||||
|
UIResultEvent[] resultsArray = results.toArray(new UIResultEvent[0]);
|
||||||
|
event.setResult(resultsArray);
|
||||||
|
// TODO: sequential
|
||||||
|
// TODO: possible sources
|
||||||
|
event.setSources(Collections.emptySet());
|
||||||
|
event.setTarget(new File(core.getMuOptions().getDownloadLocation(), resultsArray[0].getName()));
|
||||||
|
core.getEventBus().publish(event);
|
||||||
|
try {
|
||||||
|
Thread.sleep(100);
|
||||||
|
} catch (InterruptedException ignore) {}
|
||||||
|
}
|
||||||
|
// TODO: implement canceling of browse
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init(ServletConfig cfg) throws ServletException {
|
public void init(ServletConfig cfg) throws ServletException {
|
||||||
browseManager = (BrowseManager) cfg.getServletContext().getAttribute("browseManager");
|
browseManager = (BrowseManager) cfg.getServletContext().getAttribute("browseManager");
|
||||||
|
downloadManager = (DownloadManager) cfg.getServletContext().getAttribute("downloadManager");
|
||||||
|
core = (Core) cfg.getServletContext().getAttribute("core");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
class Result {
|
class Result {
|
||||||
constructor(name, size, comment, infoHash) {
|
constructor(name, size, comment, infoHash, downloading) {
|
||||||
this.name = name
|
this.name = name
|
||||||
this.size = size
|
this.size = size
|
||||||
this.infoHash = infoHash
|
this.infoHash = infoHash
|
||||||
this.comment = comment
|
this.comment = comment
|
||||||
|
this.downloading = downloading
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -89,6 +90,7 @@ function showResults(host) {
|
||||||
for (i = 0; i < results.length; i++) {
|
for (i = 0; i < results.length; i++) {
|
||||||
var name = results[i].getElementsByTagName("Name")[0].childNodes[0].nodeValue
|
var name = results[i].getElementsByTagName("Name")[0].childNodes[0].nodeValue
|
||||||
var size = results[i].getElementsByTagName("Size")[0].childNodes[0].nodeValue
|
var size = results[i].getElementsByTagName("Size")[0].childNodes[0].nodeValue
|
||||||
|
var downloading = results[i].getElementsByTagName("Downloading")[0].childNodes[0].nodeValue
|
||||||
var infoHash = results[i].getElementsByTagName("InfoHash")[0].childNodes[0].nodeValue
|
var infoHash = results[i].getElementsByTagName("InfoHash")[0].childNodes[0].nodeValue
|
||||||
var comment = results[i].getElementsByTagName("Comment")
|
var comment = results[i].getElementsByTagName("Comment")
|
||||||
if (comment != null && comment.length == 1)
|
if (comment != null && comment.length == 1)
|
||||||
|
@ -96,18 +98,22 @@ function showResults(host) {
|
||||||
else
|
else
|
||||||
comment = null
|
comment = null
|
||||||
|
|
||||||
var result = new Result(name, size, comment, infoHash)
|
var result = new Result(name, size, comment, infoHash, downloading)
|
||||||
resultsByInfoHash.set(infoHash, result)
|
resultsByInfoHash.set(infoHash, result)
|
||||||
}
|
}
|
||||||
|
|
||||||
var tableHtml = "<table><thead><tr><th>Name</th><th>Size</th></tr></thead><tbody>"
|
var tableHtml = "<table><thead><tr><th>Name</th><th>Size</th><th>Download</th></tr></thead><tbody>"
|
||||||
|
|
||||||
for (var [infoHash, result] of resultsByInfoHash) {
|
for (var [infoHash, result] of resultsByInfoHash) {
|
||||||
|
|
||||||
tableHtml += "<tr>"
|
tableHtml += "<tr>"
|
||||||
tableHtml += "<td>" + result.name + "</td>"
|
tableHtml += "<td>" + result.name + "</td>"
|
||||||
tableHtml += "<td>" + result.size + "</td>"
|
tableHtml += "<td>" + result.size + "</td>"
|
||||||
// TODO: download and show comment link
|
if (result.downloading == "true")
|
||||||
|
tableHtml += "<td>Downloading</td>"
|
||||||
|
else
|
||||||
|
tableHtml += "<td>" + getDownloadLink(host, infoHash) + "</td>"
|
||||||
|
// TODO: show comment link
|
||||||
tableHtml += "</tr>"
|
tableHtml += "</tr>"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -120,3 +126,21 @@ function showResults(host) {
|
||||||
xmlhttp.open("GET", "/MuWire/Browse?section=results&host="+browse.hostB64, true)
|
xmlhttp.open("GET", "/MuWire/Browse?section=results&host="+browse.hostB64, true)
|
||||||
xmlhttp.send()
|
xmlhttp.send()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getDownloadLink(host, infoHash) {
|
||||||
|
return "<a href='#' onclick='window.download(\"" + host + "\",\"" + infoHash + "\");return false;'>Download</a>"
|
||||||
|
}
|
||||||
|
|
||||||
|
function download(host,infoHash) {
|
||||||
|
var result = resultsByInfoHash.get(infoHash)
|
||||||
|
var hostB64 = browsesByHost.get(host).hostB64
|
||||||
|
var xmlhttp = new XMLHttpRequest()
|
||||||
|
xmlhttp.onreadystatechange = function() {
|
||||||
|
if (this.readyState == 4 && this.status == 200) {
|
||||||
|
showResults(host)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
xmlhttp.open("POST", "/MuWire/Browse", true)
|
||||||
|
xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
|
||||||
|
xmlhttp.send("action=download&infoHash="+infoHash+"&host="+hostB64)
|
||||||
|
}
|
Loading…
Reference in New Issue