Skip to content

Commit 46b57a8

Browse files
authored
Merge pull request #105 from DenisaCG/chunk_upload
Add support for multipart uploads
2 parents 106933d + 5eb0423 commit 46b57a8

File tree

2 files changed

+45
-12
lines changed

2 files changed

+45
-12
lines changed

jupyter_drives/manager.py

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ def __init__(self, config: traitlets.config.Config) -> None:
5050
self._config = DrivesConfig(config=config)
5151
self._client = httpx.AsyncClient()
5252
self._content_managers = {}
53+
self._multipartUploads = {};
5354
self._max_files_listed = 1025
5455
self._drives = None
5556
self._external_drives = {}
@@ -468,7 +469,7 @@ async def new_file(self, drive_name, path, type):
468469
}
469470
return response
470471

471-
async def save_file(self, drive_name, path, content, options_format, content_format, content_type):
472+
async def save_file(self, drive_name, path, content, options_format, content_format, content_type, options_chunk=None):
472473
"""Save file with new content.
473474
474475
Args:
@@ -487,6 +488,12 @@ async def save_file(self, drive_name, path, content, options_format, content_for
487488
if options_format == 'json':
488489
formatted_content = json.dumps(content, indent=2)
489490
formatted_content = formatted_content.encode("utf-8")
491+
492+
if options_chunk:
493+
if options_chunk == 1:
494+
self._multipartUploads[path] = '';
495+
496+
self._multipartUploads[path] = json.dumps(self._multipartUploads[path] + formatted_content,indent = 2);
490497
elif options_format == 'base64' and (content_format == 'base64' or (content_format == 'text' and content_type != 'PDF') or content_type == 'PDF' or content_type == 'notebook'):
491498
# transform base64 encoding to a UTF-8 byte array for saving or storing
492499
byte_characters = base64.b64decode(content)
@@ -499,23 +506,48 @@ async def save_file(self, drive_name, path, content, options_format, content_for
499506

500507
# combine byte arrays
501508
formatted_content = b"".join(byte_arrays)
509+
510+
if options_chunk:
511+
if options_chunk == 1:
512+
self._multipartUploads[path] = b""
513+
self._multipartUploads[path] = self._multipartUploads[path] + formatted_content
502514
elif options_format == 'text':
503515
formatted_content = content.encode("utf-8")
516+
517+
if options_chunk:
518+
if options_chunk == 1:
519+
self._multipartUploads[path] = b""
520+
self._multipartUploads[path] = self._multipartUploads[path] + formatted_content
504521
else:
505522
formatted_content = content
523+
if options_chunk:
524+
if options_chunk == 1:
525+
self._multipartUploads[path] = ""
526+
self._multipartUploads[path] = self._multipartUploads[path] + formatted_content;
506527

507-
if formatted_content is None or formatted_content == '':
508-
formatted_content = b''
528+
if options_chunk is None or options_chunk == -1:
529+
if formatted_content is None or formatted_content == '':
530+
formatted_content = b''
509531

510-
await self._file_system._pipe(drive_name + '/' + path, formatted_content)
511-
metadata = await self._file_system._info(drive_name + '/' + path)
532+
await self._file_system._pipe(drive_name + '/' + path, self._multipartUploads[path] if options_chunk == -1 else formatted_content)
533+
metadata = await self._file_system._info(drive_name + '/' + path)
512534

513-
data = {
514-
"path": path,
515-
"content": content,
516-
"last_modified": metadata["LastModified"].isoformat(),
517-
"size": metadata["size"]
518-
}
535+
data = {
536+
"path": path,
537+
"content": content,
538+
"last_modified": metadata["LastModified"].isoformat(),
539+
"size": metadata["size"]
540+
}
541+
else:
542+
data = {
543+
"path": path,
544+
"content": content,
545+
"last_modified": datetime.now().isoformat(),
546+
"size": 0
547+
}
548+
549+
if options_chunk == -1:
550+
del self._multipartUploads[path]
519551
except Exception as e:
520552
raise tornado.web.HTTPError(
521553
status_code= httpx.codes.BAD_REQUEST,

src/requests.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,8 @@ export async function saveObject(
197197
content: options.param.content,
198198
options_format: options.param.format,
199199
content_format: fileFormat,
200-
content_type: fileType
200+
content_type: fileType,
201+
options_chunk: options.param.chunk
201202
}
202203
);
203204

0 commit comments

Comments
 (0)