From 5bbca1c07358162c30a43ff8dce025b7ffda035c Mon Sep 17 00:00:00 2001 From: zanllp Date: Fri, 28 Apr 2023 21:29:50 +0800 Subject: [PATCH] Refactor cache generation logic --- .vscode/settings.json | 6 ++++++ scripts/api.py | 14 ++++---------- 2 files changed, 10 insertions(+), 10 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..6ba1afd --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "[python]": { + "editor.defaultFormatter": "ms-python.black-formatter" + }, + "python.formatting.provider": "none" +} \ No newline at end of file diff --git a/scripts/api.py b/scripts/api.py index 5afaa7d..4815b11 100644 --- a/scripts/api.py +++ b/scripts/api.py @@ -196,16 +196,10 @@ def infinite_image_browsing_api(_: Any, app: FastAPI, **kwargs): ) # 如果缓存文件不存在,则生成缩略图并保存 - with open(path, "rb") as f: - img = Image.open(BytesIO(f.read())) - w, h = size.split(",") - img.thumbnail((int(w), int(h))) - buffer = BytesIO() - img.save(buffer, "webp") - - # 将二进制数据写入缓存文件中 - with open(cache_path, "wb") as f: - f.write(buffer.getvalue()) + with Image.open(path) as img: + w, h = size.split(",") + img.thumbnail((int(w), int(h))) + img.save(cache_path, "webp") # 返回缓存文件 return FileResponse(