If a result is for a shared file, display it as Downloaded

pull/34/head
Zlatin Balevsky 2019-12-19 20:12:02 +00:00
parent 9df1d043e4
commit 5c05bd2562
5 changed files with 61 additions and 34 deletions

View File

@ -83,16 +83,22 @@ public class BrowseServlet extends HttpServlet {
return; // hmm
List<Result> wrapped = browse.getResults().stream().map(event -> {
return new Result(event, downloadManager.isDownloading(event.getInfohash()));
ResultStatus resultStatus = ResultStatus.AVAILABLE;
if (core.getFileManager().getRootToFiles().containsKey(event.getInfohash()))
resultStatus = ResultStatus.SHARED;
else if (downloadManager.isDownloading(event.getInfohash()))
resultStatus = ResultStatus.DOWNLOADING;
return new Result(event, resultStatus);
}).collect(Collectors.toList());
RESULT_COMPARATORS.sort(wrapped, req);
sb.append("<Results>");
wrapped.stream().map(Result::getEvent).forEach(result -> {
wrapped.stream().forEach(resultWrapper -> {
UIResultEvent result = resultWrapper.getEvent();
sb.append("<Result>");
sb.append("<Name>").append(Util.escapeHTMLinXML(result.getName())).append("</Name>");
sb.append("<Downloading>").append(downloadManager.isDownloading(result.getInfohash())).append("</Downloading>");
sb.append("<ResultStatus>").append(resultWrapper.resultStatus).append("</ResultStatus>");
sb.append("<Size>").append(DataHelper.formatSize2Decimal(result.getSize(), false)).append("B").append("</Size>");
sb.append("<InfoHash>").append(Base64.encode(result.getInfohash().getRoot())).append("</InfoHash>");
if (result.getComment() != null) {
@ -221,10 +227,10 @@ public class BrowseServlet extends HttpServlet {
private static class Result {
private final UIResultEvent event;
private final boolean downloading;
Result(UIResultEvent event, boolean downloading) {
private final ResultStatus resultStatus;
Result(UIResultEvent event, ResultStatus resultStatus) {
this.event = event;
this.downloading = downloading;
this.resultStatus = resultStatus;
}
UIResultEvent getEvent() {
@ -240,15 +246,15 @@ public class BrowseServlet extends HttpServlet {
return Long.compare(k.event.getSize(), v.event.getSize());
};
private static final Comparator<Result> BY_DOWNLOADING = (k, v) -> {
return Boolean.compare(k.downloading, v.downloading);
private static final Comparator<Result> BY_RESULT_STATUS = (k, v) -> {
return Collator.getInstance().compare(k.resultStatus.toString(), v.resultStatus.toString());
};
private static final ColumnComparators<Result> RESULT_COMPARATORS = new ColumnComparators<>();
static {
RESULT_COMPARATORS.add("Name", BY_NAME);
RESULT_COMPARATORS.add("Size", BY_SIZE);
RESULT_COMPARATORS.add("Download", BY_DOWNLOADING);
RESULT_COMPARATORS.add("Download", BY_RESULT_STATUS);
}
private static final Comparator<Browse> BY_HOST = (k, v) -> {

View File

@ -0,0 +1,5 @@
package com.muwire.webui;
enum ResultStatus {
AVAILABLE, DOWNLOADING, SHARED
}

View File

@ -143,8 +143,13 @@ public class SearchServlet extends HttpServlet {
Set<UIResultEvent> results = searchResults.getBySender().get(sender);
List<ResultFromSender> resultsFromSender = new ArrayList<>();
results.forEach(result -> {
ResultStatus resultStatus = ResultStatus.AVAILABLE;
if (core.getFileManager().getRootToFiles().containsKey(result.getInfohash()))
resultStatus = ResultStatus.SHARED;
else if (downloadManager.isDownloading(result.getInfohash()))
resultStatus = ResultStatus.DOWNLOADING;
ResultFromSender resultFromSender = new ResultFromSender(result,
downloadManager.isDownloading(result.getInfohash()));
resultStatus);
resultsFromSender.add(resultFromSender);
});
@ -174,9 +179,14 @@ public class SearchServlet extends HttpServlet {
List<Result> results = new ArrayList<>();
byInfohash.forEach( (infoHash, resultSet) -> {
UIResultEvent event = resultSet.iterator().next();
ResultStatus resultStatus = ResultStatus.AVAILABLE;
if (core.getFileManager().getRootToFiles().containsKey(infoHash))
resultStatus = ResultStatus.SHARED;
else if (downloadManager.isDownloading(infoHash))
resultStatus = ResultStatus.DOWNLOADING;
Result result = new Result(event.getName(),
event.getSize(),
downloadManager.isDownloading(infoHash),
resultStatus,
resultSet.size(),
infoHash);
results.add(result);
@ -299,15 +309,15 @@ public class SearchServlet extends HttpServlet {
private final String name;
private final long size;
private final InfoHash infoHash;
private final boolean downloading;
private final ResultStatus resultStatus;
private final String comment;
private final int certificates;
ResultFromSender(UIResultEvent e, boolean downloading) {
ResultFromSender(UIResultEvent e, ResultStatus resultStatus) {
this.name = e.getName();
this.size = e.getSize();
this.infoHash = e.getInfohash();
this.downloading = downloading;
this.resultStatus = resultStatus;
this.comment = e.getComment();
this.certificates = e.getCertificates();
}
@ -317,7 +327,7 @@ public class SearchServlet extends HttpServlet {
sb.append("<Name>").append(Util.escapeHTMLinXML(name)).append("</Name>");
sb.append("<Size>").append(DataHelper.formatSize2Decimal(size, false)).append("B").append("</Size>");
sb.append("<InfoHash>").append(Base64.encode(infoHash.getRoot())).append("</InfoHash>");
sb.append("<Downloading>").append(downloading).append("</Downloading>");
sb.append("<ResultStatus>").append(resultStatus).append("</ResultStatus>");
if (comment != null)
sb.append("<Comment>").append(Util.escapeHTMLinXML(comment)).append("</Comment>");
sb.append("<Certificates>").append(certificates).append("</Certificates>");
@ -328,14 +338,14 @@ public class SearchServlet extends HttpServlet {
private static class Result {
private final String name;
private final long size;
private final boolean downloading;
private final ResultStatus resultStatus;
private final int sources;
private final InfoHash infoHash;
Result(String name, long size, boolean downloading, int sources, InfoHash infoHash) {
Result(String name, long size, ResultStatus resultStatus, int sources, InfoHash infoHash) {
this.name = name;
this.size = size;
this.downloading = downloading;
this.resultStatus = resultStatus;
this.infoHash = infoHash;
this.sources = sources;
}
@ -345,7 +355,7 @@ public class SearchServlet extends HttpServlet {
sb.append("<Name>").append(Util.escapeHTMLinXML(name)).append("</Name>");
sb.append("<Size>").append(DataHelper.formatSize2Decimal(size, false)).append("B").append("</Size>");
sb.append("<InfoHash>").append(Base64.encode(infoHash.getRoot())).append("</InfoHash>");
sb.append("<Downloading>").append(downloading).append("</Downloading>");
sb.append("<ResultStatus>").append(resultStatus).append("</ResultStatus>");
sb.append("<Sources>").append(sources).append("</Sources>");
sb.append("</Result>");
}
@ -430,15 +440,15 @@ public class SearchServlet extends HttpServlet {
return Long.compare(k.size, v.size);
};
private static final Comparator<ResultFromSender> RESULT_FROM_SENDER_BY_DOWNLOAD = (k, v) -> {
return Boolean.compare(k.downloading, v.downloading);
private static final Comparator<ResultFromSender> RESULT_FROM_SENDER_BY_STATUS = (k, v) -> {
return Collator.getInstance().compare(k.resultStatus.toString(), v.resultStatus.toString());
};
private static final ColumnComparators<ResultFromSender> RESULT_FROM_SENDER_COMPARATORS = new ColumnComparators<>();
static {
RESULT_FROM_SENDER_COMPARATORS.add("Name", RESULT_FROM_SENDER_BY_NAME);
RESULT_FROM_SENDER_COMPARATORS.add("Size", RESULT_FROM_SENDER_BY_SIZE);
RESULT_FROM_SENDER_COMPARATORS.add("Download", RESULT_FROM_SENDER_BY_DOWNLOAD);
RESULT_FROM_SENDER_COMPARATORS.add("Download", RESULT_FROM_SENDER_BY_STATUS);
}
private static final Comparator<Result> RESULT_BY_NAME = (k, v) -> {
@ -449,8 +459,8 @@ public class SearchServlet extends HttpServlet {
return Long.compare(k.size, v.size);
};
private static final Comparator<Result> RESULT_BY_DOWNLOAD = (k, v) -> {
return Boolean.compare(k.downloading, v.downloading);
private static final Comparator<Result> RESULT_BY_STATUS = (k, v) -> {
return Collator.getInstance().compare(k.resultStatus.toString(), v.resultStatus.toString());
};
private static final Comparator<Result> RESULT_BY_SOURCES = (l, r) -> {
@ -461,7 +471,7 @@ public class SearchServlet extends HttpServlet {
static {
RESULT_COMPARATORS.add("Name", RESULT_BY_NAME);
RESULT_COMPARATORS.add("Size", RESULT_BY_SIZE);
RESULT_COMPARATORS.add("Download", RESULT_BY_DOWNLOAD);
RESULT_COMPARATORS.add("Download", RESULT_BY_STATUS);
RESULT_COMPARATORS.add("Sources", RESULT_BY_SOURCES);
}

View File

@ -1,10 +1,10 @@
class Result {
constructor(name, size, comment, infoHash, downloading, certificates, hostB64) {
constructor(name, size, comment, infoHash, resultStatus, certificates, hostB64) {
this.name = name
this.size = size
this.infoHash = infoHash
this.comment = comment
this.downloading = downloading
this.resultStatus = resultStatus
this.certificates = certificates
this.hostB64 = hostB64
}
@ -204,7 +204,7 @@ function showResults(host, key, descending) {
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 resultStatus = results[i].getElementsByTagName("ResultStatus")[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)
@ -213,7 +213,7 @@ function showResults(host, key, descending) {
comment = null
var certificates = results[i].getElementsByTagName("Certificates")[0].childNodes[0].nodeValue
var result = new Result(name, size, comment, infoHash, downloading, certificates, browse.hostB64)
var result = new Result(name, size, comment, infoHash, resultStatus, certificates, browse.hostB64)
resultsByInfoHash.set(infoHash, result)
}
@ -236,8 +236,10 @@ function showResults(host, key, descending) {
var sizeCell = result.size
var downloadCell = null
if (result.downloading == "true")
if (result.resultStatus == "DOWNLOADING")
downloadCell = "<a href='/MuWire/Downloads'>" + _t("Downloading") + "</a>"
else if (result.resultStatus == "SHARED")
downloadCell = "<a href='/MuWire/SharedFiles'>" + _t("Downloaded") + "</a>"
else
downloadCell = getDownloadLink(host, infoHash)

View File

@ -125,7 +125,7 @@ class ResultFromSender {
this.name = xmlNode.getElementsByTagName("Name")[0].childNodes[0].nodeValue
this.size = xmlNode.getElementsByTagName("Size")[0].childNodes[0].nodeValue
this.infoHash = xmlNode.getElementsByTagName("InfoHash")[0].childNodes[0].nodeValue
this.downloading = xmlNode.getElementsByTagName("Downloading")[0].childNodes[0].nodeValue
this.resultStatus = xmlNode.getElementsByTagName("ResultStatus")[0].childNodes[0].nodeValue
this.comment = null
try {
this.comment = xmlNode.getElementsByTagName("Comment")[0].childNodes[0].nodeValue
@ -182,8 +182,10 @@ class ResultFromSender {
}
getDownloadBlock() {
if (this.downloading == "true")
if (this.resultStatus == "DOWNLOADING")
return "<a href='/MuWire/Downloads'>" + _t("Downloading") + "</a>"
if (this.resultStatus == "SHARED")
return "<a href='/MuWire/SharedFiles'>" + _t("Downloaded") + "</a>"
var link = "<a href='#' onclick='window.download(\"" + this.infoHash + "\");return false;'>" + _t("Download") + "</a>"
var block = "<span id='download-" + this.infoHash + "'>" + link + "</span>"
return block
@ -223,7 +225,7 @@ class Result {
this.name = xmlNode.getElementsByTagName("Name")[0].childNodes[0].nodeValue
this.size = xmlNode.getElementsByTagName("Size")[0].childNodes[0].nodeValue
this.infoHash = xmlNode.getElementsByTagName("InfoHash")[0].childNodes[0].nodeValue
this.downloading = xmlNode.getElementsByTagName("Downloading")[0].childNodes[0].nodeValue
this.resultStatus = xmlNode.getElementsByTagName("ResultStatus")[0].childNodes[0].nodeValue
this.sources = xmlNode.getElementsByTagName("Sources")[0].childNodes[0].nodeValue
}
@ -240,8 +242,10 @@ class Result {
return "<a href='#' onclick='window.refreshSendersForResult(\"" + this.infoHash + "\");return false;'>" + this.name + "</a>"
}
getDownloadBlock() {
if (this.downloading == "true")
if (this.resultStatus == "DOWNLOADING")
return "<a href='/MuWire/Downloads'>" + _t("Downloading") + "</a>"
if (this.resultStatus == "SHARED")
return "<a href='/MuWire/SharedFiles'>" + _t("Downloaded") + "</a>"
var link = "<a href='#' onclick='window.download(\"" + this.infoHash + "\");return false;'>" + _t("Download") + "</a>"
var block = "<span id='download-" + this.infoHash + "'>" + link + "</span>"
return block