Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix chunked file upload #1464

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
25 changes: 19 additions & 6 deletions jupyter_server/services/contents/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,15 +238,25 @@ async def _new_untitled(self, path, type="", ext=""):
validate_model(model)
self._finish_model(model)

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

async def _append(self, to_append_model, path):
"""Append to an existing file."""
chunk = to_append_model.get("chunk", None)
self.log.info(f"Appending file chunk {chunk} at path: {path}")
existing_model = self.contents_manager.get(path)
# TODO: Test binary files encoding works properly:
assert existing_model["format"] == to_append_model["format"]
existing_model["content"] = existing_model["content"] + to_append_model["content"]
model = await ensure_async(self.contents_manager.save(existing_model, path))
validate_model(model)
self._finish_model(model)

@web.authenticated
@authorized
async def post(self, path=""):
Expand Down Expand Up @@ -318,7 +328,10 @@ async def put(self, path=""):
# fall back to file if unknown type
model["type"] = "file"
if exists:
await self._save(model, path)
if not model.get("chunk") or model.get("chunk") == 1:
await self._overwrite(model, path)
else:
await self._append(model, path)
else:
await self._upload(model, path)
else:
Expand Down
Loading