This commit is contained in:
2026-03-25 22:39:30 -07:00
parent c31402c8f1
commit 562acd809c
46 changed files with 2367 additions and 1868 deletions

View File

@@ -4,7 +4,9 @@ from typing import Any, Dict, Sequence, Optional
import json
import sys
from SYS.item_accessors import get_extension_field, get_int_field
from SYS.logger import log
from SYS.payload_builders import build_file_result_payload
from . import _shared as sh
@@ -15,6 +17,7 @@ parse_cmdlet_args = sh.parse_cmdlet_args
get_field = sh.get_field
from SYS import pipeline as ctx
from SYS.result_table import Table
from SYS.result_table_helpers import add_row_columns
class Get_Metadata(Cmdlet):
@@ -176,22 +179,28 @@ class Get_Metadata(Cmdlet):
store or ""),
]
return {
"title": title or path,
"path": path,
"store": store,
"mime": mime,
"ext": ext or "",
"size_bytes": size_int,
"duration_seconds": dur_int,
"pages": pages_int,
"imported_ts": imported_ts,
"imported": imported_label,
"hash": hash_value,
"url": url,
"tag": tag or [],
"columns": columns,
}
payload = build_file_result_payload(
title=title,
fallback_title=path,
path=path,
url=url,
hash_value=hash_value,
store=store,
tag=tag or [],
ext=ext,
size_bytes=size_int,
columns=columns,
)
payload.update(
{
"mime": mime,
"duration_seconds": dur_int,
"pages": pages_int,
"imported_ts": imported_ts,
"imported": imported_label,
}
)
return payload
@staticmethod
def _add_table_body_row(table: Table, row: Dict[str, Any]) -> None:
@@ -213,16 +222,18 @@ class Get_Metadata(Cmdlet):
label, value = col
lookup[str(label)] = value
row_obj = table.add_row()
row_obj.add_column("Hash", lookup.get("Hash", ""))
row_obj.add_column("MIME", lookup.get("MIME", ""))
row_obj.add_column("Size(MB)", lookup.get("Size(MB)", ""))
columns_to_add = [
("Hash", lookup.get("Hash", "")),
("MIME", lookup.get("MIME", "")),
("Size(MB)", lookup.get("Size(MB)", "")),
]
if "Duration(s)" in lookup:
row_obj.add_column("Duration(s)", lookup.get("Duration(s)", ""))
columns_to_add.append(("Duration(s)", lookup.get("Duration(s)", "")))
elif "Pages" in lookup:
row_obj.add_column("Pages", lookup.get("Pages", ""))
columns_to_add.append(("Pages", lookup.get("Pages", "")))
else:
row_obj.add_column("Duration(s)", "")
columns_to_add.append(("Duration(s)", ""))
add_row_columns(table, columns_to_add)
def run(self, result: Any, args: Sequence[str], config: Dict[str, Any]) -> int:
"""Execute get-metadata cmdlet - retrieve and display file metadata.
@@ -247,9 +258,12 @@ class Get_Metadata(Cmdlet):
# Parse arguments
parsed = parse_cmdlet_args(args, self)
query_hash = sh.parse_single_hash_query(parsed.get("query"))
if parsed.get("query") and not query_hash:
log('No hash available - use -query "hash:<sha256>"', file=sys.stderr)
query_hash, query_valid = sh.require_single_hash_query(
parsed.get("query"),
'No hash available - use -query "hash:<sha256>"',
log_file=sys.stderr,
)
if not query_valid:
return 1
# Get hash and store from parsed args or result
@@ -266,21 +280,14 @@ class Get_Metadata(Cmdlet):
# Use storage backend to get metadata
try:
# Instantiate only the required backend when possible to avoid initializing all configured backends
try:
from Store.registry import get_backend_instance
backend = get_backend_instance(config, storage_source, suppress_debug=True)
except Exception:
backend = None
backend, _store_registry, _exc = sh.get_preferred_store_backend(
config,
storage_source,
suppress_debug=True,
)
if backend is None:
try:
from Store import Store
storage = Store(config)
backend = storage[storage_source]
except Exception:
log(f"Storage backend '{storage_source}' not found", file=sys.stderr)
return 1
log(f"Storage backend '{storage_source}' not found", file=sys.stderr)
return 1
# Get metadata from backend
metadata = backend.get_metadata(file_hash)
@@ -330,8 +337,8 @@ class Get_Metadata(Cmdlet):
# Extract metadata fields
mime_type = metadata.get("mime") or metadata.get("ext", "")
file_ext = metadata.get("ext", "") # Extract file extension separately
file_size = metadata.get("size")
file_ext = get_extension_field(metadata, "ext", "extension")
file_size = get_int_field(metadata, "size", "size_bytes")
duration_seconds = metadata.get("duration")
if duration_seconds is None:
duration_seconds = metadata.get("duration_seconds")