2015-08-02 20:59:11 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
2019-02-06 21:52:24 +01:00
|
|
|
|
2019-03-04 20:03:09 +01: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/>.
|
|
|
|
|
2019-06-06 17:10:22 +02:00
|
|
|
from __future__ import division, print_function, unicode_literals
|
2019-04-20 18:32:46 +02:00
|
|
|
import sys
|
2019-06-06 17:10:22 +02:00
|
|
|
import os
|
|
|
|
import mimetypes
|
2019-02-08 20:11:44 +01:00
|
|
|
try:
|
|
|
|
import cPickle
|
|
|
|
except ImportError:
|
|
|
|
import pickle as cPickle
|
2019-02-06 21:52:24 +01:00
|
|
|
|
2019-06-06 17:10:22 +02:00
|
|
|
from babel import Locale as LC
|
|
|
|
from babel import negotiate_locale
|
|
|
|
from babel.core import UnknownLocaleError
|
|
|
|
from flask import Flask, request, g
|
|
|
|
from flask_login import LoginManager
|
|
|
|
from flask_babel import Babel
|
|
|
|
from flask_principal import Principal
|
|
|
|
|
|
|
|
from . import logger, cache_buster, ub
|
|
|
|
from .constants import TRANSLATIONS_DIR as _TRANSLATIONS_DIR
|
|
|
|
from .reverseproxy import ReverseProxied
|
|
|
|
|
2019-02-06 21:52:24 +01:00
|
|
|
|
2019-02-08 20:11:44 +01: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')
|
|
|
|
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')
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
2019-02-06 21:52:24 +01:00
|
|
|
lm = LoginManager()
|
|
|
|
lm.login_view = 'web.login'
|
|
|
|
lm.anonymous_user = ub.Anonymous
|
|
|
|
|
|
|
|
|
2019-02-08 20:11:44 +01:00
|
|
|
ub.init_db()
|
2019-06-06 17:10:22 +02:00
|
|
|
config = ub.Config()
|
2019-06-02 09:33:19 +02:00
|
|
|
from . import db
|
2019-02-06 21:52:24 +01:00
|
|
|
|
2019-04-20 18:32:46 +02:00
|
|
|
try:
|
2019-06-06 17:10:22 +02:00
|
|
|
with open(os.path.join(_TRANSLATIONS_DIR, 'iso639.pickle'), 'rb') as f:
|
2019-04-20 18:32:46 +02:00
|
|
|
language_table = cPickle.load(f)
|
|
|
|
except cPickle.UnpicklingError as error:
|
|
|
|
print("Can't read file cps/translations/iso639.pickle: %s" % error)
|
|
|
|
sys.exit(1)
|
|
|
|
|
2019-02-06 21:52:24 +01:00
|
|
|
searched_ids = {}
|
|
|
|
|
2019-06-02 09:33:19 +02:00
|
|
|
from .worker import WorkerThread
|
2019-02-06 21:52:24 +01:00
|
|
|
global_WorkerThread = WorkerThread()
|
|
|
|
|
2019-06-10 19:19:27 +02:00
|
|
|
from .server import WebServer
|
|
|
|
web_server = WebServer()
|
2019-02-06 21:52:24 +01:00
|
|
|
|
2019-06-23 09:22:26 +02:00
|
|
|
from .ldap_login import Ldap
|
|
|
|
ldap1 = Ldap()
|
2019-06-16 08:20:01 +02:00
|
|
|
|
2019-02-08 20:11:44 +01:00
|
|
|
babel = Babel()
|
2019-06-16 08:20:01 +02:00
|
|
|
|
2019-06-11 18:35:20 +02:00
|
|
|
log = logger.create()
|
2019-06-06 17:10:22 +02:00
|
|
|
|
2019-02-06 21:52:24 +01:00
|
|
|
|
|
|
|
def create_app():
|
|
|
|
app.wsgi_app = ReverseProxied(app.wsgi_app)
|
2019-06-23 09:22:26 +02:00
|
|
|
# For python2 convert path to unicode
|
|
|
|
if sys.version_info < (3, 0):
|
|
|
|
app.static_folder = app.static_folder.decode('utf-8')
|
|
|
|
app.root_path = app.root_path.decode('utf-8')
|
|
|
|
app.instance_path = app.instance_path .decode('utf-8')
|
|
|
|
|
2019-02-06 21:52:24 +01:00
|
|
|
cache_buster.init_cache_busting(app)
|
|
|
|
|
2019-06-11 18:35:20 +02:00
|
|
|
log.info('Starting Calibre Web...')
|
2019-02-06 21:52:24 +01:00
|
|
|
Principal(app)
|
|
|
|
lm.init_app(app)
|
|
|
|
app.secret_key = os.getenv('SECRET_KEY', 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT')
|
2019-06-10 19:19:27 +02:00
|
|
|
web_server.init_app(app, config)
|
2019-02-06 21:52:24 +01:00
|
|
|
db.setup_db()
|
2019-02-08 20:11:44 +01:00
|
|
|
babel.init_app(app)
|
2019-06-23 09:22:26 +02:00
|
|
|
ldap1.init_app(app)
|
2019-02-06 21:52:24 +01:00
|
|
|
global_WorkerThread.start()
|
|
|
|
return app
|
2019-02-08 20:11:44 +01:00
|
|
|
|
|
|
|
@babel.localeselector
|
|
|
|
def get_locale():
|
|
|
|
# if a user is logged in, use the locale from the user settings
|
|
|
|
user = getattr(g, 'user', None)
|
|
|
|
# user = None
|
|
|
|
if user is not None and hasattr(user, "locale"):
|
|
|
|
if user.nickname != 'Guest': # if the account is the guest account bypass the config lang settings
|
|
|
|
return user.locale
|
|
|
|
translations = [str(item) for item in babel.list_translations()] + ['en']
|
|
|
|
preferred = list()
|
|
|
|
for x in request.accept_languages.values():
|
|
|
|
try:
|
|
|
|
preferred.append(str(LC.parse(x.replace('-', '_'))))
|
|
|
|
except (UnknownLocaleError, ValueError) as e:
|
2019-06-11 18:35:20 +02:00
|
|
|
log.warning('Could not parse locale "%s": %s', x, e)
|
2019-02-08 20:11:44 +01:00
|
|
|
preferred.append('en')
|
|
|
|
return negotiate_locale(preferred, translations)
|
|
|
|
|
|
|
|
|
|
|
|
@babel.timezoneselector
|
|
|
|
def get_timezone():
|
|
|
|
user = getattr(g, 'user', None)
|
|
|
|
if user is not None:
|
|
|
|
return user.timezone
|
|
|
|
|
2019-06-02 09:33:19 +02:00
|
|
|
from .updater import Updater
|
2019-02-08 20:11:44 +01:00
|
|
|
updater_thread = Updater()
|
2019-06-06 17:10:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
__all__ = ['app']
|