Skip to content

Commit 794e2fc

Browse files
committed
refactor create object functionality
1 parent 91c9130 commit 794e2fc

File tree

5 files changed

+41
-10
lines changed

5 files changed

+41
-10
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,6 @@ dmypy.json
123123

124124
# Yarn cache
125125
.yarn/
126+
127+
# untitled notebooks
128+
Untitled.ipynb

jupyter_drives/handlers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ async def get(self, drive: str = "", path: str = ""):
8888

8989
@tornado.web.authenticated
9090
async def post(self, drive: str = "", path: str = ""):
91-
result = await self._manager.new_file(drive, path)
91+
body = self.get_json_body()
92+
result = await self._manager.new_file(drive, path, **body)
9293
self.finish(result)
9394

9495
@tornado.web.authenticated

jupyter_drives/manager.py

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import json
33
import logging
44
from typing import Dict, List, Optional, Tuple, Union, Any
5-
from datetime import timedelta
5+
from datetime import timedelta, datetime
66

77
import os
88
import tornado
@@ -309,27 +309,31 @@ async def get_contents(self, drive_name, path):
309309

310310
return response
311311

312-
async def new_file(self, drive_name, path):
312+
async def new_file(self, drive_name, path, type):
313313
"""Create a new file or directory at the given path.
314314
315315
Args:
316316
drive_name: name of drive where the new content is created
317317
path: path where new content should be created
318+
type: whether we are dealing with a file or a directory
318319
"""
319320
data = {}
320321
try:
321322
# eliminate leading and trailing backslashes
322323
path = path.strip('/')
323324

324-
# TO DO: switch to mode "created", which is not implemented yet
325-
await obs.put_async(self._content_managers[drive_name]["store"], path, b"", mode = "overwrite")
326-
metadata = await obs.head_async(self._content_managers[drive_name]["store"], path)
327-
325+
if type == 'directory' and self._config.provider == 's3':
326+
await self._create_dir(drive_name, path)
327+
else:
328+
await self._file_system._touch(drive_name + '/' + path)
329+
metadata = await self._file_system._info(drive_name + '/' + path)
330+
328331
data = {
329332
"path": path,
330333
"content": "",
331-
"last_modified": metadata["last_modified"].isoformat(),
332-
"size": metadata["size"]
334+
"last_modified": metadata["LastModified"].isoformat() if metadata["type"]=='file' else datetime.now().isoformat(),
335+
"size": metadata["size"],
336+
"type": metadata["type"]
333337
}
334338
except Exception as e:
335339
raise tornado.web.HTTPError(
@@ -592,6 +596,24 @@ async def _check_object(self, drive_name, path):
592596

593597
return isDir
594598

599+
async def _create_dir(self, drive_name, path):
600+
"""Helping function to create an empty directory.
601+
602+
Args:
603+
drive_name: name of drive where object should be created
604+
path: path to object to create
605+
"""
606+
try:
607+
async with self._s3_session.create_client('s3', aws_secret_access_key=self._config.secret_access_key, aws_access_key_id=self._config.access_key_id, aws_session_token=self._config.session_token) as client:
608+
await client.put_object(Bucket=drive_name, Key=path + '/')
609+
except Exception as e:
610+
raise tornado.web.HTTPError(
611+
status_code= httpx.codes.BAD_REQUEST,
612+
reason=f"The following error occured when creating the directory: {e}",
613+
)
614+
615+
return
616+
595617
async def _call_provider(
596618
self,
597619
url: str,

src/contents.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ export class Drive implements Contents.IDrive {
310310
data = await createObject(currentDrive.name, {
311311
name: name,
312312
path: relativePath,
313+
type: options.type,
313314
registeredFileTypes: this._registeredFileTypes
314315
});
315316
} else {

src/requests.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ export async function createObject(
218218
options: {
219219
name: string;
220220
path: string;
221+
type: string;
221222
registeredFileTypes: IRegisteredFileTypes;
222223
}
223224
) {
@@ -226,7 +227,10 @@ export async function createObject(
226227
: options.name;
227228
const response = await requestAPI<any>(
228229
'drives/' + driveName + '/' + path,
229-
'POST'
230+
'POST',
231+
{
232+
type: options.type
233+
}
230234
);
231235

232236
const [fileType, fileMimeType, fileFormat] = getFileType(

0 commit comments

Comments
 (0)