fix: delete bug

This commit is contained in:
tao.chen
2026-08-14 20:37:25 +08:00
parent 1079b62f0a
commit f72dfd10e8
3 changed files with 262 additions and 60 deletions
+51 -10
View File
@@ -48,6 +48,24 @@ def compute_jupyter_relative_path(script_path: str, resource_relative: str) -> s
return os.path.relpath(resource_relative, start=script_dir)
def resource_directory(
object_key: str,
workspace_id: str,
owner_user_id: str,
) -> str:
"""从 object_key 解析资源所在目录(相对于用户根目录,根目录返回 "")。
object_key 形如 ``{ws_id}/{user_id}/{target_path}/{file_name}``;
不匹配该前缀的键(如无 ws/user 前缀的旧数据)统一视为根目录。
"""
prefix = f"{workspace_id}/{owner_user_id}/"
if not object_key.startswith(prefix):
return ""
tail = object_key[len(prefix):]
directory, _, _ = tail.rpartition("/")
return directory
def resource_payload(
resource: DataResources,
storage_object: StorageObjects,
@@ -242,18 +260,41 @@ async def bind_resource(
item.visibility = payload.visibility
# (description lives on DataResources, not on StorageObjects.)
existing_active = await session.scalar(
select(DataResources).where(
DataResources.workspace_id == context.workspace.workspace_id,
DataResources.resource_name == payload.resource_name,
DataResources.status == "active",
)
# 同名查重按「owner + 目录 + 名称」维度:目录从 object_key 解析,
# 不同目录、不同 owner 均允许重名。
new_directory = resource_directory(
item.object_key,
context.workspace.workspace_id,
context.user.user_id,
)
if existing_active is not None:
raise HTTPException(
status.HTTP_409_CONFLICT,
"a data resource with this name already exists in this workspace",
same_name_rows = (
await session.execute(
select(DataResources, StorageObjects)
.join(
StorageObjects,
StorageObjects.storage_object_id
== DataResources.storage_object_id,
)
.where(
DataResources.workspace_id == context.workspace.workspace_id,
DataResources.owner_user_id == context.user.user_id,
DataResources.resource_name == payload.resource_name,
DataResources.status == "active",
DataResources.storage_object_id != item.storage_object_id,
)
)
).all()
for existing_resource, existing_object in same_name_rows:
existing_directory = resource_directory(
existing_object.object_key,
existing_resource.workspace_id,
existing_resource.owner_user_id,
)
if existing_directory == new_directory:
raise HTTPException(
status.HTTP_409_CONFLICT,
"a data resource with this name already exists in this directory",
)
existing = await session.scalar(
select(DataResources).where(