From ba370979e6de9b7ccaff31580dffd875722a6fc9 Mon Sep 17 00:00:00 2001 From: Jessie Ross Date: Wed, 23 Oct 2024 15:20:55 +0700 Subject: [PATCH 1/2] Fix chunked file upload --- jupyter_server/services/contents/handlers.py | 25 +++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/jupyter_server/services/contents/handlers.py b/jupyter_server/services/contents/handlers.py index 13e987809b..98ab576695 100644 --- a/jupyter_server/services/contents/handlers.py +++ b/jupyter_server/services/contents/handlers.py @@ -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=""): @@ -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: From 9e785563ff0d8e8d8b0e1a7b4c6eeee0ab7481e6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 23 Oct 2024 08:29:34 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- jupyter_server/services/contents/handlers.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/jupyter_server/services/contents/handlers.py b/jupyter_server/services/contents/handlers.py index 98ab576695..c915ea93bc 100644 --- a/jupyter_server/services/contents/handlers.py +++ b/jupyter_server/services/contents/handlers.py @@ -251,8 +251,8 @@ async def _append(self, to_append_model, path): 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'] + 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)