This commit is contained in:
nose
2025-12-13 00:18:30 -08:00
parent 85750247cc
commit 30eb628aa3
18 changed files with 1056 additions and 407 deletions

View File

@@ -17,8 +17,32 @@ class HydrusNetwork(Store):
Each instance represents a specific Hydrus client connection.
Maintains its own HydrusClient with session key.
"""
def __new__(cls, *args: Any, **kwargs: Any) -> "HydrusNetwork":
instance = super().__new__(cls)
name = kwargs.get("NAME")
api = kwargs.get("API")
url = kwargs.get("URL")
if name is not None:
setattr(instance, "NAME", str(name))
if api is not None:
setattr(instance, "API", str(api))
if url is not None:
setattr(instance, "URL", str(url))
return instance
setattr(__new__, "keys", ("NAME", "API", "URL"))
def __init__(self, instance_name: str, api_key: str, url: str) -> None:
def __init__(
self,
instance_name: Optional[str] = None,
api_key: Optional[str] = None,
url: Optional[str] = None,
*,
NAME: Optional[str] = None,
API: Optional[str] = None,
URL: Optional[str] = None,
) -> None:
"""Initialize Hydrus storage backend.
Args:
@@ -27,18 +51,41 @@ class HydrusNetwork(Store):
url: Hydrus client URL (e.g., 'http://192.168.1.230:45869')
"""
from API.HydrusNetwork import HydrusNetwork as HydrusClient
if instance_name is None and NAME is not None:
instance_name = str(NAME)
if api_key is None and API is not None:
api_key = str(API)
if url is None and URL is not None:
url = str(URL)
if not instance_name or not api_key or not url:
raise ValueError("HydrusNetwork requires NAME, API, and URL")
self._instance_name = instance_name
self._api_key = api_key
self._url = url
self.NAME = instance_name
self.API = api_key
self.URL = url
# Create persistent client with session key for this instance
self._client = HydrusClient(url=url, access_key=api_key)
# Self health-check: acquire a session key immediately so broken configs
# fail-fast and the registry can skip registering this backend.
try:
if self._client is not None:
self._client.ensure_session_key()
except Exception as exc:
# Best-effort cleanup so partially constructed objects don't linger.
try:
self._client = None
except Exception:
pass
raise RuntimeError(f"Hydrus '{self.NAME}' unavailable: {exc}") from exc
def name(self) -> str:
return self._instance_name
return self.NAME
def get_name(self) -> str:
return self._instance_name
return self.NAME
def add_file(self, file_path: Path, **kwargs: Any) -> str:
"""Upload file to Hydrus with full metadata support.
@@ -281,7 +328,7 @@ class HydrusNetwork(Store):
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}"
file_url = f"{self.URL.rstrip('/')}/get_files/file?hash={hash_hex}"
results.append({
"hash": hash_hex,
"url": file_url,
@@ -289,7 +336,7 @@ class HydrusNetwork(Store):
"title": title,
"size": size,
"size_bytes": size,
"store": self._instance_name,
"store": self.NAME,
"tag": all_tags,
"file_id": file_id,
"mime": mime_type,
@@ -314,7 +361,7 @@ class HydrusNetwork(Store):
break
if match:
file_url = f"{self._url.rstrip('/')}/get_files/file?hash={hash_hex}"
file_url = f"{self.URL.rstrip('/')}/get_files/file?hash={hash_hex}"
results.append({
"hash": hash_hex,
"url": file_url,
@@ -322,7 +369,7 @@ class HydrusNetwork(Store):
"title": title,
"size": size,
"size_bytes": size,
"store": self._instance_name,
"store": self.NAME,
"tag": all_tags,
"file_id": file_id,
"mime": mime_type,
@@ -345,8 +392,8 @@ class HydrusNetwork(Store):
debug(f"[HydrusNetwork.get_file] Starting for hash: {file_hash[:12]}...")
# Build browser URL with access key
base_url = self._client.url.rstrip('/')
access_key = self._client.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"[HydrusNetwork.get_file] Opening URL: {browser_url}")