refactored and updated tags cmdlet and hydrusnetwork interaction plugin features

This commit is contained in:
2026-05-04 15:08:18 -07:00
parent 5534812426
commit bca85defa4
17 changed files with 380 additions and 175 deletions
+76 -22
View File
@@ -18,7 +18,7 @@ from urllib.request import pathname2url
from SYS import pipeline as ctx
from . import _shared as sh
from SYS.item_accessors import get_result_title
from SYS.logger import log, debug
from SYS.logger import log, debug, debug_panel
from SYS.config import resolve_output_dir
from API.HTTP import _download_direct_file
from SYS.payload_builders import build_file_result_payload
@@ -53,9 +53,18 @@ class Get_File(sh.Cmdlet):
def run(self, result: Any, args: Sequence[str], config: Dict[str, Any]) -> int:
"""Export file via hash+store backend."""
debug(f"[get-file] run() called with result type: {type(result)}")
parsed = sh.parse_cmdlet_args(args, self)
debug(f"[get-file] parsed args: {parsed}")
try:
debug_panel(
"get-file",
[
("result_type", type(result).__name__),
("parsed_args", parsed),
],
border_style="cyan",
)
except Exception:
pass
query_hash, query_valid = sh.require_single_hash_query(
parsed.get("query"),
@@ -70,8 +79,6 @@ class Get_File(sh.Cmdlet):
output_path = parsed.get("path")
output_name = parsed.get("name")
debug(f"[get-file] file_hash={file_hash} store_name={store_name}")
if not file_hash:
log(
'Error: No file hash provided (pipe an item or use -query "hash:<sha256>")'
@@ -88,7 +95,19 @@ class Get_File(sh.Cmdlet):
log("Error: Invalid hash format")
return 1
debug(f"[get-file] Getting storage backend: {store_name}")
try:
debug_panel(
"get-file selection",
[
("hash", file_hash),
("instance", store_name),
("output_path", output_path or "<default>"),
("output_name", output_name or "<auto>"),
],
border_style="blue",
)
except Exception:
pass
backend, _store_registry, _exc = sh.get_preferred_store_backend(
config,
@@ -99,17 +118,23 @@ class Get_File(sh.Cmdlet):
log(f"Error: Storage backend '{store_name}' not found", file=sys.stderr)
return 1
debug(f"[get-file] Backend retrieved: {type(backend).__name__}")
# Get file metadata to determine name and extension
debug("[get-file] Getting metadata for hash...")
metadata = backend.get_metadata(file_hash)
if not metadata:
log(f"Error: File metadata not found for hash {file_hash}")
return 1
debug(
f"[get-file] Metadata retrieved: title={metadata.get('title')}, ext={metadata.get('ext')}"
)
try:
debug_panel(
"get-file backend",
[
("backend", type(backend).__name__),
("title", metadata.get("title") or ""),
("ext", metadata.get("ext") or ""),
],
border_style="green",
)
except Exception:
pass
def resolve_display_title() -> str:
candidates = [
@@ -124,17 +149,12 @@ class Get_File(sh.Cmdlet):
return text
return ""
debug(f"[get-file] Calling backend.get_file({file_hash})")
# Get file from backend (may return Path or URL string depending on backend).
# We pass url=True if no explicit path was provided, which hints the backend
# (specifically Hydrus) to return a browser-friendly URL instead of a local path.
want_url = (output_path is None)
debug(f"[get-file] Requesting file from backend (url_hint={want_url})...")
source_path = backend.get_file(file_hash, url=want_url)
debug(f"[get-file] backend.get_file returned: {source_path}")
download_url = None
if isinstance(source_path, str):
if source_path.startswith("http://") or source_path.startswith("https://"):
@@ -142,6 +162,19 @@ class Get_File(sh.Cmdlet):
else:
source_path = Path(source_path)
try:
debug_panel(
"get-file fetch",
[
("url_hint", want_url),
("mode", "browser-url" if download_url else "local-path"),
("source", download_url or source_path or "<missing>"),
],
border_style="magenta",
)
except Exception:
pass
if download_url and output_path is None:
# Hydrus backend returns a URL; open it only when no output path
try:
@@ -149,7 +182,18 @@ class Get_File(sh.Cmdlet):
except Exception as exc:
log(f"Error opening browser: {exc}", file=sys.stderr)
else:
debug(f"Opened in browser: {download_url}", file=sys.stderr)
try:
debug_panel(
"get-file open",
[
("action", "browser-open"),
("url", download_url),
],
file=sys.stderr,
border_style="green",
)
except Exception:
pass
ctx.emit(
build_file_result_payload(
@@ -172,7 +216,6 @@ class Get_File(sh.Cmdlet):
else:
output_dir = resolve_output_dir(config)
debug(f"[get-file] Output dir: {output_dir}")
output_dir.mkdir(parents=True, exist_ok=True)
# Determine output filename (only when exporting)
@@ -202,13 +245,25 @@ class Get_File(sh.Cmdlet):
suggested_filename=filename,
)
dest_path = downloaded.path
debug(f"[get-file] Downloaded remote file to {dest_path}", file=sys.stderr)
else:
dest_path = self._unique_path(output_dir / filename)
# Copy file to destination
debug(f"[get-file] Copying {source_path} -> {dest_path}", file=sys.stderr)
shutil.copy2(source_path, dest_path)
try:
debug_panel(
"get-file export",
[
("mode", "download" if download_url else "copy"),
("destination", dest_path),
("filename", filename),
],
file=sys.stderr,
border_style="green",
)
except Exception:
pass
log(f"Exported: {dest_path}", file=sys.stderr)
# Emit result for pipeline
@@ -221,7 +276,6 @@ class Get_File(sh.Cmdlet):
)
)
debug("[get-file] Completed successfully")
return 0
def _open_file_default(self, path: Path) -> None: