2015-08-02 20:59:11 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2019-07-13 20:45:48 +02:00
|
|
|
|
|
|
|
# This file is part of the Calibre-Web (https://github.com/janeczku/calibre-web)
|
|
|
|
# Copyright (C) 2018-2019 OzzieIsaacs, cervinko, jkrehm, bodybybuddha, ok11,
|
|
|
|
# andy29485, idalin, Kyosfonica, wuqi, Kennyl, lemmsh,
|
|
|
|
# falgh1, grunjol, csitko, ytils, xybydy, trasba, vrabe,
|
|
|
|
# ruben-herold, marblepebble, JackED42, SiphonSquirrel,
|
|
|
|
# apetresc, nanu-c, mutschler
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2021-11-27 12:26:28 +01:00
|
|
|
__package__ = "cps"
|
2019-07-13 20:45:48 +02:00
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import mimetypes
|
|
|
|
|
|
|
|
from babel import Locale as LC
|
|
|
|
from babel import negotiate_locale
|
|
|
|
from babel.core import UnknownLocaleError
|
|
|
|
from flask import Flask, request, g
|
2021-10-10 10:26:01 +02:00
|
|
|
from .MyLoginManager import MyLoginManager
|
2019-07-13 20:45:48 +02:00
|
|
|
from flask_babel import Babel
|
|
|
|
from flask_principal import Principal
|
|
|
|
|
2020-03-28 07:13:51 +01:00
|
|
|
from . import config_sql, logger, cache_buster, cli, ub, db
|
2019-07-13 20:45:48 +02:00
|
|
|
from .reverseproxy import ReverseProxied
|
2019-07-14 19:28:32 +02:00
|
|
|
from .server import WebServer
|
2021-11-04 13:38:55 +01:00
|
|
|
from .dep_check import dependency_check
|
2019-07-13 20:45:48 +02:00
|
|
|
|
2021-08-27 14:27:35 +02:00
|
|
|
try:
|
|
|
|
import lxml
|
|
|
|
lxml_present = True
|
|
|
|
except ImportError:
|
|
|
|
lxml_present = False
|
2020-05-21 18:16:11 +02:00
|
|
|
|
2021-10-04 17:50:32 +02:00
|
|
|
try:
|
|
|
|
from flask_wtf.csrf import CSRFProtect
|
|
|
|
wtf_present = True
|
|
|
|
except ImportError:
|
|
|
|
wtf_present = False
|
|
|
|
|
2019-07-13 20:45:48 +02:00
|
|
|
mimetypes.init()
|
|
|
|
mimetypes.add_type('application/xhtml+xml', '.xhtml')
|
|
|
|
mimetypes.add_type('application/epub+zip', '.epub')
|
|
|
|
mimetypes.add_type('application/fb2+zip', '.fb2')
|
|
|
|
mimetypes.add_type('application/x-mobipocket-ebook', '.mobi')
|
|
|
|
mimetypes.add_type('application/x-mobipocket-ebook', '.prc')
|
|
|
|
mimetypes.add_type('application/vnd.amazon.ebook', '.azw')
|
2021-01-17 07:54:28 +01:00
|
|
|
mimetypes.add_type('application/x-mobi8-ebook', '.azw3')
|
2019-07-13 20:45:48 +02:00
|
|
|
mimetypes.add_type('application/x-cbr', '.cbr')
|
|
|
|
mimetypes.add_type('application/x-cbz', '.cbz')
|
|
|
|
mimetypes.add_type('application/x-cbt', '.cbt')
|
|
|
|
mimetypes.add_type('image/vnd.djvu', '.djvu')
|
|
|
|
mimetypes.add_type('application/mpeg', '.mpeg')
|
|
|
|
mimetypes.add_type('application/mpeg', '.mp3')
|
|
|
|
mimetypes.add_type('application/mp4', '.m4a')
|
|
|
|
mimetypes.add_type('application/mp4', '.m4b')
|
|
|
|
mimetypes.add_type('application/ogg', '.ogg')
|
|
|
|
mimetypes.add_type('application/ogg', '.oga')
|
2021-10-27 05:33:30 +02:00
|
|
|
mimetypes.add_type('text/css', '.css')
|
|
|
|
mimetypes.add_type('text/javascript; charset=UTF-8', '.js')
|
2019-07-13 20:45:48 +02:00
|
|
|
|
|
|
|
app = Flask(__name__)
|
2020-05-09 14:42:28 +02:00
|
|
|
app.config.update(
|
|
|
|
SESSION_COOKIE_HTTPONLY=True,
|
|
|
|
SESSION_COOKIE_SAMESITE='Lax',
|
2020-05-10 19:04:29 +02:00
|
|
|
REMEMBER_COOKIE_SAMESITE='Lax', # will be available in flask-login 0.5.1 earliest
|
2021-10-17 16:00:02 +02:00
|
|
|
WTF_CSRF_SSL_STRICT=False
|
2020-05-09 14:42:28 +02:00
|
|
|
)
|
|
|
|
|
2019-07-13 20:45:48 +02:00
|
|
|
|
2021-10-10 10:26:01 +02:00
|
|
|
lm = MyLoginManager()
|
2019-07-13 20:45:48 +02:00
|
|
|
lm.login_view = 'web.login'
|
|
|
|
lm.anonymous_user = ub.Anonymous
|
2020-05-09 14:34:14 +02:00
|
|
|
lm.session_protection = 'strong'
|
2019-07-13 20:45:48 +02:00
|
|
|
|
2021-10-04 17:50:32 +02:00
|
|
|
if wtf_present:
|
|
|
|
csrf = CSRFProtect()
|
|
|
|
csrf.init_app(app)
|
|
|
|
else:
|
|
|
|
csrf = None
|
|
|
|
|
2019-07-13 20:45:48 +02:00
|
|
|
ub.init_db(cli.settingspath)
|
2019-07-14 13:43:40 +02:00
|
|
|
# pylint: disable=no-member
|
2019-07-13 20:45:48 +02:00
|
|
|
config = config_sql.load_configuration(ub.session)
|
|
|
|
|
|
|
|
web_server = WebServer()
|
|
|
|
|
|
|
|
babel = Babel()
|
|
|
|
_BABEL_TRANSLATIONS = set()
|
|
|
|
|
|
|
|
log = logger.create()
|
|
|
|
|
2021-11-04 13:38:55 +01:00
|
|
|
|
2020-03-28 07:13:51 +01:00
|
|
|
from . import services
|
2019-07-13 20:45:48 +02:00
|
|
|
|
2021-05-26 13:35:35 +02:00
|
|
|
db.CalibreDB.update_config(config)
|
|
|
|
db.CalibreDB.setup_db(config.config_calibre_dir, cli.settingspath)
|
|
|
|
|
2020-09-12 04:52:40 +02:00
|
|
|
|
2020-05-21 18:16:11 +02:00
|
|
|
calibre_db = db.CalibreDB()
|
|
|
|
|
2019-07-13 20:45:48 +02:00
|
|
|
def create_app():
|
2021-08-27 14:27:35 +02:00
|
|
|
if sys.version_info < (3, 0):
|
|
|
|
log.info(
|
|
|
|
'*** Python2 is EOL since end of 2019, this version of Calibre-Web is no longer supporting Python2, please update your installation to Python3 ***')
|
|
|
|
print(
|
|
|
|
'*** Python2 is EOL since end of 2019, this version of Calibre-Web is no longer supporting Python2, please update your installation to Python3 ***')
|
2021-10-23 18:44:29 +02:00
|
|
|
web_server.stop(True)
|
2021-08-27 14:27:35 +02:00
|
|
|
sys.exit(5)
|
|
|
|
if not lxml_present:
|
|
|
|
log.info('*** "lxml" is needed for calibre-web to run. Please install it using pip: "pip install lxml" ***')
|
|
|
|
print('*** "lxml" is needed for calibre-web to run. Please install it using pip: "pip install lxml" ***')
|
2021-10-23 18:44:29 +02:00
|
|
|
web_server.stop(True)
|
2021-08-27 14:27:35 +02:00
|
|
|
sys.exit(6)
|
2021-10-04 17:50:32 +02:00
|
|
|
if not wtf_present:
|
2021-10-04 18:26:46 +02:00
|
|
|
log.info('*** "flask-WTF" is needed for calibre-web to run. Please install it using pip: "pip install flask-WTF" ***')
|
|
|
|
print('*** "flask-WTF" is needed for calibre-web to run. Please install it using pip: "pip install flask-WTF" ***')
|
2021-10-23 18:44:29 +02:00
|
|
|
web_server.stop(True)
|
2021-10-16 18:42:26 +02:00
|
|
|
sys.exit(7)
|
2021-11-04 13:38:55 +01:00
|
|
|
for res in dependency_check() + dependency_check(True):
|
2021-11-06 18:17:48 +01:00
|
|
|
log.info('*** "{}" version does not fit the requirements. Should: {}, Found: {}, please consider installing required version ***'
|
2021-11-04 13:38:55 +01:00
|
|
|
.format(res['name'],
|
|
|
|
res['target'],
|
|
|
|
res['found']))
|
2020-05-02 18:17:52 +02:00
|
|
|
app.wsgi_app = ReverseProxied(app.wsgi_app)
|
2019-07-13 20:45:48 +02:00
|
|
|
|
2020-12-08 11:39:23 +01:00
|
|
|
if os.environ.get('FLASK_DEBUG'):
|
2021-03-27 17:30:10 +01:00
|
|
|
cache_buster.init_cache_busting(app)
|
2019-07-13 20:45:48 +02:00
|
|
|
log.info('Starting Calibre Web...')
|
2021-08-27 14:27:35 +02:00
|
|
|
|
2019-07-13 20:45:48 +02:00
|
|
|
Principal(app)
|
|
|
|
lm.init_app(app)
|
2020-05-04 19:02:03 +02:00
|
|
|
app.secret_key = os.getenv('SECRET_KEY', config_sql.get_flask_session_key(ub.session))
|
2019-07-13 20:45:48 +02:00
|
|
|
|
|
|
|
web_server.init_app(app, config)
|
|
|
|
|
|
|
|
babel.init_app(app)
|
|
|
|
_BABEL_TRANSLATIONS.update(str(item) for item in babel.list_translations())
|
|
|
|
_BABEL_TRANSLATIONS.add('en')
|
|
|
|
|
|
|
|
if services.ldap:
|
|
|
|
services.ldap.init_app(app, config)
|
2019-08-20 18:32:04 +02:00
|
|
|
if services.goodreads_support:
|
|
|
|
services.goodreads_support.connect(config.config_goodreads_api_key,
|
|
|
|
config.config_goodreads_api_secret,
|
|
|
|
config.config_use_goodreads)
|
2022-03-13 12:34:21 +01:00
|
|
|
config.store_calibre_uuid(calibre_db, db.LibraryId)
|
2019-07-13 20:45:48 +02:00
|
|
|
return app
|
|
|
|
|
|
|
|
@babel.localeselector
|
2019-09-06 20:56:17 +02:00
|
|
|
def get_locale():
|
2019-07-13 20:45:48 +02:00
|
|
|
# if a user is logged in, use the locale from the user settings
|
|
|
|
user = getattr(g, 'user', None)
|
|
|
|
if user is not None and hasattr(user, "locale"):
|
2021-03-21 18:55:02 +01:00
|
|
|
if user.name != 'Guest': # if the account is the guest account bypass the config lang settings
|
2019-07-13 20:45:48 +02:00
|
|
|
return user.locale
|
|
|
|
|
2020-02-01 13:40:29 +01:00
|
|
|
preferred = list()
|
2019-07-13 20:45:48 +02:00
|
|
|
if request.accept_languages:
|
|
|
|
for x in request.accept_languages.values():
|
|
|
|
try:
|
2020-02-01 13:40:29 +01:00
|
|
|
preferred.append(str(LC.parse(x.replace('-', '_'))))
|
2019-07-13 20:45:48 +02:00
|
|
|
except (UnknownLocaleError, ValueError) as e:
|
2020-02-01 13:40:29 +01:00
|
|
|
log.debug('Could not parse locale "%s": %s', x, e)
|
2019-07-13 20:45:48 +02:00
|
|
|
|
|
|
|
return negotiate_locale(preferred or ['en'], _BABEL_TRANSLATIONS)
|
|
|
|
|
|
|
|
|
|
|
|
@babel.timezoneselector
|
|
|
|
def get_timezone():
|
|
|
|
user = getattr(g, 'user', None)
|
2019-07-17 19:02:53 +02:00
|
|
|
return user.timezone if user else None
|
2019-07-13 20:45:48 +02:00
|
|
|
|
2021-07-05 18:55:54 +02:00
|
|
|
|
2019-07-13 20:45:48 +02:00
|
|
|
from .updater import Updater
|
|
|
|
updater_thread = Updater()
|
2022-01-29 10:44:33 +01:00
|
|
|
|
|
|
|
# Perform dry run of updater and exit afterwards
|
|
|
|
if cli.dry_run:
|
|
|
|
updater_thread.dry_run()
|
|
|
|
sys.exit(0)
|
2020-03-29 16:22:11 +02:00
|
|
|
updater_thread.start()
|