Skip to content

Commit 2c71785

Browse files
committed
small refactoring
1 parent 261ade4 commit 2c71785

File tree

1 file changed

+6
-11
lines changed

1 file changed

+6
-11
lines changed

mergin/client.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import pytz
2727
import dateutil.parser
2828

29-
from .utils import generate_checksum, move_file
29+
from .utils import generate_checksum, move_file, save_to_file
3030

3131

3232
class InvalidProject(Exception):
@@ -450,8 +450,6 @@ def backup_if_conflict(path, checksum):
450450
fetch_files = pull_changes["added"] + pull_changes["updated"]
451451
if fetch_files:
452452
temp_dir = os.path.join(directory, '.mergin', 'fetch_{}-{}'.format(local_info["version"], server_info["version"]))
453-
if not os.path.exists(temp_dir):
454-
os.makedirs(temp_dir)
455453
for file in fetch_files:
456454
self._download_file(project_path, server_info['version'], file, temp_dir)
457455
src = os.path.join(temp_dir, file["path"])
@@ -485,29 +483,26 @@ def _download_file(self, project_path, project_version, file, directory):
485483
:param directory: Project's directory
486484
:type directory: String
487485
"""
488-
chunk_size = 2 * 1024 * 1024
486+
chunk_size = 10 * 1024 * 1024
489487
query_params = {
490488
"file": file['path'],
491489
"version": project_version
492490
}
491+
file_dir = os.path.dirname(os.path.normpath(os.path.join(directory, file['path'])))
492+
basename = os.path.basename(file['path'])
493493
length = 0
494494
count = 0
495495
while length < file['size']:
496496
range_header = {"Range": "bytes={}-{}".format(length, length + chunk_size)}
497497
resp = self.get("/v1/project/raw/{}".format(project_path), data=query_params, headers=range_header)
498498
# TODO some kind of recovery? do_request already raises exception
499499
if resp.status in [200, 206]:
500-
file_dir = os.path.dirname(os.path.normpath(file['path']))
501-
if file_dir:
502-
if not os.path.exists(os.path.join(directory, file_dir)):
503-
os.makedirs(os.path.join(directory, file_dir))
504-
with open(os.path.join(directory, file['path'] + ".{}".format(count)), 'wb') as output:
505-
output.write(resp.read())
500+
save_to_file(resp, os.path.join(file_dir, basename+".{}".format(count)))
506501
length += (chunk_size + 1)
507502
count += 1
508503

509504
# merge chunks together (maybe do checksum check? (might be costly))
510-
with open(os.path.join(directory, file['path']), 'wb') as final:
505+
with open(os.path.join(file_dir, basename), 'wb') as final:
511506
for i in range(count):
512507
with open(os.path.join(directory, file['path'] + ".{}".format(i)), 'rb') as chunk:
513508
shutil.copyfileobj(chunk, final)

0 commit comments

Comments
 (0)