mirror of
https://github.com/langgenius/dify.git
synced 2026-03-03 23:06:20 +08:00
- Updated API routes to use app_id instead of sandbox_id for file operations, aligning with user-specific sandbox workspaces. - Enhanced SandboxFileService and related classes to accommodate app_id in file listing and download functionalities. - Refactored storage key generation for sandbox archives to include app_id, ensuring proper file organization. - Adjusted frontend contracts and services to reflect the new app_id parameter in API calls.
59 lines
2.1 KiB
Python
59 lines
2.1 KiB
Python
from __future__ import annotations
|
|
|
|
from pathlib import PurePosixPath
|
|
|
|
from core.sandbox.entities.files import SandboxFileDownloadTicket, SandboxFileNode
|
|
from core.sandbox.inspector.archive_source import SandboxFileArchiveSource
|
|
from core.sandbox.inspector.base import SandboxFileSource
|
|
from core.sandbox.inspector.runtime_source import SandboxFileRuntimeSource
|
|
from core.sandbox.manager import SandboxManager
|
|
|
|
|
|
class SandboxFileBrowser:
|
|
def __init__(self, *, tenant_id: str, app_id: str, sandbox_id: str):
|
|
self._tenant_id = tenant_id
|
|
self._app_id = app_id
|
|
self._sandbox_id = sandbox_id
|
|
|
|
@staticmethod
|
|
def _normalize_workspace_path(path: str | None) -> str:
|
|
raw = (path or ".").strip()
|
|
if raw == "":
|
|
raw = "."
|
|
|
|
p = PurePosixPath(raw)
|
|
if p.is_absolute():
|
|
raise ValueError("path must be relative")
|
|
if any(part == ".." for part in p.parts):
|
|
raise ValueError("path must not contain '..'")
|
|
|
|
normalized = str(p)
|
|
return "." if normalized in (".", "") else normalized
|
|
|
|
def _backend(self) -> SandboxFileSource:
|
|
sandbox = SandboxManager.get(self._sandbox_id)
|
|
if sandbox is not None:
|
|
return SandboxFileRuntimeSource(
|
|
tenant_id=self._tenant_id,
|
|
app_id=self._app_id,
|
|
sandbox_id=self._sandbox_id,
|
|
runtime=sandbox.vm,
|
|
)
|
|
return SandboxFileArchiveSource(
|
|
tenant_id=self._tenant_id,
|
|
app_id=self._app_id,
|
|
sandbox_id=self._sandbox_id,
|
|
)
|
|
|
|
def exists(self) -> bool:
|
|
"""Check if the sandbox source exists and is available."""
|
|
return self._backend().exists()
|
|
|
|
def list_files(self, *, path: str | None = None, recursive: bool = False) -> list[SandboxFileNode]:
|
|
workspace_path = self._normalize_workspace_path(path)
|
|
return self._backend().list_files(path=workspace_path, recursive=recursive)
|
|
|
|
def download_file(self, *, path: str) -> SandboxFileDownloadTicket:
|
|
workspace_path = self._normalize_workspace_path(path)
|
|
return self._backend().download_file(path=workspace_path)
|