From c7c88dec04450b6f2588e24ce41c52a8fcf32603 Mon Sep 17 00:00:00 2001 From: CalDescent Date: Fri, 10 Dec 2021 12:22:26 +0000 Subject: [PATCH] Attempt to differentiate between resources that are downloading, and ones where downloading hasn't been attempted yet. --- .../api/model/ArbitraryResourceSummary.java | 1 + .../arbitrary/ArbitraryDataResource.java | 39 ++++++++++++++++++- src/main/resources/loading/index.html | 4 ++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/qortal/api/model/ArbitraryResourceSummary.java b/src/main/java/org/qortal/api/model/ArbitraryResourceSummary.java index d8d531d0..38e56c43 100644 --- a/src/main/java/org/qortal/api/model/ArbitraryResourceSummary.java +++ b/src/main/java/org/qortal/api/model/ArbitraryResourceSummary.java @@ -7,6 +7,7 @@ import javax.xml.bind.annotation.XmlAccessorType; public class ArbitraryResourceSummary { public enum ArbitraryResourceStatus { + NOT_STARTED, DOWNLOADING, DOWNLOADED, BUILDING, diff --git a/src/main/java/org/qortal/arbitrary/ArbitraryDataResource.java b/src/main/java/org/qortal/arbitrary/ArbitraryDataResource.java index 1849b913..b19471d7 100644 --- a/src/main/java/org/qortal/arbitrary/ArbitraryDataResource.java +++ b/src/main/java/org/qortal/arbitrary/ArbitraryDataResource.java @@ -68,9 +68,12 @@ public class ArbitraryDataResource { // Check if we have all data locally for this resource if (!this.allFilesDownloaded()) { - if (this.isDataPotentiallyAvailable()) { + if (this.isDownloading()) { return new ArbitraryResourceSummary(ArbitraryResourceStatus.DOWNLOADING); } + else if (this.isDataPotentiallyAvailable()) { + return new ArbitraryResourceSummary(ArbitraryResourceStatus.NOT_STARTED); + } return new ArbitraryResourceSummary(ArbitraryResourceStatus.MISSING_DATA); } @@ -146,6 +149,40 @@ public class ArbitraryDataResource { } + /** + * Best guess as to whether we are currently downloading a resource + * This is only used to give an indication to the user of progress + * @return - whether we are trying to download the resource + */ + private boolean isDownloading() { + try { + this.fetchTransactions(); + Long now = NTP.getTime(); + if (now == null) { + return false; + } + + List transactionDataList = new ArrayList<>(this.transactions); + + for (ArbitraryTransactionData transactionData : transactionDataList) { + long lastRequestTime = ArbitraryDataManager.getInstance().lastRequestForSignature(transactionData.getSignature()); + // If were have requested data in the last 30 seconds, treat it as "downloading" + if (lastRequestTime > 0 && now - lastRequestTime < 30 * 1000L) { + return true; + } + } + + // FUTURE: we may want to check for file hashes (including the metadata file hash) in + // ArbitraryDataManager.arbitraryDataFileRequests and return true if one is found. + + return false; + + } catch (DataException e) { + return false; + } + } + + private void fetchTransactions() throws DataException { if (this.transactions != null && !this.transactions.isEmpty()) { diff --git a/src/main/resources/loading/index.html b/src/main/resources/loading/index.html index eaa382a4..3c9baa22 100644 --- a/src/main/resources/loading/index.html +++ b/src/main/resources/loading/index.html @@ -78,6 +78,10 @@ else if (json.status == "BUILD_FAILED") { textStatus = "Build failed. Please try again later."; } + else if (json.status == "NOT_STARTED") { + textStatus = "Waiting..."; + retryInterval = 1000; + } else if (json.status == "DOWNLOADING") { textStatus = "Locating and downloading files..."; retryInterval = 1000;