From 1cef29fc8476096c7f9286843e448ae40bea4fc5 Mon Sep 17 00:00:00 2001 From: Veselin Penev Date: Sun, 5 Jan 2025 00:06:26 +0100 Subject: [PATCH] more fixes in file uploading & downloading --- src/lib/api_file_transfer.py | 6 ++-- src/lib/system.py | 11 +++++++ src/main.py | 3 -- src/screens/screen_device_info.py | 4 +++ src/screens/screen_single_private_file.py | 31 ++++++++++---------- src/screens/screen_single_shared_file.py | 35 ++++++++++++----------- 6 files changed, 51 insertions(+), 39 deletions(-) diff --git a/src/lib/api_file_transfer.py b/src/lib/api_file_transfer.py index 42c3fae..6fedc45 100644 --- a/src/lib/api_file_transfer.py +++ b/src/lib/api_file_transfer.py @@ -11,7 +11,7 @@ #------------------------------------------------------------------------------ -def file_download(source_path, destination_path, chunk_size=32*1024, result_callback=None): +def file_download(source_path, destination_path, chunk_size=64*1024, result_callback=None): if _Debug: print('api_file_transfer.file_download', source_path, destination_path) try: @@ -67,14 +67,14 @@ def on_chunk_received(resp, offset): @mainthread def do_chunk_request(offset): if _Debug: - print('api_file_transfer.file_download.do_chunk_request', source_path, offset) + print('api_file_transfer.file_download.do_chunk_request', source_path, destination_path, offset) api_client.chunk_read(path=source_path, offset=offset, max_size=chunk_size, cb=lambda resp: on_chunk_received(resp, offset)) do_chunk_request(0) #------------------------------------------------------------------------------ -def file_upload(source_path, chunk_size=32*1024, result_callback=None): +def file_upload(source_path, chunk_size=64*1024, result_callback=None): if _Debug: print('api_file_transfer.file_upload', source_path) try: diff --git a/src/lib/system.py b/src/lib/system.py index d216a94..30213bf 100644 --- a/src/lib/system.py +++ b/src/lib/system.py @@ -1,6 +1,7 @@ import os import subprocess import time +import platformdirs import threading from queue import Queue @@ -81,6 +82,16 @@ def get_app_data_path(): return os.path.join(os.path.expanduser('~'), '.bitdust') + +def get_downloads_dir(): + if is_android(): + from android.storage import app_storage_path # @UnresolvedImport + downloads_dir = os.path.join(app_storage_path(), 'downloads') + if not os.path.exists(downloads_dir): + os.makedirs(downloads_dir) + return downloads_dir + return platformdirs.user_downloads_dir() + #------------------------------------------------------------------------------ def android_sdk_version(): diff --git a/src/main.py b/src/main.py index beec131..b000efa 100644 --- a/src/main.py +++ b/src/main.py @@ -111,7 +111,6 @@ #------------------------------------------------------------------------------ if system.is_android(): - # from jnius import autoclass, cast # @UnresolvedImport import encodings.idna # @UnusedImport from android import mActivity # @UnresolvedImport @@ -127,8 +126,6 @@ print('BitDustApp primary_external_storage_path=%r' % primary_external_storage_path()) print('BitDustApp app_storage_path=%r' % app_storage_path()) - # from lib.permissions import check_permission, request_permissions # @UnresolvedImport - PACKAGE_NAME = u'org.bitdust_io.bitdust1' SERVICE_NAME = u'{packagename}.Service{servicename}'.format( packagename=PACKAGE_NAME, diff --git a/src/screens/screen_device_info.py b/src/screens/screen_device_info.py index 2acaa37..3a557be 100644 --- a/src/screens/screen_device_info.py +++ b/src/screens/screen_device_info.py @@ -38,6 +38,7 @@ def __init__(self, **kwargs): self.automat_index = None self.automat_id = None self.url = '' + self.deleted = False super(DeviceInfoScreen, self).__init__(**kwargs) def init_kwargs(self, **kw): @@ -83,6 +84,8 @@ def on_leave(self, *args): def populate(self, **kwargs): if _Debug: print('DeviceInfoScreen.populate') + if self.deleted: + return api_client.device_info( name=self.device_name, cb=self.on_device_info_result, @@ -135,6 +138,7 @@ def on_drop_down_menu_item_clicked(self, btn): def on_device_remove_result(self, resp): if _Debug: print('DeviceInfoScreen.on_device_remove_result', resp) + self.deleted = True screen.select_screen('settings_screen') screen.close_screen(screen_id='device_info_{}'.format(self.device_name)) screen.stack_clear() diff --git a/src/screens/screen_single_private_file.py b/src/screens/screen_single_private_file.py index cd1aa82..8413faa 100644 --- a/src/screens/screen_single_private_file.py +++ b/src/screens/screen_single_private_file.py @@ -1,6 +1,4 @@ import os -import tempfile -import platformdirs #------------------------------------------------------------------------------ @@ -54,7 +52,7 @@ def init_kwargs(self, **kw): self.details = kw.pop('details', {}) self.local_uri = None self.file_name = self.details.get('path') - self.downloaded_path = os.path.join(platformdirs.user_downloads_dir(), self.file_name) + self.downloaded_path = os.path.join(system.get_downloads_dir(), self.file_name) return kw def get_title(self): @@ -77,7 +75,7 @@ def populate(self, **kwargs): if _Debug: print('SinglePrivateFileScreen.populate', self.details) if system.is_android(): - download_path = self.local_uri or '' + download_path = self.downloaded_path or '' else: download_path = (self.downloaded_path if (self.downloaded_path and os.path.exists(self.downloaded_path)) else '') or '' ctx = self.details.copy() @@ -100,7 +98,7 @@ def populate(self, **kwargs): self.ids.open_file_button.disabled = not self.downloaded_path or not os.path.exists(self.downloaded_path) else: if system.is_android(): - self.ids.open_file_button.disabled = not self.local_uri + self.ids.open_file_button.disabled = not self.downloaded_path or not os.path.exists(self.downloaded_path) else: self.ids.open_file_button.disabled = not self.downloaded_path or not os.path.exists(self.downloaded_path) self.ids.download_file_button.disabled = screen.main_window().state_file_transfering @@ -151,30 +149,26 @@ def on_file_download_result(self, resp): snackbar.error(text=api_client.response_err(resp)) return local_path = api_client.response_result(resp).get('local_path') - destination_path = os.path.join(platformdirs.user_downloads_dir(), self.file_name) + destination_path = os.path.join(system.get_downloads_dir(), self.file_name) if not local_path: self.downloaded_path = None - # self.ids.open_file_button.disabled = True snackbar.error(text='file was not downloaded') self.populate() return if screen.control().is_local: try: - os.rename(local_path, platformdirs.user_downloads_dir()) + os.rename(local_path, system.get_downloads_dir()) except Exception as exc: self.downloaded_path = None - # self.ids.open_file_button.disabled = True snackbar.error(str(exc)) self.populate() return if os.path.exists(destination_path): self.downloaded_path = destination_path - # self.ids.open_file_button.disabled = False snackbar.success(text='downloading is complete') self.populate() else: self.downloaded_path = None - # self.ids.open_file_button.disabled = True snackbar.error(text='file was not downloaded') self.populate() return @@ -192,14 +186,12 @@ def on_file_transfer_result(self, result): if isinstance(result, Exception): snackbar.error(text=str(result)) return - destination_path = os.path.join(platformdirs.user_downloads_dir(), self.file_name) + destination_path = os.path.join(system.get_downloads_dir(), self.file_name) if system.is_android(): from androidstorage4kivy import SharedStorage # @UnresolvedImport self.local_uri = SharedStorage().copy_to_shared(private_file=destination_path) - # self.ids.open_file_button.disabled = not self.local_uri else: self.downloaded_path = destination_path - # self.ids.open_file_button.disabled = not os.path.exists(self.downloaded_path) snackbar.success(text='downloading is complete') self.populate() @@ -211,8 +203,13 @@ def on_open_file_button_clicked(self): system.open_path_in_os(self.downloaded_path) else: if system.is_android(): - if self.local_uri: - system.open_path_in_os(self.local_uri) + if self.downloaded_path: + from androidstorage4kivy import SharedStorage # @UnresolvedImport + self.local_uri = SharedStorage().copy_to_shared(private_file=self.downloaded_path) + if self.local_uri: + system.open_path_in_os(self.local_uri) + # if self.local_uri: + # system.open_path_in_os(self.local_uri) else: if self.downloaded_path: system.open_path_in_os(self.downloaded_path) @@ -241,6 +238,8 @@ def on_private_file_info_result(self, resp, remote_path, global_id): def on_private_file_details_ref_pressed(self, *args): if _Debug: print('SinglePrivateFileScreen.on_private_file_details_ref_pressed', args) + if screen.main_window().state_file_transfering: + return if args[1].startswith('download_'): backup_id = args[1][9:] screen.main_window().state_file_transfering = True diff --git a/src/screens/screen_single_shared_file.py b/src/screens/screen_single_shared_file.py index 7e39272..28c275b 100644 --- a/src/screens/screen_single_shared_file.py +++ b/src/screens/screen_single_shared_file.py @@ -1,5 +1,4 @@ import os -import platformdirs #------------------------------------------------------------------------------ @@ -53,7 +52,7 @@ def init_kwargs(self, **kw): self.details = kw.pop('details', {}) self.local_uri = None self.file_name = self.details.get('path') - self.downloaded_path = os.path.join(platformdirs.user_downloads_dir(), self.file_name) + self.downloaded_path = os.path.join(system.get_downloads_dir(), self.file_name) return kw def get_title(self): @@ -76,7 +75,7 @@ def populate(self, **kwargs): if _Debug: print('SingleSharedFileScreen.populate', self.details) if system.is_android(): - download_path = self.local_uri or '' + download_path = self.downloaded_path or '' else: download_path = (self.downloaded_path if (self.downloaded_path and os.path.exists(self.downloaded_path)) else '') or '' ctx = self.details.copy() @@ -99,7 +98,7 @@ def populate(self, **kwargs): self.ids.open_file_button.disabled = not self.downloaded_path or not os.path.exists(self.downloaded_path) else: if system.is_android(): - self.ids.open_file_button.disabled = not self.local_uri + self.ids.open_file_button.disabled = not self.downloaded_path or not os.path.exists(self.downloaded_path) else: self.ids.open_file_button.disabled = not self.downloaded_path or not os.path.exists(self.downloaded_path) self.ids.download_file_button.disabled = screen.main_window().state_file_transfering @@ -148,30 +147,26 @@ def on_file_download_result(self, resp): snackbar.error(text=api_client.response_err(resp)) return local_path = api_client.response_result(resp).get('local_path') - destination_path = os.path.join(platformdirs.user_downloads_dir(), self.file_name) + destination_path = os.path.join(system.get_downloads_dir(), self.file_name) if not local_path: self.downloaded_path = None - # self.ids.open_file_button.disabled = True snackbar.error(text='file was not downloaded') self.populate() return if screen.control().is_local: try: - os.rename(local_path, platformdirs.user_downloads_dir()) + os.rename(local_path, system.get_downloads_dir()) except Exception as exc: self.downloaded_path = None - # self.ids.open_file_button.disabled = True snackbar.error(str(exc)) self.populate() return if os.path.exists(destination_path): self.downloaded_path = destination_path - # self.ids.open_file_button.disabled = False snackbar.success(text='downloading is complete') self.populate() else: self.downloaded_path = None - # self.ids.open_file_button.disabled = True snackbar.error(text='file was not downloaded') self.populate() return @@ -189,14 +184,13 @@ def on_file_transfer_result(self, result): if isinstance(result, Exception): snackbar.error(text=str(result)) return - destination_path = os.path.join(platformdirs.user_downloads_dir(), self.file_name) + destination_path = os.path.join(system.get_downloads_dir(), self.file_name) if system.is_android(): - from androidstorage4kivy import SharedStorage # @UnresolvedImport - self.local_uri = SharedStorage().copy_to_shared(private_file=destination_path) - # self.ids.open_file_button.disabled = not self.local_uri + # from androidstorage4kivy import SharedStorage # @UnresolvedImport + # self.local_uri = SharedStorage().copy_to_shared(private_file=destination_path) + self.downloaded_path = destination_path else: self.downloaded_path = destination_path - # self.ids.open_file_button.disabled = not os.path.exists(self.downloaded_path) snackbar.success(text='downloading is complete') self.populate() @@ -208,8 +202,13 @@ def on_open_file_button_clicked(self): system.open_path_in_os(self.downloaded_path) else: if system.is_android(): - if self.local_uri: - system.open_path_in_os(self.local_uri) + if self.downloaded_path: + from androidstorage4kivy import SharedStorage # @UnresolvedImport + self.local_uri = SharedStorage().copy_to_shared(private_file=self.downloaded_path) + if self.local_uri: + system.open_path_in_os(self.local_uri) + # if self.local_uri: + # system.open_path_in_os(self.local_uri) else: if self.downloaded_path: system.open_path_in_os(self.downloaded_path) @@ -238,6 +237,8 @@ def on_shared_file_info_result(self, resp, remote_path, global_id): def on_shared_file_details_ref_pressed(self, *args): if _Debug: print('SingleSharedFileScreen.on_shared_file_details_ref_pressed', args) + if screen.main_window().state_file_transfering: + return if args[1].startswith('download_'): backup_id = args[1][9:] screen.main_window().state_file_transfering = True