diff --git a/file.py b/file.py index 868c3d6..03cbaeb 100644 --- a/file.py +++ b/file.py @@ -59,6 +59,7 @@ def raedZip(bookid: str, index: int): # 读取图片数据 image_data = zip_ref.read(image_filename) + zip_ref.close() return image_data, image_filename except zipfile.BadZipFile: # 异常处理 @@ -74,6 +75,7 @@ def thumbnail(input,size=(400,800)): im.thumbnail(size) output_io = io.BytesIO() im.save(output_io,format='WEBP') + im.close() output_io.seek(0) return output_io @@ -83,6 +85,7 @@ def imageToWebP(input): im = im.convert('RGB') output_io = io.BytesIO() im.save(output_io,format='WEBP') + im.close() output_io.seek(0) return output_io diff --git a/main.py b/main.py index ef72ba0..7b01f3c 100644 --- a/main.py +++ b/main.py @@ -1,4 +1,4 @@ -import configparser, os +import configparser, gc import db, file from flask import * @@ -68,16 +68,17 @@ def img(bookid, index): # 图片接口 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) else: data = file.imageToWebP(data) - if isinstance(data, str): - abort(404) response = make_response(data) #读取文件 del data response.headers.set('Content-Type', 'image/Webp') response.headers.set('Content-Disposition', 'inline', filename=filename) + gc.collect() return response @@ -85,7 +86,10 @@ def img(bookid, index): # 图片接口 def book(bookid): # 接口 if request.cookies.get("islogin") is None: return abort(403) - return render_template("view.html",data = bookid) + data = db.searchByid(bookid) + if data == "": + return abort(404) + return render_template("view.html",id = bookid , index = range(1,data[0][3])) @app.route("/view/") diff --git a/static/loading.gif b/static/loading.gif new file mode 100644 index 0000000..358e038 Binary files /dev/null and b/static/loading.gif differ diff --git a/templates/view.html b/templates/view.html index a12f17b..95f007a 100644 --- a/templates/view.html +++ b/templates/view.html @@ -1,5 +1,6 @@ + @@ -23,56 +24,49 @@ height: auto; margin-bottom: 20px; } + + img { + display: block; + width: 100%; + min-height: 100px; + margin-top: 10px; + } + -
+ {% for i in index %} + {{ i }} + {% endfor %} +
+ +
+ + function lazyLoad(imgs) { + var H = document.documentElement.clientHeight;//获取可视区域高度 + var S = document.documentElement.scrollTop || document.body.scrollTop; + for (var i = 0; i < imgs.length; i++) { + if (H + S > getTop(imgs[i])) { + imgs[i].src = imgs[i].getAttribute('data-src'); + } + } + } + + window.onload = window.onscroll = function () { //onscroll()在滚动条滚动的时候触发 + lazyLoad(imgs); + } + - + + \ No newline at end of file