Skip to content

Commit 15581b7

Browse files
extract util
1 parent 35ec289 commit 15581b7

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

services/storage/src/simcore_service_storage/simcore_s3_dsm.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,11 @@
8080
from .s3 import get_s3_client
8181
from .s3_utils import S3TransferDataCB, update_task_progress
8282
from .settings import Settings
83-
from .simcore_s3_dsm_utils import expand_directory, get_directory_file_id
83+
from .simcore_s3_dsm_utils import (
84+
compute_file_id_prefix,
85+
expand_directory,
86+
get_directory_file_id,
87+
)
8488
from .utils import (
8589
convert_db_to_model,
8690
download_to_file_or_raise,
@@ -517,11 +521,6 @@ async def delete_file(
517521
# Only use this in those circumstances where a collaborator requires to delete a file (the current
518522
# permissions model will not allow him to do so, even though this is a legitimate action)
519523
# SEE https://github.com/ITISFoundation/osparc-simcore/issues/5159
520-
def _get_subpath(path: str, levels: int):
521-
components = path.strip("/").split("/")
522-
subpath = "/".join(components[:levels])
523-
return "/" + subpath if subpath else "/"
524-
525524
async with self.engine.acquire() as conn:
526525
if enforce_access_rights:
527526
can: AccessRights = await get_file_access_rights(conn, user_id, file_id)
@@ -544,7 +543,7 @@ def _get_subpath(path: str, levels: int):
544543
user_or_project_filter=UserOrProjectFilter(
545544
user_id=user_id, project_ids=[]
546545
),
547-
file_id_prefix=_get_subpath(file_id, 2),
546+
file_id_prefix=compute_file_id_prefix(file_id, 2),
548547
partial_file_id=None,
549548
is_directory=True,
550549
sha256_checksum=None,

services/storage/src/simcore_service_storage/simcore_s3_dsm_utils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,9 @@ async def _get_fmd(
119119
directory_file_id_fmd = await _get_fmd(conn, directory_file_id)
120120

121121
return directory_file_id if directory_file_id_fmd else None
122+
123+
124+
def compute_file_id_prefix(file_id: str, levels: int):
125+
components = file_id.strip("/").split("/")
126+
subpath = "/".join(components[:levels])
127+
return "/" + subpath if subpath else "/"
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import pytest
2+
from simcore_service_storage.simcore_s3_dsm_utils import compute_file_id_prefix
3+
4+
5+
@pytest.mark.parametrize(
6+
"file_id, levels, expected",
7+
[
8+
(
9+
"b21a3b80-d578-4b33-a224-e24ee2e4966a/42b9cc07-60f5-4d29-a063-176d1467901c/my/amazing/sub/folder/with/a/file.bin",
10+
2,
11+
"b21a3b80-d578-4b33-a224-e24ee2e4966a/42b9cc07-60f5-4d29-a063-176d1467901c/my",
12+
),
13+
(
14+
"api/42b9cc07-60f5-4d29-a063-176d1467901c/my/amazing/sub/folder/with/a/file.bin",
15+
2,
16+
"api/42b9cc07-60f5-4d29-a063-176d1467901c/my",
17+
),
18+
],
19+
)
20+
def test_compute_file_id_prefix(file_id, levels, expected):
21+
assert compute_file_id_prefix(file_id, levels) == expected

0 commit comments

Comments
 (0)