Implement additional application download events

This commit is contained in:
codeking 2024-12-08 02:59:36 +01:00
parent 513c2139d6
commit 2ee1d50a13
3 changed files with 30 additions and 3 deletions

View file

@ -71,12 +71,31 @@ class ApplicationVersionController:
if response.status_code == 200: 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: if file_hash != application_version.file_hash:
raise FileIntegrityError('Application version file integrity could not be verified.') 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.extractall(application_version.get_installation_path())
tar_file.close() tar_file.close()
@ -100,4 +119,6 @@ class ApplicationVersionController:
hasher.update(buffer) hasher.update(buffer)
buffer = file.read(65536) buffer = file.read(65536)
file.seek(0)
return hasher.hexdigest() return hasher.hexdigest()

View file

@ -4,4 +4,7 @@ from core.observers.BaseObserver import BaseObserver
class ApplicationVersionObserver(BaseObserver): class ApplicationVersionObserver(BaseObserver):
def __init__(self): def __init__(self):
self.on_downloading = [] self.on_downloading = []
self.on_download_progress = []
self.on_download_finished = []

View file

@ -36,7 +36,10 @@ if __name__ == '__main__':
invoice_observer = InvoiceObserver() invoice_observer = InvoiceObserver()
profile_observer = ProfileObserver() 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')) 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')) connection_observer.subscribe('connecting', lambda event: print(f'[{event.subject.get("attempt_count")}/{event.subject.get("maximum_number_of_attempts")}] Performing connection attempt...\n'))