Skip to content

Commit

Permalink
Merge pull request #98 from vesellov/master
Browse files Browse the repository at this point in the history
more fixes in file uploading & downloading
  • Loading branch information
vesellov authored Jan 5, 2025
2 parents 6d1be56 + 1cef29f commit 949e568
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 39 deletions.
6 changes: 3 additions & 3 deletions src/lib/api_file_transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
11 changes: 11 additions & 0 deletions src/lib/system.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import subprocess
import time
import platformdirs
import threading
from queue import Queue

Expand Down Expand Up @@ -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():
Expand Down
3 changes: 0 additions & 3 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@
#------------------------------------------------------------------------------

if system.is_android():
# from jnius import autoclass, cast # @UnresolvedImport
import encodings.idna # @UnusedImport

from android import mActivity # @UnresolvedImport
Expand All @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions src/screens/screen_device_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand Down
31 changes: 15 additions & 16 deletions src/screens/screen_single_private_file.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import os
import tempfile
import platformdirs

#------------------------------------------------------------------------------

Expand Down Expand Up @@ -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):
Expand All @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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()

Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
35 changes: 18 additions & 17 deletions src/screens/screen_single_shared_file.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
import platformdirs

#------------------------------------------------------------------------------

Expand Down Expand Up @@ -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):
Expand All @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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()

Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 949e568

Please sign in to comment.