Skip to content

Commit 111c795

Browse files
committed
add backend functionality to save file
1 parent 4edd731 commit 111c795

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

jupyter_drives/manager.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,40 @@ async def new_file(self, drive_name, path, **kwargs):
170170
path: path where new content should be created
171171
"""
172172
print('New file function called.')
173+
174+
async def save_file(self, drive_name, path, content):
175+
"""Save file with new content.
176+
177+
Args:
178+
drive_name: name of drive where file exists
179+
path: path where new content should be saved
180+
"""
181+
data = {}
182+
try:
183+
# eliminate leading and trailing backslashes
184+
path = path.strip('/')
185+
# format body when dealing with base64 enconding or files of type PDF
186+
formattedContent = content.encode("utf-8")
187+
188+
await obs.put_async(self._content_managers[drive_name], path, formattedContent, mode = "overwrite")
189+
metadata = await obs.head_async(self._content_managers[drive_name], path)
190+
191+
data = {
192+
"path": path,
193+
"content": content,
194+
"last_modified": metadata["last_modified"].isoformat(),
195+
"size": metadata["size"]
196+
}
197+
except Exception as e:
198+
raise tornado.web.HTTPError(
199+
status_code= httpx.codes.BAD_REQUEST,
200+
reason=f"The following error occured when saving the file: {e}",
201+
)
202+
203+
response = {
204+
"data": data
205+
}
206+
return response
173207

174208
async def rename_file(self, drive_name, path, **kwargs):
175209
"""Rename a file.

0 commit comments

Comments
 (0)