Files
ComfyAssets-ComfyUI_PromptM…/web/js/gallery.js
T
Vito da322f8c94 Hardening: security fixes, queue-time run counting, background jobs, 85%+ test coverage (#153)
* chore: valid packaging metadata, flake8 config file, ignore local config with secrets

pyproject.toml carried requirements.txt comment lines as dependencies, which
fails PEP 508 parsing. flake8 never read [tool.flake8] from pyproject, so the
settings move to .flake8 and pre-commit runs the full rule set. config.json
(holds the CivitAI API key and machine paths) and other local artifacts are
now ignored. coverage joins the dev requirements.

* ci: test on Linux, Windows and macOS with a coverage gate and lint job

* refactor: drop validate_workflow_name, the column it validated no longer exists

* chore: keep agent worktrees out of coverage measurement

* fix: image routes serve only media files inside allowed directories

serve_image and serve_output_image now reject anything whose extension
is not in IMAGE_EXTENSIONS, fail closed when no output directory is
configured, and answer 403 (not 404) for dot-dot, absolute and
symlink-escaping paths. Containment is checked after realpath+normcase
so it behaves the same on Windows, macOS and Linux. 500 responses use
safe_error_message so OSError text no longer leaks absolute paths.

* fix: bound page sizes on list endpoints

/search, /recent and /images/recent share parse_page_params: limit is
clamped to [1, MAX_PAGE_LIMIT=500], offset to >= 0, and non-integer
values answer 400 instead of 500. /search now honours offset and all
three return a pagination block with the effective limit and offset.

* perf: thumbnail generation no longer blocks the event loop

generate_thumbnails_with_progress is split into _thumbnail_targets
(scan, executor), _generate_one (one PIL/ffmpeg job, executor) and an
async driver that awaits each file via _run_in_executor, emits one
progress frame per file and yields between files. Per-file failures
are reported as file_error events (error stays fatal for the UI) and
the loop continues. The wildcard Access-Control-Allow-Origin header is
gone. The blocking POST variant reuses the same helpers. The scan never
follows symlinks and is capped by MAX_SCAN_DEPTH / MAX_THUMBNAIL_FILES.

* fix: cap every request-driven resource in the image routes

- /images/recent, /images/all and /images/output parse limit/offset
  through parse_page_params on every path (absent, 0, negative, float
  strings), and the clamped values are what reach the DB. /images/all
  gets its own MAX_BULK_LIMIT ceiling instead of an unbounded query;
  /images/recent caps its fetch window at MAX_RECENT_IMAGES_WINDOW.
- parse_page_params also clamps offset to MAX_PAGE_OFFSET so values past
  int64 no longer surface as a 500 from SQLite.
- Gallery and thumbnail scans share _iter_media_files: no symlink
  following, MAX_SCAN_DEPTH, and MAX_GALLERY_FILES / MAX_THUMBNAIL_FILES
  caps. Thumbnail size comes only from named presets.
- JSON bodies on /images/link and /images/generate-thumbnails are read
  in bounded chunks; over MAX_JSON_BODY_BYTES answers 413, malformed or
  non-object JSON answers 400.
- /images/link only accepts media paths inside the allowed directories
  (403 otherwise), closing a path-existence oracle.

* test: cover image and prompt routes

Every route in py/api/images.py and py/api/prompts.py now has a success
path and an error path test: save/update/delete/rate/tag/category/bulk
endpoints, image list/link/unlink/delete/metadata/scan endpoints, the
thumbnail SSE stream, clear-thumbnails, and the video thumbnail chain
(fake cv2 success/failure, ffmpeg absent or failing, PIL placeholder).
Combined statement+branch coverage: images.py 97.7%, prompts.py 97.4%.

* fix: autotag endpoints no longer read arbitrary paths or run on GET

* fix: CivitAI key is never returned or sent off-host

* fix: autotag/single checks real paths and DB rows against output dirs

* fix: CivitAI downloads never follow redirects

* refactor: autotag model loading is injectable

* test: cover autotag and lora integration

* fix: saving LoRA settings without a key keeps the stored CivitAI key

* refactor: share the internal error message in autotag routes

* fix: never treat negative-only PromptManager nodes as the run prompt

run_prompt_nodes fell back to every PromptManager node whenever no positive
node was traced, including graphs that do have a sampler and whose only
PromptManager node feeds its negative input. Fall back only when the graph
has no recognisable sampler (new has_sampler helper). Samplers named like
one are recognised even without a model link. Adversarial graphs (cycles,
10k-node fan-out, non-dict inputs) are covered.

* fix: do not follow negative text inputs when tracing the positive prompt

_TEXT_KEY matched input names such as text_negative, so a styler node feeding
a sampler's positive input pulled its negative PromptManager node into the
positive trace. Input names matching (^|_)neg(ative)?($|_) are now skipped
(new follows_input helper).

* fix: count prompt runs when ComfyUI dequeues the prompt, not when it is submitted

The on-prompt hook runs before ComfyUI validates the request, so counting
there bumped run_count for prompts that were rejected or deleted from the
queue. The hook now only resolves the positive PromptManager texts, mints a
UUID4 prompt_id when the client sent none, and stashes the hashes under that
id (QueuedRuns, bounded to 1000 entries). PromptQueue.get is wrapped
idempotently; when a prompt is dequeued for execution its stashed runs are
counted (or remembered as pending first use). Servers without a prompt_queue
fall back to counting at submit time with one warning.

__init__ defines _global_db before the monitoring block and passes a factory
that raises RuntimeError while the database is unavailable; the package is
now imported under test with server and folder_paths stubbed.

* fix: keep batch image queue aligned when unrelated workflows save images

The metadata strategy returned None for images whose embedded workflow has
no positive PromptManager node, so the handler popped the batch FIFO for
them and every later batch image linked to the wrong prompt. It now returns
a tri-state: linked (prompt found), foreign (no PromptManager prompt in the
workflow, so the image is skipped and no fallback applies) or unknown (only
then the queue, snapshot, tracker and recent-prompt fallbacks run).

* fix: image monitor validates directories before creating the observer

start_monitoring assigned self.observer before checking that any directory
existed, so with no valid directory the monitor was stuck "already running"
and stop_monitoring raised on join of a never-started observer. Directories
are validated first and only a started observer is kept; stop_monitoring is
safe to call at any time and always joins. New is_monitoring property.

The per-image threading.Timer is replaced by one daemon worker: paths are
deduplicated and bounded (MAX_PENDING_IMAGES), processed in creation order
(batch images pop the prompt queue in order) after the processing delay and
once the file size is unchanged across two reads.

* refactor: one prompt hash implementation

utils.hashing.generate_prompt_hash is now the only prompt hash:
PromptManagerBase._generate_hash delegates to it and the inline
hashlib.sha256 copies in the prompt tracker and image monitor are gone.

Remove PromptManagerBase._open_web_interface (it targeted a web_interface
folder that never existed) and the unused restart_gallery.py script.

IS_CHANGED on PromptManager and PromptManagerText now includes category and
tags, so editing either re-runs the node and updates the saved metadata.

* fix: stop ComfyUI from loading the admin bundle into the canvas page

WEB_DIRECTORY = "web" made ComfyUI inject every .js under web/ (admin.js,
gallery.js, vendored Tailwind and ViewerJS) into the canvas page. The node
extension moves to web/comfy/prompt_manager.js (content unchanged; its
../../scripts/app.js import resolves against the served URL, which is the
same) and WEB_DIRECTORY points at that folder only. Admin and gallery assets
are still served from web/ by the extension's own routes.

tests/test_sort_contract.py still reads web/prompt_manager.js and must be
updated to the new path by its owner.

* fix: image monitor recent-prompt fallback reads the paginated result

get_recent_prompts returns {"prompts": [...]} but _get_fallback_prompt
indexed it like a list, so the last-resort link to the most recent prompt
always failed silently. Accept both shapes.

* test: cover prompt_manager_base, prompt_manager and prompt_search_list

Node base: init wiring, metadata update on re-save, run counting, database
error paths, tag parsing, search and recent API wrappers, gallery start
failure, LoRA trigger-word injection (disabled, missing modules, enabled).
PromptManager: missing CLIP error, encoding of the combined text with a fake
CLIP, tracking failure tolerance, empty text. PromptManagerText: tracking
failure tolerance. PromptSearchList: filters, multipart and LoRA-only
skipping, newline collapsing, preview, empty and error results.

* fix: diagnostics dependency check survives a watchdog without __version__

check_dependencies read watchdog.__version__ for its log line; recent
watchdog releases have no such attribute, so the AttributeError escaped and
took the whole diagnostics run down. Read it with a default.

Also covers utils/diagnostics.py: database and images-table checks against
a real temporary database, file system check, ComfyUI output detection
(folder_paths, configured directories, relative fallback, nothing found),
dependencies, the full run and the test image link.

* test: cover utils/logging_config

Isolated logger manager on a temporary log directory: memory buffer order,
level filter and 1000-entry cap, rotation and backup listing, tail and whole
file reads, path traversal rejection, truncation (including removal
errors), level changes across prompt_manager loggers, handler rebuilds.

* test: cover utils/comfyui_integration

Singleton re-initialisation keeps registrations, latest prompt per thread,
cross-thread fallback with the five-minute recency limit, global lookup for
the current thread, and cleanup of old registrations.

* test: cover __init__

Version parsing from pyproject (and the unknown fallback), and a server
whose add_on_prompt_handler raises: nodes and monitoring still come up.

* docs: point at the moved canvas extension under web/comfy

* fix: give every thread its own sqlite connection

PromptModel.get_connection() handed every thread the same
check_same_thread=False connection, so a commit or rollback from the
watchdog, executor or queue-hook thread could land on another thread's
half-done write (sqlite3 raised 'bad parameter or other API misuse'
under concurrent saves). Connections now live in threading.local, each
with WAL, Row factory, foreign_keys and busy_timeout, and are tracked so
they can be closed. PromptModel.close() / PromptDatabase.close() close
the calling thread's connection so tests and restores can delete the
file on Windows.

* fix: saving an existing prompt concurrently returns its id

Node execution, the queue hook and the image monitor all do
get_prompt_by_hash() then save_prompt(). When two of them race on the
same text the second INSERT failed on UNIQUE(hash) and surfaced an
IntegrityError. save_prompt now catches it, re-fetches by hash and
returns the existing id, so eight threads saving one prompt end with a
single row and every record_prompt_use counted.

* fix: backups include WAL data and restores are integrity-checked

The database runs in WAL mode, so a shutil.copy2 of prompts.db silently
dropped every committed row still in prompts.db-wal; the backup
download and the pre-restore safety copy both did that. Restore also
overwrote the live file with connections open, read the whole upload
into memory before checking the 100 MB cap and never ran
PRAGMA integrity_check.

PromptModel.backup_database now uses the SQLite online backup API,
verify_database_file opens a candidate read-only and checks header,
integrity and the prompts table, and restore_from_file backs up the
live database, closes every thread's connection, copies the file,
removes stale -wal/-shm and re-runs migrations. The admin handlers
stream the upload to a temp file with an early size cutoff, reject
anything verify_database_file refuses with a 400 and keep the
{success: ...} JSON shape. The schema-init connection is now closed
instead of leaking.

* fix: migrations are idempotent and schema init runs once per process

Every PromptDatabase() (nodes, image monitor, API, diagnostics) re-ran
table creation and all migrations. Schema init is now cached per
normalised path (class-level set under a lock) and re-run only when the
file disappeared, after a restore, or after reset_schema_cache().

The copy-and-swap migrations drop a stale *_new table left by an
interrupted run, execute inside an explicit transaction with
PRAGMA foreign_keys = OFF (DROP TABLE prompts used to cascade-delete
every generated_images row), roll back cleanly and log and re-raise
sqlite3.Error instead of swallowing it.

* fix: image uniqueness by full path, escaped LIKE search, no usage stamp on save

generated_images was UNIQUE(prompt_id, filename) on the basename, so
ComfyUI's same-named files in different date folders were silently
ignored by INSERT OR IGNORE, and the lastrowid == 0 branch after it
never ran. The table now carries file_path (normcase/normpath/abspath
of the image path) with UNIQUE(prompt_id, file_path); the migration
collapses rows that normalise to one file and supersedes the old
basename migration, which would otherwise undo it on every start.

search_prompts and the tag search escaped nothing, so 100% matched
'100 percent' and a_b matched aXb; user text now goes through
escape_like with ESCAPE. save_prompt no longer stamps last_used_at, the
usage backfill runs only on the upgrade that adds the columns, and the
Recently Used / Most Used orders put never-run prompts (NULL) after run
ones instead of first.

* fix: escape LIKE wildcards in image prompt search

search_images_by_prompt interpolated the user's text into LIKE
unescaped, so '50%' also matched '50 percent'. It now goes through the
same escape_like helper as search_prompts. Also drops an unused import
and an unused cursor assignment flagged by pyflakes.

* test: cover database operations

Behaviour tests for export (json/csv/errors), duplicate detection and
merging, image metadata storage and lookup, missing-file cleanup,
statistics, bulk operations, orphan pruning, consistency checks,
subfolder listing, tag queries and management edge cases, legacy tag
column parsing, config-driven path resolution, model housekeeping and
the TEXT -> INTEGER prompt_id migration. database/operations.py and
database/models.py are both above 85% statement+branch coverage.

* test: isolate the clock and log buffer from background threads of other tests

* fix: gallery roots are restricted to ComfyUI directories

A gallery root could be set to '/' (or 'C:\') through POST
/prompt_manager/settings because the deny list only matched a few
system prefixes. The root was persisted to config.json and fed into the
image-serving containment check, which then allowed any file on disk to
be read through /prompt_manager/images/serve/.

- GalleryConfig.validate_gallery_root() accepts a directory only when its
  realpath lies inside a ComfyUI directory (folder_paths output/input/
  temp/user) or a directory listed in PROMPT_MANAGER_EXTRA_GALLERY_ROOTS;
  filesystem roots, the home directory, files and missing paths are
  rejected.
- save_settings returns 400 and persists nothing when any root fails.
- _get_all_output_dirs() filters configured roots through the validator
  and never falls back to a rejected root.
- PROMPT_MANAGER_CONFIG_PATH relocates config.json; load/save default
  to it.

* fix: log config accepts only known keys

POST /prompt_manager/logs/config forwarded the whole request body into
PromptManagerLogger.update_config(), so any key could be injected into
the logging configuration. The handler now accepts only LOG_CONFIG_KEYS
(level, max_file_size, backup_count, console_logging, file_logging,
buffer_size), type-checks each value (level name, non-negative int,
bool) and answers 400 listing unknown keys. Malformed or non-object
bodies are 400 instead of 500.

* fix: no absolute server paths in API responses

Settings, diagnostics, duplicate/output scans and the logging endpoints
returned absolute filesystem paths, and several error envelopes echoed
str(e) which for OSError carries the full path.

- _public_path() renders a path relative to the ComfyUI base directory
  (folder_paths.base_path, else the output dir's parent) or to the
  parent of an opted-in extra gallery root, falling back to the
  basename; _public_error() reduces OSError to strerror + basename.
- GET /settings returns gallery roots and monitored directories in that
  form; POST /settings resolves relative roots against the same anchors
  (GalleryConfig.resolve_gallery_root) so the GET output round-trips.
- Diagnostics output_dirs, monitored_directories and the missing-DB
  message no longer carry absolute paths; the image monitor is reached
  through the package-or-top-level import fallback instead of a
  three-dot relative import that failed outside the package.
- Duplicate scan 'path' is now relative to the output root and the
  delete endpoint accepts relative paths.
- /logs/files 'path' and /logs/stats 'log_directory' use the public
  form; unexpected errors answer with a generic message.

* perf: run duplicate and output scans off the event loop

The duplicate scan walked the output tree and hashed every file on the
aiohttp event loop, and the duplicate delete removed files inline; the
output scan ran its per-file database work one executor hop at a time.

- _find_duplicate_images_sync / _describe_media_file do the walk and
  hashing; find_duplicate_images() runs them via _run_in_executor.
- _delete_duplicate_images_sync / _delete_one_duplicate do the removal
  (absolute or output-relative paths) off the loop.
- The output scan is split into _collect_output_media_sync,
  _extract_batch_metadata_sync and _ingest_scan_batch_sync (one
  executor call per batch for all DB work) with a small SSE generator.
- Media collection is shared and de-duplicates by normcase'd path.

* refactor: settings reads the image monitor through its module

get_settings located the ImageMonitor singleton by scanning sys.modules
for any module with 'image_monitor' in its name, a workaround for the
package being importable under two identities. It now uses the same
try-package / except-top-level import as the rest of the API
(_current_image_monitor) and falls back to the configured roots when no
monitor is running.

* fix: clamp the logs limit and reject non-integers

GET /prompt_manager/logs accepted any limit up to 1000 and answered 500
when the value was not an integer. The limit is now clamped to [1, 500]
and a non-integer value is a 400.

* test: cover core API module and gzip middleware

* refactor: split diagnostics, maintenance and HTML serving into helpers

- run_diagnostics composes four module-level probes (_diagnose_database,
  _diagnose_dependencies, _diagnose_output_dirs, _diagnose_image_monitor).
- run_maintenance is table-driven: _maintenance_runners maps each
  operation name to a callable and _run_maintenance_operations reports
  per-operation success or a public error message.
- _read_json answers 400 for malformed or non-object JSON bodies in
  settings, duplicate delete, test-link and maintenance instead of 500.
- test_image_link delegates to _link_test_image.
- The four HTML page routes share _serve_html_page.

* test: cover admin and core API routes

Route tests for stats, cleanup, duplicate scan/delete, settings,
diagnostics, test-link, maintenance (every operation, success and
failure), the output scan internals, and contract-level tests for
backup/restore (status codes and envelope only).

* test: make admin, logging and diagnostics tests independent of test order

The diagnostics message no longer carries the absolute path, the restore test
closes its raw sqlite connections before the live file is replaced, the logs
tests declare their own folder_paths base directory, and the isolated logger
filters every handler to the test thread.

* fix: truncating logs no longer rolls the main log over or pads it with zeros

truncate_logs emptied prompt_manager.log through a second open(), leaving the
rotating handler's stream at its old offset: the next record was written past
the new end (zero padding on POSIX) and tell() still reported the old size, so
the file rolled over and resurrected the backup that had just been deleted.
It also logged once per file from inside the deletion loop, which could do the
same with a small size limit. The main log is now truncated through the
handler's own stream and a single summary line is logged afterwards.

* fix: HTML escaping covers attribute context

escapeHtml now escapes & < > " ' (shared web/js/html-escape.js, loaded by
admin, gallery and metadata pages) instead of the textContent/innerHTML
round-trip that left quotes intact, so a tag, category, checkpoint or file
name containing a quote can no longer break out of a data-*, value or title
attribute.

Inline onclick handlers that interpolated server data now carry data-action
and data-* attributes and are dispatched through one delegated listener per
page (web/js/data-actions.js); the img onerror fallback uses
data-fallback-src with a capturing error listener. Every remaining template
value written into an attribute or text node (prompts, checkpoints, file
paths, log lines, directory names, categories, suggestions) is escaped.

metadata.html no longer builds an "explorer /select" shell command from the
file name (Electron-only branch); the path is copied to the clipboard.

* refactor: remove shadowed and dead frontend code

admin.js defined parsePNGMetadata, extractComfyUIData, showFullPrompt,
showWorkflowData, downloadWorkflowJSON and copyAllMetadata twice; the first
definitions were unreachable because the later ones shadow them.
updateMetadataPanel, showMetadataError, copyPrompt and tryFallbackMetadata
only ever wrote to #metadataContent, an element no page creates, so they are
gone too. gallery.js defined escapeHtml and showNotification twice. The admin
keydown/resize handlers referenced closeImageViewer, previousImage, nextImage
and applyImageSizing, none of which exist, and the imageViewMode state they
used; only the Escape-closes-gallery branch remains.

Debug console.log calls are removed; the ones that were the only statement of
a catch block are console.warn so parse failures stay visible.

* fix: stale responses, double submits and leaked listeners

web/js/api-client.js wraps fetch with one failure path (ApiError for non-2xx,
success:false and non-JSON bodies) and latestOnly(), which aborts the previous
in-flight request and resolves superseded calls to STALE. The admin search and
recent-page loads share one guard, the gallery page load has its own, so fast
typing or clicking can no longer paint an older result over a newer one.

In-place prompt editing used a blur listener with {once:true} plus a keydown
listener added on every edit, which sent the PUT twice and stacked handlers.
The list now has one keydown/focusout pair bound at construction and
finishEdit() clears the edit state before touching the element, so a save is
sent exactly once. Mutations re-fetch the page currently shown (refreshList)
instead of calling search(), and delete, bulk delete, edit, restore and scan no
longer reload the whole page.

Pagination state is committed only after a successful fetch via the pure
ListState.nextPageState(current, response) in both admin and gallery, so a
failed or aborted page request leaves the current page intact. The gallery
lightbox removed its keydown handlers on a non-existent 'remove' event; a
MutationObserver now drops them when the modal leaves the document.

web/js/sse-stream.js reads server-sent events over fetch so POST-only progress
endpoints can stream.

* fix: follow the POST-only autotag streams and the new LoRA and list contracts

autotag/download and autotag/start are POST-only now; EventSource can only
GET, so both pages stream their progress through SseStream.connect(url,
{method: 'POST'}), which keeps the onmessage/onerror/close surface the
handlers already use. autotag/single is called with image_id and the re-tag
dialog shows the server's filename instead of splitting an absolute path.

lora/status reports has_civitai_api_key instead of the key: the field shows a
'key is stored' placeholder, an empty field keeps the key on save and a
'Clear the stored key on save' checkbox sends clear_civitai_api_key.

Thumbnail generation reports per-file failures as file_error events; both
progress views count them (and show error_count from progress) without
closing the stream. The auto-tag review follows pagination.has_more on the
capped /images/all endpoint, and /search is paged like /recent so paging in
search mode no longer switches the list back to recent prompts.

* refactor: one PNG metadata parser

admin.js, gallery.js and metadata.html each carried a copy of the PNG chunk
walker that reads ComfyUI's tEXt/iTXt entries (admin.js had two until the
shadowed one was removed). web/js/png-metadata.js is now the only parser:
it checks the signature, bounds-checks every chunk so a truncated file or a
bogus length cannot throw, decodes tEXt as Latin-1 and iTXt as UTF-8, skips
compressed zTXt/iTXt payloads instead of decoding them as garbage, and stops
at IEND. The three page-level parsePNGMetadata functions delegate to it.

* fix: tags deep link renders and admin modals close

admin.html loaded tags-page.js after admin.js, so opening the page at #/tags
found no TagsPageManager and rendered nothing; the script order is now
theme-bridge, html-escape, api-client, png-metadata, (list-state, sse-stream,
data-actions), viewer, comfy-metadata, prompt-list-sort, view-router,
tags-page, admin. Route resolution lives in the pure ViewRouter.resolveView().

The auto-tag modal's close buttons called a bare hideModal(), which only
gallery.js defines; they call window.admin.hideModal(). PromptAdmin is
constructed from startAdmin() on DOMContentLoaded (or immediately when the
document is already parsed) instead of at script evaluation time.

* test: cover frontend modules

browser-globals.test.js loads every page module through vm with a fake
window, the way the pages do, and asserts the global each one exposes
(HtmlEscape/escapeHtml, DataActions, ApiClient, ListState, SseStream,
PngMetadata, ViewRouter, PromptListSort, ComfyMetadata). sse-stream gains
cases for a POST body, missing handlers and close() before or during
delivery. Every pure module is above 85% line coverage under
node --test --experimental-test-coverage.

* fix: page image listings in SQL and drop three unused helpers

get_recent_images and search_images_by_prompt take limit/offset so the recent
and search image routes no longer over-fetch a 5000-row window and slice it.
generate_content_hash, PromptModel.migrate_database and find_duplicates had no
callers outside their own tests. The isolated logger tests re-apply their
thread filter after update_config rebuilds the handlers. Long lines in the
touched database and image modules are wrapped so the full flake8 hook passes.

* fix: restore through SQLite's backup API, close_all for shared files, safer output scans

restore_from_file copied the uploaded file over the live database and deleted
its WAL while other PromptModel instances (nodes, image monitor, queue hook)
still held connections, which corrupts on POSIX and fails on Windows. It now
copies pages through the online backup API into the live connection so every
open handle sees the new content. PromptDatabase.close_all() closes the
connections of every thread, which tests need before deleting a database.
Output scans skip symlinked files, filesystem entries get a stable digest id
instead of a per-process hash(), and thumbnail streaming stops when the
client disconnects.

* style: wrap long lines in node modules

Split tooltip, description and docstring strings that exceed 88 columns
using implicit concatenation; string values are unchanged. Drop the unused
typing imports from prompt_search_list and mark the deliberately late node
imports in __init__ with noqa: E402.

* style: wrap long lines in the API layer, autotag and config

Split long f-string log messages, response messages, URL builders and
docstrings; DEFAULT_PROMPT and SYSTEM_PROMPT are rewritten as implicit
concatenations with identical values. Drop the unused shutil import in
admin and the unused HFCacheInfo import in autotag; keep the
hf_hub_download/snapshot_download availability probe with noqa: F401 and
mark the deliberately late imports in config with noqa: E402.

* style: wrap long lines and drop unused imports in utils

Re-wrap module docstrings, comments and f-string log messages; remove the
unused typing.List and PngInfo imports; mark the PIL availability probe in
diagnostics with noqa: F401; turn two placeholder-less f-strings into plain
strings; strip trailing whitespace inside the diagnostics SQL.

* style: wrap long lines in tests

Split long docstrings and SQL literals (values unchanged) and drop the
unused os import from test_path_security.

* chore: flake8 ignores the gitignored standalone tagger script

* fix: escape image dimensions and workflow JSON before rendering

openGalleryModal interpolated width, height and file_size (stored from a
POST body, so possibly strings) straight into data-caption and the card
text. showWorkflowData wrote JSON.stringify(workflow) raw into a blank
same-origin window, so a workflow containing </pre><script> ran. Both now
go through ImageHelpers (new pure module, node:test covered); a string
file_size no longer reaches formatFileSize's toFixed either, and a blocked
popup shows a notice instead of throwing.

* fix: escape remaining server values rendered through innerHTML

Maintenance and diagnostics results, review-tag errors, video info and
metadata error messages, consistency issues, recent image filenames,
output directory names, the prompt rating attribute, gallery media types
and duplicate group headers were interpolated unescaped. The copy
fallback modal also inlined the prompt text inside <textarea>, which a
closing tag could break out of; it now sets textarea.value.

* fix: address gallery auto-tag review images by path, not digest id

The gallery review flow builds its list from /prompt_manager/images/output,
whose id is a 16-hex path digest, but posted it as image_id to
autotag/single, so the server looked up a database row and every request
404ed. autotagSingleBody (pure, tested) now sends path for output-folder
entries and keeps image_id for database rows, which the admin review flow
(/images/all) still uses.

* fix: read thumbnail generation progress over POST with SseStream

images/generate-thumbnails/progress is becoming POST-only, which the
browser EventSource cannot request. Both thumbnail progress consumers in
the gallery now use SseStream.connect(url, {method: 'POST'}) exactly as
the autotag streams do. SseStream gains addEventListener/removeEventListener
for named event: frames (tested) so the status, start, progress,
file_error, complete and error listeners stay as they were, and an onclose
guard rejects when the stream ends without a terminal frame.

* fix: drop the unused copy-text data-action handler

Nothing in the admin markup emits data-action="copy-text"; the prompt
copy buttons call copyToClipboard directly.

* fix(lora): confine LoraManager paths to custom_nodes and hide server paths

- is_civitai_url also refuses URLs with userinfo or a non-443 port, so
  https://evil.com\@civitai.com/x and https://civitai.com:8443/x are rejected.
- resolve_lora_manager_path / custom_nodes_directories: lora/enable and
  lora/scan only accept a LoraManager directory that is a direct child of
  ComfyUI's custom_nodes (folder_paths base_path or registered custom_nodes
  folders, else this package's parent) and whose name contains "lora",
  compared via realpath+normcase; relative public paths resolve against the
  API path anchors. Anything else is 400, so find_lora_directories can no
  longer widen the image allow-list with arbitrary trees.
- lora/enable persists through PromptManagerConfig.save_to_file(), which
  honours PROMPT_MANAGER_CONFIG_PATH, instead of a hard-coded config.json.
- detect/status/enable return path and detected_path through _public_path;
  every lora handler reports exceptions via _public_error, and lora/scan
  errors raised after the SSE stream opened become an error event.
- Tests close the database before unlinking it.

* fix(autotag): contain batch rows, require media extensions, bound the scan

- The batch tagger runs every generated_images row through
  _resolve_allowed_image_path; rows that exist but lie outside the served
  output directories are skipped and counted in skipped_outside_roots on
  progress and complete events, and the engine receives the resolved path.
- _resolve_allowed_image_path requires a media extension (.png .jpg .jpeg
  .webp .gif .bmp .tiff .tif), so autotag/single returns 403 for other files.
- autotag/start holds a module-level lock; a concurrent start gets 409
  {"success": false, "error": "Auto-tagging is already running"}.
- scan_output_dir walks with a 50,000-file and 12-level cap, skips symlinked
  files and thumbnails, returns path through _public_path, relative_path in
  POSIX form, and a truncated flag.
- Autotag handlers report exceptions via the shared _public_error; the
  local copy is removed. Tests close the database before unlinking it.

* test: close every database before unlinking and anchor public paths in image tests

Each owned test that opens a PromptDatabase now calls close_all() before
removing the file, and temp directories holding a database use
ignore_cleanup_errors, so the suite tears down cleanly on Windows. The
image API fixture lays out a fake ComfyUI tree (<tmp>/output) and pins
GalleryConfig.path_anchors to it so responses can be checked for
root-relative paths.

* fix: validate image file_info before it reaches the generated_images columns

link_image_to_prompt stored size, dimensions and format straight from the
request body, so a string such as '"><img src=x>' ended up in INTEGER
columns and was echoed back to the gallery. Size and dimensions are now
coerced with int() and bounded (size 0..10^12, width/height 0..65535),
format is matched against a whitelist of media formats, and anything
else is stored as NULL while the image still links. Non-dict metadata or
file_info no longer aborts the link.

* fix: clamp paging on tag listings and the page-derived recent offset

tags/stats, tags/{tag}/prompts and tags/filter parsed limit/offset with a
bare int(), so limit=999999 was passed straight to SQLite and a huge
offset raised OverflowError (500). They now share parse_page_params
(limit 1..500, offset 0..MAX_PAGE_OFFSET, 400 on non-integers), and
/recent clamps the offset it derives from ?page= the same way.

* fix: make the thumbnail progress stream POST-only

GET /prompt_manager/images/generate-thumbnails/progress wrote thumbnails
to disk, so any prefetch, link preview or <img src> could start a full
output-directory scan. The route now accepts POST only (GET answers
405); quality still comes from the query string so the SSE client can
keep the same URL shape.

* fix: describe images relative to the ComfyUI tree in API responses

Gallery and prompt responses carried absolute server paths: /images/output
entries (path, root_dir), thumbnails_path, the images/prompt lookup, every
image list (image_path plus its normalised file_path duplicate) and the
preview images attached to prompt listings. They now go through
_public_path: /images/output 'path' is relative to its root (what the
delete and autotag endpoints resolve against), 'root_dir' and every
other image_path are relative to the ComfyUI base, and file_path is
dropped. recent/all/search image lists also gain url/thumbnail_url via
the same enrichment the prompt-images route already had. Remaining
str(e) error bodies in both modules use the basename-only error text.

* fix: check thumbnail directory containment with a real path test

clear_thumbnails compared resolved paths with str.startswith, so a
thumbnails directory that resolved to a sibling with a shared prefix
(out vs out2) passed the safety check and was emptied. It now uses the
module's _is_within (realpath + normcase + Path.is_relative_to).

* fix: decode tag names from the URL only once

The tag lookup, rename and delete routes ran urllib.parse.unquote on
match_info values that aiohttp had already percent-decoded, so a tag
literally named '%41' (requested as %2541) was handled as 'A'. The
second decode is dropped.

* fix: validate tags on every tag mutation route

add_prompt_tag, add_tags_to_prompt, bulk_add_tags, merge_tags and
rename_tag accepted whatever the body carried; a non-string tag raised
AttributeError (500) and over-long or control-character tags were
stored. They now run utils.validators.validate_tags through a shared
tags_error helper and answer 400 with the validator's message. Blank
entries in add_tags_to_prompt are still skipped rather than rejected.

* fix: export every prompt by paging instead of truncating at 10000

/prompt_manager/export fetched a single page of 10000 prompts and
silently dropped the rest. It now walks the table in EXPORT_PAGE_SIZE
pages (1000) inside one executor job until a short page is returned.

* fix: gate the LoraManager serving roots and accept relative link paths

A LoraManager path hand-edited into config.json went straight into
find_lora_directories and so into the image-serving allow-list. When a
path is configured it must now pass lora_utils.resolve_lora_manager_path
(a custom_nodes child whose name contains 'lora'); otherwise, or when the
resolver is unavailable, no LoRA directories are served. An empty path
keeps the custom_nodes-only auto-detection.

POST /images/link now also accepts the relative forms the API hands out
(root-relative 'sub/a.png' and ComfyUI-relative 'output/sub/a.png'),
resolving them against the allowed roots and anchors; '..' segments in
a relative path are refused and unknown files are 404.

* fix: review flow uses the served image url and the lora-dirs test pins its config

Image paths in API responses are now relative to the ComfyUI tree, so the
auto-tag review page can no longer derive a serve URL from image_path; the
server always provides url. The lora-dirs serving test assumed an empty
LoraManager path, which only held in a checkout without a local config.json.

* test: stop leaking a mocked utils.comfyui_integration and mock the IS_CHANGED clock

test_image_monitor_singleton replaced utils.comfyui_integration in
sys.modules with a MagicMock and never restored it, so running it before
test_comfyui_integration failed eight tests. The mock was unused; drop it
and install the server stub with setdefault in every owned test module.

The PromptSearchList IS_CHANGED test slept 10 ms between two time.time()
reads; the Windows clock ticks every 15.6 ms, so it was flaky there. Mock
the clock with an incrementing counter instead.

* fix: resolve_text follows a node shared by two branches

The cycle guard used one visited set for the whole walk, so a string node
read twice (a diamond, or StringConcatenate with the same source on both
inputs) made the second branch None and the prompt text unknowable. Cycle
detection is now scoped to the current path; a separate visit counter
keeps MAX_VISITS as the bound on total work.

* fix: bound resolve_text on diamond chains with memoisation, depth and length caps

A per-path visited set alone makes a chain of diamonds (node i reads node
i+1 on both inputs) exponential in depth. Each node's text is now
memoised so it is expanded once, every expansion is charged to the shared
MAX_VISITS budget, chains deeper than MAX_DEPTH (64) are unknowable so
recursion stays bounded, and text longer than MAX_TEXT_LENGTH (100k) is
unknowable so StringConcatenate doubling cannot exhaust memory.

* fix: static routes resolve under web/ with an extension allow-list

/prompt_manager/lib/{filepath} and /js/{filepath} only rejected '..' and a
leading '/'. On Windows os.path.join(root, 'C:/x') or a leading backslash
re-anchors the path, so any file on the host could be read. Requests are
now refused when the path is absolute on any platform (slash, backslash,
drive letter, UNC prefix), carries an empty, '.' or '..' segment, has an
extension outside the allow-list, or resolves (symlinks included) outside
the static directory; the resolved real path is served with its MIME type.

* feat: capped JSON body reader and single-flight lock for long admin jobs

Add _read_json_body(request, max_bytes=1_000_000) to py/api: reads the
body in chunks, answers 413 past the cap (declared or streamed) and 400
for invalid or non-object JSON. Every JSON POST in the admin and logging
handlers goes through it; it is exposed as PromptManagerAPI._read_json_body
for the other route mixins.

Add _acquire_long_job(name) / _long_job_busy_response(label) on the API
object: one asyncio.Lock per job family, handed out as an async context
manager or None when busy, so a handler answers 409 instead of starting a
second scan. The duplicate scan and delete share the 'duplicates' family.

* fix: harden database restore and keep server paths out of admin responses

Restore: verify_database_file and restore_from_file run through
_run_in_executor; the upload is also opened read-only with
PRAGMA trusted_schema=OFF and refused when sqlite_master lists a trigger
or a view (a prompts database has neither, a crafted one could use them
to run SQL against the live data); at most MAX_SAFETY_BACKUPS (10)
'<db>.backup_*' files are kept next to the live database, oldest first.

Responses: backup_created and the maintenance statistics database_path
go through _public_path, and backup/restore failure messages through
_public_error, so no absolute server path appears in an admin body.

* fix: validate hand-edited gallery roots everywhere and bound output scans

A root written straight into config.json skipped the containment check
the settings endpoint applies. GalleryConfig.update_config (and so
load_from_file) now drops entries that fail validate_gallery_root with a
warning, _find_comfyui_output_dir validates the configured directory
before using it, and ImageMonitor.start_monitoring validates every
directory before scheduling the observer.

The duplicate cleaner only deletes files with a media extension and
never anything under a thumbnails directory. _collect_media_files walks
with os.walk(followlinks=False), skips symlinked files and directories,
prunes thumbnails, stops MAX_SCAN_DEPTH (12) levels down and after
MAX_SCAN_FILES (50000) files.

* fix: bound log file size and backup count in the logging config endpoint

max_file_size must be between 1 MB and 1024 MB (bytes) and backup_count
between 0 and 50; anything else is a 400. Also close every database
connection in the logging test teardown.

* fix: private config.json, merged settings save, synthetic test-link path

PromptManagerConfig.save_to_file and the settings endpoint write
config.json through write_private_json: created with mode 0600 (chmod on
POSIX, best effort on Windows) because the file can hold an API key.
_persist_settings merges web_ui and gallery.monitoring.directories into
the existing file instead of overwriting it, so the integrations section
and any other key survive a settings save.

The test-link diagnostic no longer stores a client-supplied path; it
records a marker file name under the output directory.

* fix: image worker thread exits on shutdown and restarts cleanly

ImageGenerationHandler.shutdown() clears the pending set, sets a stop
event the worker's delay and settle waits listen to, and puts a None
sentinel on the queue; the worker exits within the current wait. Each
worker owns its queue and stop event so a draining worker never swallows
an image scheduled after shutdown. ImageMonitor.stop_monitoring calls it.

* test: close all database connections before deleting temp files

Windows refuses to unlink an open SQLite file and executor threads keep
their own connections, so every owned test that creates a PromptDatabase
calls close_all() before cleanup; the diagnostics temp dir ignores
cleanup errors.

* fix: output scan runs as a background job that survives the browser going away

Closing the dashboard tab or clicking outside the scan modal raised
ClientConnectionResetError out of scan_images and abandoned the scan
mid-batch, leaving a half-scanned database. Two overlapping scans could
also run at once.

- py/api/scan_job.py: ScanJob fans progress out to any number of SSE
  subscribers (latest snapshot first, then live events)
- POST /prompt_manager/scan starts the scan as an asyncio task, or attaches
  to the one already running; a write to a dead socket ends that stream
  quietly while the scan continues
- GET /prompt_manager/scan/status reports the running flag and last event
- dashboard: dismissing the modal while a scan runs minimizes it to a
  progress pill, the page re-attaches to a running scan after a reload,
  a lost stream reconnects, and leaving the page mid-scan asks first

* feat: worker-thread setting speeds up the output scan and thumbnail generation

Both jobs processed one file at a time on a single core. Pillow decodes the
whole image to reach trailing PNG text chunks and releases the GIL while it
does, so a bounded thread pool scales almost linearly: on a 1 GB sample of
400 PNGs the metadata pass dropped from 18 s to 2.5 s at 8 threads, and 120
thumbnails from 4.6 s to 0.6 s.

- utils/parallel.py: map_parallel(func, items, workers) keeps order, runs on
  the caller's thread when workers is 1, and caps the pool at the item count
- PromptManagerConfig.WORKER_THREADS defaults to half the detected cores,
  is clamped to max_worker_threads() when loaded from config.json, and is
  persisted under "performance"
- settings API returns worker_threads and cpu_count and validates saves;
  the settings modal gets a "Worker threads" slider capped at the core count
- the scan reads each metadata batch on the configured threads; thumbnail
  generation processes batches of files per executor job, still emitting
  one progress event per file

Threads rather than processes: this runs inside ComfyUI's server, where
forking a CUDA-initialised process is unsafe and the spawn start method
would re-import ComfyUI's main module in every child.

* fix: scan modal ignores backdrop clicks so a focus click cannot dismiss it

Only its own buttons dismiss it: Cancel before a scan starts, Hide (minimize
to the progress pill) while one runs.

* feat: scan reads prompts embedded in videos; unreadable files no longer log errors

The output scan handed every .mp4 to Pillow and logged one ERROR per file.
ComfyUI's video savers store the queued graph as JSON in the container's
comment tag, so those files carry prompts worth linking.

- utils/video_metadata.py reads the comment/description tag through
  ffprobe with a timeout, normalised to the PNG text-chunk shape so the
  existing parser handles both; without ffprobe videos are skipped and a
  single INFO notice is logged
- _extract_comfyui_metadata routes videos there and logs unreadable images
  as warnings, since the scan carries on past them

Test hygiene: every aiohttp test base class overrode tearDownAsync without
calling super(), so no test client was ever closed. That produced thousands
of ResourceWarnings, "Unclosed client session" errors from asyncio that
broke log assertions, and doubled the suite's run time.

* fix: thumbnail runs skip fresh thumbnails before the cap and report what remains

The per-run cap of 10,000 applied to the directory walk itself, so a
library with more files than that never had thumbnails generated for the
rest, and a run that found 9,987 existing thumbnails spent its whole
budget on skipping them.

- _plan_thumbnails walks the whole tree (bounded like the gallery listing),
  counts files whose thumbnail is already newer as skipped without queuing
  them, and caps only the pending files; the leftover count is returned
- start and complete payloads carry skipped and remaining; total_images
  counts every file considered and the processing rate uses the files
  actually worked on
- the gallery page shows a warning naming the pending count instead of a
  success toast when a run stopped at the cap

* feat(gallery): warn with the pending count when a thumbnail run stops at the cap

* fix: thumbnails are written atomically and served under mtime-versioned urls

Cards showed "Failed to load" for thumbnails that were valid on disk. The
browser had fetched them while the thumbnail run was still writing the
file and, because image responses are cached for an hour, kept the broken
bytes. The card also had no fallback to the original.

- image and video thumbnails are written to a hidden temp sibling and
  renamed into place, so a partial file is never visible at the final path
- every thumbnail_url (gallery listing, prompt images, duplicates, autotag
  listing) carries ?v=<mtime>, so a regenerated thumbnail gets a new URL
  instead of the cached old one
- a 404 from the image serve route is marked no-store; ComfyUI's cache
  middleware would otherwise let browsers cache the miss for an hour
- image-helpers gains fallbackImageSource(): the original is tried once
  before a card gives up

Test hygiene: the autotag tests now stub ComfyUI's server module like the
other API test modules, so the module passes when run on its own.

* fix(gallery): display settings apply at load, columns work, media info toggles

Default View Mode, Items per Page and Grid Columns were only applied when
Save was pressed, never when the page loaded; the column setting swapped a
Tailwind class that was never compiled (only 3, 6 and 8 exist) and a wider
breakpoint class overrode it anyway; Show Media Info was saved and never
read.

- gallery-settings.js validates what localStorage holds (view mode, limit
  option, column range, booleans) and falls back to defaults per value
- settings are applied in the constructor before the first load; changing
  the page size reloads, toggling media info re-renders
- the column count sets an explicit grid template through a CSS variable
  that wins on desktop widths
- media info lines honour the toggle in grid and list views
- image load failures are handled by one delegated listener that tries the
  original before showing the failed-to-load card

* feat(gallery): duplicate scan runs as a background job with progress; infinite scrolling

The duplicate scan was one blocking request with a spinner: no progress,
a modal that could not be put away, and a result lost if the tab closed.

- POST /prompt_manager/scan_duplicates/stream starts the scan as a
  background job (same ScanJob fan-out as the output scan) or attaches to
  the running one; GET /prompt_manager/scan_duplicates/status reports it.
  The job holds the duplicates lock, so deletes are refused while it runs.
- hashing runs on the configured worker threads and reports progress
  about every percent
- the gallery modal shows a progress bar, closing it minimizes to a pill
  that reopens it, results wait in the pill if the modal was closed, and a
  reload re-attaches to a running scan

Infinite scrolling is a new Display Setting (off by default): the next page
is appended when the viewport nears the bottom, pagination controls hide,
and the load-more rule lives in list-state.js where it is unit tested.
Thumb-size buttons and the column setting no longer override each other.

* fix(gallery): Rescan Output Folder drops the server listing cache and rewalks

The button only re-requested the listing, which the server answers from a
30-second in-memory cache, so a rescan showed exactly what was already on
screen.

- POST /prompt_manager/gallery/rescan invalidates the gallery cache,
  rewalks every output root and returns the file and root counts
- the button calls it, closes the settings modal, reloads the folder tree
  and the first page, and reports what it found

* feat: tag search box autocompletes, narrowed by the tags already typed

- PromptDatabase.suggest_tags(prefix, with_tags, limit): tags starting
  with the prefix, counted over the prompts that carry every context tag,
  context tags excluded, ordered by count then name; LIKE wildcards in the
  prefix are escaped
- GET /prompt_manager/tags/suggest?q=&with=&limit= exposes it (limit
  clamped to 1..50, context capped at 10 tags, registered before the
  {tag_name} routes)
- tag-autocomplete.js splits the comma-separated input into committed tags
  and the one being typed, builds the query, applies a pick and handles
  wrap-around keyboard selection
- the dashboard Tags box shows suggestions with counts on focus and while
  typing, arrow keys and Enter/Tab pick, Escape closes, and each pick
  immediately offers the next tag narrowed by the previous ones

* feat(dashboard): infinite scrolling for the prompt list, saved default plus session toggle

- PromptManagerConfig.INFINITE_SCROLL (web_ui.infinite_scroll) is reported
  and saved through the settings API; only booleans are accepted
- Settings modal gains an "Infinite Scrolling" default; saving applies it
  at once
- the results header gains an "Infinite scroll" checkbox that overrides the
  default for the current session without saving
- when on, the next page is appended as the list nears its bottom (same
  load-more rule as the gallery), page controls hide, a short page keeps
  loading until the list fills, and a refresh after an edit restarts from
  the top

* fix: first CI run on Windows and macOS; tests now enforce closed database files

The first push of the branch ran the Tests matrix on all three operating
systems. Every Python job failed; the causes were:

- numpy missing in CI: the WD14 engine tests import it (requirements-dev)
- macOS: temp directories resolve through /private; the admin API tests
  now use the real path like the gallery roots they compare against
- Windows: the startup banner printed an emoji, which raises
  UnicodeEncodeError on a legacy-code-page console and aborted loading
  the whole node; the banner is ASCII now and a test loads the package
  under a cp1252 stdout
- Windows: gallery subfolders came back with backslashes, which neither
  the folder tree nor the search filter understand; they are "/" joined
  on every platform. A relative DEFAULT_DB_PATH kept its forward slashes
  after being joined to the extension root; it is normalised now
- Windows: sqlite connections opened with `with sqlite3.connect()` are
  not closed by the context manager and linger until garbage collection,
  which holds a file lock there. Production sites (vacuum, diagnostics,
  restore inspection) close explicitly; a restore that swaps the API's
  database object closes the previous one; PromptModel keeps a registry
  so a shutdown or a test can close every instance
- Windows: served files were still open when their temp directory was
  removed (tests now read the body), a log download test compared text
  mode newlines, two tests compared paths with a hard-coded separator

tests/open_handles.py makes the Linux run fail the way Windows would:
every database test base checks, right before deleting its files, that
nothing in the process still holds them open.

* fix: Windows case-folded gallery roots in tests, "/" subfolders everywhere, longer busy timeout

Second CI run on Windows:
- gallery roots are stored resolved and case-folded there; the admin API
  tests now compare against that form instead of the mixed-case temp path
- the subfolder fallback for images outside every root also uses "/", and
  the test expects it
- a relative diagnostics db path is normalised after joining
- "database is locked" under many writers on a slow disk: connections now
  wait up to 30 seconds (connect timeout and busy_timeout), long enough for
  a scan or thumbnail run while ComfyUI saves images

* fix: serve every gallery video format, validate settings before applying, dedupe output dirs

Review findings on #153 (Codex):
- the image serve route allowed only three video types while the gallery
  lists seven, so .avi/.mkv/.m4v/.wmv cards opened to a 403; the serve
  allow-list now covers every gallery video format
- a settings payload with one invalid field still applied the valid ones
  before returning 400; the whole payload is validated first and nothing
  changes on failure. webui_display_mode is validated too

Third Windows CI run:
- the same output directory spelled twice (different case or a link) was
  listed twice in diagnostics and served roots; both dedupe on the
  resolved, case-folded path
- the root-less subfolder test expects the "/" form

Code-quality bot: index definitions are (name, target) pairs instead of
implicitly concatenated strings; redundant local asyncio imports and
two trivial lambdas in test stubs removed.

* docs: version 3.3.0, README changelog and refreshed sections

Bump to 3.3.0: new features (worker threads, background scans, tag
autocomplete, infinite scrolling, video prompt scanning) and API contract
changes (POST-only side-effect routes, relative image paths, paginated
search) go beyond a patch release.

README: changelog entries for 3.2.4 and 3.3.0, refreshed bulk scanning,
gallery settings, search, configuration and testing sections, and the
roadmap's recently-completed list.
2026-10-08 18:53:11 -07:00

3682 lines
193 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
class GalleryManager {
constructor() {
this.images = [];
this.currentPage = 1;
this.limit = 100;
this.total = 0;
this.viewMode = 'grid'; // 'grid' or 'list'
this.viewer = null;
this.currentSubfolder = '';
this.expandedFolders = new Set();
this.thumbSize = 'md'; // sm, md, lg
this.infiniteScroll = false;
this.loadingMore = false;
// Duplicate scan: a server-side job the modal follows (see scanDuplicates)
this.dupScan = ScanProgress.initialState();
this.dupSource = null;
this.dupResults = null;
this.api = ApiClient.createApiClient();
// Only the newest page request may update the grid
this.imagesRequest = ApiClient.latestOnly();
// Elements rendered with data-action="..." are dispatched here instead of through
// inline onclick handlers, so file names and URLs never land inside JavaScript source.
this.actionHandlers = {
"open-url": ({ url }) => window.open(url, '_blank'),
"copy-url": ({ url, label }, el) => this.copyUrlWithFeedback(el, url, label),
"select-suggestion": ({ tag }) => this.selectSuggestion(tag),
"remove-tag": ({ index }) => this.removeTag(index),
"remove-review-tag": ({ index }) => this.removeReviewTag(index),
};
this.initializeEventListeners();
this.applySettings(this.getSettings());
this.loadFolderTree();
this.loadImages();
this.resumeDuplicateScan();
// Check thumbnails at startup if enabled
this.checkThumbnailsAtStartup();
}
handleActionClick(e) {
const target = e.target.closest('[data-action]');
if (target) DataActions.dispatch(target.dataset, this.actionHandlers, target);
}
copyUrlWithFeedback(el, url, label) {
navigator.clipboard.writeText(url).then(() => {
el.textContent = '✓ Copied!';
setTimeout(() => { el.textContent = label; }, 1000);
});
}
handleMediaError(img) {
if (!img || img.tagName !== 'IMG' || !img.dataset || img.dataset.original === undefined) return;
const next = ImageHelpers.fallbackImageSource(img.getAttribute('src'), img.dataset);
if (next) {
// The thumbnail failed (stale cache, half-written file): show the original instead
img.dataset.fellBack = '1';
img.src = next;
return;
}
img.style.display = 'none';
const fallback = img.nextElementSibling;
if (fallback && fallback.dataset && fallback.dataset.mediaFallback !== undefined) {
fallback.style.display = 'flex';
}
}
initializeEventListeners() {
document.addEventListener('click', (e) => this.handleActionClick(e));
// Image load failures do not bubble; capture them once for every card
document.addEventListener('error', (e) => this.handleMediaError(e.target), true);
document.getElementById('refreshBtn').addEventListener('click', () => this.loadImages());
document.getElementById('limitSelector').addEventListener('change', (e) => this.changeLimit(parseInt(e.target.value)));
document.getElementById('gridViewBtn').addEventListener('click', () => this.setViewMode('grid'));
document.getElementById('listViewBtn').addEventListener('click', () => this.setViewMode('list'));
document.getElementById('settingsBtn').addEventListener('click', () => this.showSettings());
document.getElementById('autoTagBtn').addEventListener('click', () => this.showAutoTagModal());
document.getElementById('addPromptBtn').addEventListener('click', () => this.showAddPromptModal());
document.getElementById('retryBtn').addEventListener('click', () => this.loadImages());
document.getElementById('prevPageBtn').addEventListener('click', () => this.previousPage());
document.getElementById('nextPageBtn').addEventListener('click', () => this.nextPage());
document.getElementById('pageInput').addEventListener('change', (e) => this.goToPage(parseInt(e.target.value)));
document.getElementById('pageInput').addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
this.goToPage(parseInt(e.target.value));
}
});
// Settings modal event listeners
document.getElementById('closeSettingsBtn').addEventListener('click', () => this.hideSettings());
document.getElementById('saveSettingsBtn').addEventListener('click', () => this.saveSettings());
document.getElementById('resetSettingsBtn').addEventListener('click', () => this.resetSettings());
document.getElementById('generateThumbnailsBtn').addEventListener('click', () => this.generateThumbnails());
document.getElementById('clearCacheBtn').addEventListener('click', () => this.clearCache());
document.getElementById('rescanFolderBtn').addEventListener('click', () => this.rescanFolder());
document.getElementById('scanDuplicatesBtn').addEventListener('click', () => this.scanDuplicates());
document.getElementById('dupScanPill').addEventListener('click', () => this.openDuplicatesModal());
document.getElementById('galleryScroll').addEventListener('scroll', () => this.maybeLoadMore(), { passive: true });
// Duplicates modal event listeners
document.getElementById('closeDuplicatesBtn').addEventListener('click', () => this.hideDuplicatesModal());
document.getElementById('selectAllBtn').addEventListener('click', () => this.selectAllDuplicates());
document.getElementById('removeDuplicatesBtn').addEventListener('click', () => this.removeDuplicates());
// Close modal on outside click
document.getElementById('settingsModal').addEventListener('click', (e) => {
if (e.target.id === 'settingsModal') {
this.hideSettings();
}
});
document.getElementById('duplicatesModal').addEventListener('click', (e) => {
if (e.target.id === 'duplicatesModal') {
this.hideDuplicatesModal();
}
});
// Auto Tag modal event listeners
document.getElementById('startAutoTagBtn').addEventListener('click', () => this.startAutoTag());
document.getElementById('startReviewBtn').addEventListener('click', () => this.startReview());
document.getElementById('skipReviewBtn').addEventListener('click', () => this.skipReviewImage());
document.getElementById('applyReviewBtn').addEventListener('click', () => this.applyReviewTags());
document.getElementById('cancelAutoTagBtn').addEventListener('click', () => this.cancelAutoTag());
document.getElementById('cancelDownloadBtn').addEventListener('click', () => this.cancelDownload());
document.getElementById('unloadModelBtn').addEventListener('click', () => this.unloadModel());
// WD14 model selection toggle (prompt vs thresholds)
document.querySelectorAll('input[name="autoTagModel"]').forEach(radio => {
radio.addEventListener('change', (e) => {
const isWd14 = e.target.value.startsWith('wd14');
document.getElementById('promptSection').classList.toggle('hidden', isWd14);
document.getElementById('wd14ThresholdSection').classList.toggle('hidden', !isWd14);
});
});
// WD14 threshold slider value display
document.getElementById('wd14GeneralThreshold').addEventListener('input', (e) => {
document.getElementById('wd14GeneralThresholdValue').textContent = parseFloat(e.target.value).toFixed(2);
});
document.getElementById('wd14CharacterThreshold').addEventListener('input', (e) => {
document.getElementById('wd14CharacterThresholdValue').textContent = parseFloat(e.target.value).toFixed(2);
});
// Add Prompt modal event listeners
document.getElementById('saveNewPromptBtn').addEventListener('click', () => this.saveNewPrompt());
document.getElementById('addPromptTagInput').addEventListener('input', (e) => this.handleTagInput(e));
document.getElementById('addPromptTagInput').addEventListener('keydown', (e) => this.handleTagKeydown(e));
document.getElementById('clearRatingBtn').addEventListener('click', () => this.clearRating());
// Rating stars click handlers
document.querySelectorAll('#addPromptRating .rating-star').forEach(star => {
star.addEventListener('click', (e) => this.setRating(parseInt(e.target.dataset.rating)));
});
// Close AutoTag modals on outside click
['autoTagModal', 'autoTagReviewModal', 'autoTagProgressModal', 'autoTagDownloadModal', 'addPromptModal'].forEach(modalId => {
document.getElementById(modalId).addEventListener('click', (e) => {
if (e.target.id === modalId) {
this.hideModal(modalId);
}
});
});
// Thumbnail size buttons
['Sm', 'Md', 'Lg'].forEach(size => {
const btn = document.getElementById('thumb' + size + 'Btn');
if (btn) btn.addEventListener('click', () => this.setThumbSize(size.toLowerCase()));
});
// Folder tree events
document.getElementById('clearFolderFilterBtn').addEventListener('click', () => {
this.currentSubfolder = '';
this.renderFolderTree();
this.loadImages(1);
});
// Resize handle for folder panel
const resizeHandle = document.getElementById('folderResizeHandle');
if (resizeHandle) {
let isResizing = false;
resizeHandle.addEventListener('mousedown', (e) => {
isResizing = true;
document.body.style.cursor = 'col-resize';
document.body.style.userSelect = 'none';
e.preventDefault();
});
document.addEventListener('mousemove', (e) => {
if (!isResizing) return;
const panel = document.getElementById('folderTreePanel');
const newWidth = Math.max(160, Math.min(500, e.clientX - panel.getBoundingClientRect().left));
panel.style.width = newWidth + 'px';
});
document.addEventListener('mouseup', () => {
if (isResizing) {
isResizing = false;
document.body.style.cursor = '';
document.body.style.userSelect = '';
}
});
}
}
async loadFolderTree() {
try {
const response = await fetch('/prompt_manager/gallery/subfolders?include_ancestors=true');
if (!response.ok) return;
const data = await response.json();
if (data.success && data.subfolders.length > 0) {
this.folderList = data.subfolders;
document.getElementById('folderTreePanel').classList.remove('hidden');
const rh = document.getElementById('folderResizeHandle');
if (rh) rh.classList.remove('hidden');
this.renderFolderTree();
}
} catch (error) {
console.error('Folder tree error:', error);
}
}
renderFolderTree() {
const container = document.getElementById('folderTree');
container.innerHTML = '';
// Build tree structure from flat sorted paths
const tree = {};
for (const path of this.folderList) {
const parts = path.replace(/\\/g, '/').split('/');
let node = tree;
for (const part of parts) {
if (!node[part]) node[part] = {};
node = node[part];
}
}
const renderNode = (obj, parentPath, depth) => {
const entries = Object.keys(obj).sort();
for (const name of entries) {
const fullPath = parentPath ? parentPath + '/' + name : name;
const hasChildren = Object.keys(obj[name]).length > 0;
const isExpanded = this.expandedFolders.has(fullPath);
const isActive = this.currentSubfolder === fullPath;
const row = document.createElement('div');
row.className = 'flex items-center px-2 py-1 cursor-pointer hover:bg-pm-hover transition-colors'
+ (isActive ? ' bg-pm-accent/10 text-pm-accent font-medium' : ' text-pm');
row.style.paddingLeft = (8 + depth * 16) + 'px';
// Toggle arrow
const arrow = document.createElement('span');
arrow.className = 'inline-block w-4 text-center text-pm-secondary mr-1 select-none';
if (hasChildren) {
arrow.textContent = isExpanded ? '\u25BE' : '\u25B8';
arrow.addEventListener('click', (e) => {
e.stopPropagation();
if (this.expandedFolders.has(fullPath)) {
this.expandedFolders.delete(fullPath);
} else {
this.expandedFolders.add(fullPath);
}
this.renderFolderTree();
});
} else {
arrow.textContent = '';
}
row.appendChild(arrow);
const label = document.createElement('span');
label.textContent = name;
label.className = 'truncate';
row.appendChild(label);
// Click to filter
row.addEventListener('click', () => {
this.currentSubfolder = fullPath;
this.renderFolderTree();
this.loadImages(1);
});
container.appendChild(row);
if (hasChildren && isExpanded) {
renderNode(obj[name], fullPath, depth + 1);
}
}
};
// "All" option at top
const allRow = document.createElement('div');
allRow.className = 'flex items-center px-2 py-1 cursor-pointer hover:bg-pm-hover transition-colors'
+ (!this.currentSubfolder ? ' bg-pm-accent/10 text-pm-accent font-medium' : ' text-pm');
allRow.style.paddingLeft = '8px';
allRow.innerHTML = '<span class="inline-block w-4 mr-1"></span><span>All Images</span>';
allRow.addEventListener('click', () => {
this.currentSubfolder = '';
this.renderFolderTree();
this.loadImages(1);
});
container.appendChild(allRow);
renderNode(tree, '', 0);
}
setThumbSize(size) {
this.thumbSize = size;
const grid = document.getElementById('galleryGrid');
const sizes = {
sm: { cols: 'repeat(auto-fill, minmax(100px, 1fr))', gap: '4px' },
md: { cols: 'repeat(auto-fill, minmax(180px, 1fr))', gap: '8px' },
lg: { cols: 'repeat(auto-fill, minmax(300px, 1fr))', gap: '12px' },
};
const s = sizes[size] || sizes.md;
grid.classList.remove('gallery-columns-custom'); // an explicit size wins over the column setting
grid.style.gridTemplateColumns = s.cols;
grid.style.gap = s.gap;
// Update button states
['Sm', 'Md', 'Lg'].forEach(s => {
const btn = document.getElementById('thumb' + s + 'Btn');
if (btn) {
if (s.toLowerCase() === size) {
btn.className = btn.className.replace('bg-pm-input', 'bg-pm-accent').replace('text-pm ', 'text-pm-accent-fg ');
} else {
btn.className = btn.className.replace('bg-pm-accent', 'bg-pm-input').replace('text-pm-accent-fg', 'text-pm');
}
}
});
}
/**
* Load one page of images. `page` is committed to this.currentPage only after the
* server answered, and a newer request supersedes an older one still in flight.
*/
async loadImages(page = this.currentPage, { append = false } = {}) {
if (!append) this.showLoading();
else document.getElementById('loadingStatus').textContent = 'Loading more...';
const offset = (page - 1) * this.limit;
let url = `/prompt_manager/images/output?limit=${this.limit}&offset=${offset}`;
if (this.currentSubfolder) {
url += `&subfolder=${encodeURIComponent(this.currentSubfolder)}`;
}
try {
const data = await this.imagesRequest((signal) => this.api.get(url, { signal }));
if (data === ApiClient.STALE) return;
const next = ListState.nextPageState(
{ page: this.currentPage, limit: this.limit, total: this.total },
{ page, total: data.total },
);
this.currentPage = next.page;
this.total = next.total;
this.images = append ? this.images.concat(data.images) : data.images;
this.updateStats();
this.renderGallery();
this.updatePagination();
document.getElementById('loadingStatus').textContent = 'Loaded';
this.maybeLoadMore(); // a short page may not fill the viewport
} catch (error) {
console.error('Error loading images:', error);
this.showError(error.message);
}
}
maybeLoadMore() {
const el = document.getElementById('galleryScroll');
if (!el) return;
const wanted = ListState.shouldLoadMore({
infiniteScroll: this.infiniteScroll,
loading: this.loadingMore,
page: this.currentPage,
limit: this.limit,
total: this.total,
scrollTop: el.scrollTop,
clientHeight: el.clientHeight,
scrollHeight: el.scrollHeight,
});
if (wanted) this.loadMoreImages();
}
async loadMoreImages() {
if (this.loadingMore) return;
this.loadingMore = true;
try {
await this.loadImages(this.currentPage + 1, { append: true });
} finally {
this.loadingMore = false;
}
}
showLoading() {
document.getElementById('loadingState').classList.remove('hidden');
document.getElementById('errorState').classList.add('hidden');
document.getElementById('galleryGrid').classList.add('hidden');
document.getElementById('galleryList').classList.add('hidden');
document.getElementById('paginationControls').classList.add('hidden');
document.getElementById('loadingStatus').textContent = 'Loading...';
}
showError(message) {
document.getElementById('loadingState').classList.add('hidden');
document.getElementById('errorState').classList.remove('hidden');
document.getElementById('galleryGrid').classList.add('hidden');
document.getElementById('galleryList').classList.add('hidden');
document.getElementById('paginationControls').classList.add('hidden');
document.getElementById('errorMessage').textContent = message;
document.getElementById('loadingStatus').textContent = 'Error';
}
updateStats() {
const start = this.infiniteScroll ? 1 : (this.currentPage - 1) * this.limit + 1;
const end = this.infiniteScroll ? this.images.length : Math.min(this.currentPage * this.limit, this.total);
document.getElementById('showingStart').textContent = this.images.length > 0 ? start : 0;
document.getElementById('showingEnd').textContent = end;
document.getElementById('totalImages').textContent = this.total;
}
setViewMode(mode) {
this.viewMode = mode;
// Update button states
const gridBtn = document.getElementById('gridViewBtn');
const listBtn = document.getElementById('listViewBtn');
if (mode === 'grid') {
gridBtn.classList.remove('bg-pm-input');
gridBtn.classList.add('bg-pm-accent');
listBtn.classList.remove('bg-pm-accent');
listBtn.classList.add('bg-pm-input');
} else {
listBtn.classList.remove('bg-pm-input');
listBtn.classList.add('bg-pm-accent');
gridBtn.classList.remove('bg-pm-accent');
gridBtn.classList.add('bg-pm-input');
}
this.renderGallery();
}
renderGallery() {
if (this.images.length === 0) {
this.showError('No images found in the output folder');
return;
}
document.getElementById('loadingState').classList.add('hidden');
document.getElementById('errorState').classList.add('hidden');
if (this.viewMode === 'grid') {
this.renderGridView();
} else {
this.renderListView();
}
// Initialize ViewerJS after rendering
setTimeout(() => this.initializeViewer(), 100);
// Add simple fallback click handlers
this.addFallbackClickHandlers();
}
renderGridView() {
const grid = document.getElementById('galleryGrid');
const list = document.getElementById('galleryList');
const showInfo = this.getSettings().showImageInfo;
list.classList.add('hidden');
grid.classList.remove('hidden');
grid.innerHTML = this.images.map((image, index) => {
// Use thumbnail for display if available, fallback to original
const displayUrl = image.thumbnail_url || image.url;
const hasThumb = !!image.thumbnail_url;
const isVideo = image.is_video || false;
const mediaType = image.media_type || 'image';
return `
<div class="image-item bg-pm-surface rounded-pm-md overflow-hidden border border-pm hover:border-pm cursor-pointer group">
<div class="aspect-square bg-pm-primary overflow-hidden relative">
<img src="${escapeHtml(displayUrl)}"
alt="${escapeHtml(image.filename)}"
class="w-full h-full object-cover group-hover:scale-105 transition-transform duration-300"
loading="lazy"
data-original="${escapeHtml(image.url)}"
data-thumbnail="${escapeHtml(image.thumbnail_url || '')}"
data-caption="${escapeHtml(this.formatImageCaption(image))}"
data-media-type="${escapeHtml(mediaType)}"
data-is-video="${isVideo}"
>
<!-- Fallback for failed thumbnails -->
<div data-media-fallback class="hidden absolute inset-0 bg-pm-surface text-pm-secondary flex items-center justify-center">
<div class="text-center">
<div class="text-lg mb-1">${isVideo ? '🎬' : '🖼️'}</div>
<div class="text-xs">Failed to load</div>
</div>
</div>
<div class="absolute inset-0 bg-black/0 group-hover:bg-black/20 transition-opacity duration-300 flex items-center justify-center">
${isVideo ? `
<div class="w-12 h-12 bg-black/60 rounded-full flex items-center justify-center opacity-0 group-hover:opacity-100 transition-opacity duration-300">
<svg class="w-6 h-6 text-pm ml-1" fill="currentColor" viewBox="0 0 24 24">
<path d="M8 5v14l11-7z"/>
</svg>
</div>
` : `
<svg class="w-8 h-8 text-pm opacity-0 group-hover:opacity-100 transition-opacity duration-300" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"></path>
</svg>
`}
</div>
${hasThumb ? '<div class="absolute top-2 right-2 w-3 h-3 bg-pm-success rounded-full" title="Thumbnail available"></div>' : ''}
${isVideo ? '<div class="absolute top-2 left-2 px-2 py-1 bg-pm-error text-pm text-xs rounded" title="Video file">VIDEO</div>' : ''}
</div>
<div class="p-2">
<div class="text-xs text-pm-secondary truncate" title="${escapeHtml(image.filename)}">${escapeHtml(image.filename)}</div>
${showInfo ? `<div class="text-xs text-pm-muted">${this.formatFileSize(image.size)}${hasThumb ? ' • Fast' : ''}${isVideo ? ' • Video' : ''}</div>` : ''}
</div>
</div>
`;
}).join('');
}
renderListView() {
const grid = document.getElementById('galleryGrid');
const list = document.getElementById('galleryList');
const showInfo = this.getSettings().showImageInfo;
grid.classList.add('hidden');
list.classList.remove('hidden');
list.innerHTML = this.images.map((image, index) => {
// Use thumbnail for display if available, fallback to original
const displayUrl = image.thumbnail_url || image.url;
const hasThumb = !!image.thumbnail_url;
const isVideo = image.is_video || false;
const mediaType = image.media_type || 'image';
return `
<div class="image-item bg-pm-surface rounded-pm-md border border-pm hover:border-pm cursor-pointer group flex items-center p-4">
<div class="w-16 h-16 bg-pm-primary rounded-pm-md overflow-hidden flex-shrink-0 mr-4 relative">
<img src="${escapeHtml(displayUrl)}"
alt="${escapeHtml(image.filename)}"
class="w-full h-full object-cover group-hover:scale-105 transition-transform duration-300"
loading="lazy"
data-original="${escapeHtml(image.url)}"
data-thumbnail="${escapeHtml(image.thumbnail_url || '')}"
data-caption="${escapeHtml(this.formatImageCaption(image))}"
data-media-type="${escapeHtml(mediaType)}"
data-is-video="${isVideo}">
${hasThumb ? '<div class="absolute top-1 right-1 w-2 h-2 bg-pm-success rounded-full" title="Thumbnail available"></div>' : ''}
${isVideo ? '<div class="absolute top-1 left-1 w-4 h-3 bg-pm-error text-pm text-xs flex items-center justify-center rounded" title="Video">▶</div>' : ''}
</div>
<div class="flex-1 min-w-0">
<div class="text-sm font-medium text-pm truncate">${escapeHtml(image.filename)}${isVideo ? ' 🎬' : ''}</div>
${showInfo ? `<div class="text-xs text-pm-secondary">${this.formatFileSize(image.size)} • ${new Date(image.modified_time * 1000).toLocaleDateString()}${hasThumb ? ' • Fast' : ''}${isVideo ? ' • Video' : ''}</div>` : ''}
<div class="text-xs text-pm-muted truncate">${escapeHtml(image.relative_path)}</div>
</div>
<div class="flex-shrink-0 ml-4">
${isVideo ? `
<svg class="w-5 h-5 text-pm-error group-hover:text-pm-error transition-colors" fill="currentColor" viewBox="0 0 24 24">
<path d="M8 5v14l11-7z"/>
</svg>
` : `
<svg class="w-5 h-5 text-pm-secondary group-hover:text-pm transition-colors" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"></path>
</svg>
`}
</div>
</div>
`;
}).join('');
}
initializeViewer() {
// Destroy existing viewer if it exists
if (this.viewer) {
this.viewer.destroy();
}
const container = this.viewMode === 'grid' ? document.getElementById('galleryGrid') : document.getElementById('galleryList');
// Find all images in the container
const images = container.querySelectorAll('img[data-original]');
if (images.length === 0) {
console.warn('No images found for viewer initialization');
return;
}
this.viewer = new Viewer(container, {
inline: false,
button: true,
backdrop: true,
navbar: true,
title: true,
// IMPORTANT: Use original images for viewing, not thumbnails
url: 'data-original', // Tell ViewerJS to use data-original attribute
toolbar: {
zoomIn: 1,
zoomOut: 1,
oneToOne: 1,
reset: 1,
prev: 1,
play: {
show: 1,
size: 'large',
},
next: 1,
rotateLeft: 1,
rotateRight: 1,
flipHorizontal: 1,
flipVertical: 1,
},
className: '',
title: [1, (image, imageData) => `${imageData.alt} (${this.images.length} images) - Original Image`],
viewed: (event) => {
// Add metadata sidebar when ViewerJS opens
// Use the original image URL from data-original attribute
const originalImg = event.detail.originalImage;
setTimeout(() => this.addMetadataSidebar(originalImg), 100);
},
show: function() {
},
shown: function() {
},
hide: function() {
// Remove sidebar when viewer closes
const sidebar = document.getElementById('metadata-sidebar');
if (sidebar) {
sidebar.remove();
}
}
});
// Don't add click handlers here - let ViewerJS handle them
}
addMetadataSidebar(originalImage) {
const viewerContainer = document.querySelector('.viewer-container');
if (!viewerContainer || document.getElementById('metadata-sidebar')) return;
const sidebar = document.createElement('div');
sidebar.id = 'metadata-sidebar';
sidebar.className = 'metadata-sidebar';
sidebar.innerHTML = `
<!-- Collapse Button -->
<div class="metadata-collapse-btn" onclick="this.closest('.metadata-sidebar').classList.toggle('collapsed')">
<svg class="w-4 h-4 text-pm-secondary" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7"></path>
</svg>
</div>
<!-- Header -->
<div class="flex items-center justify-between p-4 border-b border-pm">
<div class="flex items-center gap-2">
<svg class="w-5 h-5 text-pm-secondary" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"></path>
</svg>
<h1 class="text-sm font-semibold text-pm">Generation data</h1>
</div>
<button class="text-xs text-pm-accent hover:text-pm-accent transition-colors metadata-copy-all">
📋 COPY ALL
</button>
</div>
<!-- Scrollable Content -->
<div id="metadata-content" class="flex-1 overflow-y-auto p-4 space-y-3">
<!-- Loading state -->
<div class="text-center text-pm-muted py-8">
<div class="w-8 h-8 border-2 border-pm-accent/30 border-t-pm-accent rounded-full animate-spin mx-auto mb-2"></div>
<p class="text-sm">Loading metadata...</p>
</div>
</div>
`;
document.body.appendChild(sidebar);
// Apply default collapsed state from settings
if (this.getSettings().sidebarCollapsedByDefault) {
sidebar.classList.add('collapsed');
}
// Load metadata for the current image (ALWAYS use original for metadata)
const originalImageUrl = originalImage.dataset.original || originalImage.src;
this.loadImageMetadata(originalImageUrl, sidebar.querySelector('#metadata-content'));
// Listen for ViewerJS view changes to update metadata
this.setupViewerMetadataUpdates(sidebar);
}
setupViewerMetadataUpdates(sidebar) {
// Listen for ViewerJS navigation events
const viewerContainer = document.querySelector('.viewer-container');
if (!viewerContainer) return;
// Override ViewerJS navigation to update metadata
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.type === 'attributes' && mutation.attributeName === 'src') {
const img = mutation.target;
if (img.tagName === 'IMG' && img.closest('.viewer-canvas')) {
// Find the corresponding original image
const imageUrl = img.src;
this.loadImageMetadata(imageUrl, sidebar.querySelector('#metadata-content'));
}
}
});
});
const canvas = viewerContainer.querySelector('.viewer-canvas');
if (canvas) {
observer.observe(canvas, {
childList: true,
subtree: true,
attributes: true,
attributeFilter: ['src']
});
}
}
addFallbackClickHandlers() {
// Add simple click handlers that work even if ViewerJS fails
const container = this.viewMode === 'grid' ? document.getElementById('galleryGrid') : document.getElementById('galleryList');
const imageItems = container.querySelectorAll('.image-item');
imageItems.forEach((item, index) => {
item.addEventListener('click', (e) => {
e.preventDefault();
e.stopPropagation();
const img = item.querySelector('img[data-original]');
if (img) {
// ALWAYS use the original media URL for viewing
const originalMediaUrl = img.dataset.original;
const caption = img.dataset.caption;
const isVideo = img.dataset.isVideo === 'true';
const mediaType = img.dataset.mediaType || 'image';
if (isVideo) {
// For videos, use our custom video modal
this.showVideoModal(originalMediaUrl, caption, index);
} else {
// For images, try ViewerJS first, fallback to simple modal
if (this.viewer) {
try {
this.viewer.view(index);
} catch (e) {
console.warn('ViewerJS failed, using fallback:', e);
this.showSimpleImageModal(originalMediaUrl, caption);
}
} else {
this.showSimpleImageModal(originalMediaUrl, caption);
}
}
}
});
});
}
async showSimpleImageModal(imageUrl, caption) {
// Create a modal that matches the screenshot design
const modal = document.createElement('div');
modal.className = 'fixed inset-0 bg-black z-50 flex';
modal.id = 'image-viewer-modal';
modal.innerHTML = `
<!-- Image area with navigation -->
<div class="flex-1 flex items-center justify-center relative image-viewer-area">
<!-- Navigation arrows -->
<button class="absolute left-4 top-1/2 transform -translate-y-1/2 w-10 h-10 bg-black/50 hover:bg-black/70 rounded-full flex items-center justify-center text-pm text-xl z-10" id="prevImageBtn">
‹
</button>
<button class="absolute right-4 top-1/2 transform -translate-y-1/2 w-10 h-10 bg-black/50 hover:bg-black/70 rounded-full flex items-center justify-center text-pm text-xl z-10" id="nextImageBtn">
›
</button>
<!-- Close button -->
<button class="absolute top-4 right-4 w-10 h-10 bg-black/50 hover:bg-black/70 rounded-full flex items-center justify-center text-pm text-xl z-10" onclick="this.parentElement.parentElement.remove()">
×
</button>
<!-- Main image -->
<img src="${escapeHtml(imageUrl)}" alt="${escapeHtml(caption)}" class="max-w-full max-h-full object-contain" id="modalImage">
</div>
<!-- Metadata sidebar (matching the screenshot) -->
<div class="metadata-sidebar">
<!-- Collapse Button -->
<div class="metadata-collapse-btn" onclick="this.closest('.metadata-sidebar').classList.toggle('collapsed')">
<svg class="w-4 h-4 text-pm-secondary" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7"></path>
</svg>
</div>
<!-- Header -->
<div class="flex items-center justify-between p-4 border-b border-pm">
<div class="flex items-center gap-2">
<svg class="w-5 h-5 text-pm-secondary" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"></path>
</svg>
<h1 class="text-sm font-semibold text-pm">Generation data</h1>
</div>
<button class="text-xs text-pm-accent hover:text-pm-accent transition-colors metadata-copy-all">
📋 COPY ALL
</button>
</div>
<!-- Scrollable Content -->
<div id="metadata-content" class="flex-1 overflow-y-auto p-4 space-y-3">
<!-- Loading state -->
<div class="text-center text-pm-muted py-8">
<div class="w-8 h-8 border-2 border-pm-accent/30 border-t-pm-accent rounded-full animate-spin mx-auto mb-2"></div>
<p class="text-sm">Loading metadata...</p>
</div>
</div>
</div>
`;
// Close on click outside image area
modal.addEventListener('click', (e) => {
if (e.target === modal || e.target.id === 'modalImage') {
modal.remove();
}
});
// Close on Escape key; the listener is dropped once the modal leaves the
// document, whichever way it was closed
const escapeHandler = (e) => {
if (e.key === 'Escape') modal.remove();
};
document.addEventListener('keydown', escapeHandler);
const closeObserver = new MutationObserver(() => {
if (modal.isConnected) return;
document.removeEventListener('keydown', escapeHandler);
closeObserver.disconnect();
});
closeObserver.observe(document.body, { childList: true });
document.body.appendChild(modal);
// Apply default collapsed state from settings
const sidebarInModal = modal.querySelector('.metadata-sidebar');
if (sidebarInModal && this.getSettings().sidebarCollapsedByDefault) {
sidebarInModal.classList.add('collapsed');
}
// Add navigation functionality
this.setupImageNavigation(modal, imageUrl);
// Load and display metadata
await this.loadImageMetadata(imageUrl, modal.querySelector('#metadata-content'));
}
async showVideoModal(videoUrl, caption, videoIndex) {
const settings = this.getSettings();
// Create a modal for video viewing
const modal = document.createElement('div');
modal.className = 'fixed inset-0 bg-black z-50 flex';
modal.innerHTML = `
<!-- Video area with navigation -->
<div class="flex-1 flex items-center justify-center relative">
<!-- Navigation arrows -->
<button class="absolute left-4 top-1/2 transform -translate-y-1/2 w-10 h-10 bg-black/50 hover:bg-black/70 rounded-full flex items-center justify-center text-pm text-xl z-10" id="prevVideoBtn">
‹
</button>
<button class="absolute right-4 top-1/2 transform -translate-y-1/2 w-10 h-10 bg-black/50 hover:bg-black/70 rounded-full flex items-center justify-center text-pm text-xl z-10" id="nextVideoBtn">
›
</button>
<!-- Close button -->
<button class="absolute top-4 right-4 w-10 h-10 bg-black/50 hover:bg-black/70 rounded-full flex items-center justify-center text-pm text-xl z-10" onclick="this.parentElement.parentElement.remove()">
×
</button>
<!-- Video controls -->
<div class="absolute bottom-4 right-4 flex space-x-2 z-10">
<button id="videoMuteBtn" class="px-3 py-2 bg-black/50 hover:bg-black/70 rounded text-pm text-sm">
🔊
</button>
<button id="videoLoopBtn" class="px-3 py-2 bg-black/50 hover:bg-black/70 rounded text-pm text-sm">
🔁
</button>
</div>
<!-- Main video -->
<video id="modalVideo"
src="${escapeHtml(videoUrl)}"
class="max-w-full max-h-full object-contain"
controls
${settings.videoAutoplay ? 'autoplay' : ''}
${settings.videoMute ? 'muted' : ''}
${settings.videoLoop ? 'loop' : ''}>
Your browser does not support the video tag.
</video>
</div>
<!-- Metadata sidebar (videos have limited metadata) -->
<div class="metadata-sidebar">
<!-- Collapse Button -->
<div class="metadata-collapse-btn" onclick="this.closest('.metadata-sidebar').classList.toggle('collapsed')">
<svg class="w-4 h-4 text-pm-secondary" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7"></path>
</svg>
</div>
<!-- Header -->
<div class="flex items-center justify-between p-4 border-b border-pm">
<div class="flex items-center gap-2">
<svg class="w-5 h-5 text-pm-error" fill="currentColor" viewBox="0 0 24 24">
<path d="M8 5v14l11-7z"/>
</svg>
<h1 class="text-sm font-semibold text-pm">Video Info</h1>
</div>
<button class="text-xs text-pm-accent hover:text-pm-accent transition-colors" data-action="copy-url" data-url="${escapeHtml(videoUrl)}" data-label="📋 COPY URL">
📋 COPY URL
</button>
</div>
<!-- Video info content -->
<div id="video-info-content" class="flex-1 overflow-y-auto p-4 space-y-3">
<!-- Video details will be loaded here -->
</div>
</div>
`;
// Close on click outside video area
modal.addEventListener('click', (e) => {
if (e.target === modal) {
modal.remove();
}
});
// Close on Escape key; the listener is dropped once the modal leaves the
// document, whichever way it was closed
const escapeHandler = (e) => {
if (e.key === 'Escape') modal.remove();
};
document.addEventListener('keydown', escapeHandler);
const closeObserver = new MutationObserver(() => {
if (modal.isConnected) return;
document.removeEventListener('keydown', escapeHandler);
closeObserver.disconnect();
});
closeObserver.observe(document.body, { childList: true });
document.body.appendChild(modal);
// Apply default collapsed state from settings
const sidebarInVideoModal = modal.querySelector('.metadata-sidebar');
if (sidebarInVideoModal && this.getSettings().sidebarCollapsedByDefault) {
sidebarInVideoModal.classList.add('collapsed');
}
// Set up video controls
this.setupVideoControls(modal, settings);
// Add navigation functionality
this.setupVideoNavigation(modal, videoUrl, videoIndex);
// Load and display video info
await this.loadVideoInfo(videoUrl, modal.querySelector('#video-info-content'));
}
setupVideoControls(modal, settings) {
const video = modal.querySelector('#modalVideo');
const muteBtn = modal.querySelector('#videoMuteBtn');
const loopBtn = modal.querySelector('#videoLoopBtn');
// Mute/unmute toggle
muteBtn.addEventListener('click', () => {
video.muted = !video.muted;
muteBtn.textContent = video.muted ? '🔇' : '🔊';
// Save setting
const newSettings = this.getSettings();
newSettings.videoMute = video.muted;
this.saveSettings(newSettings);
});
// Loop toggle
loopBtn.addEventListener('click', () => {
video.loop = !video.loop;
loopBtn.style.opacity = video.loop ? '1' : '0.5';
// Save setting
const newSettings = this.getSettings();
newSettings.videoLoop = video.loop;
this.saveSettings(newSettings);
});
// Set initial states
muteBtn.textContent = video.muted ? '🔇' : '🔊';
loopBtn.style.opacity = video.loop ? '1' : '0.5';
}
setupVideoNavigation(modal, currentVideoUrl, currentIndex) {
const prevBtn = modal.querySelector('#prevVideoBtn');
const nextBtn = modal.querySelector('#nextVideoBtn');
// Get all video items from current gallery
const container = this.viewMode === 'grid' ? document.getElementById('galleryGrid') : document.getElementById('galleryList');
const videoItems = Array.from(container.querySelectorAll('.image-item img[data-is-video="true"]'));
if (videoItems.length <= 1) {
prevBtn.style.display = 'none';
nextBtn.style.display = 'none';
return;
}
const currentVideoIndex = videoItems.findIndex(img => img.dataset.original === currentVideoUrl);
prevBtn.addEventListener('click', () => {
const prevIndex = currentVideoIndex > 0 ? currentVideoIndex - 1 : videoItems.length - 1;
const prevVideo = videoItems[prevIndex];
if (prevVideo) {
modal.remove();
this.showVideoModal(prevVideo.dataset.original, prevVideo.dataset.caption, prevIndex);
}
});
nextBtn.addEventListener('click', () => {
const nextIndex = currentVideoIndex < videoItems.length - 1 ? currentVideoIndex + 1 : 0;
const nextVideo = videoItems[nextIndex];
if (nextVideo) {
modal.remove();
this.showVideoModal(nextVideo.dataset.original, nextVideo.dataset.caption, nextIndex);
}
});
// Add keyboard navigation
const keyHandler = (e) => {
if (e.key === 'ArrowLeft') {
e.preventDefault();
prevBtn.click();
} else if (e.key === 'ArrowRight') {
e.preventDefault();
nextBtn.click();
}
};
document.addEventListener('keydown', keyHandler);
// Clean up listener when modal is removed
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.type === 'childList') {
mutation.removedNodes.forEach((node) => {
if (node === modal) {
document.removeEventListener('keydown', keyHandler);
observer.disconnect();
}
});
}
});
});
observer.observe(document.body, { childList: true });
}
async loadVideoInfo(videoUrl, container) {
try {
// Extract basic video information
const fileName = videoUrl.split('/').pop();
const fileExt = fileName.split('.').pop().toUpperCase();
container.innerHTML = `
<!-- File Info -->
<div class="space-y-2">
<h2 class="text-sm font-medium text-pm-secondary">File Info</h2>
<div class="text-xs text-pm-secondary font-mono bg-pm-surface p-2 rounded break-all cursor-pointer hover:bg-pm-hover"
data-action="copy-url" data-url="${escapeHtml(videoUrl)}" data-label="${escapeHtml(fileName)}"
title="Click to copy path">
${escapeHtml(fileName)}
</div>
<div class="text-xs text-pm-muted">
Type: ${escapeHtml(fileExt)} Video
</div>
</div>
<!-- Video Properties -->
<div class="space-y-2" id="video-properties">
<h2 class="text-sm font-medium text-pm-secondary">Video Properties</h2>
<div class="text-xs text-pm-secondary">
Loading video information...
</div>
</div>
<!-- Video Settings -->
<div class="space-y-3">
<h2 class="text-sm font-medium text-pm-secondary">Playback Settings</h2>
<div class="space-y-2 text-xs">
<div class="flex justify-between items-center">
<span class="text-pm-secondary">Autoplay:</span>
<span class="text-pm">${this.getSettings().videoAutoplay ? 'Enabled' : 'Disabled'}</span>
</div>
<div class="flex justify-between items-center">
<span class="text-pm-secondary">Muted:</span>
<span class="text-pm">${this.getSettings().videoMute ? 'Yes' : 'No'}</span>
</div>
<div class="flex justify-between items-center">
<span class="text-pm-secondary">Loop:</span>
<span class="text-pm">${this.getSettings().videoLoop ? 'Enabled' : 'Disabled'}</span>
</div>
</div>
</div>
<!-- Note about metadata -->
<div class="bg-pm-accent-tint border border-pm-accent rounded-pm-md p-3">
<h4 class="text-pm-accent font-medium mb-1 text-sm">ℹ️ Video Note</h4>
<p class="text-pm-secondary text-xs">
Videos don't contain the same metadata as PNG images. ComfyUI workflow data is only embedded in PNG outputs.
</p>
</div>
`;
// Try to get video metadata from the video element
const video = document.querySelector('#modalVideo');
if (video) {
video.addEventListener('loadedmetadata', () => {
const propertiesDiv = container.querySelector('#video-properties');
if (propertiesDiv) {
const duration = video.duration;
const width = video.videoWidth;
const height = video.videoHeight;
propertiesDiv.innerHTML = `
<h2 class="text-sm font-medium text-pm-secondary">Video Properties</h2>
<div class="space-y-1 text-xs">
<div class="flex justify-between">
<span class="text-pm-secondary">Duration:</span>
<span class="text-pm">${this.formatDuration(duration)}</span>
</div>
<div class="flex justify-between">
<span class="text-pm-secondary">Resolution:</span>
<span class="text-pm">${width} × ${height}</span>
</div>
<div class="flex justify-between">
<span class="text-pm-secondary">Aspect Ratio:</span>
<span class="text-pm">${(width/height).toFixed(2)}:1</span>
</div>
</div>
`;
}
});
}
} catch (error) {
console.error('Error loading video info:', error);
container.innerHTML = `
<div class="bg-pm-error-tint border border-pm-error rounded-pm-md p-4">
<h4 class="text-pm-error font-medium mb-2">❌ Video Info Error</h4>
<p class="text-pm-secondary text-sm">${escapeHtml(error.message)}</p>
</div>
`;
}
}
formatDuration(seconds) {
if (isNaN(seconds) || seconds === 0) return '0:00';
const hours = Math.floor(seconds / 3600);
const minutes = Math.floor((seconds % 3600) / 60);
const secs = Math.floor(seconds % 60);
if (hours > 0) {
return `${hours}:${minutes.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
} else {
return `${minutes}:${secs.toString().padStart(2, '0')}`;
}
}
async loadImageMetadata(imageUrl, container) {
try {
// First try to get linked prompt from database
let databasePrompt = null;
try {
// Extract relative path from the URL for the API call
const urlParts = imageUrl.split('/prompt_manager/images/serve/');
if (urlParts.length > 1) {
const relativePath = urlParts[1];
const response = await fetch(`/prompt_manager/images/prompt/${encodeURIComponent(relativePath)}`);
if (response.ok) {
const data = await response.json();
if (data.success && data.prompt) {
databasePrompt = data.prompt;
}
}
}
} catch (dbError) {
// No database prompt found, will fall back to metadata extraction
}
// If we have a database prompt, use it; otherwise extract from metadata
let metadata;
if (databasePrompt) {
// Create metadata object from database prompt
metadata = {
positivePrompt: databasePrompt.text,
negativePrompt: 'No negative prompt found', // Database doesn't separate positive/negative
checkpoint: 'Unknown',
steps: 'Unknown',
cfgScale: 'Unknown',
sampler: 'Unknown',
seed: 'Unknown',
workflow: databasePrompt.workflow_data,
imagePath: imageUrl,
source: 'database', // Mark this as coming from database
promptId: databasePrompt.prompt_id,
category: databasePrompt.category,
tags: databasePrompt.tags,
rating: databasePrompt.rating,
notes: databasePrompt.notes
};
// Still try to extract technical parameters from PNG metadata if available
try {
const pngMetadata = await this.extractImageMetadata(imageUrl);
if (pngMetadata) {
metadata.checkpoint = pngMetadata.checkpoint;
metadata.steps = pngMetadata.steps;
metadata.cfgScale = pngMetadata.cfgScale;
metadata.sampler = pngMetadata.sampler;
metadata.seed = pngMetadata.seed;
// Use negative prompt from PNG if available
if (pngMetadata.negativePrompt && pngMetadata.negativePrompt !== 'No negative prompt found') {
metadata.negativePrompt = pngMetadata.negativePrompt;
}
}
} catch (pngError) {
// Could not extract PNG metadata, using database-only data
}
} else {
// Fall back to PNG metadata extraction
metadata = await this.extractImageMetadata(imageUrl);
if (metadata) {
metadata.source = 'png'; // Mark this as coming from PNG
}
}
this.displayMetadata(metadata, container);
} catch (error) {
console.error('Error loading metadata:', error);
container.innerHTML = `
<div class="bg-pm-error-tint border border-pm-error rounded-pm-md p-4">
<h4 class="text-pm-error font-medium mb-2">❌ Metadata Error</h4>
<p class="text-pm-secondary text-sm">${escapeHtml(error.message)}</p>
</div>
`;
}
}
displayMetadata(metadata, container) {
if (!metadata) {
container.innerHTML = `
<div class="text-center text-pm-muted py-8">
<svg class="w-16 h-16 mx-auto mb-4 opacity-30" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"></path>
</svg>
<p class="text-sm">No metadata found in this image</p>
</div>
`;
return;
}
const html = `
<!-- File Path -->
<div class="space-y-2">
<h2 class="text-sm font-medium text-pm-secondary">File Path</h2>
<div class="text-xs text-pm-secondary font-mono bg-pm-surface p-2 rounded break-all cursor-pointer hover:bg-pm-hover" data-copy-path title="Click to copy">
${escapeHtml(metadata.imagePath || 'Unknown')}
</div>
</div>
<!-- Resources used -->
<div class="space-y-3">
<h2 class="text-sm font-medium text-pm-secondary">Resources used</h2>
<div class="space-y-2 text-xs">
<div class="flex justify-between">
<span class="text-pm-secondary">Model:</span>
<span class="text-pm">${escapeHtml(metadata.checkpoint || 'Unknown')}</span>
</div>
</div>
</div>
<!-- Prompt Section -->
<div class="space-y-3">
<div class="flex items-center justify-between">
<h2 class="text-sm font-medium text-pm-secondary">Prompt</h2>
<button class="text-xs px-2 py-1 bg-pm-warning hover:bg-pm-warning text-pm rounded transition-colors" data-copy-type="positive">
COPY PROMPT
</button>
</div>
<div class="text-xs text-pm bg-pm-surface p-3 rounded max-h-32 overflow-y-auto break-words leading-relaxed">
${this.formatPromptText(metadata.positivePrompt)}
<div class="mt-2 pt-2 border-t border-pm">
<button class="text-pm-accent hover:text-pm-accent underline text-xs" data-show-type="positive">
Show more
</button>
</div>
</div>
</div>
<!-- Negative Prompt -->
<div class="space-y-3">
<h2 class="text-sm font-medium text-pm-secondary">Negative prompt</h2>
<div class="text-xs text-pm bg-pm-surface p-3 rounded max-h-32 overflow-y-auto break-words leading-relaxed">
${this.formatPromptText(metadata.negativePrompt)}
<div class="mt-2 pt-2 border-t border-pm">
<button class="text-pm-accent hover:text-pm-accent underline text-xs" data-show-type="negative">
Show more
</button>
</div>
</div>
</div>
<!-- Other metadata -->
<div class="space-y-3">
<h2 class="text-sm font-medium text-pm-secondary">Other metadata</h2>
<div class="space-y-2 text-xs">
<div class="flex justify-between">
<span class="text-pm-secondary">CFG SCALE:</span>
<span class="text-pm px-2 py-1 bg-pm-surface rounded text-xs">${escapeHtml(metadata.cfgScale || 'Unknown')}</span>
</div>
<div class="flex justify-between">
<span class="text-pm-secondary">STEPS:</span>
<span class="text-pm px-2 py-1 bg-pm-surface rounded text-xs">${escapeHtml(metadata.steps || 'Unknown')}</span>
</div>
<div class="flex justify-between">
<span class="text-pm-secondary">SAMPLER:</span>
<span class="text-pm px-2 py-1 bg-pm-surface rounded text-xs">${escapeHtml(metadata.sampler || 'Unknown')}</span>
</div>
<div class="flex justify-between">
<span class="text-pm-secondary">SEED:</span>
<span class="text-pm font-mono px-2 py-1 bg-pm-surface rounded text-xs">${escapeHtml(metadata.seed || 'Unknown')}</span>
</div>
</div>
</div>
<!-- ComfyUI Workflow -->
${metadata.workflow ? `
<div class="space-y-3">
<h2 class="text-sm font-medium text-pm-secondary">ComfyUI Workflow</h2>
<div class="space-y-2">
<button class="text-xs text-pm-accent hover:text-pm-accent underline flex items-center gap-1" data-action="show-workflow">
<span>View Raw Workflow JSON</span>
<svg class="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 6H6a2 2 0 00-2 2v10a2 2 0 002 2h10a2 2 0 002-2v-4M14 4h6m0 0v6m0-6L10 14"></path>
</svg>
</button>
<button class="text-xs text-pm-accent hover:text-pm-accent underline flex items-center gap-1" data-action="download-workflow">
<span>Download Workflow</span>
<svg class="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 10v6m0 0l-3-3m3 3l3-3m2 8H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"></path>
</svg>
</button>
</div>
</div>
` : ''}
`;
container.innerHTML = html;
// Store metadata for actions
this.currentMetadata = metadata;
// Add click handlers
this.setupMetadataActions(container);
}
setupImageNavigation(modal, currentImageUrl) {
// Find current image index
this.currentImageIndex = this.images.findIndex(img => img.url === currentImageUrl);
const prevBtn = modal.querySelector('#prevImageBtn');
const nextBtn = modal.querySelector('#nextImageBtn');
const modalImage = modal.querySelector('#modalImage');
const updateImage = async (newIndex) => {
if (newIndex >= 0 && newIndex < this.images.length) {
this.currentImageIndex = newIndex;
const newImage = this.images[newIndex];
// ALWAYS use original URL for viewing and metadata
modalImage.src = newImage.url; // This is always the original URL
modalImage.alt = this.formatImageCaption(newImage);
// Update metadata using original image
const metadataContainer = modal.querySelector('#metadata-content');
metadataContainer.innerHTML = `
<div class="text-center text-pm-muted py-8">
<div class="w-8 h-8 border-2 border-pm-accent/30 border-t-pm-accent rounded-full animate-spin mx-auto mb-2"></div>
<p class="text-sm">Loading metadata from original image...</p>
</div>
`;
// Load metadata from original image (which has embedded workflow data)
await this.loadImageMetadata(newImage.url, metadataContainer);
}
// Update button states
prevBtn.style.opacity = newIndex > 0 ? '1' : '0.5';
nextBtn.style.opacity = newIndex < this.images.length - 1 ? '1' : '0.5';
};
prevBtn.addEventListener('click', () => {
if (this.currentImageIndex > 0) {
updateImage(this.currentImageIndex - 1);
}
});
nextBtn.addEventListener('click', () => {
if (this.currentImageIndex < this.images.length - 1) {
updateImage(this.currentImageIndex + 1);
}
});
// Keyboard navigation
const keyHandler = (e) => {
if (e.key === 'ArrowLeft' && this.currentImageIndex > 0) {
updateImage(this.currentImageIndex - 1);
} else if (e.key === 'ArrowRight' && this.currentImageIndex < this.images.length - 1) {
updateImage(this.currentImageIndex + 1);
}
};
document.addEventListener('keydown', keyHandler);
// 'remove' is not a DOM event; watch for the modal leaving the document instead
const navObserver = new MutationObserver(() => {
if (modal.isConnected) return;
document.removeEventListener('keydown', keyHandler);
navObserver.disconnect();
});
navObserver.observe(document.body, { childList: true });
// Initial button state
updateImage(this.currentImageIndex);
}
setupMetadataActions(container) {
// Copy prompt buttons
const copyButtons = container.querySelectorAll('[data-copy-type]');
copyButtons.forEach(btn => {
btn.addEventListener('click', (e) => {
const type = btn.getAttribute('data-copy-type');
if (this.currentMetadata) {
const text = type === 'positive' ? this.currentMetadata.positivePrompt : this.currentMetadata.negativePrompt;
this.copyToClipboard(text);
}
});
});
// File path copy
const pathEl = container.querySelector('[data-copy-path]');
if (pathEl) {
pathEl.addEventListener('click', () => {
if (this.currentMetadata) {
this.copyToClipboard(this.currentMetadata.imagePath);
}
});
}
// Show more buttons
const showMoreBtns = container.querySelectorAll('[data-show-type]');
showMoreBtns.forEach(btn => {
btn.addEventListener('click', (e) => {
const type = btn.getAttribute('data-show-type');
this.showFullPrompt(type);
});
});
// Workflow action buttons
const actionBtns = container.querySelectorAll('[data-action]');
actionBtns.forEach(btn => {
btn.addEventListener('click', (e) => {
const action = btn.getAttribute('data-action');
if (action === 'show-workflow') {
this.showWorkflowData();
} else if (action === 'download-workflow') {
this.downloadWorkflowJSON();
}
});
});
// Copy all button
const copyAllBtn = container.closest('.metadata-sidebar').querySelector('.metadata-copy-all');
if (copyAllBtn) {
copyAllBtn.addEventListener('click', () => {
this.copyAllMetadata();
});
}
}
showFullPrompt(type) {
if (this.currentMetadata) {
const prompt = type === 'positive' ? this.currentMetadata.positivePrompt : this.currentMetadata.negativePrompt;
const safeType = escapeHtml(type.charAt(0).toUpperCase() + type.slice(1));
const newWindow = window.open('', '_blank');
const doc = newWindow.document;
doc.open();
doc.write('<!DOCTYPE html><html><head><title>' + safeType + ' Prompt</title></head><body></body></html>');
doc.close();
doc.body.style.cssText = 'background:#111;color:#fff;font-family:monospace;padding:20px;';
const h2 = doc.createElement('h2');
h2.textContent = type.charAt(0).toUpperCase() + type.slice(1) + ' Prompt';
doc.body.appendChild(h2);
const pre = doc.createElement('pre');
pre.style.cssText = 'background:#222;padding:15px;border-radius:5px;white-space:pre-wrap;line-height:1.5;';
pre.textContent = prompt;
doc.body.appendChild(pre);
const btn = doc.createElement('button');
btn.textContent = 'Copy to Clipboard';
btn.style.cssText = 'margin-top:20px;padding:10px 20px;background:#444;color:#fff;border:none;border-radius:5px;cursor:pointer;';
btn.addEventListener('click', () => { navigator.clipboard.writeText(pre.textContent).then(() => alert('Copied!')); });
doc.body.appendChild(btn);
}
}
showWorkflowData() {
if (this.currentMetadata && this.currentMetadata.workflow) {
const newWindow = window.open('', '_blank');
if (!newWindow) {
this.showNotification('Popup blocked: allow popups to view the workflow', 'warning');
return;
}
newWindow.document.write(ImageHelpers.workflowDocumentHtml(this.currentMetadata.workflow));
newWindow.document.close();
}
}
async copyToClipboard(text) {
try {
await navigator.clipboard.writeText(text);
this.showNotification('Copied to clipboard!', 'success');
} catch (err) {
console.error('Copy failed:', err);
this.showNotification('Copy failed', 'error');
}
}
copyAllMetadata() {
if (!this.currentMetadata) return;
const allData = `Checkpoint: ${this.currentMetadata.checkpoint || 'Unknown'}
Positive Prompt: ${this.currentMetadata.positivePrompt}
Negative Prompt: ${this.currentMetadata.negativePrompt}
Steps: ${this.currentMetadata.steps || 'Unknown'}
CFG Scale: ${this.currentMetadata.cfgScale || 'Unknown'}
Sampler: ${this.currentMetadata.sampler || 'Unknown'}
Seed: ${this.currentMetadata.seed || 'Unknown'}`;
this.copyToClipboard(allData);
}
downloadWorkflowJSON() {
if (this.currentMetadata && this.currentMetadata.workflow) {
const dataStr = JSON.stringify(this.currentMetadata.workflow, null, 2);
const dataBlob = new Blob([dataStr], {type: 'application/json'});
const url = URL.createObjectURL(dataBlob);
const link = document.createElement('a');
link.href = url;
link.download = 'comfyui_workflow.json';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(url);
this.showNotification('Workflow downloaded!', 'success');
}
}
formatPromptText(text) {
if (!text || text === 'No prompt found' || text === 'No negative prompt found') {
return `<span class="text-pm-muted italic">${text || 'No prompt found'}</span>`;
}
return escapeHtml(text);
}
unescapeHtml(text) {
const div = document.createElement('div');
div.innerHTML = text;
return div.textContent;
}
formatImageCaption(image) {
const date = new Date(image.modified_time * 1000);
return `${image.filename} | ${this.formatFileSize(image.size)} | ${date.toLocaleString()}`;
}
async extractImageMetadata(imageUrl) {
try {
// Fetch the image as an array buffer
const response = await fetch(imageUrl);
const arrayBuffer = await response.arrayBuffer();
// Parse PNG metadata
const metadata = await this.parsePNGMetadata(arrayBuffer);
const comfyData = this.extractComfyUIData(metadata);
return this.parseWorkflowData(comfyData, imageUrl);
} catch (error) {
console.error('Error extracting metadata:', error);
return null;
}
}
parsePNGMetadata(arrayBuffer) {
return PngMetadata.parsePngTextChunks(arrayBuffer);
}
extractComfyUIData(metadata) {
let workflowData = null;
let promptData = null;
// Common ComfyUI metadata field names
const workflowFields = ['workflow', 'Workflow', 'comfy', 'ComfyUI'];
const promptFields = ['prompt', 'Prompt', 'parameters', 'Parameters'];
for (const field of workflowFields) {
if (metadata[field]) {
try {
// Clean NaN values from JSON string before parsing
let cleanedJson = metadata[field];
cleanedJson = cleanedJson.replace(/:\s*NaN\b/g, ': null');
cleanedJson = cleanedJson.replace(/\bNaN\b/g, 'null');
workflowData = JSON.parse(cleanedJson);
break;
} catch (e) {
console.warn('Failed to parse workflow field:', field);
}
}
}
for (const field of promptFields) {
if (metadata[field]) {
try {
// Clean NaN values from JSON string before parsing
let cleanedJson = metadata[field];
cleanedJson = cleanedJson.replace(/:\s*NaN\b/g, ': null');
cleanedJson = cleanedJson.replace(/\bNaN\b/g, 'null');
promptData = JSON.parse(cleanedJson);
break;
} catch (e) {
console.warn('Failed to parse prompt field:', field);
}
}
}
return { workflow: workflowData, prompt: promptData };
}
parseWorkflowData(comfyData, imagePath) {
// Trace prompts, model and sampler settings through the node graph (#75)
const {
checkpoint, positivePrompt, negativePrompt, steps, cfgScale, sampler, seed,
} = window.ComfyMetadata.extractGenerationParams(comfyData);
return {
checkpoint,
positivePrompt,
negativePrompt,
steps,
cfgScale,
sampler,
seed,
workflow: comfyData.workflow,
imagePath
};
}
// Settings functionality
showSettings() {
this.loadSettingsFromStorage();
document.getElementById('settingsModal').classList.remove('hidden');
document.getElementById('settingsModal').classList.add('flex');
document.body.style.overflow = 'hidden';
}
hideSettings() {
document.getElementById('settingsModal').classList.add('hidden');
document.getElementById('settingsModal').classList.remove('flex');
document.body.style.overflow = '';
}
loadSettingsFromStorage() {
const settings = this.getSettings();
// Load checkbox settings
document.getElementById('lazyLoadingToggle').checked = settings.lazyLoading;
document.getElementById('autoLoadMetadataToggle').checked = settings.autoLoadMetadata;
document.getElementById('cacheMetadataToggle').checked = settings.cacheMetadata;
document.getElementById('showFilePathsToggle').checked = settings.showFilePaths;
document.getElementById('sidebarCollapsedByDefaultToggle').checked = settings.sidebarCollapsedByDefault;
document.getElementById('infiniteScrollToggle').checked = settings.infiniteScroll;
document.getElementById('showImageInfoToggle').checked = settings.showImageInfo;
document.getElementById('debugModeToggle').checked = settings.debugMode;
document.getElementById('checkThumbnailsAtStartup').checked = settings.checkThumbnailsAtStartup;
// Load video settings
document.getElementById('videoAutoplayToggle').checked = settings.videoAutoplay;
document.getElementById('videoMuteToggle').checked = settings.videoMute;
document.getElementById('videoLoopToggle').checked = settings.videoLoop;
// Load select settings
document.getElementById('imageQualitySelect').value = settings.imageQuality;
document.getElementById('defaultViewModeSelect').value = settings.defaultViewMode;
document.getElementById('defaultLimitSelect').value = settings.defaultLimit;
document.getElementById('gridColumnsSelect').value = settings.gridColumns;
// Load input settings
document.getElementById('apiTimeoutInput').value = settings.apiTimeout;
// Update thumbnail status
document.getElementById('thumbnailStatus').textContent = settings.thumbnailsGenerated ? 'Generated' : 'Not generated';
}
getSettings() {
let stored = null;
try {
const raw = localStorage.getItem('gallerySettings');
if (raw) stored = JSON.parse(raw);
} catch (e) {
console.warn('Failed to load settings from localStorage:', e);
}
return GallerySettings.normalizeGallerySettings(stored);
}
saveSettings() {
const settings = {
lazyLoading: document.getElementById('lazyLoadingToggle').checked,
imageQuality: document.getElementById('imageQualitySelect').value,
defaultViewMode: document.getElementById('defaultViewModeSelect').value,
defaultLimit: parseInt(document.getElementById('defaultLimitSelect').value),
gridColumns: parseInt(document.getElementById('gridColumnsSelect').value),
showImageInfo: document.getElementById('showImageInfoToggle').checked,
autoLoadMetadata: document.getElementById('autoLoadMetadataToggle').checked,
cacheMetadata: document.getElementById('cacheMetadataToggle').checked,
showFilePaths: document.getElementById('showFilePathsToggle').checked,
sidebarCollapsedByDefault: document.getElementById('sidebarCollapsedByDefaultToggle').checked,
infiniteScroll: document.getElementById('infiniteScrollToggle').checked,
debugMode: document.getElementById('debugModeToggle').checked,
apiTimeout: parseInt(document.getElementById('apiTimeoutInput').value),
thumbnailsGenerated: this.getSettings().thumbnailsGenerated, // Preserve this
checkThumbnailsAtStartup: document.getElementById('checkThumbnailsAtStartup').checked,
// Video settings
videoAutoplay: document.getElementById('videoAutoplayToggle').checked,
videoMute: document.getElementById('videoMuteToggle').checked,
videoLoop: document.getElementById('videoLoopToggle').checked
};
try {
localStorage.setItem('gallerySettings', JSON.stringify(settings));
this.showNotification('Settings saved successfully!', 'success');
this.hideSettings();
// Apply some settings immediately
this.applySettings(settings);
} catch (e) {
console.error('Failed to save settings:', e);
this.showNotification('Failed to save settings', 'error');
}
}
resetSettings() {
if (confirm('Reset all settings to defaults? This cannot be undone.')) {
localStorage.removeItem('gallerySettings');
this.loadSettingsFromStorage();
this.showNotification('Settings reset to defaults', 'success');
}
}
applySettings(settings) {
// Runs at startup and after Save, so every display setting takes effect
// without a reload. Settings that change what is rendered re-render.
const loaded = this.images.length > 0;
this.updateGridColumns(settings.gridColumns);
const limitChanged = this.limit !== settings.defaultLimit;
this.limit = settings.defaultLimit;
const scrollChanged = this.infiniteScroll !== settings.infiniteScroll;
this.infiniteScroll = settings.infiniteScroll;
const limitSelector = document.getElementById('limitSelector');
if (limitSelector) limitSelector.value = String(settings.defaultLimit);
if (this.viewMode !== settings.defaultViewMode) {
this.setViewMode(settings.defaultViewMode);
} else if (loaded && !limitChanged) {
this.renderGallery(); // e.g. "Show Media Info" toggled
}
if (loaded && (limitChanged || scrollChanged)) this.loadImages(1);
}
updateGridColumns(columns) {
const gridContainer = document.getElementById('galleryGrid');
if (!gridContainer) return;
// The Tailwind column classes are compiled for a fixed set of values and a
// wider breakpoint overrides them; an explicit template wins on desktop widths.
gridContainer.style.removeProperty('grid-template-columns'); // undo a thumb-size override
gridContainer.style.setProperty('--gallery-columns', GallerySettings.gridColumnsStyle(columns));
gridContainer.classList.add('gallery-columns-custom');
}
async generateThumbnails() {
const btn = document.getElementById('generateThumbnailsBtn');
const status = document.getElementById('thumbnailStatus');
const progressContainer = document.getElementById('thumbnailProgress');
const progressBar = document.getElementById('progressBar');
const progressText = document.getElementById('progressText');
const progressPercent = document.getElementById('progressPercent');
const progressDetails = document.getElementById('progressDetails');
const progressETA = document.getElementById('progressETA');
btn.disabled = true;
btn.textContent = 'Generating...';
status.textContent = 'Initializing thumbnail generation...';
progressContainer.classList.remove('hidden');
// Initialize progress display
progressBar.style.width = '0%';
progressPercent.textContent = 'Starting...';
progressText.textContent = 'Connecting to server...';
progressDetails.textContent = 'Preparing to scan output directory...';
progressETA.textContent = '';
try {
// Start thumbnail generation with progress updates
const result = await this.generateThumbnailsWithProgress({
quality: document.getElementById('imageQualitySelect').value
});
// Update settings on success
const settings = this.getSettings();
settings.thumbnailsGenerated = true;
localStorage.setItem('gallerySettings', JSON.stringify(settings));
status.textContent = 'Generation completed';
progressText.textContent = 'Completed!';
progressDetails.textContent = `Generated ${result.count} new, skipped ${result.skipped || 0} existing (${result.total_images} total) in ${result.elapsed_time}s`;
this.notifyThumbnailsDone(result);
// Hide progress after a delay
setTimeout(() => {
progressContainer.classList.add('hidden');
}, 3000);
} catch (error) {
console.error('Thumbnail generation error:', error);
console.error('Error details:', error.stack);
status.textContent = 'Generation failed';
progressText.textContent = 'Failed';
progressDetails.textContent = error.message;
progressBar.style.width = '0%';
progressBar.classList.add('bg-pm-error');
progressPercent.textContent = 'Error';
this.showNotification('Failed to generate thumbnails: ' + error.message, 'error');
// Hide progress after a delay
setTimeout(() => {
progressContainer.classList.add('hidden');
progressBar.classList.remove('bg-pm-error');
}, 5000);
} finally {
btn.disabled = false;
btn.textContent = 'Generate';
}
}
async generateThumbnailsWithProgress(options) {
const progressBar = document.getElementById('progressBar');
const progressText = document.getElementById('progressText');
const progressPercent = document.getElementById('progressPercent');
const progressDetails = document.getElementById('progressDetails');
const progressETA = document.getElementById('progressETA');
const cancelBtn = document.getElementById('cancelThumbnailsBtn');
return new Promise((resolve, reject) => {
// Show cancel button
cancelBtn.classList.remove('hidden');
// Set up Server-Sent Events
// The progress route is POST (it generates files), so read it with SseStream like the autotag streams
const eventSource = SseStream.connect(`/prompt_manager/images/generate-thumbnails/progress?quality=${encodeURIComponent(options.quality)}`, { method: 'POST' });
// Log connection
let cancelled = false;
let resultData = null;
// Handle cancellation
const cancelHandler = () => {
cancelled = true;
eventSource.close();
cancelBtn.classList.add('hidden');
reject(new Error('Thumbnail generation cancelled by user'));
};
cancelBtn.addEventListener('click', cancelHandler);
// Handle scanning/status events
eventSource.addEventListener('status', (event) => {
if (cancelled) return;
const data = JSON.parse(event.data);
progressText.textContent = data.message || 'Processing...';
if (data.phase === 'scanning') {
progressDetails.textContent = 'Scanning output folder for images and videos...';
progressBar.style.width = '0%';
progressPercent.textContent = 'Scanning...';
} else {
progressDetails.textContent = 'Preparing thumbnail generation...';
}
// Log status updates
if (this.getSettings().debugMode) {
}
});
// Handle start event
eventSource.addEventListener('start', (event) => {
if (cancelled) return;
const data = JSON.parse(event.data);
// Show detailed file type breakdown if available
const fileBreakdown = (data.image_count !== undefined && data.video_count !== undefined)
? ` (${data.image_count} images, ${data.video_count} videos)`
: '';
progressText.textContent = `Starting to process ${data.total_images} media files${fileBreakdown}...`;
progressDetails.textContent = data.message || `Found ${data.total_images} images and videos to process`;
progressBar.style.width = '0%';
progressPercent.textContent = '0%';
progressETA.textContent = 'Calculating...';
// Log start event
if (this.getSettings().debugMode) {
}
});
// Handle progress updates
eventSource.addEventListener('progress', (event) => {
if (cancelled) return;
const data = JSON.parse(event.data);
// Update progress bar
progressBar.style.width = `${data.percentage}%`;
progressPercent.textContent = `${data.percentage}%`;
// Update text with current file being processed and action
const fileInfo = data.current_file ? ` - ${data.current_file}` : '';
const actionText = data.action === 'skipped' ? ' [Skipping]' : data.action === 'generating' ? ' [Generating]' : '';
progressText.textContent = `Processing ${data.file_type || 'file'}... (${data.processed}/${data.total_images})${fileInfo}${actionText}`;
// More detailed progress information
const elapsedText = data.elapsed ? ` | Time: ${Math.floor(data.elapsed)}s` : '';
const failedText = data.error_count ? `, Failed: ${data.error_count}` : '';
progressDetails.textContent = `Generated: ${data.generated}, Skipped: ${data.skipped}${failedText} | Rate: ${data.rate} img/s${elapsedText}`;
// Update ETA
if (data.eta > 0 && data.eta < 3600) {
const etaText = data.eta > 60 ? `${Math.floor(data.eta / 60)}m ${Math.floor(data.eta % 60)}s` : `${Math.floor(data.eta)}s`;
progressETA.textContent = `ETA: ${etaText}`;
} else {
progressETA.textContent = '';
}
// Log to console for debugging if debug mode is enabled
if (this.getSettings().debugMode) {
}
});
// Handle completion
eventSource.addEventListener('complete', (event) => {
if (cancelled) return;
resultData = JSON.parse(event.data);
// Update final progress
progressBar.style.width = '100%';
progressPercent.textContent = '100%';
progressText.textContent = 'Completed!';
// Show detailed completion message
const errorCount = resultData.error_count || (resultData.errors ? resultData.errors.length : 0);
const errors = errorCount > 0 ? ` (${errorCount} errors)` : '';
const rateText = resultData.processing_rate ? ` at ${resultData.processing_rate} img/s` : '';
progressDetails.textContent = `Generated: ${resultData.count} new, Skipped: ${resultData.skipped} existing | Total: ${resultData.total_images} files | Time: ${resultData.elapsed_time}s${rateText}${errors}`;
// Show first error if any occurred
if (resultData.errors && resultData.errors.length > 0 && this.getSettings().debugMode) {
console.error('First thumbnail error:', resultData.errors[0]);
}
progressETA.textContent = 'Done';
// Log completion details
if (this.getSettings().debugMode) {
if (resultData.errors && resultData.errors.length > 0) {
console.warn('Thumbnail generation errors:', resultData.errors);
}
}
// Clean up
eventSource.close();
cancelBtn.removeEventListener('click', cancelHandler);
cancelBtn.classList.add('hidden');
resolve(resultData);
});
// Per-file failures: count them, keep listening
eventSource.addEventListener('file_error', (event) => {
if (cancelled) return;
try {
const data = JSON.parse(event.data);
console.warn('Thumbnail failed:', data.current_file || data.file || '', data.error || '');
} catch (e) {
console.warn('Thumbnail failed (unparseable event)');
}
});
// Handle errors
eventSource.addEventListener('error', (event) => {
if (cancelled) return;
const data = JSON.parse(event.data);
// Clean up
eventSource.close();
cancelBtn.removeEventListener('click', cancelHandler);
cancelBtn.classList.add('hidden');
reject(new Error(data.error || 'Thumbnail generation failed'));
});
// Handle EventSource errors
eventSource.onerror = (error) => {
if (cancelled) return;
console.error('EventSource error:', error);
console.error('EventSource readyState:', eventSource.readyState);
// Check if connection is closing normally
if (eventSource.readyState === eventSource.CLOSED) {
} else {
console.error('EventSource connection failed');
}
// Clean up
eventSource.close();
cancelBtn.removeEventListener('click', cancelHandler);
cancelBtn.classList.add('hidden');
// Only reject if we don't have result data
if (!resultData) {
reject(new Error('Connection to server lost during thumbnail generation'));
}
};
// Stream ended without a complete/error frame (close() does not trigger this)
eventSource.onclose = () => {
if (cancelled || resultData) return;
cancelBtn.removeEventListener('click', cancelHandler);
cancelBtn.classList.add('hidden');
reject(new Error('Connection to server closed before thumbnail generation finished'));
};
});
}
async clearCache() {
if (confirm('Clear PromptManager cache and thumbnails? This will NOT affect your original images.')) {
try {
// Clear only PromptManager-related browser caches (never touch user images)
if ('caches' in window) {
const cacheNames = await caches.keys();
// Only clear caches that are specifically ours
const ourCaches = cacheNames.filter(name =>
name.includes('prompt-manager') ||
name.includes('gallery') ||
name.includes('thumbnail')
);
await Promise.all(ourCaches.map(name => caches.delete(name)));
}
// Clear only our localStorage entries (never touch user data)
Object.keys(localStorage).forEach(key => {
if (key.startsWith('gallery_cache_') ||
key.startsWith('metadata_cache_') ||
key.startsWith('promptManager_') ||
key.startsWith('pm_')) {
localStorage.removeItem(key);
}
});
// Call backend to clear server-side thumbnails (only our generated thumbnails)
try {
const response = await fetch('/prompt_manager/images/clear-thumbnails', {
method: 'POST',
headers: { 'Content-Type': 'application/json' }
});
if (response.ok) {
const data = await response.json();
}
} catch (e) {
console.warn('Could not clear server thumbnails:', e);
// Not a critical error - continue with local cleanup
}
// Update thumbnail status
const settings = this.getSettings();
settings.thumbnailsGenerated = false;
localStorage.setItem('gallerySettings', JSON.stringify(settings));
document.getElementById('thumbnailStatus').textContent = 'Not generated';
this.showNotification('PromptManager cache cleared successfully! Original images untouched.', 'success');
} catch (error) {
console.error('Clear cache error:', error);
this.showNotification('Failed to clear cache', 'error');
}
}
}
async rescanFolder() {
const btn = document.getElementById('rescanFolderBtn');
btn.disabled = true;
btn.textContent = 'Scanning...';
try {
// The listing is cached on the server; ask it to rewalk the folders first
const result = await this.api.post('/prompt_manager/gallery/rescan', {});
this.hideSettings();
await Promise.all([this.loadFolderTree(), this.loadImages(1)]);
const where = result.roots === 1 ? 'the output folder' : `${result.roots} folders`;
this.showNotification(`Rescanned ${where}: ${result.total} files`, 'success');
} catch (error) {
console.error('Rescan error:', error);
this.showNotification('Failed to rescan folder', 'error');
} finally {
btn.disabled = false;
btn.textContent = 'Rescan';
}
}
// ── Duplicate scan ──────────────────────────────────────────────
// The scan runs on the server (POST /scan_duplicates/stream starts or attaches).
// Closing the modal only hides it: a pill shows progress and reopens it, and a
// reload re-attaches to a scan that is still running.
scanDuplicates() {
if (this.dupScan.status === ScanProgress.RUNNING) {
this.openDuplicatesModal();
return;
}
this.dupResults = null;
this.dupScan = ScanProgress.reduceScanEvent(ScanProgress.initialState(), {
type: 'progress', progress: 0, status: 'Starting...', processed: 0, found: 0,
});
this.setDuplicateButton(true);
this.openDuplicatesModal();
this.attachDuplicateStream();
}
attachDuplicateStream() {
if (this.dupSource) this.dupSource.close();
const source = SseStream.connect('/prompt_manager/scan_duplicates/stream', { method: 'POST', body: '{}' });
this.dupSource = source;
source.onmessage = (e) => {
let data;
try {
data = JSON.parse(e.data);
} catch (_) {
return;
}
this.applyDuplicateEvent(data);
};
source.onerror = () => {
if (this.dupSource !== source || this.dupScan.status !== ScanProgress.RUNNING) return;
setTimeout(() => this.resumeDuplicateScan(), 2000);
};
source.onclose = () => {
if (this.dupSource === source && this.dupScan.status === ScanProgress.RUNNING) this.resumeDuplicateScan();
};
}
async resumeDuplicateScan() {
let status;
try {
const response = await fetch('/prompt_manager/scan_duplicates/status');
if (!response.ok) return;
status = await response.json();
} catch (_) {
return;
}
const state = ScanProgress.fromStatus(status);
if (state.status !== ScanProgress.RUNNING) {
if (this.dupScan.status === ScanProgress.RUNNING) {
// The scan we were following ended while we were away
const last = status.last_event;
const terminal = last && (last.type === 'complete' || last.type === 'error');
this.applyDuplicateEvent(terminal ? last : { type: 'error', message: 'the server no longer reports a running scan' });
}
return;
}
this.dupScan = state;
this.setDuplicateButton(true);
this.renderDuplicateProgress();
if (document.getElementById('duplicatesModal').classList.contains('hidden')) this.showDuplicatePill();
this.attachDuplicateStream();
}
applyDuplicateEvent(data) {
const next = ScanProgress.reduceScanEvent(this.dupScan, data);
if (next === this.dupScan) return;
this.dupScan = next;
this.renderDuplicateProgress();
if (next.status === ScanProgress.DONE) {
this.dupResults = Array.isArray(data.duplicates) ? data.duplicates : [];
this.setDuplicateButton(false);
const modalOpen = !document.getElementById('duplicatesModal').classList.contains('hidden');
if (modalOpen) {
this.hideDuplicatePill();
this.displayDuplicates(this.dupResults);
} else {
this.showDuplicatePill(`Duplicate scan done: ${this.dupResults.length} groups · click to view`);
}
this.showNotification(`Found ${this.dupResults.length} duplicate image groups`, 'info');
} else if (next.status === ScanProgress.FAILED) {
this.setDuplicateButton(false);
this.hideDuplicatePill();
this.hideDuplicatesModal();
this.showNotification(`Duplicate scan failed: ${next.message}`, 'error');
}
}
renderDuplicateProgress() {
const state = this.dupScan;
const bar = document.getElementById('dupProgressBar');
const text = document.getElementById('dupProgressText');
if (bar) bar.style.width = `${state.progress}%`;
if (text) text.textContent = state.status === ScanProgress.RUNNING ? `${state.progress}% · ${state.processed} files hashed` : state.statusText;
const pill = document.getElementById('dupScanPillText');
if (pill && state.status === ScanProgress.RUNNING) pill.textContent = `Finding duplicates ${state.progress}% · ${state.processed} files`;
}
setDuplicateButton(running) {
const btn = document.getElementById('scanDuplicatesBtn');
if (!btn) return;
btn.disabled = running;
btn.textContent = running ? 'Scanning...' : 'Scan';
}
showDuplicatePill(label) {
const pill = document.getElementById('dupScanPill');
if (label) document.getElementById('dupScanPillText').textContent = label;
pill.classList.remove('hidden');
}
hideDuplicatePill() {
document.getElementById('dupScanPill').classList.add('hidden');
}
/** Show the modal in whatever state the scan is in: progress, or results. */
openDuplicatesModal() {
this.hideDuplicatePill();
document.getElementById('duplicatesModal').classList.remove('hidden');
document.getElementById('duplicatesModal').classList.add('flex');
document.body.style.overflow = 'hidden';
if (this.dupScan.status === ScanProgress.DONE && this.dupResults) {
this.displayDuplicates(this.dupResults);
return;
}
document.getElementById('duplicatesScanStatus').classList.remove('hidden');
document.getElementById('duplicatesContent').classList.add('hidden');
document.getElementById('duplicatesFooter').classList.add('hidden');
this.renderDuplicateProgress();
}
showDuplicatesModal() {
this.openDuplicatesModal();
}
hideDuplicatesModal() {
document.getElementById('duplicatesModal').classList.add('hidden');
document.getElementById('duplicatesModal').classList.remove('flex');
document.body.style.overflow = '';
// Minimize rather than abandon: the scan keeps running on the server
if (this.dupScan.status === ScanProgress.RUNNING) this.showDuplicatePill();
}
displayDuplicates(duplicates) {
const scanStatus = document.getElementById('duplicatesScanStatus');
const content = document.getElementById('duplicatesContent');
const footer = document.getElementById('duplicatesFooter');
const list = document.getElementById('duplicatesList');
const count = document.getElementById('duplicatesCount');
scanStatus.classList.add('hidden');
if (duplicates.length === 0) {
list.innerHTML = '<div class="text-center py-4 text-pm-secondary">No duplicate images found! 🎉</div>';
content.classList.remove('hidden');
return;
}
count.textContent = duplicates.length;
list.innerHTML = '';
duplicates.forEach((group, groupIndex) => {
const groupEl = document.createElement('div');
groupEl.className = 'bg-pm-surface rounded-pm-md p-4 border border-pm';
groupEl.innerHTML = `
<div class="mb-3 text-sm font-medium text-pm-secondary">
Duplicate Group ${groupIndex + 1} (${group.images.length} identical ${escapeHtml(group.images[0].media_type)}s)
</div>
<div class="mb-3 p-3 bg-pm-primary rounded text-sm text-pm-secondary">
<strong>Content Hash:</strong> ${escapeHtml(String(group.hash || '').substring(0, 16))}...
</div>
<div class="grid grid-cols-1 lg:grid-cols-2 gap-3">
${group.images.map((image, imageIndex) => `
<div class="flex items-start p-3 bg-pm-primary rounded border border-pm ${imageIndex === 0 ? 'border-pm-success' : ''}">
<div class="flex-shrink-0 mr-3">
${image.media_type === 'video' ? `
<div class="w-20 h-20 bg-pm-input rounded flex items-center justify-center relative">
${image.thumbnail_url ? `
<img src="${escapeHtml(image.thumbnail_url)}" alt="Video thumbnail" class="w-full h-full object-cover rounded">
` : `
<svg class="w-8 h-8 text-pm-secondary" fill="currentColor" viewBox="0 0 20 20">
<path d="M2 6a2 2 0 012-2h6l2 2h6a2 2 0 012 2v6a2 2 0 01-2 2H4a2 2 0 01-2-2V6zM14.553 7.106A1 1 0 0014 8v4a1 1 0 00.553.894l2 1A1 1 0 0018 13V7a1 1 0 00-1.447-.894l-2 1z"/>
</svg>
`}
<div class="absolute bottom-0 right-0 bg-black/75 text-pm text-xs px-1 rounded">VIDEO</div>
</div>
` : `
<img src="${escapeHtml(image.thumbnail_url || image.url)}" alt="${escapeHtml(image.filename)}"
class="w-20 h-20 object-cover rounded cursor-pointer hover:opacity-80"
onclick="this.parentElement.parentElement.querySelector('.image-preview').click()">
`}
</div>
<div class="flex-1 min-w-0">
<div class="text-sm text-pm-secondary mb-2">
<div class="font-medium truncate" title="${escapeHtml(image.filename)}">${escapeHtml(image.filename)}</div>
<div class="text-xs text-pm-secondary mt-1">
<span class="font-medium">Size:</span> ${this.formatFileSize(image.size)} •
<span class="font-medium">Modified:</span> ${new Date(image.modified_time * 1000).toLocaleDateString()}
</div>
<div class="text-xs text-pm-muted mt-1 truncate" title="${escapeHtml(image.relative_path)}">
${escapeHtml(image.relative_path)}
</div>
</div>
<div class="flex items-center justify-between">
<div class="flex items-center space-x-2">
<button class="image-preview text-xs bg-pm-accent hover:bg-pm-accent-hover text-pm px-2 py-1 rounded"
data-action="open-url" data-url="${escapeHtml(image.url)}">
View Full
</button>
</div>
<div class="flex items-center">
${imageIndex === 0 ? `
<span class="px-3 py-1 bg-pm-success text-pm text-xs rounded">KEEP (Oldest)</span>
` : `
<label class="flex items-center">
<input type="checkbox" class="duplicate-checkbox" data-group="${groupIndex}" data-image-path="${escapeHtml(image.path)}"
class="w-4 h-4 text-pm-error bg-pm-input border-pm rounded focus:ring-pm-error">
<span class="ml-2 text-sm text-pm-error">Delete</span>
</label>
`}
</div>
</div>
</div>
</div>
`).join('')}
</div>
`;
list.appendChild(groupEl);
});
content.classList.remove('hidden');
footer.classList.remove('hidden');
}
selectAllDuplicates() {
const checkboxes = document.querySelectorAll('.duplicate-checkbox');
const selectBtn = document.getElementById('selectAllBtn');
const allChecked = Array.from(checkboxes).every(cb => cb.checked);
checkboxes.forEach(cb => {
cb.checked = !allChecked;
});
selectBtn.textContent = allChecked ? 'Select All to Delete' : 'Deselect All';
}
async removeDuplicates() {
const checkboxes = document.querySelectorAll('.duplicate-checkbox:checked');
if (checkboxes.length === 0) {
this.showNotification('No duplicate images selected for removal', 'warning');
return;
}
const confirmation = confirm(`Are you sure you want to permanently delete ${checkboxes.length} duplicate image files? This action cannot be undone!`);
if (!confirmation) return;
const btn = document.getElementById('removeDuplicatesBtn');
btn.disabled = true;
btn.textContent = 'Deleting...';
try {
const imagePaths = Array.from(checkboxes).map(cb => cb.dataset.imagePath);
const response = await fetch('/prompt_manager/delete_duplicate_images', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ image_paths: imagePaths })
});
const data = await response.json();
if (data.success) {
this.showNotification(`Successfully deleted ${imagePaths.length} duplicate image files`, 'success');
this.hideDuplicatesModal();
this.loadImages(); // Refresh the gallery
} else {
throw new Error(data.error || 'Failed to delete duplicate images');
}
} catch (error) {
console.error('Remove duplicates error:', error);
this.showNotification('Failed to delete duplicate images', 'error');
} finally {
btn.disabled = false;
btn.textContent = 'Remove Selected';
}
}
formatFileSize(bytes) {
if (bytes === 0) return '0 Bytes';
const k = 1024;
const sizes = ['Bytes', 'KB', 'MB', 'GB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
}
changeLimit(newLimit) {
this.limit = newLimit;
this.loadImages(1);
}
updatePagination() {
const totalPages = Math.ceil(this.total / this.limit);
const hasPrev = this.currentPage > 1;
const hasNext = this.currentPage < totalPages;
document.getElementById('prevPageBtn').disabled = !hasPrev;
document.getElementById('nextPageBtn').disabled = !hasNext;
document.getElementById('pageInfo').textContent = `of ${totalPages}`;
const pageInput = document.getElementById('pageInput');
pageInput.value = this.currentPage;
pageInput.max = totalPages;
if (totalPages > 1 && !this.infiniteScroll) {
document.getElementById('paginationControls').classList.remove('hidden');
} else {
document.getElementById('paginationControls').classList.add('hidden');
}
}
previousPage() {
if (this.currentPage > 1) this.loadImages(this.currentPage - 1);
}
nextPage() {
const totalPages = Math.ceil(this.total / this.limit);
if (this.currentPage < totalPages) this.loadImages(this.currentPage + 1);
}
goToPage(page) {
const totalPages = Math.ceil(this.total / this.limit);
if (isNaN(page) || page < 1 || page > totalPages) {
// Reset to current page if invalid input
document.getElementById('pageInput').value = this.currentPage;
return;
}
if (page !== this.currentPage) this.loadImages(page);
}
async checkThumbnailsAtStartup() {
// Wait a moment to ensure page is fully loaded
setTimeout(async () => {
const settings = this.getSettings();
// Only check if the feature is enabled
if (!settings.checkThumbnailsAtStartup) {
console.debug('Thumbnail startup check disabled in settings');
return;
}
try {
// Check a sample of images to see if any are missing thumbnails
const response = await fetch('/prompt_manager/images/output?limit=50&offset=0');
const data = await response.json();
console.debug('Checking images for missing thumbnails:', data);
if (data.success && data.images && data.images.length > 0) {
// Count images missing thumbnails
let missingThumbnails = 0;
for (const image of data.images) {
if (!image.thumbnail_url) {
missingThumbnails++;
}
}
console.debug(`Found ${missingThumbnails} images without thumbnails in sample of ${data.images.length}`);
// If more than 10% of sampled images are missing thumbnails, show prompt
const missingPercentage = (missingThumbnails / data.images.length) * 100;
if (missingThumbnails > 0 && missingPercentage > 10) {
// Estimate total missing based on sample
const estimatedMissing = Math.round((missingThumbnails / data.images.length) * data.total);
console.debug(`Estimated ${estimatedMissing} total images missing thumbnails (${missingPercentage.toFixed(1)}% of sample)`);
this.showThumbnailGenerationPrompt(data.total, estimatedMissing);
} else if (missingThumbnails > 0) {
console.debug(`Only ${missingPercentage.toFixed(1)}% of images missing thumbnails, not showing prompt`);
} else {
console.debug('All sampled images have thumbnails');
}
} else {
console.debug('No images found or failed to fetch images');
}
} catch (error) {
console.debug('Could not check for missing thumbnails at startup:', error);
}
}, 1000); // Wait 1 second for page to settle
}
showThumbnailGenerationPrompt(imageCount, missingCount = imageCount) {
const modal = document.createElement('div');
modal.className = 'fixed inset-0 bg-black/50 flex items-center justify-center z-50';
modal.innerHTML = `
<div class="bg-pm-surface rounded-pm-md max-w-2xl w-full mx-4 border border-pm">
<div class="p-4">
<h3 class="text-sm font-semibold text-pm mb-3 flex items-center">
<span class="mr-2">🚀</span>
Generate Thumbnails?
</h3>
<p class="text-pm-secondary mb-4">
Found ${missingCount} images without thumbnails (${imageCount} total images). Would you like to generate thumbnails for faster loading?
</p>
<p class="text-sm text-pm-muted mb-3">
💡 Thumbnails speed up gallery loading significantly. You can change this setting in Gallery Settings.
</p>
<div class="flex space-x-3">
<button id="thumbnailPromptNo" class="flex-1 px-4 py-2 bg-pm-input hover:bg-pm-hover text-pm rounded-pm-md transition-colors">
Not Now
</button>
<button id="thumbnailPromptYes" class="flex-1 px-4 py-2 bg-pm-accent hover:bg-pm-accent-hover text-pm rounded-pm-md transition-colors">
Generate
</button>
</div>
</div>
</div>
`;
document.body.appendChild(modal);
// Handle button clicks
document.getElementById('thumbnailPromptNo').addEventListener('click', () => {
document.body.removeChild(modal);
});
document.getElementById('thumbnailPromptYes').addEventListener('click', () => {
// Don't remove modal yet - transform it to show progress
this.startThumbnailGenerationInModal(modal, imageCount, missingCount);
});
// Close on outside click
modal.addEventListener('click', (e) => {
if (e.target === modal) {
document.body.removeChild(modal);
}
});
}
async startThumbnailGenerationInModal(modal, imageCount, missingCount) {
// Transform the modal to show progress
const modalContent = modal.querySelector('.bg-pm-surface');
modalContent.innerHTML = `
<div class="p-4">
<h3 class="text-sm font-semibold text-pm mb-3 flex items-center">
<span class="mr-2">🚀</span>
Generating Thumbnails
</h3>
<p class="text-pm-secondary mb-4">
Processing ${missingCount} images out of ${imageCount} total...
</p>
<!-- Progress Section -->
<div class="mb-3">
<div class="flex items-center justify-between text-sm text-pm-secondary mb-2">
<span id="modalProgressText">Initializing...</span>
<span id="modalProgressPercent">0%</span>
</div>
<div class="w-full bg-pm-input rounded-full h-3">
<div id="modalProgressBar" class="bg-pm-accent h-3 rounded-full transition-all duration-300" style="width: 0%"></div>
</div>
<div class="flex items-center justify-between text-xs text-pm-muted mt-2">
<span id="modalProgressDetails">Starting thumbnail generation...</span>
<span id="modalProgressETA" class="text-pm-muted"></span>
</div>
</div>
<!-- Status Messages -->
<div id="modalStatusMessages" class="text-sm text-pm-secondary mb-4 max-h-40 overflow-y-auto">
<div class="text-pm-accent">• Starting thumbnail generation...</div>
</div>
<!-- Buttons -->
<div class="flex space-x-3">
<button id="modalCancelBtn" class="flex-1 px-4 py-2 bg-pm-error hover:bg-pm-error text-pm rounded-pm-md transition-colors">
Cancel
</button>
<button id="modalCloseBtn" class="flex-1 px-4 py-2 bg-pm-input hover:bg-pm-hover text-pm rounded-pm-md transition-colors hidden">
Close
</button>
</div>
</div>
`;
// Setup progress tracking
let isCancelled = false;
const progressText = modal.querySelector('#modalProgressText');
const progressPercent = modal.querySelector('#modalProgressPercent');
const progressBar = modal.querySelector('#modalProgressBar');
const progressDetails = modal.querySelector('#modalProgressDetails');
const progressETA = modal.querySelector('#modalProgressETA');
const statusMessages = modal.querySelector('#modalStatusMessages');
const cancelBtn = modal.querySelector('#modalCancelBtn');
const closeBtn = modal.querySelector('#modalCloseBtn');
// Add status message helper
const addStatusMessage = (message, type = 'info') => {
const colors = {
'info': 'text-pm-secondary',
'success': 'text-pm-success',
'error': 'text-pm-error',
'warning': 'text-pm-warning'
};
const msgEl = document.createElement('div');
msgEl.className = colors[type] || colors.info;
msgEl.textContent = `• ${message}`;
statusMessages.appendChild(msgEl);
statusMessages.scrollTop = statusMessages.scrollHeight;
};
// Handle cancel
cancelBtn.addEventListener('click', () => {
isCancelled = true;
addStatusMessage('Cancellation requested...', 'warning');
cancelBtn.disabled = true;
cancelBtn.textContent = 'Cancelling...';
});
// Handle close (only appears when done)
closeBtn.addEventListener('click', () => {
document.body.removeChild(modal);
// Refresh the gallery to show new thumbnails
this.loadImages();
});
// Disable outside click while processing
modal.removeEventListener('click', modal.clickHandler);
try {
addStatusMessage('Connecting to thumbnail generation service...');
// Start thumbnail generation with progress monitoring
const thumbResult = await this.generateThumbnailsWithProgressInModal({
progressText,
progressPercent,
progressBar,
progressDetails,
progressETA,
addStatusMessage,
isCancelled: () => isCancelled
});
// Show completion
if (!isCancelled) {
progressText.textContent = 'Completed!';
progressPercent.textContent = '100%';
progressBar.style.width = '100%';
addStatusMessage('Thumbnail generation completed successfully!', 'success');
// Update settings
const settings = this.getSettings();
settings.thumbnailsGenerated = true;
localStorage.setItem('gallerySettings', JSON.stringify(settings));
// Show notification
this.notifyThumbnailsDone(thumbResult);
}
} catch (error) {
console.error('Thumbnail generation failed:', error);
addStatusMessage(`Generation failed: ${error.message}`, 'error');
progressBar.classList.add('bg-pm-error');
progressBar.classList.remove('bg-pm-accent');
} finally {
// Show close button and hide cancel
cancelBtn.classList.add('hidden');
closeBtn.classList.remove('hidden');
// Re-enable outside click to close
setTimeout(() => {
modal.addEventListener('click', (e) => {
if (e.target === modal) {
document.body.removeChild(modal);
// Refresh the gallery to show new thumbnails
this.loadImages();
}
});
}, 100);
}
} async generateThumbnailsWithProgressInModal(uiElements) {
const { progressText, progressPercent, progressBar, progressDetails, progressETA, addStatusMessage, isCancelled } = uiElements;
return new Promise((resolve, reject) => {
addStatusMessage('Starting thumbnail generation...');
// Get quality setting
const quality = this.getSettings().imageQuality || 'medium';
// Set up Server-Sent Events (same as the settings modal)
// The progress route is POST (it generates files), so read it with SseStream like the autotag streams
const eventSource = SseStream.connect(`/prompt_manager/images/generate-thumbnails/progress?quality=${encodeURIComponent(quality)}`, { method: 'POST' });
let resultData = null;
// Handle scanning/status events
eventSource.addEventListener('status', (event) => {
if (isCancelled()) {
eventSource.close();
return;
}
const data = JSON.parse(event.data);
if (data.phase === 'scanning') {
progressText.textContent = 'Scanning for images...';
progressDetails.textContent = data.message || 'Scanning output folder for images and videos...';
addStatusMessage(data.message || 'Scanning output folder...', 'info');
} else if (data.phase === 'processing' && data.message.includes('Error')) {
addStatusMessage(data.message, 'error');
} else {
progressDetails.textContent = data.message || 'Preparing...';
}
});
// Handle start event
eventSource.addEventListener('start', (event) => {
if (isCancelled()) {
eventSource.close();
return;
}
const data = JSON.parse(event.data);
// Show detailed file type breakdown
const fileBreakdown = (data.image_count !== undefined && data.video_count !== undefined)
? ` (${data.image_count} images, ${data.video_count} videos)`
: '';
progressText.textContent = `Processing ${data.total_images} files${fileBreakdown}...`;
progressDetails.textContent = data.message || `Found ${data.total_images} media files to process`;
progressBar.style.width = '0%';
progressPercent.textContent = '0%';
progressETA.textContent = 'Calculating...';
addStatusMessage(`Found ${data.total_images} media files to process${fileBreakdown}`, 'info');
});
// Handle progress updates
eventSource.addEventListener('progress', (event) => {
if (isCancelled()) {
eventSource.close();
return;
}
const data = JSON.parse(event.data);
// Update progress bar
progressBar.style.width = `${data.percentage}%`;
progressPercent.textContent = `${data.percentage}%`;
// Update text with current file and action
const actionText = data.action === 'skipped' ? ' [Skipped]' : data.action === 'generating' ? ' [Generating]' : '';
// Show full path but truncate if too long
const fullPath = data.current_file || '';
const displayPath = fullPath.length > 60 ? '...' + fullPath.slice(-57) : fullPath;
progressText.textContent = `Processing ${data.file_type || 'file'} ${data.processed}/${data.total_images}: ${displayPath}${actionText}`;
// Update details
const elapsedText = data.elapsed ? ` | Time: ${Math.floor(data.elapsed)}s` : '';
const failedText = data.error_count ? `, Failed: ${data.error_count}` : '';
progressDetails.textContent = `Generated: ${data.generated}, Skipped: ${data.skipped}${failedText} | Rate: ${data.rate} img/s${elapsedText}`;
// Update ETA
if (data.eta > 0 && data.eta < 3600) {
const etaText = data.eta > 60 ? `${Math.floor(data.eta / 60)}m ${Math.floor(data.eta % 60)}s` : `${Math.floor(data.eta)}s`;
progressETA.textContent = `ETA: ${etaText}`;
} else {
progressETA.textContent = '';
}
// Add status message periodically
if (data.processed % 50 === 0 || data.processed === data.total_images) {
addStatusMessage(`Processed ${data.processed}/${data.total_images} files (${data.generated} generated, ${data.skipped} skipped)`, 'info');
}
});
// Handle completion
eventSource.addEventListener('complete', (event) => {
if (isCancelled()) {
eventSource.close();
return;
}
resultData = JSON.parse(event.data);
// Update final progress
progressBar.style.width = '100%';
progressPercent.textContent = '100%';
progressText.textContent = 'Completed!';
// Show detailed completion message
const errorCount = resultData.error_count || (resultData.errors ? resultData.errors.length : 0);
const errors = errorCount > 0 ? ` (${errorCount} errors)` : '';
const rateText = resultData.processing_rate ? ` at ${resultData.processing_rate} img/s` : '';
progressDetails.textContent = `Generated: ${resultData.count} new, Skipped: ${resultData.skipped} | Total: ${resultData.total_images} files | Time: ${resultData.elapsed_time}s${rateText}${errors}`;
progressETA.textContent = 'Done';
// Add completion message
if (errorCount > 0) {
addStatusMessage(`Completed with ${errorCount} errors: Generated ${resultData.count} new thumbnails, skipped ${resultData.skipped} existing`, 'warning');
if (resultData.errors && resultData.errors.length > 0) {
// Show first few errors
resultData.errors.slice(0, 3).forEach(err => {
addStatusMessage(err, 'error');
});
}
} else {
addStatusMessage(`Successfully generated ${resultData.count} new thumbnails, skipped ${resultData.skipped} existing`, 'success');
}
// Clean up
eventSource.close();
resolve(resultData);
});
// Per-file failures: report them, keep listening
eventSource.addEventListener('file_error', (event) => {
if (isCancelled()) return;
let detail = 'Thumbnail failed';
try {
const data = JSON.parse(event.data);
detail = `Thumbnail failed: ${data.current_file || data.file || ''} ${data.error || ''}`.trim();
} catch (e) {
// non-JSON frame; keep the generic message
}
addStatusMessage(detail, 'error');
});
// Handle errors
eventSource.addEventListener('error', (event) => {
if (isCancelled()) {
eventSource.close();
return;
}
let errorMsg = 'Thumbnail generation failed';
try {
const data = JSON.parse(event.data);
errorMsg = data.error || errorMsg;
} catch (e) {
// If not JSON, it's a connection error
console.error('EventSource error in modal:', e);
}
addStatusMessage(errorMsg, 'error');
progressBar.classList.add('bg-pm-error');
progressBar.classList.remove('bg-pm-accent');
// Clean up
eventSource.close();
reject(new Error(errorMsg));
});
// Handle EventSource connection errors
eventSource.onerror = (error) => {
if (isCancelled()) {
eventSource.close();
return;
}
console.error('EventSource connection error in modal:', error);
// Check if connection is closing normally (after complete event)
if (eventSource.readyState === eventSource.CLOSED && resultData) {
return;
}
addStatusMessage('Connection to server lost', 'error');
progressBar.classList.add('bg-pm-error');
progressBar.classList.remove('bg-pm-accent');
// Clean up
eventSource.close();
if (!resultData) {
reject(new Error('Connection to server lost during thumbnail generation'));
}
};
// Stream ended without a complete/error frame (close() does not trigger this)
eventSource.onclose = () => {
if (isCancelled() || resultData) return;
addStatusMessage('Connection to server closed before thumbnail generation finished', 'error');
reject(new Error('Connection to server closed before thumbnail generation finished'));
};
});
}
// ==========================================
// Add Prompt Feature Methods
// ==========================================
// State for Add Prompt feature
addPromptState = {
allTags: [],
allCategories: [],
selectedTags: [],
selectedRating: null
};
async showAddPromptModal() {
// Reset form
this.resetAddPromptForm();
// Load tags and categories for autocomplete
await this.loadTagsAndCategories();
this.showModal('addPromptModal');
document.getElementById('addPromptText').focus();
}
hideAddPromptModal() {
this.hideModal('addPromptModal');
this.hideSuggestions();
}
resetAddPromptForm() {
document.getElementById('addPromptText').value = '';
document.getElementById('addPromptCategory').value = '';
document.getElementById('addPromptNotes').value = '';
document.getElementById('addPromptTagInput').value = '';
document.getElementById('addPromptProtected').checked = true;
document.getElementById('addPromptRatingValue').value = '';
this.addPromptState.selectedTags = [];
this.addPromptState.selectedRating = null;
this.renderSelectedTags();
this.updateRatingStars(0);
}
async loadTagsAndCategories() {
try {
// Load tags
const tagsResponse = await fetch('/prompt_manager/tags');
const tagsData = await tagsResponse.json();
if (tagsData.success) {
// Filter out the __protected__ tag from suggestions
this.addPromptState.allTags = tagsData.tags.filter(tag => tag !== '__protected__');
}
// Load categories
const categoriesResponse = await fetch('/prompt_manager/categories');
const categoriesData = await categoriesResponse.json();
if (categoriesData.success) {
this.addPromptState.allCategories = categoriesData.categories;
this.populateCategoryDatalist();
}
} catch (error) {
console.error('Error loading tags/categories:', error);
}
}
populateCategoryDatalist() {
const datalist = document.getElementById('categoryList');
datalist.innerHTML = this.addPromptState.allCategories
.map(cat => `<option value="${escapeHtml(cat)}">`)
.join('');
}
handleTagInput(e) {
const query = e.target.value.toLowerCase().trim();
if (query.length < 1) {
this.hideSuggestions();
return;
}
// Filter tags that match query and aren't already selected
const suggestions = this.addPromptState.allTags
.filter(tag =>
tag.toLowerCase().includes(query) &&
!this.addPromptState.selectedTags.includes(tag)
)
.slice(0, 10);
if (suggestions.length > 0) {
this.showSuggestions(suggestions);
} else {
this.hideSuggestions();
}
}
handleTagKeydown(e) {
if (e.key === 'Enter' || e.key === ',') {
e.preventDefault();
const input = e.target;
const value = input.value.replace(',', '').trim();
if (value && !this.addPromptState.selectedTags.includes(value)) {
this.addTag(value);
input.value = '';
this.hideSuggestions();
}
} else if (e.key === 'Backspace' && e.target.value === '') {
// Remove last tag on backspace with empty input
if (this.addPromptState.selectedTags.length > 0) {
this.removeTag(this.addPromptState.selectedTags.length - 1);
}
} else if (e.key === 'Escape') {
this.hideSuggestions();
} else if (e.key === 'ArrowDown' || e.key === 'ArrowUp') {
this.navigateSuggestions(e.key === 'ArrowDown' ? 1 : -1);
e.preventDefault();
}
}
showSuggestions(suggestions) {
const container = document.getElementById('tagSuggestions');
container.innerHTML = suggestions
.map((tag, index) => `
<div class="px-3 py-2 hover:bg-pm-hover cursor-pointer text-pm text-sm suggestion-item"
data-index="${index}"
data-action="select-suggestion" data-tag="${escapeHtml(tag)}">
${escapeHtml(tag)}
</div>
`)
.join('');
container.classList.remove('hidden');
}
hideSuggestions() {
document.getElementById('tagSuggestions').classList.add('hidden');
}
navigateSuggestions(direction) {
const container = document.getElementById('tagSuggestions');
const items = container.querySelectorAll('.suggestion-item');
if (items.length === 0) return;
const current = container.querySelector('.bg-pm-hover');
let nextIndex = 0;
if (current) {
current.classList.remove('bg-pm-hover');
nextIndex = parseInt(current.dataset.index) + direction;
if (nextIndex < 0) nextIndex = items.length - 1;
if (nextIndex >= items.length) nextIndex = 0;
} else if (direction === -1) {
nextIndex = items.length - 1;
}
items[nextIndex].classList.add('bg-pm-hover');
}
selectSuggestion(tag) {
this.addTag(tag);
document.getElementById('addPromptTagInput').value = '';
this.hideSuggestions();
document.getElementById('addPromptTagInput').focus();
}
addTag(tag) {
if (!this.addPromptState.selectedTags.includes(tag)) {
this.addPromptState.selectedTags.push(tag);
this.renderSelectedTags();
}
}
removeTag(index) {
this.addPromptState.selectedTags.splice(index, 1);
this.renderSelectedTags();
}
renderSelectedTags() {
const container = document.getElementById('addPromptTagsContainer');
const input = document.getElementById('addPromptTagInput');
// Remove existing tag chips (keep only the input)
container.querySelectorAll('.tag-chip').forEach(el => el.remove());
// Add tag chips before the input
this.addPromptState.selectedTags.forEach((tag, index) => {
const chip = document.createElement('span');
chip.className = 'tag-chip';
chip.innerHTML = `
${escapeHtml(tag)}
<span class="tag-remove" data-action="remove-tag" data-index="${index}">×</span>
`;
container.insertBefore(chip, input);
});
}
setRating(rating) {
this.addPromptState.selectedRating = rating;
document.getElementById('addPromptRatingValue').value = rating;
this.updateRatingStars(rating);
}
clearRating() {
this.addPromptState.selectedRating = null;
document.getElementById('addPromptRatingValue').value = '';
this.updateRatingStars(0);
}
updateRatingStars(rating) {
const stars = document.querySelectorAll('#addPromptRating .rating-star');
stars.forEach((star, index) => {
if (index < rating) {
star.textContent = '★';
star.classList.remove('text-pm-muted');
star.classList.add('text-pm-warning');
} else {
star.textContent = '☆';
star.classList.remove('text-pm-warning');
star.classList.add('text-pm-muted');
}
});
}
async saveNewPrompt() {
const text = document.getElementById('addPromptText').value.trim();
const category = document.getElementById('addPromptCategory').value.trim();
const notes = document.getElementById('addPromptNotes').value.trim();
const isProtected = document.getElementById('addPromptProtected').checked;
const rating = this.addPromptState.selectedRating;
// Validation
if (!text) {
this.showNotification('Prompt text is required', 'error');
document.getElementById('addPromptText').focus();
return;
}
// Build tags array - add __protected__ if checkbox is checked
let tags = [...this.addPromptState.selectedTags];
if (isProtected && !tags.includes('__protected__')) {
tags.push('__protected__');
}
// Disable button during save
const saveBtn = document.getElementById('saveNewPromptBtn');
saveBtn.disabled = true;
saveBtn.textContent = 'Saving...';
try {
const response = await fetch('/prompt_manager/save', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
text: text,
category: category || null,
tags: tags,
rating: rating,
notes: notes || null
})
});
const data = await response.json();
if (data.success) {
const message = data.is_duplicate
? 'Prompt already exists (updated metadata)'
: 'Prompt added successfully!';
this.showNotification(message, 'success');
this.hideAddPromptModal();
} else {
this.showNotification(`Failed to save prompt: ${data.error}`, 'error');
}
} catch (error) {
console.error('Error saving prompt:', error);
this.showNotification('Failed to save prompt', 'error');
} finally {
saveBtn.disabled = false;
saveBtn.textContent = '➕ Add Prompt';
}
}
// ==========================================
// Auto Tag Feature Methods
// ==========================================
// State for AutoTag feature
autoTagState = {
eventSource: null,
downloadEventSource: null,
reviewImages: [],
reviewIndex: 0,
currentTags: [],
cancelled: false
};
showModal(modalId) {
const modal = document.getElementById(modalId);
modal.classList.remove('hidden');
modal.classList.add('flex');
document.body.style.overflow = 'hidden';
}
hideModal(modalId) {
const modal = document.getElementById(modalId);
modal.classList.add('hidden');
modal.classList.remove('flex');
document.body.style.overflow = '';
}
async showAutoTagModal() {
this.showModal("autoTagModal");
await this.checkAutoTagModels();
}
// _setModelStatus uses innerHTML with hardcoded model keys (not user input)
// to render download buttons or status indicators for known model types
_setModelStatus(el, downloaded, modelKey) {
if (downloaded) {
el.textContent = '';
const span = document.createElement('span');
span.className = 'text-pm-success text-sm';
span.textContent = '\u2713 Downloaded';
el.appendChild(span);
} else {
el.textContent = '';
const btn = document.createElement('button');
btn.className = 'px-3 py-1 bg-pm-accent hover:bg-pm-accent-hover text-pm text-xs rounded transition-colors';
btn.textContent = 'Download';
btn.addEventListener('click', () => window.gallery.downloadModel(modelKey));
el.appendChild(btn);
}
}
async checkAutoTagModels() {
const ggufStatus = document.getElementById("ggufModelStatus");
const hfStatus = document.getElementById("hfModelStatus");
const wd14SwinV2Status = document.getElementById("wd14SwinV2ModelStatus");
const wd14VitStatus = document.getElementById("wd14VitModelStatus");
const modelLoadedStatus = document.getElementById("modelLoadedStatus");
const loadedModelType = document.getElementById("loadedModelType");
const unloadModelBtn = document.getElementById("unloadModelBtn");
const statusEls = [ggufStatus, hfStatus, wd14SwinV2Status, wd14VitStatus];
statusEls.forEach(el => { el.textContent = 'Checking...'; });
try {
const response = await fetch('/prompt_manager/autotag/models');
const data = await response.json();
if (data.success) {
this._setModelStatus(ggufStatus, data.models.gguf.downloaded, 'gguf');
this._setModelStatus(hfStatus, data.models.hf.downloaded, 'hf');
this._setModelStatus(wd14SwinV2Status, data.models['wd14-swinv2']?.downloaded, 'wd14-swinv2');
this._setModelStatus(wd14VitStatus, data.models['wd14-vit']?.downloaded, 'wd14-vit');
// Update model loaded status
if (data.model_loaded && data.loaded_model_type) {
modelLoadedStatus.classList.remove('hidden');
loadedModelType.textContent = data.loaded_model_type.toUpperCase();
unloadModelBtn.classList.remove('hidden');
} else {
modelLoadedStatus.classList.add('hidden');
unloadModelBtn.classList.add('hidden');
}
// Update WD14 threshold defaults from server
if (data.wd14_general_threshold !== undefined) {
const genSlider = document.getElementById('wd14GeneralThreshold');
genSlider.value = data.wd14_general_threshold;
document.getElementById('wd14GeneralThresholdValue').textContent = parseFloat(data.wd14_general_threshold).toFixed(2);
}
if (data.wd14_character_threshold !== undefined) {
const charSlider = document.getElementById('wd14CharacterThreshold');
charSlider.value = data.wd14_character_threshold;
document.getElementById('wd14CharacterThresholdValue').textContent = parseFloat(data.wd14_character_threshold).toFixed(2);
}
} else {
statusEls.forEach(el => { el.textContent = 'Error'; });
}
} catch (error) {
console.error('Error checking models:', error);
statusEls.forEach(el => { el.textContent = 'Error'; });
}
}
async downloadModel(modelType) {
const modelNames = {
'gguf': 'GGUF Model',
'hf': 'HuggingFace Model',
'wd14-swinv2': 'WD14 SwinV2',
'wd14-vit': 'WD14 ViT'
};
const modelName = modelNames[modelType] || modelType;
document.getElementById('downloadModelName').textContent = modelName;
document.getElementById('downloadStatus').textContent = 'Preparing...';
document.getElementById('downloadProgressPercent').textContent = '0%';
document.getElementById('downloadProgressBar').style.width = '0%';
this.hideModal("autoTagModal");
this.showModal("autoTagDownloadModal");
try {
this.autoTagState.downloadEventSource = SseStream.connect(`/prompt_manager/autotag/download/${modelType}`, { method: 'POST' });
this.autoTagState.downloadEventSource.onmessage = (event) => {
const data = JSON.parse(event.data);
if (data.status === 'downloading') {
const percent = Math.round(data.progress * 100);
document.getElementById('downloadStatus').textContent = data.message || 'Downloading...';
document.getElementById('downloadProgressPercent').textContent = `${percent}%`;
document.getElementById('downloadProgressBar').style.width = `${percent}%`;
} else if (data.status === 'complete') {
this.autoTagState.downloadEventSource.close();
this.hideModal("autoTagDownloadModal");
this.showModal("autoTagModal");
this.checkAutoTagModels();
this.showNotification('Model downloaded successfully!', 'success');
} else if (data.status === 'error') {
this.autoTagState.downloadEventSource.close();
this.hideModal("autoTagDownloadModal");
this.showModal("autoTagModal");
this.showNotification(`Download failed: ${data.message}`, 'error');
}
};
this.autoTagState.downloadEventSource.onerror = () => {
this.autoTagState.downloadEventSource.close();
this.hideModal("autoTagDownloadModal");
this.showModal("autoTagModal");
this.showNotification('Download connection lost', 'error');
};
} catch (error) {
console.error('Download error:', error);
this.hideModal("autoTagDownloadModal");
this.showModal("autoTagModal");
this.showNotification('Failed to start download', 'error');
}
}
cancelDownload() {
if (this.autoTagState.downloadEventSource) {
this.autoTagState.downloadEventSource.close();
this.autoTagState.downloadEventSource = null;
}
this.hideModal("autoTagDownloadModal");
this.showModal("autoTagModal");
}
async unloadModel() {
const unloadBtn = document.getElementById("unloadModelBtn");
unloadBtn.disabled = true;
unloadBtn.textContent = 'Unloading...';
try {
const response = await fetch('/prompt_manager/autotag/unload', {
method: 'POST'
});
const data = await response.json();
if (data.success) {
this.showNotification(data.message, 'success');
await this.checkAutoTagModels(); // Refresh status
} else {
this.showNotification(`Failed to unload model: ${data.error}`, 'error');
}
} catch (error) {
console.error('Error unloading model:', error);
this.showNotification('Failed to unload model', 'error');
} finally {
unloadBtn.disabled = false;
unloadBtn.textContent = 'Unload Model';
}
}
async startAutoTag() {
const modelType = document.querySelector('input[name="autoTagModel"]:checked').value;
const prompt = document.getElementById('autoTagPrompt').value;
const keepInMemory = document.getElementById('keepModelInMemory').checked;
// Check if model is downloaded
const response = await fetch('/prompt_manager/autotag/models');
const data = await response.json();
if (data.success && !data.models[modelType].downloaded) {
this.showNotification('Please download the selected model first', 'warning');
return;
}
this.hideModal("autoTagModal");
this.showModal("autoTagProgressModal");
// Reset progress display
document.getElementById('autoTagCurrentFile').textContent = 'Loading model...';
document.getElementById('autoTagProgressPercent').textContent = '0%';
document.getElementById('autoTagProgressBar').style.width = '0%';
document.getElementById('autoTagProcessed').textContent = '0';
document.getElementById('autoTagApplied').textContent = '0';
document.getElementById('autoTagSkipped').textContent = '0';
this.autoTagState.cancelled = false;
try {
const formData = new URLSearchParams();
formData.append('model_type', modelType);
formData.append('keep_in_memory', keepInMemory);
if (modelType.startsWith('wd14')) {
formData.append('general_threshold', document.getElementById('wd14GeneralThreshold').value);
formData.append('character_threshold', document.getElementById('wd14CharacterThreshold').value);
} else {
formData.append('prompt', prompt);
}
this.autoTagState.eventSource = SseStream.connect(`/prompt_manager/autotag/start?${formData.toString()}`, { method: 'POST' });
this.autoTagState.eventSource.onmessage = (event) => {
const data = JSON.parse(event.data);
if (data.status === 'loading_model') {
document.getElementById('autoTagCurrentFile').textContent = 'Loading model...';
} else if (data.status === 'processing') {
const percent = Math.round((data.processed / data.total) * 100);
document.getElementById('autoTagCurrentFile').textContent = data.current_file || 'Processing...';
document.getElementById('autoTagProgressPercent').textContent = `${percent}%`;
document.getElementById('autoTagProgressBar').style.width = `${percent}%`;
document.getElementById('autoTagProcessed').textContent = data.processed;
document.getElementById('autoTagApplied').textContent = data.tags_applied;
document.getElementById('autoTagSkipped').textContent = data.skipped;
} else if (data.status === 'complete') {
this.autoTagState.eventSource.close();
this.hideModal("autoTagProgressModal");
this.showNotification(`Auto tagging complete! Applied tags to ${data.tags_applied} prompts.`, 'success');
this.loadImages(); // Refresh the gallery
} else if (data.status === 'error') {
this.autoTagState.eventSource.close();
this.hideModal("autoTagProgressModal");
this.showNotification(`Auto tag error: ${data.message}`, 'error');
} else if (data.status === 'cancelled') {
this.autoTagState.eventSource.close();
this.hideModal("autoTagProgressModal");
this.showNotification('Auto tagging cancelled', 'info');
}
};
this.autoTagState.eventSource.onerror = () => {
this.autoTagState.eventSource.close();
this.hideModal("autoTagProgressModal");
this.showNotification('Connection lost during auto tagging', 'error');
};
} catch (error) {
console.error('Auto tag error:', error);
this.hideModal("autoTagProgressModal");
this.showNotification('Failed to start auto tagging', 'error');
}
}
cancelAutoTag() {
this.autoTagState.cancelled = true;
if (this.autoTagState.eventSource) {
this.autoTagState.eventSource.close();
this.autoTagState.eventSource = null;
}
this.hideModal("autoTagProgressModal");
this.showNotification('Auto tagging cancelled', 'info');
}
async startReview() {
const modelType = document.querySelector('input[name="autoTagModel"]:checked').value;
// Check if model is downloaded
const response = await fetch('/prompt_manager/autotag/models');
const data = await response.json();
if (data.success && !data.models[modelType].downloaded) {
this.showNotification('Please download the selected model first', 'warning');
return;
}
this.hideModal("autoTagModal");
// Use current gallery images for review
if (!this.images || this.images.length === 0) {
this.showNotification('No images found in gallery', 'warning');
return;
}
this.autoTagState.reviewImages = this.images;
this.autoTagState.reviewIndex = 0;
this.autoTagState.modelType = modelType;
this.autoTagState.prompt = document.getElementById('autoTagPrompt').value;
this.autoTagState.generalThreshold = parseFloat(document.getElementById('wd14GeneralThreshold').value);
this.autoTagState.characterThreshold = parseFloat(document.getElementById('wd14CharacterThreshold').value);
document.getElementById('reviewTotalCount').textContent = this.images.length;
this.showModal("autoTagReviewModal");
await this.loadNextReviewImage();
}
async loadNextReviewImage() {
if (this.autoTagState.reviewIndex >= this.autoTagState.reviewImages.length) {
this.hideModal("autoTagReviewModal");
this.showNotification('Review complete!', 'success');
this.loadImages();
return;
}
const image = this.autoTagState.reviewImages[this.autoTagState.reviewIndex];
document.getElementById('reviewCurrentIndex').textContent = this.autoTagState.reviewIndex + 1;
document.getElementById('reviewImage').src = image.thumbnail_url || image.url;
document.getElementById('reviewImageName').textContent = image.filename || 'Unknown';
document.getElementById('reviewTagsContainer').innerHTML = '<div class="text-pm-secondary">Generating tags...</div>';
try {
// Gallery entries come from /images/output: id is a path digest, so address by path
const requestBody = ImageHelpers.autotagSingleBody(image, this.autoTagState);
const response = await fetch('/prompt_manager/autotag/single', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(requestBody)
});
const data = await response.json();
if (data.success) {
this.autoTagState.currentTags = data.tags;
this.renderReviewTags();
} else {
document.getElementById('reviewTagsContainer').innerHTML =
`<div class="text-pm-error">Error: ${escapeHtml(data.error)}</div>`;
}
} catch (error) {
console.error('Error generating tags:', error);
document.getElementById('reviewTagsContainer').innerHTML =
'<div class="text-pm-error">Failed to generate tags</div>';
}
}
renderReviewTags() {
const container = document.getElementById('reviewTagsContainer');
if (this.autoTagState.currentTags.length === 0) {
container.innerHTML = '<div class="text-pm-secondary">No tags generated</div>';
return;
}
container.innerHTML = this.autoTagState.currentTags.map((tag, index) => `
<span class="tag-chip">
${escapeHtml(tag)}
<span class="tag-remove" data-action="remove-review-tag" data-index="${index}">×</span>
</span>
`).join('');
}
removeReviewTag(index) {
this.autoTagState.currentTags.splice(index, 1);
this.renderReviewTags();
}
skipReviewImage() {
this.autoTagState.reviewIndex++;
this.loadNextReviewImage();
}
async applyReviewTags() {
if (this.autoTagState.currentTags.length === 0) {
this.skipReviewImage();
return;
}
const image = this.autoTagState.reviewImages[this.autoTagState.reviewIndex];
try {
const response = await fetch('/prompt_manager/autotag/apply', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
image_path: image.path,
tags: this.autoTagState.currentTags
})
});
const data = await response.json();
if (data.success) {
this.showNotification(`Applied ${this.autoTagState.currentTags.length} tags`, 'success');
} else {
this.showNotification(data.error || 'Failed to apply tags', 'warning');
}
} catch (error) {
console.error('Error applying tags:', error);
this.showNotification('Failed to apply tags', 'error');
}
this.autoTagState.reviewIndex++;
this.loadNextReviewImage();
}
cancelReview() {
this.autoTagState.reviewImages = [];
this.autoTagState.reviewIndex = 0;
this.autoTagState.currentTags = [];
this.hideModal("autoTagReviewModal");
}
notifyThumbnailsDone(result) {
// A run stops at the server's per-run cap; say so instead of claiming it is finished.
const remaining = Number(result && result.remaining) || 0;
if (remaining > 0) {
this.showNotification(
`${remaining} thumbnails still pending. Run "Generate Thumbnails" again to continue.`,
'warning'
);
return;
}
this.showNotification('Thumbnails generated successfully!', 'success');
}
showNotification(message, type = 'info') {
// Simple notification - could be enhanced with a toast library
const colors = {
success: 'bg-pm-success',
error: 'bg-pm-error',
warning: 'bg-pm-warning',
info: 'bg-pm-accent'
};
const notification = document.createElement('div');
notification.className = `fixed bottom-4 right-4 ${colors[type]} text-pm px-4 py-2 rounded-pm-md shadow-pm text-sm z-50 animate-fade-in`;
notification.textContent = message;
document.body.appendChild(notification);
setTimeout(() => {
notification.remove();
}, 3000);
}
}
// Initialize gallery when page loads
document.addEventListener('DOMContentLoaded', () => {
window.gallery = new GalleryManager();
});
// Global helper for modal close buttons
function hideModal(modalId) {
const modal = document.getElementById(modalId);
modal.classList.add('hidden');
modal.classList.remove('flex');
document.body.style.overflow = '';
}