diff --git a/webui/src/main/java/com/muwire/webui/BrowseServlet.java b/webui/src/main/java/com/muwire/webui/BrowseServlet.java index e78a02d2..7ee36b7f 100644 --- a/webui/src/main/java/com/muwire/webui/BrowseServlet.java +++ b/webui/src/main/java/com/muwire/webui/BrowseServlet.java @@ -1,7 +1,11 @@ package com.muwire.webui; import java.io.ByteArrayInputStream; +import java.io.File; import java.io.IOException; +import java.util.Collections; +import java.util.Set; +import java.util.stream.Collectors; import javax.servlet.ServletConfig; import javax.servlet.ServletException; @@ -9,7 +13,11 @@ import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import com.muwire.core.Core; +import com.muwire.core.InfoHash; import com.muwire.core.Persona; +import com.muwire.core.download.UIDownloadEvent; +import com.muwire.core.search.UIResultEvent; import com.muwire.webui.BrowseManager.Browse; import net.i2p.data.Base64; @@ -18,6 +26,8 @@ import net.i2p.data.DataHelper; public class BrowseServlet extends HttpServlet { private BrowseManager browseManager; + private DownloadManager downloadManager; + private Core core; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { @@ -64,6 +74,7 @@ public class BrowseServlet extends HttpServlet { browse.getResults().forEach(result -> { sb.append(""); sb.append("").append(Util.escapeHTMLinXML(result.getName())).append(""); + sb.append("").append(downloadManager.isDownloading(result.getInfohash())).append(""); sb.append("").append(DataHelper.formatSize2Decimal(result.getSize(), false)).append("B").append(""); sb.append("").append(Base64.encode(result.getInfohash().getRoot())).append(""); if (result.getComment() != null) { @@ -106,12 +117,66 @@ public class BrowseServlet extends HttpServlet { } browseManager.browse(host); 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 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 public void init(ServletConfig cfg) throws ServletException { browseManager = (BrowseManager) cfg.getServletContext().getAttribute("browseManager"); + downloadManager = (DownloadManager) cfg.getServletContext().getAttribute("downloadManager"); + core = (Core) cfg.getServletContext().getAttribute("core"); } } diff --git a/webui/src/main/js/browse.js b/webui/src/main/js/browse.js index cc4c6ec5..9f87c374 100644 --- a/webui/src/main/js/browse.js +++ b/webui/src/main/js/browse.js @@ -1,9 +1,10 @@ class Result { - constructor(name, size, comment, infoHash) { + constructor(name, size, comment, infoHash, downloading) { this.name = name this.size = size this.infoHash = infoHash this.comment = comment + this.downloading = downloading } } @@ -89,6 +90,7 @@ function showResults(host) { for (i = 0; i < results.length; i++) { var name = results[i].getElementsByTagName("Name")[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 comment = results[i].getElementsByTagName("Comment") if (comment != null && comment.length == 1) @@ -96,18 +98,22 @@ function showResults(host) { else comment = null - var result = new Result(name, size, comment, infoHash) + var result = new Result(name, size, comment, infoHash, downloading) resultsByInfoHash.set(infoHash, result) } - var tableHtml = "" + var tableHtml = "
NameSize
" for (var [infoHash, result] of resultsByInfoHash) { tableHtml += "" tableHtml += "" tableHtml += "" - // TODO: download and show comment link + if (result.downloading == "true") + tableHtml += "" + else + tableHtml += "" + // TODO: show comment link tableHtml += "" } @@ -119,4 +125,22 @@ function showResults(host) { } xmlhttp.open("GET", "/MuWire/Browse?section=results&host="+browse.hostB64, true) xmlhttp.send() +} + +function getDownloadLink(host, infoHash) { + return "Download" +} + +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) } \ No newline at end of file
NameSizeDownload
" + result.name + "" + result.size + "Downloading" + getDownloadLink(host, infoHash) + "