Implement additional application download events
This commit is contained in:
parent
513c2139d6
commit
2ee1d50a13
3 changed files with 30 additions and 3 deletions
|
@ -71,12 +71,31 @@ class ApplicationVersionController:
|
|||
|
||||
if response.status_code == 200:
|
||||
|
||||
file_hash = ApplicationVersionController.__calculate_file_hash(BytesIO(response.content))
|
||||
response_size = int(response.headers.get('Content-Length', 0))
|
||||
response_buffer = BytesIO()
|
||||
|
||||
block_size = 1024
|
||||
bytes_written = 0
|
||||
|
||||
for data in response.iter_content(block_size):
|
||||
|
||||
bytes_written += len(data)
|
||||
response_buffer.write(data)
|
||||
progress = (bytes_written / response_size) * 100 if response_size > 0 else 0
|
||||
|
||||
application_version_observer.notify('download_progress', application_version, dict(
|
||||
progress=progress
|
||||
))
|
||||
|
||||
application_version_observer.notify('download_finished', application_version)
|
||||
response_buffer.seek(0)
|
||||
|
||||
file_hash = ApplicationVersionController.__calculate_file_hash(response_buffer)
|
||||
|
||||
if file_hash != application_version.file_hash:
|
||||
raise FileIntegrityError('Application version file integrity could not be verified.')
|
||||
|
||||
with tarfile.open(fileobj=BytesIO(response.content), mode = 'r:gz') as tar_file:
|
||||
with tarfile.open(fileobj=response_buffer, mode = 'r:gz') as tar_file:
|
||||
|
||||
tar_file.extractall(application_version.get_installation_path())
|
||||
tar_file.close()
|
||||
|
@ -100,4 +119,6 @@ class ApplicationVersionController:
|
|||
hasher.update(buffer)
|
||||
buffer = file.read(65536)
|
||||
|
||||
file.seek(0)
|
||||
|
||||
return hasher.hexdigest()
|
||||
|
|
|
@ -4,4 +4,7 @@ from core.observers.BaseObserver import BaseObserver
|
|||
class ApplicationVersionObserver(BaseObserver):
|
||||
|
||||
def __init__(self):
|
||||
|
||||
self.on_downloading = []
|
||||
self.on_download_progress = []
|
||||
self.on_download_finished = []
|
||||
|
|
5
main.py
5
main.py
|
@ -36,7 +36,10 @@ if __name__ == '__main__':
|
|||
invoice_observer = InvoiceObserver()
|
||||
profile_observer = ProfileObserver()
|
||||
|
||||
application_version_observer.subscribe('downloading', lambda event: print(f'Downloading {ApplicationController.get(event.subject.application_code).name}, version {event.subject.version_number}...\n'))
|
||||
application_version_observer.subscribe('downloading', lambda event: print(f'Downloading {ApplicationController.get(event.subject.application_code).name}, version {event.subject.version_number}...'))
|
||||
application_version_observer.subscribe('download_progress', lambda event: print(f'Current progress: {event.meta.get('progress'):.2f}%', flush=True, end='\r'))
|
||||
application_version_observer.subscribe('download_finished', lambda event: print('\n'))
|
||||
|
||||
client_observer.subscribe('synchronizing', lambda event: print('Synchronizing...\n'))
|
||||
connection_observer.subscribe('connecting', lambda event: print(f'[{event.subject.get("attempt_count")}/{event.subject.get("maximum_number_of_attempts")}] Performing connection attempt...\n'))
|
||||
|
||||
|
|
Loading…
Reference in a new issue