download details view

This commit is contained in:
Zlatin Balevsky
2019-12-15 11:34:04 +00:00
parent 0cf368c1af
commit b08333c5ea
4 changed files with 115 additions and 35 deletions

View File

@@ -265,6 +265,10 @@ public class Downloader {
} }
active active
} }
public int getTotalWorkers() {
return activeWorkers.size();
}
public void resume() { public void resume() {
paused = false paused = false

View File

@@ -7,6 +7,7 @@ import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
@@ -54,38 +55,65 @@ public class DownloadServlet extends HttpServlet {
resp.sendError(403, "Not initialized"); resp.sendError(403, "Not initialized");
return; return;
} }
String section = req.getParameter("section");
List<Download> downloads = new ArrayList<>();
downloadManager.getDownloaders().forEach(d -> {
int speed = d.speed();
long ETA = Long.MAX_VALUE;
if (speed > 0)
ETA = (d.getNPieces() - d.donePieces()) * d.getPieceSize() * 1000 / speed;
int percent = -1;
if (d.getNPieces() != 0)
percent = (int)(d.donePieces() * 100 / d.getNPieces());
Download download = new Download(d.getInfoHash(),
d.getFile().getName(),
d.getCurrentState(),
speed,
ETA,
percent,
d.getLength());
downloads.add(download);
});
COMPARATORS.sort(downloads, req);
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append("<?xml version='1.0' encoding='UTF-8'?>"); sb.append("<?xml version='1.0' encoding='UTF-8'?>");
sb.append("<Downloads>"); if (section.equals("list")) {
downloads.forEach(d -> d.toXML(sb)); List<Download> downloads = new ArrayList<>();
sb.append("</Downloads>"); downloadManager.getDownloaders().forEach(d -> {
int speed = d.speed();
long ETA = Long.MAX_VALUE;
if (speed > 0)
ETA = (d.getNPieces() - d.donePieces()) * d.getPieceSize() * 1000 / speed;
int percent = -1;
if (d.getNPieces() != 0)
percent = (int)(d.donePieces() * 100 / d.getNPieces());
Download download = new Download(d.getInfoHash(),
d.getFile().getName(),
d.getCurrentState(),
speed,
ETA,
percent,
d.getLength());
downloads.add(download);
});
COMPARATORS.sort(downloads, req);
sb.append("<Downloads>");
downloads.forEach(d -> d.toXML(sb));
sb.append("</Downloads>");
} else if (section.equals("details")) {
String infoHashB64 = req.getParameter("infoHash");
InfoHash infoHash;
try {
infoHash = new InfoHash(Base64.decode(infoHashB64));
} catch (Exception bad) {
resp.sendError(403, "Bad param");
return;
}
Optional<Downloader> optional = downloadManager.getDownloaders().filter(d -> d.getInfoHash().equals(infoHash)).findFirst();
if (optional.isPresent()) {
Downloader downloader = optional.get();
sb.append("<Details>");
sb.append("<Path>").append(Util.escapeHTMLinXML(downloader.getFile().getAbsolutePath())).append("</Path>");
sb.append("<PieceSize>").append(downloader.getPieceSize()).append("</PieceSize>");
sb.append("<KnownSources>").append(downloader.getTotalWorkers()).append("</KnownSources>");
sb.append("<ActiveSources>").append(downloader.activeWorkers()).append("</ActiveSources>");
sb.append("<TotalPieces>").append(downloader.getNPieces()).append("</TotalPieces>");
sb.append("<DonePieces>").append(downloader.donePieces()).append("</DonePieces>");
sb.append("</Details>");
} else {
resp.sendError(404, "Not found");
return;
}
}
resp.setContentType("text/xml"); resp.setContentType("text/xml");
resp.setCharacterEncoding("UTF-8"); resp.setCharacterEncoding("UTF-8");

View File

@@ -24,6 +24,7 @@ public class Util {
private static final String[] jsStrings = { private static final String[] jsStrings = {
// alphabetical please // alphabetical please
_x("Actions"), _x("Actions"),
_x("Active Sources"),
_x("Allow browsing"), _x("Allow browsing"),
_x("Allow only trusted connections"), _x("Allow only trusted connections"),
_x("Allow others to view my trust list"), _x("Allow others to view my trust list"),
@@ -35,9 +36,11 @@ public class Util {
_x("Clear Finished"), _x("Clear Finished"),
_x("Comment"), _x("Comment"),
_x("Details for {0}"), _x("Details for {0}"),
_x("Done Pieces"),
_x("Down"), _x("Down"),
_x("Download"), _x("Download"),
_x("Download Settings"), _x("Download Settings"),
_x("Download Location"),
_x("Downloader"), _x("Downloader"),
_x("Downloading"), _x("Downloading"),
_x("Enter Reason (Optional)"), _x("Enter Reason (Optional)"),
@@ -52,6 +55,7 @@ public class Util {
_x("Imported"), _x("Imported"),
_x("Inbound tunnel length"), _x("Inbound tunnel length"),
_x("Inbound tunnel quantity"), _x("Inbound tunnel quantity"),
_x("Known Sources"),
_x("Last Updated"), _x("Last Updated"),
_x("Mark Distrusted"), _x("Mark Distrusted"),
_x("Mark Neutral"), _x("Mark Neutral"),
@@ -60,6 +64,7 @@ public class Util {
_x("Outbound tunnel length"), _x("Outbound tunnel length"),
_x("Outbound tunnel quantity"), _x("Outbound tunnel quantity"),
_x("Pause"), _x("Pause"),
_x("Piece Size"),
_x("Progress"), _x("Progress"),
_x("Query"), _x("Query"),
_x("Reason"), _x("Reason"),
@@ -91,6 +96,7 @@ public class Util {
_x("Store incomplete files in"), _x("Store incomplete files in"),
_x("Subscribe"), _x("Subscribe"),
_x("Subscribed"), _x("Subscribed"),
_x("Total Pieces"),
_x("Total upload slots (-1 means unlimited)"), _x("Total upload slots (-1 means unlimited)"),
_x("Trust"), _x("Trust"),
_x("Trust Settings"), _x("Trust Settings"),

View File

@@ -91,10 +91,49 @@ function cancelDownload(infoHash) {
} }
function updateDownloader(infoHash) { function updateDownloader(infoHash) {
var selected = downloaders.get(infoHash); downloader = infoHash
var xmlhttp = new XMLHttpRequest()
var downloadDetailsDiv = document.getElementById("downloadDetails"); xmlhttp.onreadystatechange = function() {
downloadDetailsDiv.innerHTML = "<p>" + _t("Details for {0}", selected.name) + "</p>" if (this.readyState == 4 && this.status == 200) {
var path = this.responseXML.getElementsByTagName("Path")[0].childNodes[0].nodeValue
var pieceSize = this.responseXML.getElementsByTagName("PieceSize")[0].childNodes[0].nodeValue
var knownSources = this.responseXML.getElementsByTagName("KnownSources")[0].childNodes[0].nodeValue
var activeSources = this.responseXML.getElementsByTagName("ActiveSources")[0].childNodes[0].nodeValue
var totalPieces = this.responseXML.getElementsByTagName("TotalPieces")[0].childNodes[0].nodeValue
var donePieces = this.responseXML.getElementsByTagName("DonePieces")[0].childNodes[0].nodeValue
var html = "<table>"
html += "<tr>"
html += "<td>" + _t("Download Location") + "</td>"
html += "<td>" + "<p align='right'>" + path + "</p>" + "</td>"
html += "</tr>"
html += "<tr>"
html += "<td>" + _t("Known Sources") + "</td>"
html += "<td>" + "<p align='right'>" + knownSources + "</p>" + "</td>"
html += "</tr>"
html += "<tr>"
html += "<td>" + _t("Active Location") + "</td>"
html += "<td>" + "<p align='right'>" + activeSources + "</p>" + "</td>"
html += "</tr>"
html += "<tr>"
html += "<td>" + _t("Piece Size") + "</td>"
html += "<td>" + "<p align='right'>" + pieceSize + "</p>" + "</td>"
html += "</tr>"
html += "<tr>"
html += "<td>" + _t("Total Pieces") + "</td>"
html += "<td>" + "<p align='right'>" + totalPieces + "</p>" + "</td>"
html += "</tr>"
html += "<tr>"
html += "<td>" + _t("Done Pieces") + "</td>"
html += "<td>" + "<p align='right'>" + donePieces + "</p>" + "</td>"
html += "</tr>"
html += "</table>"
var downloadDetailsDiv = document.getElementById("downloadDetails");
downloadDetailsDiv.innerHTML = html
}
}
xmlhttp.open("GET", "/MuWire/Download?section=details&infoHash=" + infoHash)
xmlhttp.send()
} }
function refreshDownloader() { function refreshDownloader() {
@@ -134,13 +173,16 @@ function refreshDownloader() {
} else { } else {
downloadsDiv.innerHTML = "" downloadsDiv.innerHTML = ""
clearDiv.innerHTML = "" clearDiv.innerHTML = ""
downloader = null
var downloadDetailsDiv = document.getElementById("downloadDetails");
downloadDetailsDiv.innerHTML = ""
} }
if (downloader != null) if (downloader != null)
updateDownloader(downloader); updateDownloader(downloader);
} }
} }
var sortParam = "key=" + downloadsSortKey + "&order=" + downloadsSortOrder var sortParam = "&key=" + downloadsSortKey + "&order=" + downloadsSortOrder
xmlhttp.open("GET", "/MuWire/Download?" + sortParam, true); xmlhttp.open("GET", "/MuWire/Download?section=list" + sortParam, true);
xmlhttp.send(); xmlhttp.send();
} }