Skip to content

Commit 51bc021

Browse files
fix file deletion
1 parent 34374b4 commit 51bc021

File tree

2 files changed

+56
-12
lines changed

2 files changed

+56
-12
lines changed

services/storage/src/simcore_service_storage/simcore_s3_dsm.py

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,18 @@ async def __get_link(
499499

500500
return link
501501

502+
def find_enclosing(self, file_id: str, kwnown_file_ids: list[str]) -> str | None:
503+
current_path = Path(file_id)
504+
kwnon_file_paths = [Path(file_id) for file_id in kwnown_file_ids]
505+
506+
while current_path and current_path != Path(current_path).parent:
507+
if current_path in kwnon_file_paths:
508+
return f"{current_path}"
509+
510+
current_path = Path(current_path).parent
511+
512+
return None
513+
502514
async def delete_file(
503515
self,
504516
user_id: UserID,
@@ -523,22 +535,33 @@ async def delete_file(
523535
if not can.delete:
524536
raise FileAccessRightError(access_right="delete", file_id=file_id)
525537

526-
with suppress(FileMetaDataNotFoundError):
527-
# NOTE: deleting might be slow, so better ensure we release the connection
528-
async with self.engine.acquire() as conn:
529-
file: FileMetaDataAtDB = await db_file_meta_data.get(
530-
conn, TypeAdapter(SimcoreS3FileID).validate_python(file_id)
531-
)
538+
# NOTE: deleting might be slow, so better ensure we release the connection
539+
async with self.engine.acquire() as conn:
540+
enclosing_file_id = self.find_enclosing(
541+
file_id,
542+
[
543+
known_file.file_id
544+
for known_file in await db_file_meta_data.list_fmds(
545+
conn, user_id=user_id
546+
)
547+
],
548+
)
549+
550+
enclosing_file = await db_file_meta_data.get(
551+
conn, TypeAdapter(SimcoreS3FileID).validate_python(enclosing_file_id)
552+
)
553+
554+
if await db_file_meta_data.exists(conn, file_id):
555+
await db_file_meta_data.delete(conn, [file_id])
556+
532557
await get_s3_client(self.app).delete_objects_recursively(
533-
bucket=file.bucket_name,
558+
bucket=enclosing_file.bucket_name,
534559
prefix=(
535-
ensure_ends_with(file.file_id, "/")
536-
if file.is_directory
537-
else file.file_id
560+
ensure_ends_with(enclosing_file.file_id, "/")
561+
if enclosing_file.is_directory and enclosing_file_id == file_id
562+
else file_id
538563
),
539564
)
540-
async with self.engine.acquire() as conn:
541-
await db_file_meta_data.delete(conn, [file.file_id])
542565

543566
async def delete_project_simcore_s3(
544567
self, user_id: UserID, project_id: ProjectID, node_id: NodeID | None = None

services/storage/tests/unit/test_handlers_files.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,6 +1386,27 @@ async def test_upload_file_is_directory_and_remove_content(
13861386
)
13871387
assert len(list_of_files) == SUBDIR_COUNT * FILE_COUNT
13881388

1389+
# DELETE ONE FILE FROM THE DIRECTORY
1390+
1391+
assert client.app
1392+
delete_url = (
1393+
client.app.router["delete_file"]
1394+
.url_for(
1395+
location_id=f"{location_id}",
1396+
file_id=urllib.parse.quote(list_of_files[0].file_id, safe=""),
1397+
)
1398+
.with_query(user_id=user_id)
1399+
)
1400+
response = await client.delete(f"{delete_url}")
1401+
_, error = await assert_status(response, status.HTTP_204_NO_CONTENT)
1402+
assert error is None
1403+
1404+
list_of_files: list[FileMetaDataGet] = await _list_files_legacy(
1405+
client, user_id, location_id, directory_file_upload
1406+
)
1407+
1408+
assert len(list_of_files) == SUBDIR_COUNT * FILE_COUNT - 1
1409+
13891410
# DIRECTORY REMOVAL
13901411

13911412
await delete_directory(directory_file_upload=directory_file_upload)

0 commit comments

Comments
 (0)