This commit is contained in:
2026-01-11 02:26:39 -08:00
parent 450a923273
commit 086793790d
4 changed files with 313 additions and 21 deletions

View File

@@ -316,7 +316,7 @@ class HydrusNetwork(Store):
metadata = client.fetch_file_metadata(
hashes=[file_hash],
include_service_keys_to_tags=False,
include_file_url=False,
include_file_url=True,
include_duration=False,
include_size=False,
include_mime=False,
@@ -808,7 +808,7 @@ class HydrusNetwork(Store):
payload = client.fetch_file_metadata(
file_ids=chunk,
include_service_keys_to_tags=True,
include_file_url=False,
include_file_url=True,
include_duration=True,
include_size=True,
include_mime=True,
@@ -882,11 +882,18 @@ class HydrusNetwork(Store):
)
_collect(display_tags)
file_url = f"{self.URL.rstrip('/')}/get_files/file?hash={hash_hex}"
# Unique tags
all_tags = sorted(list(set(all_tags)))
# Use known URLs (source URLs) from Hydrus if available (matches get-url cmdlet)
item_url = meta.get("known_urls") or meta.get("urls") or meta.get("url") or []
if not item_url:
item_url = meta.get("file_url") or f"{self.URL.rstrip('/')}/view_file?hash={hash_hex}"
results.append(
{
"hash": hash_hex,
"url": file_url,
"url": item_url,
"name": title,
"title": title,
"size": size,
@@ -912,7 +919,7 @@ class HydrusNetwork(Store):
metadata = client.fetch_file_metadata(
file_ids=file_ids,
include_service_keys_to_tags=True,
include_file_url=False,
include_file_url=True,
include_duration=True,
include_size=True,
include_mime=True,
@@ -922,7 +929,7 @@ class HydrusNetwork(Store):
metadata = client.fetch_file_metadata(
hashes=hashes,
include_service_keys_to_tags=True,
include_file_url=False,
include_file_url=True,
include_duration=True,
include_size=True,
include_mime=True,
@@ -946,7 +953,7 @@ class HydrusNetwork(Store):
metadata = client.fetch_file_metadata(
file_ids=file_ids,
include_service_keys_to_tags=True,
include_file_url=False,
include_file_url=True,
include_duration=True,
include_size=True,
include_mime=True,
@@ -956,7 +963,7 @@ class HydrusNetwork(Store):
metadata = client.fetch_file_metadata(
hashes=hashes,
include_service_keys_to_tags=True,
include_file_url=False,
include_file_url=True,
include_duration=True,
include_size=True,
include_mime=True,
@@ -1020,6 +1027,9 @@ class HydrusNetwork(Store):
# Also consider top-level flattened tags payload if provided (Hydrus API sometimes includes it)
top_level_tags = meta.get("tags_flat", []) or meta.get("tags", [])
_collect(top_level_tags)
# Unique tags
all_tags = sorted(list(set(all_tags)))
# Prefer Hydrus-provided extension (e.g. ".webm"); fall back to MIME map.
mime_type = meta.get("mime")
@@ -1038,14 +1048,18 @@ class HydrusNetwork(Store):
# Just include what the tag search returned
has_namespace = ":" in query_lower
# Use known URLs (source URLs) from Hydrus if available (matches get-url cmdlet)
item_url = meta.get("known_urls") or meta.get("urls") or meta.get("url") or []
if not item_url:
item_url = meta.get("file_url") or f"{self.URL.rstrip('/')}/view_file?hash={hash_hex}"
if has_namespace:
# Explicit namespace search - already filtered by Hydrus tag search
# Include this result as-is
file_url = f"{self.URL.rstrip('/')}/get_files/file?hash={hash_hex}"
results.append(
{
"hash": hash_hex,
"url": file_url,
"url": item_url,
"name": title,
"title": title,
"size": size,
@@ -1074,11 +1088,10 @@ class HydrusNetwork(Store):
break
if match:
file_url = f"{self.URL.rstrip('/')}/get_files/file?hash={hash_hex}"
results.append(
{
"hash": hash_hex,
"url": file_url,
"url": item_url,
"name": title,
"title": title,
"size": size,
@@ -1113,7 +1126,7 @@ class HydrusNetwork(Store):
raise
def get_file(self, file_hash: str, **kwargs: Any) -> Path | str | None:
"""Return a browser URL for the file.
"""Return the local file system path if available, else a browser URL.
IMPORTANT: this method must be side-effect free (do not auto-open a browser).
Only explicit user actions (e.g. the get-file cmdlet) should open files.
@@ -1121,13 +1134,33 @@ class HydrusNetwork(Store):
debug(f"{self._log_prefix()} get_file: start hash={file_hash[:12]}...")
# Build browser URL with access key
# Try to get the local disk path if possible (works if Hydrus is on same machine)
server_path = None
try:
path_res = self._client.get_file_path(file_hash)
if isinstance(path_res, dict) and "path" in path_res:
server_path = path_res["path"]
if server_path:
local_path = Path(server_path)
if local_path.exists():
debug(f"{self._log_prefix()} get_file: found local path: {local_path}")
return local_path
except Exception as e:
debug(f"{self._log_prefix()} get_file: could not resolve path from API: {e}")
# If we found a path on the server but it's not locally accessible,
# return it as a string so it can be displayed in metadata panels.
if server_path:
debug(f"{self._log_prefix()} get_file: returning server path (not local): {server_path}")
return server_path
# Fallback to browser URL with access key
base_url = str(self.URL).rstrip("/")
access_key = str(self.API)
browser_url = (
f"{base_url}/get_files/file?hash={file_hash}&Hydrus-Client-API-Access-Key={access_key}"
)
debug(f"{self._log_prefix()} get_file: url={browser_url}")
debug(f"{self._log_prefix()} get_file: falling back to url={browser_url}")
return browser_url
def download_to_temp(
@@ -1449,7 +1482,7 @@ class HydrusNetwork(Store):
payload = client.fetch_file_metadata(
hashes=[file_hash],
include_service_keys_to_tags=True,
include_file_url=False
include_file_url=True
)
items = payload.get("metadata") if isinstance(payload, dict) else None
@@ -1603,7 +1636,7 @@ class HydrusNetwork(Store):
payload = client.fetch_file_metadata(
hashes=[file_hash],
include_file_url=False
include_file_url=True
)
items = payload.get("metadata") if isinstance(payload, dict) else None
if not isinstance(items, list) or not items: