#!/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) app = Flask(__name__, static_folder='frontend/dist/static', template_folder='frontend/dist') app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 604800 # 1 week CORS(app) db = CalibreDB() # kind of redundant, but avoids returning 200 if page does not exist @app.route('/') @app.route('/series/') @app.route('/volume/') 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//cover') def get_series_cover(series_id): return send_from_cwd(db.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(thumbnail, mimetype='image/jpeg') @app.route('/api/series/') def get_series_info(series_id): return jsonify(db.get_series_volumes(series_id)) @app.route('/api/volume//cover') def get_volume_cover(volume_id): return send_from_cwd(db.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(thumbnail, mimetype='image/jpeg') @app.route('/api/volume/') def get_volume_info(volume_id): return jsonify(db.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) return send_file(page['data'], mimetype=page['mimetype']) if __name__ == '__main__': app.run(host='0.0.0.0')