implement progress updates while downloading nomadnet files and pages

This commit is contained in:
liamcottle
2024-05-02 00:14:13 +12:00
parent 850c4f457e
commit 37633a3f60
2 changed files with 51 additions and 15 deletions

View File

@@ -418,17 +418,22 @@
// handle success
if(nomadnetPageDownload.status === "success" && nomadnetPageDownloadCallback.onSuccessCallback){
nomadnetPageDownloadCallback.onSuccessCallback(nomadnetPageDownload.page_content);
delete this.nomadnetPageDownloadCallbacks[getNomadnetPageDownloadCallbackKey];
return;
}
// handle failure
if(nomadnetPageDownload.status === "failure" && nomadnetPageDownloadCallback.onFailureCallback){
nomadnetPageDownloadCallback.onFailureCallback(nomadnetPageDownload.failure_reason);
delete this.nomadnetPageDownloadCallbacks[getNomadnetPageDownloadCallbackKey];
return;
}
// forget callbacks for this page download
delete this.nomadnetPageDownloadCallbacks[getNomadnetPageDownloadCallbackKey];
// handle progress
if(nomadnetPageDownload.status === "progress" && nomadnetPageDownloadCallback.onProgressCallback){
nomadnetPageDownloadCallback.onProgressCallback(nomadnetPageDownload.progress);
return;
}
break;
@@ -449,17 +454,22 @@
// handle success
if(nomadnetFileDownload.status === "success" && nomadnetFileDownloadCallback.onSuccessCallback){
nomadnetFileDownloadCallback.onSuccessCallback(nomadnetFileDownload.file_name, nomadnetFileDownload.file_bytes);
delete this.nomadnetFileDownloadCallbacks[getNomadnetFileDownloadCallbackKey];
return;
}
// handle failure
if(nomadnetFileDownload.status === "failure" && nomadnetFileDownloadCallback.onFailureCallback){
nomadnetFileDownloadCallback.onFailureCallback(nomadnetFileDownload.failure_reason);
delete this.nomadnetFileDownloadCallbacks[getNomadnetFileDownloadCallbackKey];
return;
}
// forget callbacks for this file download
delete this.nomadnetFileDownloadCallbacks[getNomadnetFileDownloadCallbackKey];
// handle progress
if(nomadnetFileDownload.status === "progress" && nomadnetFileDownloadCallback.onProgressCallback){
nomadnetFileDownloadCallback.onProgressCallback(nomadnetFileDownload.progress);
return;
}
break;
@@ -567,7 +577,7 @@
"display_name": this.displayName,
});
},
async downloadNomadNetFile(destinationHash, filePath, onSuccessCallback, onFailureCallback) {
async downloadNomadNetFile(destinationHash, filePath, onSuccessCallback, onFailureCallback, onProgressCallback) {
// do nothing if not connected to websocket
if(!this.isWebsocketConnected){
@@ -581,6 +591,7 @@
this.nomadnetFileDownloadCallbacks[this.getNomadnetFileDownloadCallbackKey(destinationHash, filePath)] = {
onSuccessCallback: onSuccessCallback,
onFailureCallback: onFailureCallback,
onProgressCallback: onProgressCallback,
};
// ask reticulum to download file from nomadnet
@@ -597,7 +608,7 @@
}
},
async downloadNomadNetPage(destinationHash, pagePath, onSuccessCallback, onFailureCallback) {
async downloadNomadNetPage(destinationHash, pagePath, onSuccessCallback, onFailureCallback, onProgressCallback) {
// do nothing if not connected to websocket
if(!this.isWebsocketConnected){
@@ -611,6 +622,7 @@
this.nomadnetPageDownloadCallbacks[this.getNomadnetPageDownloadCallbackKey(destinationHash, pagePath)] = {
onSuccessCallback: onSuccessCallback,
onFailureCallback: onFailureCallback,
onProgressCallback: onProgressCallback,
};
// ask reticulum to download page from nomadnet

42
web.py
View File

@@ -226,10 +226,22 @@ class ReticulumWebChat:
},
})))
# handle file download progress
def on_file_download_progress(progress):
asyncio.run(client.send(json.dumps({
"type": "nomadnet.file.download",
"nomadnet_file_download": {
"status": "progress",
"progress": progress,
"destination_hash": destination_hash.hex(),
"file_path": file_path,
},
})))
# todo: handle file download progress
# download the file
NomadnetFileDownloader(destination_hash, file_path, on_file_download_success, on_file_download_failure)
NomadnetFileDownloader(destination_hash, file_path, on_file_download_success, on_file_download_failure, on_file_download_progress)
# handle downloading a page from a nomadnet node
elif _type == "nomadnet.page.download":
@@ -265,10 +277,22 @@ class ReticulumWebChat:
},
})))
# handle page download progress
def on_page_download_progress(progress):
asyncio.run(client.send(json.dumps({
"type": "nomadnet.page.download",
"nomadnet_page_download": {
"status": "progress",
"progress": progress,
"destination_hash": destination_hash.hex(),
"page_path": page_path,
},
})))
# todo: handle page download progress
# download the page
NomadnetPageDownloader(destination_hash, page_path, on_page_download_success, on_page_download_failure)
NomadnetPageDownloader(destination_hash, page_path, on_page_download_success, on_page_download_failure, on_page_download_progress)
# unhandled type
else:
@@ -536,7 +560,7 @@ class LXMFAnnounceHandler:
class NomadnetDownloader:
def __init__(self, destination_hash: bytes, path: str, on_download_success: Callable[[bytes], None], on_download_failure: Callable[[str], None], timeout: int|None = None, auto_download=True):
def __init__(self, destination_hash: bytes, path: str, on_download_success: Callable[[bytes], None], on_download_failure: Callable[[str], None], on_progress_update: Callable[[float], None], timeout: int|None = None, auto_download=True):
self.app_name = "nomadnetwork"
self.aspects = "node"
self.destination_hash = destination_hash
@@ -544,6 +568,7 @@ class NomadnetDownloader:
self.timeout = timeout
self.on_download_success = on_download_success
self.on_download_failure = on_download_failure
self.on_progress_update = on_progress_update
if auto_download:
self.download()
@@ -595,16 +620,15 @@ class NomadnetDownloader:
# handle download progress
def on_progress(self, request_receipt):
print("response_progressed")
print(request_receipt)
self.on_progress_update(request_receipt.progress)
class NomadnetPageDownloader(NomadnetDownloader):
def __init__(self, destination_hash: bytes, page_path: str, on_page_download_success: Callable[[str], None], on_page_download_failure: Callable[[str], None], timeout: int|None = None, auto_download=True):
def __init__(self, destination_hash: bytes, page_path: str, on_page_download_success: Callable[[str], None], on_page_download_failure: Callable[[str], None], on_progress_update: Callable[[float], None], timeout: int|None = None, auto_download=True):
self.on_page_download_success = on_page_download_success
self.on_page_download_failure = on_page_download_failure
super().__init__(destination_hash, page_path, self.on_download_success, self.on_download_failure, timeout, auto_download)
super().__init__(destination_hash, page_path, self.on_download_success, self.on_download_failure, on_progress_update, timeout, auto_download)
# page download was successful, decode the response and send to provided callback
def on_download_success(self, response_bytes):
@@ -618,10 +642,10 @@ class NomadnetPageDownloader(NomadnetDownloader):
class NomadnetFileDownloader(NomadnetDownloader):
def __init__(self, destination_hash: bytes, page_path: str, on_file_download_success: Callable[[str, bytes], None], on_file_download_failure: Callable[[str], None], timeout: int|None = None, auto_download=True):
def __init__(self, destination_hash: bytes, page_path: str, on_file_download_success: Callable[[str, bytes], None], on_file_download_failure: Callable[[str], None], on_progress_update: Callable[[float], None], timeout: int|None = None, auto_download=True):
self.on_file_download_success = on_file_download_success
self.on_file_download_failure = on_file_download_failure
super().__init__(destination_hash, page_path, self.on_download_success, self.on_download_failure, timeout, auto_download)
super().__init__(destination_hash, page_path, self.on_download_success, self.on_download_failure, on_progress_update, timeout, auto_download)
# file download was successful, decode the response and send to provided callback
def on_download_success(self, response):