backend: always send etags to make use of cache-control
Some checks reported errors
continuous-integration/drone/push Build was killed

This commit is contained in:
Simon Bruder 2019-07-05 20:47:56 +00:00
parent c69117f3c5
commit a35311111e
No known key found for this signature in database
GPG key ID: 6F03E0000CC5B62F
2 changed files with 18 additions and 5 deletions

View file

@ -184,16 +184,19 @@ class CalibreDB:
path = self.get_volume_filepath(volume_id) path = self.get_volume_filepath(volume_id)
with ZipFile(path, 'r') as volume: with ZipFile(path, 'r') as volume:
try: try:
page_filename = volume.filelist[page_number - 1].filename zip_info = volume.filelist[page_number - 1]
except IndexError: except IndexError:
raise exceptions.NotFound() raise exceptions.NotFound()
return None return None
page_filename = zip_info.filename
page_data = BytesIO() page_data = BytesIO()
page_data.write(volume.read(page_filename)) page_data.write(volume.read(page_filename))
page_data.seek(0) page_data.seek(0)
return { return {
'data': page_data, 'data': page_data,
'mimetype': mimetypes[os.path.splitext(page_filename)[1]] 'mimetype': mimetypes[os.path.splitext(page_filename)[1]],
'etag': str(zip_info.CRC)
} }

View file

@ -3,6 +3,7 @@ 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 zlib import crc32
import os import os
@ -10,6 +11,13 @@ def send_from_cwd(filename):
return send_from_directory(os.getcwd(), filename) return send_from_directory(os.getcwd(), filename)
def send_file_with_etag(fp, **kwargs):
response = send_file(fp, **kwargs)
response.set_etag(str(crc32(fp.read())))
fp.seek(0)
return response
app = Flask(__name__, static_folder='frontend/dist/static', template_folder='frontend/dist') app = Flask(__name__, static_folder='frontend/dist/static', template_folder='frontend/dist')
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 604800 # 1 week app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 604800 # 1 week
CORS(app) CORS(app)
@ -37,7 +45,7 @@ def get_series_cover(series_id):
@app.route('/api/series/<int:series_id>/cover/thumbnail') @app.route('/api/series/<int:series_id>/cover/thumbnail')
def get_series_cover_thumbnail(series_id): def get_series_cover_thumbnail(series_id):
thumbnail = db.get_series_cover_thumbnail(series_id) thumbnail = db.get_series_cover_thumbnail(series_id)
return send_file(thumbnail, mimetype='image/jpeg') return send_file_with_etag(thumbnail, mimetype='image/jpeg')
@app.route('/api/series/<int:series_id>') @app.route('/api/series/<int:series_id>')
@ -53,7 +61,7 @@ def get_volume_cover(volume_id):
@app.route('/api/volume/<int:volume_id>/cover/thumbnail') @app.route('/api/volume/<int:volume_id>/cover/thumbnail')
def get_volume_cover_thumbnail(volume_id): def get_volume_cover_thumbnail(volume_id):
thumbnail = db.get_volume_cover_thumbnail(volume_id) thumbnail = db.get_volume_cover_thumbnail(volume_id)
return send_file(thumbnail, mimetype='image/jpeg') return send_file_with_etag(thumbnail, mimetype='image/jpeg')
@app.route('/api/volume/<int:volume_id>') @app.route('/api/volume/<int:volume_id>')
@ -64,7 +72,9 @@ 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)
return send_file(page['data'], mimetype=page['mimetype']) response = send_file(page['data'], mimetype=page['mimetype'])
response.set_etag(page['etag'])
return response
if __name__ == '__main__': if __name__ == '__main__':