From 7b3252c588e79dd909a1385d56370e5177a98530 Mon Sep 17 00:00:00 2001 From: Leo Vasanko Date: Thu, 23 Apr 2026 19:02:54 +0000 Subject: [PATCH] Handle missing preview files with silent 404 --- cista/preview.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/cista/preview.py b/cista/preview.py index 502d1ba..7f39598 100644 --- a/cista/preview.py +++ b/cista/preview.py @@ -97,7 +97,15 @@ async def preview(req, path): quality = int(req.args.get("q", 60)) rel = PurePosixPath(sanitize(unquote(path))) filepath = config.config.path / rel - stat = filepath.lstat() + if not filepath.is_file(): + raise NotFound() + + try: + stat = filepath.lstat() + except FileNotFoundError: + # File disappeared between existence check and stat. + raise NotFound() from None + etag = config.derived_secret( "preview", rel, stat.st_mtime_ns, quality, maxsize, maxzoom ).hex() @@ -112,9 +120,6 @@ async def preview(req, path): logger.debug(f"Preview cache hit: {rel}") return raw(cached.body, headers=cached.headers) - if not filepath.is_file(): - raise NotFound("File not found") - # Generate preview img = await asyncio.get_event_loop().run_in_executor( req.app.ctx.threadexec, dispatch, filepath, quality, maxsize, maxzoom