Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix deletion of files in folders #6935

Merged
merged 39 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
51bc021
fix file deletion
giancarloromeo Dec 10, 2024
27c9462
Merge branch 'master' into fix-file-deletion
giancarloromeo Dec 11, 2024
d612a58
update implementation
giancarloromeo Dec 11, 2024
c6a0f82
Merge branch 'master' into fix-file-deletion
giancarloromeo Dec 11, 2024
325ae15
fix pylint
giancarloromeo Dec 11, 2024
3d1699f
Merge branch 'fix-file-deletion' of github.com:giancarloromeo/osparc-…
giancarloromeo Dec 11, 2024
85d1577
Merge branch 'master' into fix-file-deletion
giancarloromeo Dec 11, 2024
a361b74
Merge branch 'master' into fix-file-deletion
giancarloromeo Dec 11, 2024
e83f183
Merge branch 'master' into fix-file-deletion
giancarloromeo Dec 11, 2024
e6852f2
update enclosing
giancarloromeo Dec 11, 2024
f12a49d
Merge branch 'master' into fix-file-deletion
giancarloromeo Dec 11, 2024
cac2c8e
fix await
giancarloromeo Dec 11, 2024
11c45fb
Merge branch 'fix-file-deletion' of github.com:giancarloromeo/osparc-…
giancarloromeo Dec 11, 2024
21cf5a5
fix typo
giancarloromeo Dec 11, 2024
551c68d
set undefined file_size
giancarloromeo Dec 11, 2024
77f548a
Merge branch 'master' into fix-file-deletion
giancarloromeo Dec 11, 2024
8f9231f
Merge branch 'master' into fix-file-deletion
giancarloromeo Dec 11, 2024
f5de42b
enhance implementation
giancarloromeo Dec 12, 2024
2b31008
Merge branch 'master' into fix-file-deletion
giancarloromeo Dec 12, 2024
1e0c7d9
fix
giancarloromeo Dec 12, 2024
c7d0946
fix delete dirs
giancarloromeo Dec 12, 2024
5c710dc
add constant
giancarloromeo Dec 12, 2024
4190053
refine
giancarloromeo Dec 12, 2024
37c18d4
Merge branch 'master' into fix-file-deletion
giancarloromeo Dec 12, 2024
0795cc9
Merge branch 'master' into fix-file-deletion
giancarloromeo Dec 12, 2024
755a1a1
enhance impl
giancarloromeo Dec 12, 2024
1926198
Merge branch 'fix-file-deletion' of github.com:giancarloromeo/osparc-…
giancarloromeo Dec 12, 2024
cab5b55
fix test
giancarloromeo Dec 12, 2024
a090109
add exception handling
giancarloromeo Dec 12, 2024
6f0c616
get best match
giancarloromeo Dec 12, 2024
25ac01c
Merge branch 'master' into fix-file-deletion
giancarloromeo Dec 12, 2024
4e07227
direct delete
giancarloromeo Dec 13, 2024
35ec289
Merge branch 'fix-file-deletion' of github.com:giancarloromeo/osparc-…
giancarloromeo Dec 13, 2024
15581b7
extract util
giancarloromeo Dec 13, 2024
a0d4011
fix test
giancarloromeo Dec 13, 2024
0c664fa
is_directory is required
giancarloromeo Dec 13, 2024
12a936d
fix missing
giancarloromeo Dec 13, 2024
b282d0e
Merge branch 'master' into fix-file-deletion
giancarloromeo Dec 13, 2024
a71c718
add comment
giancarloromeo Dec 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 18 additions & 17 deletions services/storage/src/simcore_service_storage/simcore_s3_dsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@
from .s3 import get_s3_client
from .s3_utils import S3TransferDataCB, update_task_progress
from .settings import Settings
from .simcore_s3_dsm_utils import expand_directory, get_directory_file_id
from .simcore_s3_dsm_utils import (
expand_directory,
find_enclosing_file,
get_directory_file_id,
)
from .utils import (
convert_db_to_model,
download_to_file_or_raise,
Expand Down Expand Up @@ -523,22 +527,19 @@ async def delete_file(
if not can.delete:
raise FileAccessRightError(access_right="delete", file_id=file_id)

with suppress(FileMetaDataNotFoundError):
# NOTE: deleting might be slow, so better ensure we release the connection
async with self.engine.acquire() as conn:
file: FileMetaDataAtDB = await db_file_meta_data.get(
conn, TypeAdapter(SimcoreS3FileID).validate_python(file_id)
)
await get_s3_client(self.app).delete_objects_recursively(
bucket=file.bucket_name,
prefix=(
ensure_ends_with(file.file_id, "/")
if file.is_directory
else file.file_id
),
)
async with self.engine.acquire() as conn:
await db_file_meta_data.delete(conn, [file.file_id])
enclosing_file = await find_enclosing_file(conn, user_id, file_id)
giancarloromeo marked this conversation as resolved.
Show resolved Hide resolved
if enclosing_file:
if enclosing_file.file_id == file_id:
await db_file_meta_data.delete(conn, [file_id])
else:
await db_file_meta_data.upsert(
conn, await self._update_database_from_storage(enclosing_file)
)

giancarloromeo marked this conversation as resolved.
Show resolved Hide resolved
await get_s3_client(self.app).delete_objects_recursively(
bucket=self.simcore_bucket_name,
prefix=file_id,
)
giancarloromeo marked this conversation as resolved.
Show resolved Hide resolved

async def delete_project_simcore_s3(
self, user_id: UserID, project_id: ProjectID, node_id: NodeID | None = None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from . import db_file_meta_data
from .exceptions import FileMetaDataNotFoundError
from .models import FileMetaData, FileMetaDataAtDB
from .models import FileMetaData, FileMetaDataAtDB, UserID
from .utils import convert_db_to_model


Expand Down Expand Up @@ -119,3 +119,20 @@ async def _get_fmd(
directory_file_id_fmd = await _get_fmd(conn, directory_file_id)

return directory_file_id if directory_file_id_fmd else None


async def find_enclosing_file(
conn: SAConnection, user_id: UserID, file_id: SimcoreS3FileID
) -> FileMetaDataAtDB | None:
kwnon_files = {
giancarloromeo marked this conversation as resolved.
Show resolved Hide resolved
Path(known_file.file_id): known_file
for known_file in await db_file_meta_data.list_fmds(conn, user_id=user_id)
giancarloromeo marked this conversation as resolved.
Show resolved Hide resolved
}
current_path = Path(file_id)
while current_path and current_path != Path(current_path).parent:
if current_path in kwnon_files:
giancarloromeo marked this conversation as resolved.
Show resolved Hide resolved
return kwnon_files.get(current_path)

current_path = Path(current_path).parent

return None
21 changes: 21 additions & 0 deletions services/storage/tests/unit/test_handlers_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -1386,6 +1386,27 @@ async def test_upload_file_is_directory_and_remove_content(
)
assert len(list_of_files) == SUBDIR_COUNT * FILE_COUNT

# DELETE ONE FILE FROM THE DIRECTORY

assert client.app
delete_url = (
client.app.router["delete_file"]
.url_for(
location_id=f"{location_id}",
file_id=urllib.parse.quote(list_of_files[0].file_id, safe=""),
)
.with_query(user_id=user_id)
)
response = await client.delete(f"{delete_url}")
_, error = await assert_status(response, status.HTTP_204_NO_CONTENT)
assert error is None

list_of_files: list[FileMetaDataGet] = await _list_files_legacy(
giancarloromeo marked this conversation as resolved.
Show resolved Hide resolved
client, user_id, location_id, directory_file_upload
)

assert len(list_of_files) == SUBDIR_COUNT * FILE_COUNT - 1

# DIRECTORY REMOVAL

await delete_directory(directory_file_upload=directory_file_upload)
Expand Down
Loading