mirror of
https://github.com/langgenius/dify.git
synced 2026-05-11 18:00:13 -04:00
Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: jyong <718720800@qq.com> Co-authored-by: Yansong Zhang <916125788@qq.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: hj24 <mambahj24@gmail.com> Co-authored-by: hj24 <huangjian@dify.ai> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: Stephen Zhou <38493346+hyoban@users.noreply.github.com> Co-authored-by: CodingOnStar <hanxujiang@dify.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: 非法操作 <hjlarry@163.com> Co-authored-by: Ayush Baluni <73417844+aayushbaluni@users.noreply.github.com> Co-authored-by: yyh <92089059+lyzno1@users.noreply.github.com> Co-authored-by: jimcody1995 <jjimcody@gmail.com> Co-authored-by: James <63717587+jamesrayammons@users.noreply.github.com> Co-authored-by: Yunlu Wen <yunlu.wen@dify.ai> Co-authored-by: Stephen Zhou <hi@hyoban.cc> Co-authored-by: Coding On Star <447357187@qq.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: jerryzai <jerryzh8710@protonmail.com> Co-authored-by: NVIDIAN <speedy.hpc@hotmail.com> Co-authored-by: ai-hpc <ai-hpc@users.noreply.github.com> Co-authored-by: Asuka Minato <i@asukaminato.eu.org> Co-authored-by: Junghwan <70629228+shaun0927@users.noreply.github.com> Co-authored-by: HeYinKazune <70251095+HeYin-OS@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: yyh <yuanyouhuilyz@gmail.com> Co-authored-by: Jingyi <jingyi.qi@dify.ai> Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com> Co-authored-by: sxxtony <166789813+sxxtony@users.noreply.github.com>
120 lines
4.1 KiB
Python
120 lines
4.1 KiB
Python
from __future__ import annotations
|
|
|
|
from collections.abc import Callable, Mapping
|
|
from functools import lru_cache
|
|
from typing import Any
|
|
|
|
from core.workflow.file_reference import parse_file_reference
|
|
from graphon.file import File, FileTransferMethod
|
|
|
|
|
|
@lru_cache(maxsize=1)
|
|
def _get_file_access_controller():
|
|
from core.app.file_access import DatabaseFileAccessController
|
|
|
|
return DatabaseFileAccessController()
|
|
|
|
|
|
def resolve_file_record_id(file_mapping: Mapping[str, Any]) -> str | None:
|
|
reference = file_mapping.get("reference")
|
|
if isinstance(reference, str) and reference:
|
|
parsed_reference = parse_file_reference(reference)
|
|
if parsed_reference is not None:
|
|
return parsed_reference.record_id
|
|
|
|
related_id = file_mapping.get("related_id")
|
|
if isinstance(related_id, str) and related_id:
|
|
parsed_reference = parse_file_reference(related_id)
|
|
if parsed_reference is not None:
|
|
return parsed_reference.record_id
|
|
|
|
return None
|
|
|
|
|
|
def resolve_file_mapping_tenant_id(
|
|
*,
|
|
file_mapping: Mapping[str, Any],
|
|
tenant_resolver: Callable[[], str],
|
|
) -> str:
|
|
tenant_id = file_mapping.get("tenant_id")
|
|
if isinstance(tenant_id, str) and tenant_id:
|
|
return tenant_id
|
|
|
|
return tenant_resolver()
|
|
|
|
|
|
def build_file_from_stored_mapping(
|
|
*,
|
|
file_mapping: Mapping[str, Any],
|
|
tenant_id: str,
|
|
) -> File:
|
|
"""
|
|
Canonicalize a persisted file payload against the current tenant context.
|
|
|
|
Stored JSON rows can outlive file schema changes, so rebuild storage-backed
|
|
files through the workflow factory instead of trusting serialized metadata.
|
|
Pure external ``REMOTE_URL`` payloads without a backing upload row are
|
|
passed through because there is no server-owned record to rebind.
|
|
"""
|
|
|
|
# NOTE: It's not the best way to implement this, but it's the only way to avoid circular import for now.
|
|
from factories import file_factory
|
|
|
|
mapping = dict(file_mapping)
|
|
mapping.pop("tenant_id", None)
|
|
record_id = resolve_file_record_id(mapping)
|
|
transfer_method = FileTransferMethod.value_of(mapping["transfer_method"])
|
|
|
|
match transfer_method:
|
|
case FileTransferMethod.TOOL_FILE if record_id:
|
|
mapping["tool_file_id"] = record_id
|
|
case FileTransferMethod.LOCAL_FILE | FileTransferMethod.REMOTE_URL if record_id:
|
|
mapping["upload_file_id"] = record_id
|
|
case FileTransferMethod.DATASOURCE_FILE if record_id:
|
|
mapping["datasource_file_id"] = record_id
|
|
case _:
|
|
pass
|
|
|
|
if transfer_method == FileTransferMethod.REMOTE_URL and record_id is None:
|
|
remote_url = mapping.get("remote_url")
|
|
if not isinstance(remote_url, str) or not remote_url:
|
|
url = mapping.get("url")
|
|
if isinstance(url, str) and url:
|
|
mapping["remote_url"] = url
|
|
return File.model_validate(mapping)
|
|
|
|
return file_factory.build_from_mapping(
|
|
mapping=mapping,
|
|
tenant_id=tenant_id,
|
|
access_controller=_get_file_access_controller(),
|
|
)
|
|
|
|
|
|
def build_file_from_input_mapping(
|
|
*,
|
|
file_mapping: Mapping[str, Any],
|
|
tenant_resolver: Callable[[], str],
|
|
) -> File:
|
|
"""
|
|
Rehydrate persisted model input payloads into graph `File` objects.
|
|
|
|
This compatibility layer exists because model JSON rows can outlive file payload
|
|
schema changes. Legacy rows may carry `related_id` and `tenant_id`, while newer
|
|
rows may only carry `reference`. Keep ownership resolution here, at the model
|
|
boundary, instead of pushing tenant data back into `graphon.file.File`.
|
|
"""
|
|
|
|
transfer_method = FileTransferMethod.value_of(file_mapping["transfer_method"])
|
|
record_id = resolve_file_record_id(file_mapping)
|
|
if transfer_method == FileTransferMethod.REMOTE_URL and record_id is None:
|
|
return build_file_from_stored_mapping(
|
|
file_mapping=file_mapping,
|
|
tenant_id="",
|
|
)
|
|
|
|
tenant_id = resolve_file_mapping_tenant_id(file_mapping=file_mapping, tenant_resolver=tenant_resolver)
|
|
return build_file_from_stored_mapping(
|
|
file_mapping=file_mapping,
|
|
tenant_id=tenant_id,
|
|
)
|