2019-07-04 18:06:44 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
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
|
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
|
|
def send_from_cwd(filename):
|
|
|
|
return send_from_directory(os.getcwd(), filename)
|
|
|
|
|
|
|
|
|
2019-07-19 19:26:09 +02:00
|
|
|
def send_file_with_etag(fp, etag, **kwargs):
|
2019-07-13 15:55:41 +02:00
|
|
|
if request.if_none_match and etag in request.if_none_match:
|
|
|
|
return '', 304
|
|
|
|
|
2019-07-05 22:47:56 +02:00
|
|
|
response = send_file(fp, **kwargs)
|
2019-07-13 15:55:41 +02:00
|
|
|
response.set_etag(etag)
|
2019-07-05 22:47:56 +02:00
|
|
|
fp.seek(0)
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
2019-07-19 19:26:09 +02:00
|
|
|
def send_image(image):
|
2019-12-31 16:17:37 +01:00
|
|
|
return send_file_with_etag(image['buffer'], image['etag'], mimetype=image['mimetype'])
|
2019-07-19 19:26:09 +02:00
|
|
|
|
|
|
|
|
2019-07-04 18:06:44 +02:00
|
|
|
app = Flask(__name__, static_folder='frontend/dist/static', template_folder='frontend/dist')
|
2019-07-05 21:17:42 +02:00
|
|
|
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 604800 # 1 week
|
2019-07-04 18:06:44 +02:00
|
|
|
CORS(app)
|
|
|
|
|
|
|
|
db = CalibreDB()
|
|
|
|
|
|
|
|
# kind of redundant, but avoids returning 200 if page does not exist
|
|
|
|
@app.route('/')
|
|
|
|
@app.route('/series/<id>')
|
|
|
|
@app.route('/volume/<id>')
|
|
|
|
def serve_app(**kwargs):
|
|
|
|
return render_template('index.html')
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/api/series')
|
|
|
|
def get_series_list():
|
|
|
|
return jsonify(list(db.get_series_list()))
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/api/series/<int:series_id>/cover')
|
|
|
|
def get_series_cover(series_id):
|
|
|
|
return send_from_cwd(db.get_series_cover(series_id))
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/api/series/<int:series_id>/cover/thumbnail')
|
|
|
|
def get_series_cover_thumbnail(series_id):
|
|
|
|
thumbnail = db.get_series_cover_thumbnail(series_id)
|
2019-07-19 19:26:09 +02:00
|
|
|
return send_image(thumbnail)
|
2019-07-04 18:06:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
@app.route('/api/series/<int:series_id>')
|
|
|
|
def get_series_info(series_id):
|
|
|
|
return jsonify(db.get_series_volumes(series_id))
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/api/volume/<int:volume_id>/cover')
|
|
|
|
def get_volume_cover(volume_id):
|
|
|
|
return send_from_cwd(db.get_volume_cover(volume_id))
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/api/volume/<int:volume_id>/cover/thumbnail')
|
|
|
|
def get_volume_cover_thumbnail(volume_id):
|
|
|
|
thumbnail = db.get_volume_cover_thumbnail(volume_id)
|
2019-07-19 19:26:09 +02:00
|
|
|
return send_image(thumbnail)
|
2019-07-04 18:06:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
@app.route('/api/volume/<int:volume_id>')
|
|
|
|
def get_volume_info(volume_id):
|
|
|
|
return jsonify(db.get_volume_info(volume_id))
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/api/volume/<int:volume_id>/page/<int:page_number>')
|
|
|
|
def get_volume_page(volume_id, page_number):
|
|
|
|
page = db.get_volume_page(volume_id, page_number)
|
2019-07-19 19:26:09 +02:00
|
|
|
return send_image(page)
|
2019-07-04 18:06:44 +02:00
|
|
|
|
2019-12-22 17:29:00 +01:00
|
|
|
@app.route('/api/volume/<int:volume_id>/page/<int:page_number>/original')
|
|
|
|
def get_volume_page_original(volume_id, page_number):
|
|
|
|
page = db.get_volume_page(volume_id, page_number, original=True)
|
|
|
|
return send_image(page)
|
|
|
|
|
2019-07-04 18:06:44 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
app.run(host='0.0.0.0')
|