2015-08-02 18:59:11 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2019-07-13 18:45:48 +00: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/>.
|
2022-04-29 16:16:37 +00:00
|
|
|
__package__ = "cps"
|
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import mimetypes
|
|
|
|
|
2022-04-26 09:04:00 +00:00
|
|
|
from flask import Flask
|
2021-10-10 08:26:01 +00:00
|
|
|
from .MyLoginManager import MyLoginManager
|
2019-07-13 18:45:48 +00:00
|
|
|
from flask_principal import Principal
|
|
|
|
|
2022-04-26 12:44:55 +00:00
|
|
|
from . import logger
|
2022-04-26 09:04:00 +00:00
|
|
|
from .cli import CliParameter
|
2019-07-13 18:45:48 +00:00
|
|
|
from .reverseproxy import ReverseProxied
|
2019-07-14 17:28:32 +00:00
|
|
|
from .server import WebServer
|
2021-11-04 12:38:55 +00:00
|
|
|
from .dep_check import dependency_check
|
2022-04-26 09:04:00 +00:00
|
|
|
from .updater import Updater
|
2022-04-26 09:28:20 +00:00
|
|
|
from . import config_sql
|
|
|
|
from . import cache_buster
|
|
|
|
from . import ub, db
|
2020-05-21 16:16:11 +00:00
|
|
|
|
2023-02-05 12:43:35 +00:00
|
|
|
try:
|
|
|
|
from flask_limiter import Limiter
|
|
|
|
limiter_present = True
|
|
|
|
except ImportError:
|
|
|
|
limiter_present = False
|
2021-10-04 15:50:32 +00:00
|
|
|
try:
|
|
|
|
from flask_wtf.csrf import CSRFProtect
|
|
|
|
wtf_present = True
|
|
|
|
except ImportError:
|
|
|
|
wtf_present = False
|
|
|
|
|
2022-04-26 09:04:00 +00:00
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
mimetypes.init()
|
|
|
|
mimetypes.add_type('application/xhtml+xml', '.xhtml')
|
|
|
|
mimetypes.add_type('application/epub+zip', '.epub')
|
2024-06-23 10:00:12 +00:00
|
|
|
mimetypes.add_type('application/epub+zip', '.kepub')
|
2024-12-02 14:31:46 +00:00
|
|
|
mimetypes.add_type('application/fb2+zip', '.fb2')
|
2024-06-28 17:38:01 +00:00
|
|
|
mimetypes.add_type('application/octet-stream', '.mobi')
|
|
|
|
mimetypes.add_type('application/octet-stream', '.prc')
|
2019-07-13 18:45:48 +00:00
|
|
|
mimetypes.add_type('application/vnd.amazon.ebook', '.azw')
|
2021-01-17 06:54:28 +00:00
|
|
|
mimetypes.add_type('application/x-mobi8-ebook', '.azw3')
|
2024-12-02 14:31:46 +00:00
|
|
|
mimetypes.add_type('application/x-cbr', '.cbr')
|
|
|
|
mimetypes.add_type('application/x-cbz', '.cbz')
|
2024-06-24 12:39:28 +00:00
|
|
|
mimetypes.add_type('application/x-tar', '.cbt')
|
|
|
|
mimetypes.add_type('application/x-7z-compressed', '.cb7')
|
2023-08-16 16:44:03 +00:00
|
|
|
mimetypes.add_type('image/vnd.djv', '.djv')
|
2024-06-23 10:00:12 +00:00
|
|
|
mimetypes.add_type('image/vnd.djv', '.djvu')
|
2019-07-13 18:45:48 +00:00
|
|
|
mimetypes.add_type('application/mpeg', '.mpeg')
|
2024-06-28 17:38:01 +00:00
|
|
|
mimetypes.add_type('audio/mpeg', '.mp3')
|
2024-07-11 18:10:15 +00:00
|
|
|
mimetypes.add_type('audio/x-m4a', '.m4a')
|
|
|
|
mimetypes.add_type('audio/x-m4a', '.m4b')
|
2024-08-10 16:09:52 +00:00
|
|
|
mimetypes.add_type('audio/x-hx-aac-adts', '.aac')
|
|
|
|
mimetypes.add_type('audio/vnd.dolby.dd-raw', '.ac3')
|
|
|
|
mimetypes.add_type('video/x-ms-asf', '.asf')
|
2024-06-28 17:38:01 +00:00
|
|
|
mimetypes.add_type('audio/ogg', '.ogg')
|
2019-07-13 18:45:48 +00:00
|
|
|
mimetypes.add_type('application/ogg', '.oga')
|
2021-10-27 03:33:30 +00:00
|
|
|
mimetypes.add_type('text/css', '.css')
|
2024-06-23 10:00:12 +00:00
|
|
|
mimetypes.add_type('application/x-ms-reader', '.lit')
|
2021-10-27 03:33:30 +00:00
|
|
|
mimetypes.add_type('text/javascript; charset=UTF-8', '.js')
|
2019-07-13 18:45:48 +00:00
|
|
|
|
2022-04-26 09:28:20 +00:00
|
|
|
log = logger.create()
|
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
app = Flask(__name__)
|
2020-05-09 12:42:28 +00:00
|
|
|
app.config.update(
|
|
|
|
SESSION_COOKIE_HTTPONLY=True,
|
2024-09-21 11:01:48 +00:00
|
|
|
SESSION_COOKIE_SAMESITE='Lax',
|
2024-08-07 11:53:27 +00:00
|
|
|
REMEMBER_COOKIE_SAMESITE='Strict',
|
2024-08-04 13:52:19 +00:00
|
|
|
WTF_CSRF_SSL_STRICT=False,
|
|
|
|
SESSION_COOKIE_NAME=os.environ.get('COOKIE_PREFIX', "") + "session",
|
|
|
|
REMEMBER_COOKIE_NAME=os.environ.get('COOKIE_PREFIX', "") + "remember_token"
|
2020-05-09 12:42:28 +00:00
|
|
|
)
|
|
|
|
|
2021-10-10 08:26:01 +00:00
|
|
|
lm = MyLoginManager()
|
2022-04-26 09:04:00 +00:00
|
|
|
|
|
|
|
cli_param = CliParameter()
|
2019-07-13 18:45:48 +00:00
|
|
|
|
2023-05-28 18:32:19 +00:00
|
|
|
config = config_sql.ConfigSQL()
|
|
|
|
|
2021-10-04 15:50:32 +00:00
|
|
|
if wtf_present:
|
|
|
|
csrf = CSRFProtect()
|
|
|
|
else:
|
|
|
|
csrf = None
|
|
|
|
|
2024-11-26 05:13:10 +00:00
|
|
|
calibre_db = db.CalibreDB(app)
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
web_server = WebServer()
|
|
|
|
|
2022-04-26 09:04:00 +00:00
|
|
|
updater_thread = Updater()
|
2019-07-13 18:45:48 +00:00
|
|
|
|
2023-02-05 12:43:35 +00:00
|
|
|
if limiter_present:
|
2024-02-25 05:59:25 +00:00
|
|
|
limiter = Limiter(key_func=True, headers_enabled=True, auto_check=False, swallow_errors=False)
|
2023-02-05 12:43:35 +00:00
|
|
|
else:
|
|
|
|
limiter = None
|
2019-07-13 18:45:48 +00:00
|
|
|
|
2024-06-18 18:13:26 +00:00
|
|
|
|
2022-04-26 09:04:00 +00:00
|
|
|
def create_app():
|
|
|
|
if csrf:
|
|
|
|
csrf.init_app(app)
|
2019-07-13 18:45:48 +00:00
|
|
|
|
2022-04-26 09:04:00 +00:00
|
|
|
cli_param.init()
|
2021-05-26 11:35:35 +00:00
|
|
|
|
2023-02-15 19:17:10 +00:00
|
|
|
ub.init_db(cli_param.settings_path)
|
2022-04-26 09:04:00 +00:00
|
|
|
# pylint: disable=no-member
|
2022-07-02 15:45:24 +00:00
|
|
|
encrypt_key, error = config_sql.get_encryption_key(os.path.dirname(cli_param.settings_path))
|
|
|
|
|
|
|
|
config_sql.load_configuration(ub.session, encrypt_key)
|
|
|
|
config.init_config(ub.session, encrypt_key, cli_param)
|
2023-02-15 19:17:10 +00:00
|
|
|
|
2022-07-02 15:45:24 +00:00
|
|
|
if error:
|
|
|
|
log.error(error)
|
2022-04-26 09:04:00 +00:00
|
|
|
|
2023-02-15 19:17:10 +00:00
|
|
|
ub.password_change(cli_param.user_credentials)
|
|
|
|
|
2021-08-27 12:27:35 +00:00
|
|
|
if sys.version_info < (3, 0):
|
|
|
|
log.info(
|
2022-04-26 09:28:20 +00:00
|
|
|
'*** Python2 is EOL since end of 2019, this version of Calibre-Web is no longer supporting Python2, '
|
|
|
|
'please update your installation to Python3 ***')
|
2021-08-27 12:27:35 +00:00
|
|
|
print(
|
2022-04-26 09:28:20 +00:00
|
|
|
'*** 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 16:44:29 +00:00
|
|
|
web_server.stop(True)
|
2021-08-27 12:27:35 +00:00
|
|
|
sys.exit(5)
|
2023-02-05 12:43:35 +00:00
|
|
|
|
|
|
|
lm.login_view = 'web.login'
|
|
|
|
lm.anonymous_user = ub.Anonymous
|
|
|
|
lm.session_protection = 'strong' if config.config_session == 1 else "basic"
|
|
|
|
|
2024-11-26 05:13:10 +00:00
|
|
|
db.CalibreDB.update_config(config, config.config_calibre_dir, cli_param.settings_path)
|
2023-02-05 12:43:35 +00:00
|
|
|
|
|
|
|
updater_thread.init_updater(config, web_server)
|
2024-02-29 07:23:18 +00:00
|
|
|
# Perform dry run of updater and exit afterward
|
2023-02-05 12:43:35 +00:00
|
|
|
if cli_param.dry_run:
|
|
|
|
updater_thread.dry_run()
|
|
|
|
sys.exit(0)
|
|
|
|
updater_thread.start()
|
2024-02-29 07:23:18 +00:00
|
|
|
requirements = dependency_check()
|
|
|
|
for res in requirements:
|
|
|
|
if res['found'] == "not installed":
|
|
|
|
message = ('Cannot import {name} module, it is needed to run calibre-web, '
|
|
|
|
'please install it using "pip install {name}"').format(name=res["name"])
|
|
|
|
log.info(message)
|
|
|
|
print("*** " + message + " ***")
|
|
|
|
web_server.stop(True)
|
|
|
|
sys.exit(8)
|
|
|
|
for res in requirements + dependency_check(True):
|
2023-01-21 16:01:34 +00:00
|
|
|
log.info('*** "{}" version does not meet the requirements. '
|
2022-04-26 09:28:20 +00:00
|
|
|
'Should: {}, Found: {}, please consider installing required version ***'
|
|
|
|
.format(res['name'],
|
|
|
|
res['target'],
|
|
|
|
res['found']))
|
2020-05-02 16:17:52 +00:00
|
|
|
app.wsgi_app = ReverseProxied(app.wsgi_app)
|
2019-07-13 18:45:48 +00:00
|
|
|
|
2020-12-08 10:39:23 +00:00
|
|
|
if os.environ.get('FLASK_DEBUG'):
|
2021-03-27 16:30:10 +00:00
|
|
|
cache_buster.init_cache_busting(app)
|
2019-07-13 18:45:48 +00:00
|
|
|
log.info('Starting Calibre Web...')
|
|
|
|
Principal(app)
|
|
|
|
lm.init_app(app)
|
2020-05-04 17:02:03 +00:00
|
|
|
app.secret_key = os.getenv('SECRET_KEY', config_sql.get_flask_session_key(ub.session))
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
web_server.init_app(app, config)
|
2024-09-16 15:21:39 +00:00
|
|
|
from .cw_babel import babel, get_locale
|
2023-01-22 11:09:19 +00:00
|
|
|
if hasattr(babel, "localeselector"):
|
|
|
|
babel.init_app(app)
|
|
|
|
babel.localeselector(get_locale)
|
|
|
|
else:
|
|
|
|
babel.init_app(app, locale_selector=get_locale)
|
2022-04-26 12:44:55 +00:00
|
|
|
|
|
|
|
from . import services
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
if services.ldap:
|
|
|
|
services.ldap.init_app(app, config)
|
2019-08-20 16:32:04 +00:00
|
|
|
if services.goodreads_support:
|
|
|
|
services.goodreads_support.connect(config.config_goodreads_api_key,
|
|
|
|
config.config_use_goodreads)
|
2022-04-26 09:04:00 +00:00
|
|
|
config.store_calibre_uuid(calibre_db, db.Library_Id)
|
2023-02-05 12:43:35 +00:00
|
|
|
# Configure rate limiter
|
2024-02-25 05:59:25 +00:00
|
|
|
# https://limits.readthedocs.io/en/stable/storage.html
|
2023-02-05 12:43:35 +00:00
|
|
|
app.config.update(RATELIMIT_ENABLED=config.config_ratelimiter)
|
2024-02-25 15:02:01 +00:00
|
|
|
if config.config_limiter_uri != "" and not cli_param.memory_backend:
|
2024-02-25 05:59:25 +00:00
|
|
|
app.config.update(RATELIMIT_STORAGE_URI=config.config_limiter_uri)
|
|
|
|
if config.config_limiter_options != "":
|
|
|
|
app.config.update(RATELIMIT_STORAGE_OPTIONS=config.config_limiter_options)
|
|
|
|
try:
|
|
|
|
limiter.init_app(app)
|
|
|
|
except Exception as e:
|
|
|
|
log.error('Wrong Flask Limiter configuration, falling back to default: {}'.format(e))
|
|
|
|
app.config.update(RATELIMIT_STORAGE_URI=None)
|
|
|
|
limiter.init_app(app)
|
2023-02-05 12:43:35 +00:00
|
|
|
|
2022-04-28 18:57:09 +00:00
|
|
|
# Register scheduled tasks
|
|
|
|
from .schedule import register_scheduled_tasks, register_startup_tasks
|
|
|
|
register_scheduled_tasks(config.schedule_reconnect)
|
|
|
|
register_startup_tasks()
|
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
return app
|
|
|
|
|
2021-03-20 10:32:50 +00:00
|
|
|
|