This commit is contained in:
2026-01-23 21:32:34 -08:00
parent 666f4e3181
commit 33a9d80ab4
5 changed files with 128 additions and 39 deletions

View File

@@ -20,6 +20,7 @@ from . import _shared as sh
from SYS.logger import log, debug
from Store import Store
from SYS.config import resolve_output_dir
from API.HTTP import _download_direct_file
class Get_File(sh.Cmdlet):
@@ -148,36 +149,36 @@ class Get_File(sh.Cmdlet):
debug(f"[get-file] backend.get_file returned: {source_path}")
# Check if backend returned a URL (HydrusNetwork case)
if isinstance(source_path,
str) and (source_path.startswith("http://")
or source_path.startswith("https://")):
# Hydrus backend returns a URL; open it only for this explicit user action.
download_url = None
if isinstance(source_path, str):
if source_path.startswith("http://") or source_path.startswith("https://"):
download_url = source_path
else:
source_path = Path(source_path)
if download_url and output_path is None:
# Hydrus backend returns a URL; open it only when no output path
try:
webbrowser.open(source_path)
webbrowser.open(download_url)
except Exception as exc:
log(f"Error opening browser: {exc}", file=sys.stderr)
else:
debug(f"Opened in browser: {source_path}", file=sys.stderr)
debug(f"Opened in browser: {download_url}", file=sys.stderr)
# Emit result for pipeline
ctx.emit(
{
"hash": file_hash,
"store": store_name,
"url": source_path,
"url": download_url,
"title": resolve_display_title() or "Opened",
}
)
return 0
# Otherwise treat as file path (local/folder backends)
if isinstance(source_path, str):
source_path = Path(source_path)
if not source_path or not source_path.exists():
log(f"Error: Backend could not retrieve file for hash {file_hash}")
return 1
if download_url is None:
if not source_path or not source_path.exists():
log(f"Error: Backend could not retrieve file for hash {file_hash}")
return 1
# Otherwise: export/copy to output_dir.
if output_path:
@@ -206,11 +207,21 @@ class Get_File(sh.Cmdlet):
ext = "." + ext
filename += ext
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)
dest_path: Path
if download_url:
downloaded = _download_direct_file(
download_url,
output_dir,
quiet=True,
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)
log(f"Exported: {dest_path}", file=sys.stderr)