mirror of
https://github.com/Kakune55/ComiPy.git
synced 2025-05-06 18:29:26 +08:00
使用缩略图优化了网络传输速度与流量消耗
This commit is contained in:
parent
8aff1ae64e
commit
7a5dde7169
31
file.py
31
file.py
@ -1,5 +1,6 @@
|
|||||||
import shutil, os, configparser, zipfile
|
import shutil, os, configparser, zipfile, io
|
||||||
import db
|
import db
|
||||||
|
from PIL import Image
|
||||||
|
|
||||||
config = configparser.ConfigParser()
|
config = configparser.ConfigParser()
|
||||||
config.read("./conf/app.ini")
|
config.read("./conf/app.ini")
|
||||||
@ -20,7 +21,9 @@ def auotLoadFile():
|
|||||||
if zipfile.is_zipfile(
|
if zipfile.is_zipfile(
|
||||||
config.get("file", "inputdir") + "/" + item
|
config.get("file", "inputdir") + "/" + item
|
||||||
): # 判断是否为压缩包
|
): # 判断是否为压缩包
|
||||||
with zipfile.ZipFile(config.get("file", "inputdir") + "/" + item, "r") as zip_ref:
|
with zipfile.ZipFile(
|
||||||
|
config.get("file", "inputdir") + "/" + item, "r"
|
||||||
|
) as zip_ref:
|
||||||
db.newFile(item, len(zip_ref.namelist())) # 添加数据库记录 移动到存储
|
db.newFile(item, len(zip_ref.namelist())) # 添加数据库记录 移动到存储
|
||||||
shutil.move(
|
shutil.move(
|
||||||
config.get("file", "inputdir") + "/" + item,
|
config.get("file", "inputdir") + "/" + item,
|
||||||
@ -47,7 +50,7 @@ def raedZip(bookid: str, index: int):
|
|||||||
|
|
||||||
if not image_files:
|
if not image_files:
|
||||||
return "not imgfile in zip", ""
|
return "not imgfile in zip", ""
|
||||||
|
|
||||||
if int(index) > len(image_files):
|
if int(index) > len(image_files):
|
||||||
return "404 not found", ""
|
return "404 not found", ""
|
||||||
|
|
||||||
@ -62,3 +65,25 @@ def raedZip(bookid: str, index: int):
|
|||||||
return "Bad ZipFile", ""
|
return "Bad ZipFile", ""
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return str(e), ""
|
return str(e), ""
|
||||||
|
|
||||||
|
|
||||||
|
def thumbnail(input,size=(400,800)):
|
||||||
|
im = Image.open(io.BytesIO(input))
|
||||||
|
del input
|
||||||
|
im = im.convert('RGB')
|
||||||
|
im.thumbnail(size)
|
||||||
|
output_io = io.BytesIO()
|
||||||
|
im.save(output_io,format='WEBP')
|
||||||
|
output_io.seek(0)
|
||||||
|
return output_io
|
||||||
|
|
||||||
|
def imageToWebP(input):
|
||||||
|
im = Image.open(io.BytesIO(input))
|
||||||
|
del input
|
||||||
|
im = im.convert('RGB')
|
||||||
|
output_io = io.BytesIO()
|
||||||
|
im.save(output_io,format='WEBP')
|
||||||
|
output_io.seek(0)
|
||||||
|
return output_io
|
||||||
|
|
||||||
|
|
||||||
|
9
main.py
9
main.py
@ -65,13 +65,18 @@ def img(bookid, index): # 图片接口
|
|||||||
if request.cookies.get("islogin") is None:
|
if request.cookies.get("islogin") is None:
|
||||||
return abort(403)
|
return abort(403)
|
||||||
if db.searchByid(bookid) == "":
|
if db.searchByid(bookid) == "":
|
||||||
return jsonify({'message': 'No ZIP file part'}), 400
|
return abort(404)
|
||||||
# 设置响应类型为图片
|
# 设置响应类型为图片
|
||||||
data, filename = file.raedZip(bookid,index)
|
data, filename = file.raedZip(bookid,index)
|
||||||
|
if request.args.get("mini") == "yes":
|
||||||
|
data = file.thumbnail(data)
|
||||||
|
else:
|
||||||
|
data = file.imageToWebP(data)
|
||||||
if isinstance(data, str):
|
if isinstance(data, str):
|
||||||
abort(404)
|
abort(404)
|
||||||
response = make_response(data) #读取文件
|
response = make_response(data) #读取文件
|
||||||
response.headers.set('Content-Type', 'image/{}'.format(filename.split('.')[-1]))
|
del data
|
||||||
|
response.headers.set('Content-Type', 'image/Webp')
|
||||||
response.headers.set('Content-Disposition', 'inline', filename=filename)
|
response.headers.set('Content-Disposition', 'inline', filename=filename)
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
@ -1,2 +1,3 @@
|
|||||||
shortuuid
|
shortuuid
|
||||||
flask
|
flask
|
||||||
|
Pillow
|
@ -45,10 +45,6 @@
|
|||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
}
|
}
|
||||||
|
|
||||||
.loading {
|
|
||||||
display: none;
|
|
||||||
}
|
|
||||||
|
|
||||||
#imageContainer {
|
#imageContainer {
|
||||||
display: none;
|
display: none;
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
@ -73,18 +69,18 @@
|
|||||||
<div id="gallery">
|
<div id="gallery">
|
||||||
{% for item in list %}
|
{% for item in list %}
|
||||||
<div class="image-container">
|
<div class="image-container">
|
||||||
<img src="/api/img/{{ item[1] }}/1" class="image" onclick="linkjump('{{ item[1] }}')" />
|
<img src="/api/img/{{ item[1] }}/1?mini=yes" class="image" onclick="linkjump('{{ item[1] }}')" />
|
||||||
<span>{{ item[2] }}</span>
|
<span>{{ item[2] }}</span>
|
||||||
</div>
|
</div>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</div>
|
</div>
|
||||||
<div style="display:flex;justify-content: center; align-items:center;">
|
<div style="display:flex;justify-content: center; align-items:center;">
|
||||||
<div class="btn-group"" role=" toolbar" aria-label="Toolbar with button groups">
|
<div class="btn-group" role=" toolbar" aria-label="Toolbar with button groups">
|
||||||
{% for item in lastPageList %}
|
{% for item in lastPageList %}
|
||||||
<button type="button" class="btn btn-primary" onclick="switchPage('{{item}}')">{{item}}</button>
|
<button type="button" class="btn btn-primary" onclick="switchPage('{{item}}')">{{item}}</button>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</div>
|
</div>
|
||||||
<div class="btn-group"" role=" group" aria-label="Second group">
|
<div class="btn-group" role=" group" aria-label="Second group">
|
||||||
<button type="button" class="btn btn-info">{{pagenow}}</button>
|
<button type="button" class="btn btn-info">{{pagenow}}</button>
|
||||||
</div>
|
</div>
|
||||||
<div class="btn-group" role="group" aria-label="Third group">
|
<div class="btn-group" role="group" aria-label="Third group">
|
||||||
|
@ -60,7 +60,7 @@
|
|||||||
comicContainer.appendChild(img);
|
comicContainer.appendChild(img);
|
||||||
currentImageIndex++;
|
currentImageIndex++;
|
||||||
// 添加延迟,以控制加载速度
|
// 添加延迟,以控制加载速度
|
||||||
setTimeout(loadNextImage, 1000);
|
setTimeout(loadNextImage, 500);
|
||||||
} else {
|
} else {
|
||||||
console.error('Error loading image:', response.statusText);
|
console.error('Error loading image:', response.statusText);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user