|
26 | 26 | import pytz |
27 | 27 | import dateutil.parser |
28 | 28 |
|
29 | | -from .utils import generate_checksum, move_file |
| 29 | +from .utils import generate_checksum, move_file, save_to_file |
30 | 30 |
|
31 | 31 |
|
32 | 32 | class InvalidProject(Exception): |
@@ -450,8 +450,6 @@ def backup_if_conflict(path, checksum): |
450 | 450 | fetch_files = pull_changes["added"] + pull_changes["updated"] |
451 | 451 | if fetch_files: |
452 | 452 | 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) |
455 | 453 | for file in fetch_files: |
456 | 454 | self._download_file(project_path, server_info['version'], file, temp_dir) |
457 | 455 | src = os.path.join(temp_dir, file["path"]) |
@@ -485,29 +483,26 @@ def _download_file(self, project_path, project_version, file, directory): |
485 | 483 | :param directory: Project's directory |
486 | 484 | :type directory: String |
487 | 485 | """ |
488 | | - chunk_size = 2 * 1024 * 1024 |
| 486 | + chunk_size = 10 * 1024 * 1024 |
489 | 487 | query_params = { |
490 | 488 | "file": file['path'], |
491 | 489 | "version": project_version |
492 | 490 | } |
| 491 | + file_dir = os.path.dirname(os.path.normpath(os.path.join(directory, file['path']))) |
| 492 | + basename = os.path.basename(file['path']) |
493 | 493 | length = 0 |
494 | 494 | count = 0 |
495 | 495 | while length < file['size']: |
496 | 496 | range_header = {"Range": "bytes={}-{}".format(length, length + chunk_size)} |
497 | 497 | resp = self.get("/v1/project/raw/{}".format(project_path), data=query_params, headers=range_header) |
498 | 498 | # TODO some kind of recovery? do_request already raises exception |
499 | 499 | 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))) |
506 | 501 | length += (chunk_size + 1) |
507 | 502 | count += 1 |
508 | 503 |
|
509 | 504 | # 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: |
511 | 506 | for i in range(count): |
512 | 507 | with open(os.path.join(directory, file['path'] + ".{}".format(i)), 'rb') as chunk: |
513 | 508 | shutil.copyfileobj(chunk, final) |
|
0 commit comments