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