From c5e6b9f4bf0b903331f112cf450ee16e18ce1236 Mon Sep 17 00:00:00 2001 From: Simon Bruder Date: Fri, 5 Jul 2019 19:06:52 +0000 Subject: [PATCH] backend: move mimetype recognition to backend --- backend.py | 5 ++++- mangareader.py | 6 +----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/backend.py b/backend.py index f739ca7..787319b 100644 --- a/backend.py +++ b/backend.py @@ -1,10 +1,13 @@ from PIL import Image from io import BytesIO +from mimetypes import types_map as mimetypes from zipfile import ZipFile import os.path import sqlite3 import werkzeug.exceptions as exceptions +mimetypes['.webp'] = 'image/webp' + # https://docs.python.org/3.7/library/sqlite3.html#sqlite3.Connection.row_factory def dict_factory(cursor, row): @@ -192,5 +195,5 @@ class CalibreDB: return { 'data': page_data, - 'extension': os.path.splitext(page_filename)[1] + 'mimetype': mimetypes[os.path.splitext(page_filename)[1]] } diff --git a/mangareader.py b/mangareader.py index 9f2ce5b..d61039e 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 mimetypes import types_map as mimetypes import os @@ -14,8 +13,6 @@ def send_from_cwd(filename): app = Flask(__name__, static_folder='frontend/dist/static', template_folder='frontend/dist') CORS(app) -mimetypes['.webp'] = 'image/webp' - db = CalibreDB() # kind of redundant, but avoids returning 200 if page does not exist @@ -66,8 +63,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) - mimetype = mimetypes[page['extension']] - return send_file(page['data'], mimetype=mimetype) + return send_file(page['data'], mimetype=page['mimetype']) if __name__ == '__main__':