Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions qfieldcloud_sdk/sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import requests
import urllib3
from requests.adapters import HTTPAdapter, Retry
from requests_toolbelt.multipart.encoder import MultipartEncoderMonitor

from .interfaces import QfcException, QfcRequest, QfcRequestException
from .utils import calc_etag, log, add_trailing_slash_to_url
Expand Down Expand Up @@ -665,21 +666,39 @@ def upload_file(
# if the filepath is invalid, it will throw a new error `pathvalidate.ValidationError`
is_valid_filepath(str(local_filename))

local_file_size = local_filename.stat().st_size
with open(local_filename, "rb") as local_file:
upload_file = local_file
encoder_params = {}

if show_progress:
from tqdm import tqdm
from tqdm.utils import CallbackIOWrapper

progress_bar = tqdm(
total=local_filename.stat().st_size,
total=local_file_size,
unit_scale=True,
desc=local_filename.stem,
unit="B",
desc=f'Uploading "{remote_filename}"...',
)
upload_file = CallbackIOWrapper(progress_bar.update, local_file, "read")

def cb(monitor: MultipartEncoderMonitor) -> None:
progress_bar.n = monitor.bytes_read
progress_bar.refresh()

encoder_params["callback"] = cb
else:
logger.info(f'Uploading file "{remote_filename}"…')

multipart_data = MultipartEncoderMonitor.from_fields(
fields={
"file": (
str(remote_filename),
local_file,
None,
),
},
**encoder_params,
)

if upload_type == FileTransferType.PROJECT:
url = f"files/{project_id}/{remote_filename}"
elif upload_type == FileTransferType.PACKAGE:
Expand All @@ -695,8 +714,10 @@ def upload_file(
return self._request(
"POST",
url,
files={
"file": upload_file,
data=multipart_data,
headers={
"Content-Type": multipart_data.content_type,
"Accept": "application/json",
},
)

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ charset-normalizer>=3.2.0
click>=8.1.5
idna>=3.4
requests>=2.31.0
requests-toolbelt>=1.0.0
tqdm>=4.65.0
urllib3>=2.0.7
pathvalidate>=3.2.1