-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhash_vault_browser.py
More file actions
77 lines (59 loc) · 2.3 KB
/
Copy pathhash_vault_browser.py
File metadata and controls
77 lines (59 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""Hash Vault Browser backend route.
Registers GET /akurate/hash_vault/list which walks output/hash_vault/, reads
every {hash}.json sidecar, and returns a JSON array sorted by last_accessed_at
desc. Thumbnails are served by ComfyUI's built-in /view route using
type=output&subfolder=hash_vault&filename={hash}.thumb.png — no extra route
needed on our side.
The .pt files themselves are never read here; they are opaque + expensive to
load, and the sidecar already carries everything the browser UI needs.
"""
from __future__ import annotations
import json
import os
from typing import Any
try:
import folder_paths
except ImportError:
folder_paths = None
try:
from server import PromptServer
from aiohttp import web
except ImportError:
PromptServer = None
web = None
def _vault_dir() -> str | None:
if folder_paths is None:
return None
return os.path.join(folder_paths.get_output_directory(), "hash_vault")
def _list_entries() -> list[dict[str, Any]]:
"""Walk hash_vault/, read sidecars, return enriched entries sorted by last_accessed_at desc."""
vault = _vault_dir()
if not vault or not os.path.isdir(vault):
return []
entries: list[dict[str, Any]] = []
for name in os.listdir(vault):
if not name.endswith(".json"):
continue
sidecar_path = os.path.join(vault, name)
hash_key = name[:-5] # strip .json
try:
with open(sidecar_path, "r", encoding="utf-8") as f:
sidecar = json.load(f)
except (OSError, json.JSONDecodeError):
continue
pt_path = os.path.join(vault, f"{hash_key}.pt")
pt_size = os.path.getsize(pt_path) if os.path.exists(pt_path) else 0
sidecar["pt_size"] = pt_size
sidecar["pt_exists"] = pt_size > 0
entries.append(sidecar)
entries.sort(key=lambda e: e.get("last_accessed_at", ""), reverse=True)
return entries
if PromptServer is not None and web is not None:
routes = PromptServer.instance.routes
@routes.get("/akurate/hash_vault/list")
async def hash_vault_list(request):
try:
data = _list_entries()
return web.json_response({"entries": data, "count": len(data)})
except Exception as e:
return web.json_response({"error": str(e)}, status=500)