Skip to content

Commit

Permalink
Save document through Yjs websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed Jan 19, 2022
1 parent d201529 commit 4d82190
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
24 changes: 24 additions & 0 deletions jupyter_server/services/contents/filemanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
import errno
import json
import mimetypes
import os
import shutil
Expand Down Expand Up @@ -40,6 +41,13 @@
# windows + py2
from jupyter_server.utils import samefile_simple as samefile

try:
from jupyterlab.handlers.yjs_echo_ws import ROOMS

YJS_SUPPORT = True
except Exception:
YJS_SUPPORT = False

_script_exporter = None


Expand Down Expand Up @@ -437,6 +445,13 @@ def save(self, model, path=""):

if "type" not in model:
raise web.HTTPError(400, u"No file type provided")

if YJS_SUPPORT and path in ROOMS:
if model["type"] == "notebook":
model["content"] = json.loads(ROOMS[path].source)
elif model["type"] == "file":
model["content"] = ROOMS[path].source

if "content" not in model and model["type"] != "directory":
raise web.HTTPError(400, u"No file content provided")

Expand Down Expand Up @@ -763,6 +778,15 @@ async def save(self, model, path=""):

if "type" not in model:
raise web.HTTPError(400, u"No file type provided")

if YJS_SUPPORT and path in ROOMS:
await ROOMS[path].ready_to_save.wait()
if model["type"] == "notebook":
model["content"] = json.loads(ROOMS[path].source)
elif model["type"] == "file":
model["content"] = ROOMS[path].source
ROOMS[path].ready_to_save.clear()

if "content" not in model and model["type"] != "directory":
raise web.HTTPError(400, u"No file content provided")

Expand Down
24 changes: 24 additions & 0 deletions jupyter_server/services/contents/largefilemanager.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import base64
import io
import json
import os

from anyio.to_thread import run_sync
Expand All @@ -8,6 +9,13 @@
from jupyter_server.services.contents.filemanager import AsyncFileContentsManager
from jupyter_server.services.contents.filemanager import FileContentsManager

try:
from jupyterlab.handlers.yjs_echo_ws import ROOMS

YJS_SUPPORT = True
except Exception:
YJS_SUPPORT = False


class LargeFileManager(FileContentsManager):
"""Handle large file upload."""
Expand All @@ -29,6 +37,13 @@ def save(self, model, path=""):
model["type"]
),
)

if YJS_SUPPORT and path in ROOMS:
if model["type"] == "notebook":
model["content"] = json.loads(ROOMS[path].source)
elif model["type"] == "file":
model["content"] = ROOMS[path].source

if "content" not in model and model["type"] != "directory":
raise web.HTTPError(400, u"No file content provided")

Expand Down Expand Up @@ -104,6 +119,15 @@ async def save(self, model, path=""):
model["type"]
),
)

if YJS_SUPPORT and path in ROOMS:
await ROOMS[path].ready_to_save.wait()
if model["type"] == "notebook":
model["content"] = json.loads(ROOMS[path].source)
elif model["type"] == "file":
model["content"] = ROOMS[path].source
ROOMS[path].ready_to_save.clear()

if "content" not in model and model["type"] != "directory":
raise web.HTTPError(400, u"No file content provided")

Expand Down

0 comments on commit 4d82190

Please sign in to comment.