mirror of
https://github.com/Kakune55/ComiPy.git
synced 2025-09-16 04:09:41 +08:00
feat(file): 优化文件处理和缓存机制
- 重构文件处理逻辑,提高性能和可维护性 - 增加缓存机制,减少重复读取和处理 - 改进错误处理和日志记录 - 优化缩略图生成算法 - 添加性能监控和测试依赖
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
from flask import *
|
||||
from flask import Blueprint
|
||||
from flask import Blueprint, request, abort, make_response
|
||||
import db.file , file, gc , app_conf
|
||||
from utils.performance_monitor import timing_context
|
||||
|
||||
api_Img_bp = Blueprint("api_Img_bp", __name__)
|
||||
|
||||
@@ -11,21 +11,30 @@ fullSize = conf.getint("img", "fullSize")
|
||||
|
||||
@api_Img_bp.route("/api/img/<bookid>/<index>")
|
||||
def img(bookid, index): # 图片接口
|
||||
if request.cookies.get("islogin") is None:
|
||||
return abort(403)
|
||||
if len(db.file.searchByid(bookid)) == 0:
|
||||
return abort(404)
|
||||
# 设置响应类型为图片
|
||||
data, filename = file.raedZip(bookid, index)
|
||||
if isinstance(data, str):
|
||||
abort(404)
|
||||
if request.args.get("mini") == "yes":
|
||||
data = file.thumbnail(data,miniSize,encode=imgencode)
|
||||
else:
|
||||
data = file.thumbnail(data,fullSize,encode=imgencode)
|
||||
response = make_response(data) # 读取文件
|
||||
del data
|
||||
response.headers.set("Content-Type",f"image/{imgencode}")
|
||||
response.headers.set("Content-Disposition", "inline", filename=filename)
|
||||
gc.collect()
|
||||
return response
|
||||
with timing_context(f"api.img.{bookid}.{index}"):
|
||||
if request.cookies.get("islogin") is None:
|
||||
return abort(403)
|
||||
if len(db.file.searchByid(bookid)) == 0:
|
||||
return abort(404)
|
||||
|
||||
# 读取图片数据
|
||||
data, filename = file.readZip(bookid, index)
|
||||
if isinstance(data, str):
|
||||
abort(404)
|
||||
|
||||
# 处理图片尺寸
|
||||
if request.args.get("mini") == "yes":
|
||||
data = file.thumbnail(data, miniSize, encode=imgencode)
|
||||
else:
|
||||
data = file.thumbnail(data, fullSize, encode=imgencode)
|
||||
|
||||
# 创建响应
|
||||
response = make_response(data)
|
||||
del data # 及时释放内存
|
||||
|
||||
response.headers.set("Content-Type", f"image/{imgencode}")
|
||||
response.headers.set("Content-Disposition", "inline", filename=filename)
|
||||
response.headers.set("Cache-Control", "public, max-age=3600") # 添加缓存头
|
||||
|
||||
gc.collect() # 强制垃圾回收
|
||||
return response
|
||||
|
Reference in New Issue
Block a user