f
This commit is contained in:
@@ -241,95 +241,32 @@ class Get_Url(Cmdlet):
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def _resolve_title_for_hash(backend: Any, file_hash: str, hit: Any = None) -> str:
|
||||
"""Best-effort title resolution for a found hash.
|
||||
|
||||
Strategy:
|
||||
- Use the hit's existing title/columns when present.
|
||||
- Prefer backend.get_metadata(hash) when available (direct lookup).
|
||||
- Fallback to backend.search('hash:<sha>', limit=1) and read title.
|
||||
"""
|
||||
try:
|
||||
if hit is not None:
|
||||
from_hit = Get_Url._extract_title_from_result(hit)
|
||||
if from_hit:
|
||||
return from_hit
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
try:
|
||||
if hasattr(backend, "get_metadata"):
|
||||
meta = backend.get_metadata(file_hash)
|
||||
if isinstance(meta, dict):
|
||||
t = meta.get("title")
|
||||
if isinstance(t, str) and t.strip():
|
||||
return t.strip()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
try:
|
||||
if hasattr(backend, "search"):
|
||||
hits = backend.search(f"hash:{file_hash}", limit=1)
|
||||
if isinstance(hits, list) and hits:
|
||||
t2 = Get_Url._extract_title_from_result(hits[0])
|
||||
if t2:
|
||||
return t2
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return ""
|
||||
def _extract_size_from_hit(hit: Any) -> int | None:
|
||||
for key in ("size", "file_size", "filesize", "size_bytes"):
|
||||
try:
|
||||
val = get_field(hit, key)
|
||||
except Exception:
|
||||
val = None
|
||||
if val is None:
|
||||
continue
|
||||
if isinstance(val, (int, float)):
|
||||
return int(val)
|
||||
try:
|
||||
return int(val)
|
||||
except Exception:
|
||||
continue
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def _resolve_size_ext_for_hash(backend: Any, file_hash: str, hit: Any = None) -> tuple[int | None, str]:
|
||||
"""Best-effort (size, ext) resolution for a found hash."""
|
||||
# First: see if the hit already includes these fields.
|
||||
try:
|
||||
size_val = get_field(hit, "size")
|
||||
if size_val is None:
|
||||
size_val = get_field(hit, "file_size")
|
||||
if size_val is None:
|
||||
size_val = get_field(hit, "filesize")
|
||||
if size_val is None:
|
||||
size_val = get_field(hit, "size_bytes")
|
||||
size_int = int(size_val) if isinstance(size_val, (int, float)) else None
|
||||
except Exception:
|
||||
size_int = None
|
||||
|
||||
try:
|
||||
ext_val = get_field(hit, "ext")
|
||||
if ext_val is None:
|
||||
ext_val = get_field(hit, "extension")
|
||||
ext = str(ext_val).strip().lstrip(".") if isinstance(ext_val, str) else ""
|
||||
except Exception:
|
||||
ext = ""
|
||||
|
||||
if size_int is not None or ext:
|
||||
return size_int, ext
|
||||
|
||||
# Next: backend.get_metadata(hash) when available.
|
||||
try:
|
||||
if hasattr(backend, "get_metadata"):
|
||||
meta = backend.get_metadata(file_hash)
|
||||
if isinstance(meta, dict):
|
||||
size_val2 = meta.get("size")
|
||||
if size_val2 is None:
|
||||
size_val2 = meta.get("file_size")
|
||||
if size_val2 is None:
|
||||
size_val2 = meta.get("filesize")
|
||||
if size_val2 is None:
|
||||
size_val2 = meta.get("size_bytes")
|
||||
if isinstance(size_val2, (int, float)):
|
||||
size_int = int(size_val2)
|
||||
|
||||
ext_val2 = meta.get("ext")
|
||||
if ext_val2 is None:
|
||||
ext_val2 = meta.get("extension")
|
||||
if isinstance(ext_val2, str) and ext_val2.strip():
|
||||
ext = ext_val2.strip().lstrip(".")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return size_int, ext
|
||||
def _extract_ext_from_hit(hit: Any) -> str:
|
||||
for key in ("ext", "extension"):
|
||||
try:
|
||||
ext_val = get_field(hit, key)
|
||||
except Exception:
|
||||
ext_val = None
|
||||
if isinstance(ext_val, str) and ext_val.strip():
|
||||
return ext_val.strip().lstrip(".")
|
||||
return ""
|
||||
|
||||
def _search_urls_across_stores(self,
|
||||
pattern: str,
|
||||
@@ -360,9 +297,6 @@ class Get_Url(Cmdlet):
|
||||
try:
|
||||
backend = storage[store_name]
|
||||
|
||||
title_cache: Dict[str, str] = {}
|
||||
meta_cache: Dict[str, tuple[int | None, str]] = {}
|
||||
|
||||
# Search only URL-bearing records using the backend's URL search capability.
|
||||
# This avoids the expensive/incorrect "search('*')" scan.
|
||||
try:
|
||||
@@ -431,22 +365,12 @@ class Get_Url(Cmdlet):
|
||||
search_limit,
|
||||
store_name,
|
||||
pattern_hint=target_pattern,
|
||||
minimal=True,
|
||||
)
|
||||
if search_results is None:
|
||||
continue
|
||||
|
||||
search_results = search_results or []
|
||||
if not search_results and target_pattern and not has_wildcards:
|
||||
fallback_results = self._execute_search_with_timeout(
|
||||
backend,
|
||||
"url:*",
|
||||
search_limit,
|
||||
store_name,
|
||||
pattern_hint=target_pattern,
|
||||
)
|
||||
if fallback_results is None:
|
||||
continue
|
||||
search_results = fallback_results or []
|
||||
|
||||
for hit in (search_results or []):
|
||||
if len(items) >= MAX_RESULTS:
|
||||
@@ -459,44 +383,9 @@ class Get_Url(Cmdlet):
|
||||
|
||||
file_hash = str(file_hash)
|
||||
|
||||
title = title_cache.get(file_hash, "")
|
||||
if not title:
|
||||
try:
|
||||
title = (
|
||||
get_field(hit, "title")
|
||||
or get_field(hit, "name")
|
||||
or get_field(hit, "file_title")
|
||||
or ""
|
||||
)
|
||||
except Exception:
|
||||
title = ""
|
||||
if not title:
|
||||
title = self._resolve_title_for_hash(backend, file_hash, hit)
|
||||
title_cache[file_hash] = title
|
||||
|
||||
size, ext = meta_cache.get(file_hash, (None, ""))
|
||||
if size is None and not ext:
|
||||
try:
|
||||
size = get_field(hit, "size")
|
||||
if size is None:
|
||||
size = get_field(hit, "size_bytes")
|
||||
if size is None:
|
||||
size = get_field(hit, "file_size")
|
||||
if size is None:
|
||||
size = get_field(hit, "filesize")
|
||||
size = int(size) if isinstance(size, (int, float)) else None
|
||||
except Exception:
|
||||
size = None
|
||||
|
||||
try:
|
||||
ext = get_field(hit, "ext") or get_field(hit, "extension")
|
||||
ext = str(ext).strip().lstrip(".") if isinstance(ext, str) else ""
|
||||
except Exception:
|
||||
ext = ""
|
||||
|
||||
if size is None and not ext:
|
||||
size, ext = self._resolve_size_ext_for_hash(backend, file_hash, hit)
|
||||
meta_cache[file_hash] = (size, ext)
|
||||
title = self._extract_title_from_result(hit) or ""
|
||||
size = self._extract_size_from_hit(hit)
|
||||
ext = self._extract_ext_from_hit(hit)
|
||||
|
||||
urls = self._extract_urls_from_hit(hit)
|
||||
if not urls:
|
||||
@@ -505,6 +394,7 @@ class Get_Url(Cmdlet):
|
||||
except Exception:
|
||||
urls = []
|
||||
|
||||
hit_added = False
|
||||
for url in (urls or []):
|
||||
if len(items) >= MAX_RESULTS:
|
||||
break
|
||||
@@ -526,7 +416,9 @@ class Get_Url(Cmdlet):
|
||||
ext=str(ext or ""),
|
||||
)
|
||||
)
|
||||
found_stores.add(str(store_name))
|
||||
hit_added = True
|
||||
if hit_added:
|
||||
found_stores.add(str(store_name))
|
||||
if len(items) >= MAX_RESULTS:
|
||||
break
|
||||
except Exception as exc:
|
||||
|
||||
Reference in New Issue
Block a user