Skip to content

Commit ba37097

Browse files
committed
Fix chunked file upload
1 parent 74655ce commit ba37097

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

jupyter_server/services/contents/handlers.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -238,15 +238,25 @@ async def _new_untitled(self, path, type="", ext=""):
238238
validate_model(model)
239239
self._finish_model(model)
240240

241-
async def _save(self, model, path):
242-
"""Save an existing file."""
243-
chunk = model.get("chunk", None)
244-
if not chunk or chunk == -1: # Avoid tedious log information
245-
self.log.info("Saving file at %s", path)
241+
async def _overwrite(self, model, path):
242+
"""Overwrite an existing file."""
243+
self.log.info(f"Overwriting file at {path}")
246244
model = await ensure_async(self.contents_manager.save(model, path))
247245
validate_model(model)
248246
self._finish_model(model)
249247

248+
async def _append(self, to_append_model, path):
249+
"""Append to an existing file."""
250+
chunk = to_append_model.get("chunk", None)
251+
self.log.info(f"Appending file chunk {chunk} at path: {path}")
252+
existing_model = self.contents_manager.get(path)
253+
# TODO: Test binary files encoding works properly:
254+
assert existing_model['format'] == to_append_model['format']
255+
existing_model['content'] = existing_model['content'] + to_append_model['content']
256+
model = await ensure_async(self.contents_manager.save(existing_model, path))
257+
validate_model(model)
258+
self._finish_model(model)
259+
250260
@web.authenticated
251261
@authorized
252262
async def post(self, path=""):
@@ -318,7 +328,10 @@ async def put(self, path=""):
318328
# fall back to file if unknown type
319329
model["type"] = "file"
320330
if exists:
321-
await self._save(model, path)
331+
if not model.get("chunk") or model.get("chunk") == 1:
332+
await self._overwrite(model, path)
333+
else:
334+
await self._append(model, path)
322335
else:
323336
await self._upload(model, path)
324337
else:

0 commit comments

Comments
 (0)