* refactor(thumbnails): one resolver for where previews live on disk (#320) Add py/api/preview_store.py: the store of a source, a preview's path, URL parts to disk, containment (owns), a write generation and the lock around every preview's final rename. Every preview reader, writer and remover now asks it instead of joining <source>/thumbnails by hand: serve, on-demand, capture (its path resolved when the write starts), the after-scan queue, the thumbnail plans and job payloads, clear, the temp sweep, History covers, the auto-tag listing and the owned-preview lookups. Source mode only: every path is the one built before, so nothing changes on disk or on the wire. A structural test keeps new thumbnails joins out of the backend. * refactor(thumbnails): keep the after-scan queue's store rule and pin the write lock (#320) A planned target keeps the plan's own containment rule (inside the store, as the thumbnail job writes it); captures keep the serve rule. The structural test also covers PREVIEW_FOLDER joins and the repo-root modules; the write-lock test asserts the lock instead of waiting on a timer; new tests cover nested roots in the after-scan queue and a legacy upper-case preview served through the resolver. * feat(thumbnails): keep previews in one folder, set from config.json (#320) thumbnails.location in config.json can now name one folder for every source's previews: <folder>/<source key>/<rel>_thumb<ext>. A source key is a readable slug plus a short hash of the source's identity, stored in the folder's marker (.promptmanager-thumbnails.json) with the install that made it. Serving, on-demand and capture writes, listings, clear, delete, duplicates, the temp sweep and History covers all follow it; a leftover in <source>/thumbnails is never served and other text under thumbnails/ is refused. A missing, unplugged or foreign folder is unavailable (a probe checks it every 5 s within 2 s; a stalled or failing store call flips it at once): originals are served, nothing is written anywhere, the folder is never re-created, and generate, convert and clear answer 409 location-unavailable. A bad config value is unavailable too, never a fall-back to the sources. No UI yet: POST /settings refuses thumbnail_location (409 location-needs-move); GET /settings reports it with its path, the thumbnail status without one. Sources inside or around the folder and a library move into it are refused. * fix(thumbnails): never follow a marker or a link out of the thumbnail folder (#320) Marker keys are data, not paths: only a single <slug>-<hex> folder name is ever joined onto the thumbnail folder; anything else is dropped and logged. Every preview write and delete passes one ownership check (below a key folder, lexically and resolved, no link, junction or reparse point on the way); make_parent refuses anything else and writes nothing while the folder lacks this install's marker (an unmounted mount point). Clear never descends into a junction. The configured location is in force before any request (unchecked folder mode is unavailable), and a startup check that fails leaves it invalid, never the sources. Older listings that run on the event loop no longer touch the store in folder mode. A leftover under another case of thumbnails/ on a case-insensitive disk is refused too. * test(memory): let the musl test fake os.confstr on Windows (#523) The portability run on main fails since #523: Windows has no os.confstr, so patching it without create=True raises before the test runs. * feat(thumbnails): move thumbnails between the sources and one folder (#320) POST /images/thumbnails/plan reviews a move (what is copied, removed and left, free space, too-long Windows paths, sources no longer listed) and hands out a token; POST /jobs/thumbnails {mode: move} runs it as a thumbnails job: hold automatic previews, copy and check every preview on the job pool, switch with one config commit that records the old copies, then remove them. Per-file errors still switch; a folder-wide error, a cancel before the switch or a crash leaves nothing changed (a journal is undone at the next start); old copies that could not go are removed by a finishing job at the next start. Leaving a folder removes the previews of this install's sources that are no longer listed (Q320-8). Recreate folder and Use this folder answer an unavailable folder; the Thumbnail maintenance scope prune removes the thumbnails of removed sources. * feat(system): choose where thumbnails are kept and move them (#320) System › Sources & scans gains the Thumbnail location row (D196): Inside each source or One folder, the folder path, the status line and Review move…, whose review (D197) lists what is copied, removed and left before Move thumbnails starts; a refused folder is explained under the field, a folder of your own asks for a confirmation first. Activity shows a move and a prune with their own tiles and step line (D198). An unavailable folder shows a notice on Overview and Sources & scans with Recreate folder or Use this folder (D199). Thumbnail maintenance offers Remove thumbnails of removed sources in folder mode (D200), and Move library's review says where the thumbnails stay (D201). Closes #320 * fix(thumbnails): keep a move's own previews and never walk out of a store (#320) A cleanup that waited from an earlier move never names a key folder the new move just filled, so moving back keeps the previews. Removing empty folders walks top-down and never into a link or junction. Any error while copying undoes the copies; once the switch is committed, a journal that cannot be removed no longer undoes it. Hand-edited cleanup entries and marker keys never name a folder outside the stores; removal stops at interpreter exit. The path field carries aria-invalid with its reason, and a review that went stale reviews again. * test(thumbnails): cover the prune scope, the Overview notice and the space refusal (#320) The frontend coverage gate counted the new branches of thumbnail maintenance, the Activity views, the notices' Change location and the api-client's space refusal as uncovered; page and unit tests now drive them. * fix(thumbnails): report removals that fail and explain the quiet excepts (#320) An old thumbnail the finishing move cannot remove (held open on Windows) is now an error row of the job with its path, and logged; a failed copy's temp that cannot go is logged with its path. Every except that only passes says why, and the thread runners say their caller re-raises. * fix(thumbnails): never plan or delete through a linked key folder (#320) Review finding P1 on #591: in folder mode a source key folder that is a link or junction to a folder outside was still handed to the planner, so Convert outdated could remove an older-format file outside the thumbnail folder. ensure_store now refuses a linked store before any planning, and each removal of an old preview passes may_write at the moment it runs. * fix(thumbnails): the temp sweep never walks into a link (#320) Review finding P2 on #591: Clear thumbnails and the startup sweep removed crashed-write temps with a plain os.walk, which on Windows walks into a junction even without followlinks. The sweeper now skips a folder that is a link, junction or reparse point (the top one too), and each temp passes may_write before it is removed. * fix(thumbnails): lock the folder marker across processes (#320) Review finding P2 on #591: two installs sharing one thumbnail folder could each read the marker, add a key and replace it, so the last writer dropped the other's key. write_marker now re-reads, changes and replaces the marker under a lock file made with O_CREAT | O_EXCL beside it (atomic on shares, unlike flock); a lock older than 30 s, left by a writer that died, is taken over, and a wait past 1.5 s fails as a TimeoutError like an unreachable folder. Tested with three processes adding keys at once. * test(thumbnails): cover the space refusal, Overview's Use this folder and the prune review (#320) The frontend coverage floors of activity-kinds, system-overview and thumbnail-review: a disk without the space shows the review but cannot move (a fixture route fakes the free space), Use this folder on Overview reloads the host values, the prune review's text and rows are pure functions with unit tests, and Overview takes the System drafts it is always mounted with. * fix(thumbnails): never copy through a linked folder; refuse a target inside the folder in use (#320) Moving back to Inside each source followed an existing linked folder in the destination and could replace a file outside the source (Codex P1). A target nested inside the thumbnail folder in use was accepted, and the old-copy cleanup then removed the previews just copied there (Codex P2). Every copy now checks the whole destination chain for links, also when the folders exist, and the move refuses a folder inside the one in use with location-inside-current. The cancelled branches no longer mix explicit and implicit returns (code-quality note). * fix(system): one full stop in the move-back review (#320) * fix(thumbnails): a folder check answers for the folder as it is now (#320) Probe.check() joined a check already running, or a finished one nobody had collected. A folder deleted right after configure() woke the probe therefore stayed available until the next probe, which failed the Overview notice spec in the merge queue. It now waits for a running check and starts a new one, still never two at once, and a stuck check still times out. The spec also asserts the fixture's removal took effect.
458 lines
18 KiB
Python
458 lines
18 KiB
Python
"""One resolver for where previews live on disk (#320 PR 1).
|
|
|
|
``py/api/preview_store.py`` owns every mapping between an image source, a
|
|
media file and its preview on disk. In source mode (the only mode so far)
|
|
it returns exactly the paths the code built by hand before
|
|
(``<source>/thumbnails/<rel>_thumb<ext>``), so nothing changes on disk. The
|
|
structural test keeps new ``thumbnails`` path joins out of the rest of the
|
|
backend.
|
|
"""
|
|
|
|
import asyncio
|
|
import os
|
|
import re
|
|
import sys
|
|
import tempfile
|
|
import threading
|
|
import unittest
|
|
from pathlib import Path
|
|
import unittest.mock
|
|
from unittest.mock import MagicMock
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
sys.modules.setdefault("server", MagicMock())
|
|
|
|
from py.api import preview_store, thumbnail_settings # noqa: E402
|
|
from py.api.thumbnail_encode import ( # noqa: E402
|
|
PreviewWriteHeld,
|
|
replace_unless_held,
|
|
)
|
|
|
|
REPO = Path(__file__).resolve().parent.parent
|
|
WAIT = 10
|
|
|
|
|
|
class SourceModeCase(unittest.TestCase):
|
|
def setUp(self):
|
|
self._tmp = tempfile.TemporaryDirectory()
|
|
self.addCleanup(self._tmp.cleanup)
|
|
self.root = Path(self._tmp.name) / "output"
|
|
self.root.mkdir()
|
|
|
|
|
|
class TestSourceModePaths(SourceModeCase):
|
|
def test_default_location_is_inside_each_source(self):
|
|
location = preview_store.current()
|
|
self.assertEqual(location.mode, preview_store.SOURCE_MODE)
|
|
self.assertTrue(location.available)
|
|
self.assertIsNone(location.problem)
|
|
|
|
def test_store_of_a_source_is_its_thumbnails_folder(self):
|
|
self.assertEqual(preview_store.store_dir(self.root), self.root / "thumbnails")
|
|
|
|
def test_preview_path_matches_the_old_join(self):
|
|
rel = Path("sub", "deep", "a.png")
|
|
self.assertEqual(
|
|
preview_store.preview_path(self.root, rel, ".webp"),
|
|
self.root / "thumbnails/sub/deep/a_thumb.webp",
|
|
)
|
|
self.assertEqual(
|
|
preview_store.preview_path(self.root, "a.mp4", ".jpg"),
|
|
self.root / "thumbnails" / "a_thumb.jpg",
|
|
)
|
|
|
|
def test_preview_path_defaults_to_the_written_extension(self):
|
|
from py.api.thumbnail_encode import writer_settings
|
|
|
|
self.assertEqual(
|
|
preview_store.preview_path(self.root, "a.png").suffix,
|
|
writer_settings().extension,
|
|
)
|
|
|
|
def test_request_path_maps_url_parts_to_the_old_join(self):
|
|
parts = ("thumbnails", "sub", "a_thumb.webp")
|
|
self.assertEqual(
|
|
preview_store.request_path(self.root, parts), self.root.joinpath(*parts)
|
|
)
|
|
|
|
def test_request_path_is_none_for_anything_but_preview_parts(self):
|
|
self.assertIsNone(preview_store.request_path(self.root, ("sub", "a.png")))
|
|
self.assertIsNone(preview_store.request_path(self.root, ("Thumbnails", "a")))
|
|
self.assertIsNone(preview_store.request_path(self.root, ("thumbnails",)))
|
|
|
|
def test_ensure_store_is_the_store(self):
|
|
self.assertEqual(
|
|
preview_store.ensure_store(self.root), self.root / "thumbnails"
|
|
)
|
|
|
|
def test_owns_a_preview_inside_the_store(self):
|
|
inside = self.root / "thumbnails" / "sub" / "a_thumb.webp"
|
|
self.assertTrue(preview_store.owns(self.root, inside))
|
|
self.assertFalse(preview_store.owns(self.root, self.root / "a.png"))
|
|
self.assertFalse(
|
|
preview_store.owns(self.root, self.root.parent / "elsewhere.webp")
|
|
)
|
|
|
|
@unittest.skipUnless(hasattr(os, "symlink"), "needs symlinks")
|
|
def test_owns_refuses_a_store_linked_out_of_the_source(self):
|
|
outside = Path(self._tmp.name) / "outside"
|
|
outside.mkdir()
|
|
try:
|
|
(self.root / "thumbnails").symlink_to(outside, target_is_directory=True)
|
|
except OSError:
|
|
self.skipTest("symlinks not permitted")
|
|
self.assertFalse(
|
|
preview_store.owns(self.root, self.root / "thumbnails" / "a_thumb.webp")
|
|
)
|
|
|
|
@unittest.skipUnless(hasattr(os, "symlink"), "needs symlinks")
|
|
def test_owns_refuses_a_preview_folder_linked_out_of_the_store(self):
|
|
outside = Path(self._tmp.name) / "outside"
|
|
outside.mkdir()
|
|
(self.root / "thumbnails").mkdir()
|
|
try:
|
|
(self.root / "thumbnails" / "sub").symlink_to(
|
|
outside, target_is_directory=True
|
|
)
|
|
except OSError:
|
|
self.skipTest("symlinks not permitted")
|
|
self.assertFalse(
|
|
preview_store.owns(
|
|
self.root, self.root / "thumbnails" / "sub" / "a_thumb.webp"
|
|
)
|
|
)
|
|
|
|
def test_store_container_is_the_source(self):
|
|
self.assertEqual(preview_store.store_container(self.root), self.root)
|
|
|
|
def test_given_store_argument_is_kept_unless_it_is_the_default(self):
|
|
default = self.root / "thumbnails"
|
|
other = Path(self._tmp.name) / "custom"
|
|
self.assertEqual(preview_store.given_store(self.root, default), default)
|
|
self.assertEqual(preview_store.given_store(self.root, other), other)
|
|
|
|
|
|
class TestPreviewText(unittest.TestCase):
|
|
def test_preview_text_is_an_exact_thumbnails_prefix_and_preview_name(self):
|
|
for text in (
|
|
"thumbnails/a_thumb.webp",
|
|
"thumbnails/sub/a_thumb.jxl",
|
|
"thumbnails/a_thumb.JPG",
|
|
"thumbnails/a_thumb.Png",
|
|
):
|
|
with self.subTest(text=text):
|
|
self.assertTrue(preview_store.is_preview_text(text))
|
|
|
|
def test_other_text_is_not_preview_text(self):
|
|
for text in (
|
|
"Thumbnails/a_thumb.webp",
|
|
"thumbnails/a.png",
|
|
"thumbnails/_thumb.webp",
|
|
"thumbnails/a_thumb.txt",
|
|
"sub/thumbnails/a_thumb.webp",
|
|
"thumbnails/../a_thumb.webp",
|
|
"thumbnails//a_thumb.webp",
|
|
"thumbnails/./a_thumb.webp",
|
|
"thumbnails\\a_thumb.webp",
|
|
"thumbnails/c:a_thumb.webp",
|
|
"thumbnails/sub/thumbnails/a_thumb.webp",
|
|
"thumbnails",
|
|
"",
|
|
):
|
|
with self.subTest(text=text):
|
|
self.assertFalse(preview_store.is_preview_text(text))
|
|
|
|
|
|
class TestWritesAndTheSwitch(SourceModeCase):
|
|
def _temp_and_dst(self):
|
|
folder = self.root / "thumbnails"
|
|
folder.mkdir()
|
|
tmp = folder / ".a_thumb.part-1-1.webp"
|
|
tmp.write_bytes(b"new")
|
|
return tmp, folder / "a_thumb.webp"
|
|
|
|
def test_a_write_from_the_current_generation_lands(self):
|
|
tmp, dst = self._temp_and_dst()
|
|
replace_unless_held(tmp, dst, preview_store.write_guard())
|
|
self.assertEqual(dst.read_bytes(), b"new")
|
|
|
|
def test_a_write_whose_generation_changed_is_dropped(self):
|
|
tmp, dst = self._temp_and_dst()
|
|
guard = preview_store.write_guard()
|
|
preview_store.bump_generation()
|
|
with self.assertRaises(PreviewWriteHeld):
|
|
replace_unless_held(tmp, dst, guard)
|
|
self.assertFalse(dst.exists())
|
|
|
|
def test_the_hold_check_still_applies(self):
|
|
tmp, dst = self._temp_and_dst()
|
|
with self.assertRaises(PreviewWriteHeld):
|
|
replace_unless_held(tmp, dst, preview_store.write_guard(lambda: False))
|
|
self.assertFalse(dst.exists())
|
|
|
|
def test_the_check_and_the_rename_run_under_the_write_lock(self):
|
|
"""The hold check and the rename hold ``write_lock``, the lock the
|
|
generation bump takes: a switch cannot fall between them."""
|
|
tmp, dst = self._temp_and_dst()
|
|
in_check = threading.Event()
|
|
release = threading.Event()
|
|
seen = {}
|
|
|
|
def slow_hold():
|
|
seen["locked"] = preview_store.write_lock.locked()
|
|
in_check.set()
|
|
release.wait(WAIT)
|
|
return True
|
|
|
|
guard = preview_store.write_guard(slow_hold)
|
|
writer = threading.Thread(target=replace_unless_held, args=(tmp, dst, guard))
|
|
writer.start()
|
|
try:
|
|
self.assertTrue(in_check.wait(WAIT))
|
|
self.assertTrue(seen["locked"])
|
|
self.assertTrue(preview_store.write_lock.locked())
|
|
finally:
|
|
release.set()
|
|
writer.join(WAIT)
|
|
self.assertFalse(preview_store.write_lock.locked())
|
|
self.assertEqual(dst.read_bytes(), b"new")
|
|
bumped = preview_store.bump_generation()
|
|
self.assertEqual(preview_store.generation(), bumped)
|
|
|
|
|
|
class TestOnlyTheResolverBuildsStorePaths(unittest.TestCase):
|
|
"""No backend module joins ``thumbnails`` onto a path by hand any more."""
|
|
|
|
ALLOWED = {
|
|
Path("py/api/preview_store.py"),
|
|
Path("py/api/thumbnail_settings.py"), # URL text
|
|
}
|
|
# A path join with a literal "thumbnails" operand.
|
|
JOIN = re.compile(
|
|
r"""/\s*["']thumbnails["']"""
|
|
r"""|["']thumbnails["']\s*/"""
|
|
r"""|joinpath\([^)]*["']thumbnails["']"""
|
|
r"""|os\.path\.join\([^)]*["']thumbnails["']"""
|
|
r"""|["']thumbnails/"""
|
|
r"""|/\s*PREVIEW_FOLDER|joinpath\([^)]*PREVIEW_FOLDER"""
|
|
r"""|Path\([^)]*,\s*["']thumbnails["']"""
|
|
)
|
|
|
|
def test_no_thumbnails_join_outside_the_resolver(self):
|
|
offenders = []
|
|
files = sorted(REPO.glob("*.py"))
|
|
for folder in ("py", "utils", "database"):
|
|
files += sorted((REPO / folder).rglob("*.py"))
|
|
for path in files:
|
|
rel = path.relative_to(REPO)
|
|
if rel in self.ALLOWED:
|
|
continue
|
|
for number, line in enumerate(
|
|
path.read_text(encoding="utf-8").splitlines(), 1
|
|
):
|
|
code = line.split("#", 1)[0]
|
|
if self.JOIN.search(code):
|
|
offenders.append(f"{rel}:{number}: {line.strip()}")
|
|
self.assertEqual(offenders, [])
|
|
|
|
|
|
class TestSettingsLookupsFollowTheStore(SourceModeCase):
|
|
def test_owned_thumbnails_reads_the_store(self):
|
|
folder = self.root / "thumbnails" / "sub"
|
|
folder.mkdir(parents=True)
|
|
(folder / "a_thumb.webp").write_bytes(b"x")
|
|
self.assertEqual(
|
|
thumbnail_settings.owned_thumbnails(self.root, Path("sub", "a.png")),
|
|
[folder / "a_thumb.webp"],
|
|
)
|
|
|
|
|
|
try:
|
|
from tests.test_thumbnail_auto import AutoThumbnailCase, _entry
|
|
from tests.test_api_images import make_png
|
|
except ImportError: # discovered with tests/ as the top-level directory
|
|
from test_thumbnail_auto import AutoThumbnailCase, _entry
|
|
from test_api_images import make_png
|
|
|
|
|
|
class RedirectedStoreCase(AutoThumbnailCase):
|
|
"""Every caller follows the resolver: with the store of the output root
|
|
moved to ``<tmp>/elsewhere/output-key`` (a stand-in for a later mode),
|
|
previews are read, written, listed and removed there and only there."""
|
|
|
|
async def asyncSetUp(self):
|
|
await super().asyncSetUp()
|
|
self.container = self.comfy_dir / "elsewhere"
|
|
self.store = self.container / "output-key"
|
|
self.store.mkdir(parents=True)
|
|
self.old_store = self.output_dir / "thumbnails"
|
|
root = self.output_dir
|
|
real_store = preview_store.store_dir
|
|
real_container = preview_store.store_container
|
|
|
|
def store_dir(r, location=None):
|
|
return self.store if Path(r) == root else real_store(r, location)
|
|
|
|
def store_container(r, location=None):
|
|
if Path(r) == root:
|
|
return self.container
|
|
return real_container(r, location)
|
|
|
|
for name, fake in (
|
|
("store_dir", store_dir),
|
|
("store_container", store_container),
|
|
):
|
|
patcher = unittest.mock.patch.object(preview_store, name, fake)
|
|
patcher.start()
|
|
self.addCleanup(patcher.stop)
|
|
|
|
def _preview(self, rel, ext=".webp"):
|
|
return self.store / (thumbnail_settings.preview_stem(rel) + ext)
|
|
|
|
|
|
class TestEveryCallerFollowsTheStore(RedirectedStoreCase):
|
|
async def test_an_existing_preview_is_served_from_the_store(self):
|
|
make_png(self.output_dir / "sub" / "a.png", size=(64, 64))
|
|
preview = self._preview("sub/a.png")
|
|
make_png(preview, size=(32, 32))
|
|
url = _entry(self.api, self.output_dir, "sub/a.png")["thumbnail_url"]
|
|
self.assertIn("v=", url) # the listing found it in the store
|
|
resp = await self.client.get(url)
|
|
self.assertEqual(resp.status, 200)
|
|
self.assertEqual(await resp.read(), preview.read_bytes())
|
|
|
|
async def test_a_legacy_upper_case_preview_is_served_from_the_store(self):
|
|
make_png(self.output_dir / "k.JPG", size=(64, 64))
|
|
legacy = self.store / "k_thumb.JPG"
|
|
legacy.write_bytes(b"legacy preview")
|
|
resp = await self.client.get(
|
|
"/prompt_manager/images/serve/thumbnails/k_thumb.JPG?root=0"
|
|
)
|
|
self.assertEqual(resp.status, 200)
|
|
self.assertEqual(await resp.read(), b"legacy preview")
|
|
|
|
async def test_a_missing_preview_is_made_in_the_store_on_first_view(self):
|
|
make_png(self.output_dir / "sub" / "b.png", size=(640, 480))
|
|
url = _entry(self.api, self.output_dir, "sub/b.png")["thumbnail_url"]
|
|
resp = await self.client.get(url)
|
|
self.assertEqual(resp.status, 200)
|
|
self.assertTrue(self._preview("sub/b.png").is_file())
|
|
self.assertFalse(self.old_store.exists())
|
|
|
|
async def test_a_leftover_in_the_source_folder_is_not_served(self):
|
|
make_png(self.output_dir / "c.png", size=(640, 480))
|
|
leftover = self.old_store / "c_thumb.webp"
|
|
leftover.parent.mkdir()
|
|
leftover.write_bytes(b"old preview bytes")
|
|
resp = await self.client.get(
|
|
"/prompt_manager/images/serve/thumbnails/c_thumb.webp?root=0"
|
|
)
|
|
self.assertEqual(resp.status, 200)
|
|
self.assertNotEqual(await resp.read(), b"old preview bytes")
|
|
self.assertTrue(self._preview("c.png").is_file())
|
|
|
|
async def test_capture_writes_into_the_store(self):
|
|
path = make_png(self.output_dir / "run" / "d.png", size=(64, 64))
|
|
self.assertEqual(self.api.queue_capture_thumbnail(str(path)), "queued")
|
|
await self._idle()
|
|
self.assertTrue(self._preview("run/d.png").is_file())
|
|
self.assertFalse(self.old_store.exists())
|
|
|
|
async def test_generate_and_clear_follow_the_store(self):
|
|
make_png(self.output_dir / "e.png", size=(64, 64))
|
|
payload = await asyncio.get_running_loop().run_in_executor(
|
|
None, self.api._generate_gallery_thumbnails_sync, [self.output_dir]
|
|
)
|
|
self.assertTrue(payload["success"])
|
|
self.assertTrue(self._preview("e.png").is_file())
|
|
self.assertFalse(self.old_store.exists())
|
|
self.assertTrue(payload["thumbnails_path"].endswith("output-key"))
|
|
cleared, _size = self.api._clear_gallery_thumbnails_sync([self.output_dir])
|
|
self.assertEqual(cleared, 1)
|
|
self.assertFalse(self._preview("e.png").exists())
|
|
self.assertTrue(self.store.is_dir()) # the store itself stays
|
|
|
|
async def test_removing_a_file_removes_its_preview_in_the_store(self):
|
|
from py.api.admin import _remove_thumbnail
|
|
|
|
make_png(self.output_dir / "f.png", size=(64, 64))
|
|
preview = self._preview("f.png")
|
|
preview.write_bytes(b"x")
|
|
self.assertTrue(_remove_thumbnail(self.output_dir, Path("f.png")))
|
|
self.assertFalse(preview.exists())
|
|
|
|
async def test_the_temp_sweep_walks_the_store(self):
|
|
stray = self.store / ".g_thumb.part-1-1.webp"
|
|
stray.write_bytes(b"x")
|
|
old = os.path.getmtime(stray) - 10 * 24 * 3600
|
|
os.utime(stray, (old, old))
|
|
self.assertEqual(self.api.sweep_thumbnail_temps(), 1)
|
|
self.assertFalse(stray.exists())
|
|
|
|
async def test_history_cover_writes_into_the_store(self):
|
|
from py.api import history_current
|
|
|
|
self.assertTrue(
|
|
history_current._preview_writable(self.output_dir, self._preview("h.png"))
|
|
)
|
|
self.assertFalse(
|
|
history_current._preview_writable(
|
|
self.output_dir, self.old_store / "h_thumb.webp"
|
|
)
|
|
)
|
|
|
|
|
|
class TestCaptureResolvesAtWriteTime(RedirectedStoreCase):
|
|
def test_a_queued_capture_follows_a_store_that_moved_before_its_write(self):
|
|
from py.api import thumbnail_auto as auto
|
|
|
|
written = []
|
|
thumbnailer = auto.AutoThumbnailer(
|
|
lambda src, dst, *a, **k: written.append(dst) or {"action": "generated"},
|
|
lambda: True,
|
|
self.api.logger,
|
|
start_worker=False,
|
|
)
|
|
self.addCleanup(thumbnailer.close)
|
|
src = make_png(self.output_dir / "i.png", size=(32, 32))
|
|
deferred = auto.DeferredPreview(self.output_dir, Path("i.png"))
|
|
self.assertTrue(thumbnailer.enqueue(src, deferred, False))
|
|
moved = self.container / "moved"
|
|
moved.mkdir()
|
|
self.store = moved # the location changes before the worker runs
|
|
thumbnailer._write_queued(thumbnailer._queue.popleft())
|
|
self.assertEqual(written, [moved / "i_thumb.webp"])
|
|
|
|
def test_the_after_scan_queue_maps_each_target_to_its_own_root(self):
|
|
from py.api import thumbnail_auto as auto
|
|
|
|
nested = self.output_dir / "inner"
|
|
nested.mkdir()
|
|
src = nested / "j.png"
|
|
outer = auto._deferred(
|
|
[self.output_dir, nested], src, self._preview("inner/j.png")
|
|
)
|
|
inner = auto._deferred(
|
|
[self.output_dir, nested], src, nested / "thumbnails" / "j_thumb.webp"
|
|
)
|
|
self.assertEqual(
|
|
(outer.root, outer.rel), (self.output_dir, Path("inner/j.png"))
|
|
)
|
|
self.assertEqual((inner.root, inner.rel), (nested, Path("j.png")))
|
|
stray = self.comfy_dir / "x_thumb.webp"
|
|
self.assertEqual(auto._deferred([self.output_dir], src, stray), stray)
|
|
|
|
def test_serve_and_history_share_one_flight_key(self):
|
|
from py.api import thumbnail_auto as auto
|
|
|
|
parts = ("thumbnails", "sub", "a_thumb.webp")
|
|
self.assertEqual(
|
|
auto._request_key(self.output_dir, parts),
|
|
auto._dst_key(self._preview("sub/a.png")),
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|