Files
Medios-Macina/cmdlets/get_url.py

81 lines
2.3 KiB
Python
Raw Normal View History

2025-11-25 20:09:33 -08:00
from __future__ import annotations
from typing import Any, Dict, Sequence
2025-12-01 01:10:16 -08:00
import sys
2025-11-25 20:09:33 -08:00
from . import register
import pipeline as ctx
2025-12-11 12:47:30 -08:00
from ._shared import Cmdlet, CmdletArg, SharedArgs, parse_cmdlet_args, get_field, normalize_hash
2025-11-25 20:09:33 -08:00
from helper.logger import log
2025-12-11 12:47:30 -08:00
from helper.store import FileStorage
2025-11-25 20:09:33 -08:00
2025-12-11 12:47:30 -08:00
class Get_Url(Cmdlet):
"""Get url associated with files via hash+store."""
2025-11-25 20:09:33 -08:00
2025-12-11 12:47:30 -08:00
NAME = "get-url"
SUMMARY = "List url associated with a file"
USAGE = "@1 | get-url"
ARGS = [
SharedArgs.HASH,
SharedArgs.STORE,
]
DETAIL = [
"- Lists all url associated with file identified by hash+store",
]
2025-12-01 01:10:16 -08:00
2025-12-11 12:47:30 -08:00
def run(self, result: Any, args: Sequence[str], config: Dict[str, Any]) -> int:
"""Get url for file via hash+store backend."""
parsed = parse_cmdlet_args(args, self)
# Extract hash and store from result or args
file_hash = parsed.get("hash") or get_field(result, "hash")
store_name = parsed.get("store") or get_field(result, "store")
if not file_hash:
log("Error: No file hash provided")
return 1
if not store_name:
log("Error: No store name provided")
return 1
# Normalize hash
file_hash = normalize_hash(file_hash)
if not file_hash:
log("Error: Invalid hash format")
return 1
# Get backend and retrieve url
2025-12-01 01:10:16 -08:00
try:
2025-12-11 12:47:30 -08:00
storage = FileStorage(config)
backend = storage[store_name]
url = backend.get_url(file_hash)
if url:
for url in url:
# Emit rich object for pipeline compatibility
ctx.emit({
"url": url,
"hash": file_hash,
"store": store_name,
})
return 0
else:
ctx.emit("No url found")
return 0
except KeyError:
log(f"Error: Storage backend '{store_name}' not configured")
return 1
2025-12-01 01:10:16 -08:00
except Exception as exc:
2025-12-11 12:47:30 -08:00
log(f"Error retrieving url: {exc}", file=sys.stderr)
return 1
2025-12-01 01:10:16 -08:00
2025-12-11 12:47:30 -08:00
# Register cmdlet
register(["get-url", "get_url"])(Get_Url)
2025-11-25 20:09:33 -08:00