Skip to content

Commit

Permalink
update create functionality for empty directory
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisaCG committed Nov 29, 2024
1 parent 2ebfb55 commit 6767629
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
3 changes: 2 additions & 1 deletion jupyter_drives/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ async def get(self, drive: str = "", path: str = ""):

@tornado.web.authenticated
async def post(self, drive: str = "", path: str = ""):
result = await self._manager.new_file(drive, path)
body = self.get_json_body()
result = await self._manager.new_file(drive, path, **body)
self.finish(result)

@tornado.web.authenticated
Expand Down
32 changes: 29 additions & 3 deletions jupyter_drives/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,20 +259,25 @@ async def get_contents(self, drive_name, path):

return response

async def new_file(self, drive_name, path):
async def new_file(self, drive_name, path, is_dir):
"""Create a new file or directory at the given path.
Args:
drive_name: name of drive where the new content is created
path: path where new content should be created
is_dir: boolean showing whether we are dealing with a directory or a file
"""
data = {}
try:
# eliminate leading and trailing backslashes
path = path.strip('/')

# TO DO: switch to mode "created", which is not implemented yet
await obs.put_async(self._content_managers[drive_name]["store"], path, b"", mode = "overwrite")
if is_dir == False or self._config.provider != 's3':
# TO DO: switch to mode "created", which is not implemented yet
await obs.put_async(self._content_managers[drive_name]["store"], path, b"", mode = "overwrite")
elif is_dir == True and self._config.provider == 's3':
# create an empty directory through boto, as obstore does not allow it
self._create_empty_directory(drive_name, path)
metadata = await obs.head_async(self._content_managers[drive_name]["store"], path)

data = {
Expand Down Expand Up @@ -497,6 +502,27 @@ def _check_object(self, drive_name, path):

return isDir

def _create_empty_directory(self, drive_name, path):
"""Helping function to create an empty directory, when dealing with S3 buckets.
Args:
drive_name: name of drive where to create object
path: path of new object
"""
try:
location = self._content_managers[drive_name]["location"]
if location not in self._s3_clients:
self._s3_clients[location] = self._s3_session.client('s3', location)

self._s3_clients[location].put_object(Bucket=drive_name, Key=path+'/')
except Exception as e:
raise tornado.web.HTTPError(
status_code= httpx.codes.BAD_REQUEST,
reason=f"The following error occured when creating the directory: {e}",
)

return

async def _call_provider(
self,
url: str,
Expand Down
1 change: 1 addition & 0 deletions src/contents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ export class Drive implements Contents.IDrive {
data = await createObject(currentDrive.name, {
name: name,
path: relativePath,
isDir: options.type === 'directory' ? true : false,
registeredFileTypes: this._registeredFileTypes
});
} else {
Expand Down
7 changes: 6 additions & 1 deletion src/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ export async function saveObject(
*
* @param driveName
* @param options.path The path of new object.
* @param options.isDir A boolean variable showing if we are dealing with a directory or file.
* @param options.registeredFileTypes The list containing all registered file types.
*
* @returns A promise which resolves with the contents model.
Expand All @@ -204,6 +205,7 @@ export async function createObject(
options: {
name: string;
path: string;
isDir: boolean;
registeredFileTypes: IRegisteredFileTypes;
}
) {
Expand All @@ -212,7 +214,10 @@ export async function createObject(
: options.name;
const response = await requestAPI<any>(
'drives/' + driveName + '/' + path,
'POST'
'POST',
{
is_dir: options.isDir
}
);

const [fileType, fileMimeType, fileFormat] = getFileType(
Expand Down

0 comments on commit 6767629

Please sign in to comment.