Refactor cache generation logic

pull/58/head
zanllp 2023-04-28 21:29:50 +08:00
parent 9edb42710d
commit 5bbca1c073
2 changed files with 10 additions and 10 deletions

6
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,6 @@
{
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter"
},
"python.formatting.provider": "none"
}

View File

@ -196,16 +196,10 @@ def infinite_image_browsing_api(_: Any, app: FastAPI, **kwargs):
) )
# 如果缓存文件不存在,则生成缩略图并保存 # 如果缓存文件不存在,则生成缩略图并保存
with open(path, "rb") as f: with Image.open(path) as img:
img = Image.open(BytesIO(f.read())) w, h = size.split(",")
w, h = size.split(",") img.thumbnail((int(w), int(h)))
img.thumbnail((int(w), int(h))) img.save(cache_path, "webp")
buffer = BytesIO()
img.save(buffer, "webp")
# 将二进制数据写入缓存文件中
with open(cache_path, "wb") as f:
f.write(buffer.getvalue())
# 返回缓存文件 # 返回缓存文件
return FileResponse( return FileResponse(