f
This commit is contained in:
@@ -7,6 +7,7 @@ import sys
|
||||
import shutil
|
||||
import tempfile
|
||||
import re
|
||||
from urllib.parse import urlparse
|
||||
|
||||
from SYS import models
|
||||
from SYS import pipeline as ctx
|
||||
@@ -14,6 +15,7 @@ from SYS.logger import log, debug, is_debug_enabled
|
||||
from SYS.pipeline_progress import PipelineProgress
|
||||
from SYS.utils_constant import ALL_SUPPORTED_EXTENSIONS
|
||||
from Store import Store
|
||||
from API.HTTP import _download_direct_file
|
||||
from . import _shared as sh
|
||||
|
||||
Cmdlet = sh.Cmdlet
|
||||
@@ -34,7 +36,7 @@ coerce_to_path = sh.coerce_to_path
|
||||
build_pipeline_preview = sh.build_pipeline_preview
|
||||
get_field = sh.get_field
|
||||
|
||||
from SYS.utils import sha256_file, unique_path
|
||||
from SYS.utils import sha256_file, unique_path, sanitize_filename
|
||||
from SYS.metadata import write_metadata
|
||||
|
||||
# Canonical supported filetypes for all stores/cmdlets
|
||||
@@ -1079,6 +1081,62 @@ class Add_File(Cmdlet):
|
||||
pass
|
||||
return None, None
|
||||
|
||||
@staticmethod
|
||||
def _build_provider_filename(
|
||||
pipe_obj: models.PipeObject,
|
||||
fallback_hash: Optional[str] = None,
|
||||
source_url: Optional[str] = None,
|
||||
) -> str:
|
||||
title_candidates: List[str] = []
|
||||
title_value = getattr(pipe_obj, "title", "")
|
||||
if title_value:
|
||||
title_candidates.append(str(title_value))
|
||||
|
||||
extra = getattr(pipe_obj, "extra", {})
|
||||
if isinstance(extra, dict):
|
||||
candid = extra.get("name") or extra.get("title")
|
||||
if candid:
|
||||
title_candidates.append(str(candid))
|
||||
|
||||
metadata = getattr(pipe_obj, "metadata", {})
|
||||
if isinstance(metadata, dict):
|
||||
meta_name = metadata.get("title") or metadata.get("name")
|
||||
if meta_name:
|
||||
title_candidates.append(str(meta_name))
|
||||
|
||||
text = ""
|
||||
for candidate in title_candidates:
|
||||
if candidate:
|
||||
text = candidate.strip()
|
||||
if text:
|
||||
break
|
||||
|
||||
if not text and fallback_hash:
|
||||
text = fallback_hash[:8]
|
||||
|
||||
safe_name = sanitize_filename(text or "download")
|
||||
|
||||
ext = ""
|
||||
if isinstance(metadata, dict):
|
||||
ext = metadata.get("ext") or metadata.get("extension") or ""
|
||||
if not ext and isinstance(extra, dict):
|
||||
ext = extra.get("ext") or ""
|
||||
if not ext and source_url:
|
||||
try:
|
||||
parsed = urlparse(source_url)
|
||||
ext = Path(parsed.path).suffix.lstrip(".")
|
||||
except Exception:
|
||||
ext = ""
|
||||
|
||||
if ext:
|
||||
ext_text = str(ext)
|
||||
if not ext_text.startswith("."):
|
||||
ext_text = "." + ext_text.lstrip(".")
|
||||
if not safe_name.lower().endswith(ext_text.lower()):
|
||||
safe_name = f"{safe_name}{ext_text}"
|
||||
|
||||
return safe_name or "download"
|
||||
|
||||
@staticmethod
|
||||
def _resolve_backend_by_name(store: Any, backend_name: str) -> Optional[Any]:
|
||||
if not store or not backend_name:
|
||||
@@ -1219,6 +1277,32 @@ class Add_File(Cmdlet):
|
||||
)
|
||||
if dl_path and dl_path.exists():
|
||||
return dl_path, str(r_hash), tmp_dir
|
||||
source_url = str(source).strip()
|
||||
if source_url.lower().startswith(("http://", "https://")):
|
||||
download_dir = Path(tempfile.mkdtemp(prefix="add-file-src-"))
|
||||
try:
|
||||
filename = Add_File._build_provider_filename(
|
||||
pipe_obj,
|
||||
str(r_hash),
|
||||
source_url,
|
||||
)
|
||||
downloaded = _download_direct_file(
|
||||
source_url,
|
||||
download_dir,
|
||||
quiet=True,
|
||||
suggested_filename=filename,
|
||||
)
|
||||
downloaded_path = downloaded.path
|
||||
if downloaded_path and downloaded_path.exists():
|
||||
pipe_obj.is_temp = True
|
||||
pipe_obj.path = str(downloaded_path)
|
||||
return downloaded_path, str(r_hash), download_dir
|
||||
except Exception as exc:
|
||||
debug(f"[add-file] Provider download failed: {exc}")
|
||||
try:
|
||||
shutil.rmtree(download_dir, ignore_errors=True)
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
Reference in New Issue
Block a user