backend: move mimetype recognition to backend

This commit is contained in:
Simon Bruder 2019-07-05 19:06:52 +00:00
parent 021476bfa2
commit c5e6b9f4bf
No known key found for this signature in database
GPG key ID: 6F03E0000CC5B62F
2 changed files with 5 additions and 6 deletions

View file

@ -1,10 +1,13 @@
from PIL import Image from PIL import Image
from io import BytesIO from io import BytesIO
from mimetypes import types_map as mimetypes
from zipfile import ZipFile from zipfile import ZipFile
import os.path import os.path
import sqlite3 import sqlite3
import werkzeug.exceptions as exceptions import werkzeug.exceptions as exceptions
mimetypes['.webp'] = 'image/webp'
# https://docs.python.org/3.7/library/sqlite3.html#sqlite3.Connection.row_factory # https://docs.python.org/3.7/library/sqlite3.html#sqlite3.Connection.row_factory
def dict_factory(cursor, row): def dict_factory(cursor, row):
@ -192,5 +195,5 @@ class CalibreDB:
return { return {
'data': page_data, 'data': page_data,
'extension': os.path.splitext(page_filename)[1] 'mimetype': mimetypes[os.path.splitext(page_filename)[1]]
} }

View file

@ -3,7 +3,6 @@ from backend import CalibreDB
from flask import Flask, jsonify, send_from_directory, send_file, render_template, request from flask import Flask, jsonify, send_from_directory, send_file, render_template, request
from flask_cors import CORS from flask_cors import CORS
from werkzeug.routing import BaseConverter from werkzeug.routing import BaseConverter
from mimetypes import types_map as mimetypes
import os import os
@ -14,8 +13,6 @@ def send_from_cwd(filename):
app = Flask(__name__, static_folder='frontend/dist/static', template_folder='frontend/dist') app = Flask(__name__, static_folder='frontend/dist/static', template_folder='frontend/dist')
CORS(app) CORS(app)
mimetypes['.webp'] = 'image/webp'
db = CalibreDB() db = CalibreDB()
# kind of redundant, but avoids returning 200 if page does not exist # 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/<int:volume_id>/page/<int:page_number>') @app.route('/api/volume/<int:volume_id>/page/<int:page_number>')
def get_volume_page(volume_id, page_number): def get_volume_page(volume_id, page_number):
page = db.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=page['mimetype'])
return send_file(page['data'], mimetype=mimetype)
if __name__ == '__main__': if __name__ == '__main__':