diff --git a/backend.py b/backend.py index 401a93e..08ae268 100644 --- a/backend.py +++ b/backend.py @@ -2,6 +2,7 @@ from PIL import Image from io import BytesIO from mimetypes import types_map as mimetypes from zipfile import ZipFile +from zlib import crc32 import os.path import sqlite3 import werkzeug.exceptions as exceptions @@ -20,23 +21,28 @@ def dict_factory(cursor, row): def generate_thumbnail(filepath): - if filepath in thumbnail_cache: - thumbnail = BytesIO() - thumbnail.write(thumbnail_cache[filepath]) - else: + if filepath not in thumbnail_cache: image = Image.open(filepath) image.thumbnail((512, 512)) thumbnail = BytesIO() image.save(thumbnail, 'webp') thumbnail.seek(0) - thumbnail_cache[filepath] = thumbnail.read() - thumbnail.seek(0) + data = thumbnail.read() + etag = str(crc32(data)) - return { - 'data': thumbnail, - 'mimetype': 'image/webp' - } + thumbnail_cache[filepath] = { + 'data_raw': data, + 'etag': etag, + 'mimetype': 'image/webp' + } + + thumbnail = thumbnail_cache[filepath] + thumbnail['data'] = BytesIO() + thumbnail['data'].write(thumbnail['data_raw']) + thumbnail['data'].seek(0) + + return thumbnail class CalibreDB: diff --git a/mangareader.py b/mangareader.py index 7a18512..49e6235 100755 --- a/mangareader.py +++ b/mangareader.py @@ -3,7 +3,6 @@ from backend import CalibreDB from flask import Flask, jsonify, send_from_directory, send_file, render_template, request from flask_cors import CORS from werkzeug.routing import BaseConverter -from zlib import crc32 import os @@ -11,10 +10,7 @@ def send_from_cwd(filename): return send_from_directory(os.getcwd(), filename) -def send_file_with_etag(fp, etag=None, **kwargs): - if etag is None: - etag = str(crc32(fp.read())) - +def send_file_with_etag(fp, etag, **kwargs): if request.if_none_match and etag in request.if_none_match: return '', 304 @@ -24,6 +20,10 @@ def send_file_with_etag(fp, etag=None, **kwargs): return response +def send_image(image): + return send_file_with_etag(image['data'], image['etag'], mimetype=image['mimetype']) + + app = Flask(__name__, static_folder='frontend/dist/static', template_folder='frontend/dist') app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 604800 # 1 week CORS(app) @@ -51,7 +51,7 @@ def get_series_cover(series_id): @app.route('/api/series//cover/thumbnail') def get_series_cover_thumbnail(series_id): thumbnail = db.get_series_cover_thumbnail(series_id) - return send_file_with_etag(thumbnail['data'], mimetype=thumbnail['mimetype']) + return send_image(thumbnail) @app.route('/api/series/') @@ -67,7 +67,7 @@ def get_volume_cover(volume_id): @app.route('/api/volume//cover/thumbnail') def get_volume_cover_thumbnail(volume_id): thumbnail = db.get_volume_cover_thumbnail(volume_id) - return send_file_with_etag(thumbnail['data'], mimetype=thumbnail['mimetype']) + return send_image(thumbnail) @app.route('/api/volume/') @@ -78,8 +78,7 @@ def get_volume_info(volume_id): @app.route('/api/volume//page/') def get_volume_page(volume_id, page_number): page = db.get_volume_page(volume_id, page_number) - response = send_file_with_etag(page['data'], etag=page['etag'], mimetype=page['mimetype']) - return response + return send_image(page) if __name__ == '__main__':