Skip to content

Commit e36eccd

Browse files
committed
refactor delete functionality
1 parent 1501aab commit e36eccd

File tree

2 files changed

+15
-36
lines changed

2 files changed

+15
-36
lines changed

jupyter_drives/manager.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,17 @@ async def delete_file(self, drive_name, path):
458458
try:
459459
# eliminate leading and trailing backslashes
460460
path = path.strip('/')
461-
await obs.delete_async(self._content_managers[drive_name]["store"], path)
461+
is_dir = await self._file_system._isdir(drive_name + '/' + path)
462+
if is_dir == True:
463+
await self._fix_dir(drive_name, path)
464+
await self._file_system._rm(drive_name + '/' + path, recursive = True)
465+
466+
# checking for remaining directories and deleting them
467+
stream = obs.list(self._content_managers[drive_name]["store"], path, chunk_size=100, return_arrow=True)
468+
async for batch in stream:
469+
contents_list = pyarrow.record_batch(batch).to_pylist()
470+
for object in contents_list:
471+
await self._fix_dir(drive_name, object["path"], delete_only = True)
462472

463473
except Exception as e:
464474
raise tornado.web.HTTPError(
@@ -607,7 +617,7 @@ async def _check_object(self, drive_name, path):
607617

608618
return isDir
609619

610-
async def _fix_dir(self, drive_name, path):
620+
async def _fix_dir(self, drive_name, path, delete_only = False):
611621
"""Helping function to fix a directory. It applies to the S3 folders created in the AWS console.
612622
613623
Args:
@@ -622,6 +632,8 @@ async def _fix_dir(self, drive_name, path):
622632
# delete original object
623633
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:
624634
await client.delete_object(Bucket=drive_name, Key=path+'/')
635+
if delete_only == True:
636+
return
625637
# create new directory
626638
await self._file_system._touch(drive_name + '/' + path + self._fixDir_suffix)
627639
except Exception as e:

src/requests.ts

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -267,26 +267,7 @@ export async function deleteObjects(
267267
path: string;
268268
}
269269
) {
270-
// get list of contents with given prefix (path)
271-
const response = await requestAPI<any>(
272-
'drives/' + driveName + '/' + options.path,
273-
'GET'
274-
);
275-
276-
// deleting contents of a directory
277-
if (response.data.length !== undefined && response.data.length !== 0) {
278-
await Promise.all(
279-
response.data.map(async (c: any) => {
280-
return Private.deleteSingleObject(driveName, c.path);
281-
})
282-
);
283-
}
284-
try {
285-
// always deleting the object (file or main directory)
286-
return Private.deleteSingleObject(driveName, options.path);
287-
} catch (error) {
288-
// deleting failed if directory didn't exist and was only part of a path
289-
}
270+
await requestAPI<any>('drives/' + driveName + '/' + options.path, 'DELETE');
290271
}
291272

292273
/**
@@ -517,20 +498,6 @@ export const countObjectNameAppearances = async (
517498
};
518499

519500
namespace Private {
520-
/**
521-
* Helping function for deleting files inside
522-
* a directory, in the case of deleting the directory.
523-
*
524-
* @param driveName
525-
* @param objectPath complete path of object to delete
526-
*/
527-
export async function deleteSingleObject(
528-
driveName: string,
529-
objectPath: string
530-
) {
531-
await requestAPI<any>('drives/' + driveName + '/' + objectPath, 'DELETE');
532-
}
533-
534501
/**
535502
* Helping function for renaming files inside
536503
* a directory, in the case of deleting the directory.

0 commit comments

Comments
 (0)