2019-07-13 18:45:48 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# This file is part of the Calibre-Web (https://github.com/janeczku/calibre-web)
|
|
|
|
# Copyright (C) 2019 OzzieIsaacs, pwr
|
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
|
|
|
import os
|
2019-07-14 11:20:40 +00:00
|
|
|
import sys
|
2021-04-18 09:33:14 +00:00
|
|
|
import json
|
2019-07-13 18:45:48 +00:00
|
|
|
|
2021-04-15 15:42:39 +00:00
|
|
|
from sqlalchemy import Column, String, Integer, SmallInteger, Boolean, BLOB, JSON
|
2020-12-07 18:53:34 +00:00
|
|
|
from sqlalchemy.exc import OperationalError
|
2021-04-15 15:42:39 +00:00
|
|
|
from sqlalchemy.sql.expression import text
|
2022-07-02 15:45:24 +00:00
|
|
|
from sqlalchemy import exists
|
|
|
|
from cryptography.fernet import Fernet
|
|
|
|
import cryptography.exceptions
|
|
|
|
from base64 import urlsafe_b64decode
|
2021-03-20 09:09:08 +00:00
|
|
|
try:
|
2021-04-08 17:37:08 +00:00
|
|
|
# Compatibility with sqlalchemy 2.0
|
2021-03-20 09:09:08 +00:00
|
|
|
from sqlalchemy.orm import declarative_base
|
|
|
|
except ImportError:
|
|
|
|
from sqlalchemy.ext.declarative import declarative_base
|
2019-07-13 18:45:48 +00:00
|
|
|
|
2022-04-26 09:04:00 +00:00
|
|
|
from . import constants, logger
|
2023-11-02 16:05:02 +00:00
|
|
|
from .subproc_wrapper import process_wait
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
log = logger.create()
|
|
|
|
_Base = declarative_base()
|
|
|
|
|
2022-05-21 20:52:59 +00:00
|
|
|
|
2020-05-04 17:02:03 +00:00
|
|
|
class _Flask_Settings(_Base):
|
|
|
|
__tablename__ = 'flask_settings'
|
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True)
|
2021-03-27 15:36:24 +00:00
|
|
|
flask_session_key = Column(BLOB, default=b"")
|
2020-05-04 17:02:03 +00:00
|
|
|
|
|
|
|
def __init__(self, key):
|
|
|
|
self.flask_session_key = key
|
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
# Baseclass for representing settings in app.db with email server settings and Calibre database settings
|
|
|
|
# (application settings)
|
|
|
|
class _Settings(_Base):
|
|
|
|
__tablename__ = 'settings'
|
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True)
|
2019-12-28 15:18:21 +00:00
|
|
|
mail_server = Column(String, default=constants.DEFAULT_MAIL_SERVER)
|
2019-07-13 18:45:48 +00:00
|
|
|
mail_port = Column(Integer, default=25)
|
|
|
|
mail_use_ssl = Column(SmallInteger, default=0)
|
|
|
|
mail_login = Column(String, default='mail@example.com')
|
2022-07-02 15:45:24 +00:00
|
|
|
mail_password_e = Column(String)
|
|
|
|
mail_password = Column(String)
|
2019-07-13 18:45:48 +00:00
|
|
|
mail_from = Column(String, default='automailer <mail@example.com>')
|
2020-05-09 18:29:17 +00:00
|
|
|
mail_size = Column(Integer, default=25*1024*1024)
|
2021-03-27 15:36:24 +00:00
|
|
|
mail_server_type = Column(SmallInteger, default=0)
|
|
|
|
mail_gmail_token = Column(JSON, default={})
|
2019-07-14 11:20:40 +00:00
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
config_calibre_dir = Column(String)
|
2022-02-06 13:22:55 +00:00
|
|
|
config_calibre_uuid = Column(String)
|
2023-11-07 18:30:13 +00:00
|
|
|
config_calibre_split = Column(Boolean, default=False)
|
|
|
|
config_calibre_split_dir = Column(String)
|
2019-07-13 18:45:48 +00:00
|
|
|
config_port = Column(Integer, default=constants.DEFAULT_PORT)
|
2020-07-03 16:19:11 +00:00
|
|
|
config_external_port = Column(Integer, default=constants.DEFAULT_PORT)
|
2019-07-13 18:45:48 +00:00
|
|
|
config_certfile = Column(String)
|
|
|
|
config_keyfile = Column(String)
|
2022-05-21 20:52:59 +00:00
|
|
|
config_trustedhosts = Column(String, default='')
|
2023-01-21 14:23:18 +00:00
|
|
|
config_calibre_web_title = Column(String, default='Calibre-Web')
|
2019-07-13 18:45:48 +00:00
|
|
|
config_books_per_page = Column(Integer, default=60)
|
|
|
|
config_random_books = Column(Integer, default=4)
|
|
|
|
config_authors_max = Column(Integer, default=0)
|
|
|
|
config_read_column = Column(Integer, default=0)
|
2023-01-28 17:52:50 +00:00
|
|
|
config_title_regex = Column(String, default=r'^(A|The|An|Der|Die|Das|Den|Ein|Eine|Einen|Dem|Des|Einem|Eines|Le|La|Les|L\'|Un|Une)\s+')
|
2019-07-14 11:20:40 +00:00
|
|
|
config_theme = Column(Integer, default=0)
|
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
config_log_level = Column(SmallInteger, default=logger.DEFAULT_LOG_LEVEL)
|
2023-05-28 18:32:19 +00:00
|
|
|
config_logfile = Column(String, default=logger.DEFAULT_LOG_FILE)
|
2019-07-13 18:45:48 +00:00
|
|
|
config_access_log = Column(SmallInteger, default=0)
|
2023-05-28 18:32:19 +00:00
|
|
|
config_access_logfile = Column(String, default=logger.DEFAULT_ACCESS_LOG)
|
2019-07-14 11:20:40 +00:00
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
config_uploading = Column(SmallInteger, default=0)
|
|
|
|
config_anonbrowse = Column(SmallInteger, default=0)
|
|
|
|
config_public_reg = Column(SmallInteger, default=0)
|
2019-07-14 11:20:40 +00:00
|
|
|
config_remote_login = Column(Boolean, default=False)
|
2020-01-26 15:52:40 +00:00
|
|
|
config_kobo_sync = Column(Boolean, default=False)
|
2019-07-14 11:20:40 +00:00
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
config_default_role = Column(SmallInteger, default=0)
|
2020-04-13 20:23:58 +00:00
|
|
|
config_default_show = Column(SmallInteger, default=constants.ADMIN_USER_SIDEBAR)
|
2021-10-06 16:41:01 +00:00
|
|
|
config_default_language = Column(String(3), default="all")
|
|
|
|
config_default_locale = Column(String(2), default="en")
|
2019-07-13 18:45:48 +00:00
|
|
|
config_columns_to_ignore = Column(String)
|
2020-01-05 15:43:48 +00:00
|
|
|
|
2020-02-15 09:21:45 +00:00
|
|
|
config_denied_tags = Column(String, default="")
|
2020-01-05 07:43:00 +00:00
|
|
|
config_allowed_tags = Column(String, default="")
|
2019-12-30 14:15:07 +00:00
|
|
|
config_restricted_column = Column(SmallInteger, default=0)
|
2020-02-15 09:21:45 +00:00
|
|
|
config_denied_column_value = Column(String, default="")
|
2020-01-05 07:43:00 +00:00
|
|
|
config_allowed_column_value = Column(String, default="")
|
2019-07-14 11:20:40 +00:00
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
config_use_google_drive = Column(Boolean, default=False)
|
|
|
|
config_google_drive_folder = Column(String)
|
2020-08-15 13:07:03 +00:00
|
|
|
config_google_drive_watch_changes_response = Column(JSON, default={})
|
2019-07-14 11:20:40 +00:00
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
config_use_goodreads = Column(Boolean, default=False)
|
|
|
|
config_goodreads_api_key = Column(String)
|
2022-07-02 15:45:24 +00:00
|
|
|
config_goodreads_api_secret_e = Column(String)
|
2019-07-13 18:45:48 +00:00
|
|
|
config_goodreads_api_secret = Column(String)
|
2020-05-02 08:18:01 +00:00
|
|
|
config_register_email = Column(Boolean, default=False)
|
2019-07-13 18:45:48 +00:00
|
|
|
config_login_type = Column(Integer, default=0)
|
2019-07-14 11:20:40 +00:00
|
|
|
|
2020-01-26 15:52:40 +00:00
|
|
|
config_kobo_proxy = Column(Boolean, default=False)
|
|
|
|
|
2019-12-28 06:12:18 +00:00
|
|
|
config_ldap_provider_url = Column(String, default='example.org')
|
2019-07-13 18:45:48 +00:00
|
|
|
config_ldap_port = Column(SmallInteger, default=389)
|
2020-04-17 16:16:21 +00:00
|
|
|
config_ldap_authentication = Column(SmallInteger, default=constants.LDAP_AUTH_SIMPLE)
|
2019-12-28 06:12:18 +00:00
|
|
|
config_ldap_serv_username = Column(String, default='cn=admin,dc=example,dc=org')
|
2022-07-02 15:45:24 +00:00
|
|
|
config_ldap_serv_password_e = Column(String)
|
|
|
|
config_ldap_serv_password = Column(String)
|
2020-04-05 15:31:41 +00:00
|
|
|
config_ldap_encryption = Column(SmallInteger, default=0)
|
2020-11-20 18:47:50 +00:00
|
|
|
config_ldap_cacert_path = Column(String, default="")
|
2020-04-13 20:23:58 +00:00
|
|
|
config_ldap_cert_path = Column(String, default="")
|
2020-11-20 18:47:50 +00:00
|
|
|
config_ldap_key_path = Column(String, default="")
|
2019-12-28 06:12:18 +00:00
|
|
|
config_ldap_dn = Column(String, default='dc=example,dc=org')
|
|
|
|
config_ldap_user_object = Column(String, default='uid=%s')
|
2022-05-21 20:52:59 +00:00
|
|
|
config_ldap_member_user_object = Column(String, default='')
|
2019-12-28 06:12:18 +00:00
|
|
|
config_ldap_openldap = Column(Boolean, default=True)
|
|
|
|
config_ldap_group_object_filter = Column(String, default='(&(objectclass=posixGroup)(cn=%s))')
|
|
|
|
config_ldap_group_members_field = Column(String, default='memberUid')
|
|
|
|
config_ldap_group_name = Column(String, default='calibreweb')
|
2019-07-14 11:20:40 +00:00
|
|
|
|
2020-05-09 09:03:11 +00:00
|
|
|
config_kepubifypath = Column(String, default=None)
|
|
|
|
config_converterpath = Column(String, default=None)
|
2023-11-02 16:05:02 +00:00
|
|
|
config_binariesdir = Column(String, default=None)
|
2019-07-13 18:45:48 +00:00
|
|
|
config_calibre = Column(String)
|
2020-05-09 09:03:11 +00:00
|
|
|
config_rarfile_location = Column(String, default=None)
|
2020-05-02 09:24:30 +00:00
|
|
|
config_upload_formats = Column(String, default=','.join(constants.EXTENSIONS_UPLOAD))
|
2021-09-26 07:02:48 +00:00
|
|
|
config_unicode_filename = Column(Boolean, default=False)
|
2023-12-09 10:23:26 +00:00
|
|
|
config_embed_metadata = Column(Boolean, default=True)
|
2019-07-14 11:20:40 +00:00
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
config_updatechannel = Column(Integer, default=constants.UPDATE_STABLE)
|
|
|
|
|
2020-01-05 07:43:00 +00:00
|
|
|
config_reverse_proxy_login_header_name = Column(String)
|
|
|
|
config_allow_reverse_proxy_header_login = Column(Boolean, default=False)
|
|
|
|
|
2021-09-26 07:02:48 +00:00
|
|
|
schedule_start_time = Column(Integer, default=4)
|
2022-04-25 15:00:07 +00:00
|
|
|
schedule_duration = Column(Integer, default=10)
|
2021-09-26 07:02:48 +00:00
|
|
|
schedule_generate_book_covers = Column(Boolean, default=False)
|
|
|
|
schedule_generate_series_covers = Column(Boolean, default=False)
|
2022-04-25 15:00:07 +00:00
|
|
|
schedule_reconnect = Column(Boolean, default=False)
|
2023-03-25 09:42:36 +00:00
|
|
|
schedule_metadata_backup = Column(Boolean, default=False)
|
2021-09-26 07:02:48 +00:00
|
|
|
|
2022-06-16 06:33:39 +00:00
|
|
|
config_password_policy = Column(Boolean, default=True)
|
|
|
|
config_password_min_length = Column(Integer, default=8)
|
|
|
|
config_password_number = Column(Boolean, default=True)
|
|
|
|
config_password_lower = Column(Boolean, default=True)
|
|
|
|
config_password_upper = Column(Boolean, default=True)
|
2024-02-29 07:23:18 +00:00
|
|
|
config_password_character = Column(Boolean, default=True)
|
2022-06-16 06:33:39 +00:00
|
|
|
config_password_special = Column(Boolean, default=True)
|
|
|
|
config_session = Column(Integer, default=1)
|
2023-02-05 12:43:35 +00:00
|
|
|
config_ratelimiter = Column(Boolean, default=True)
|
2024-02-25 05:59:25 +00:00
|
|
|
config_limiter_uri = Column(String, default="")
|
|
|
|
config_limiter_options = Column(String, default="")
|
2022-06-16 06:33:39 +00:00
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return self.__class__.__name__
|
|
|
|
|
|
|
|
|
|
|
|
# Class holds all application specific settings in calibre-web
|
2022-07-02 15:45:24 +00:00
|
|
|
class ConfigSQL(object):
|
2019-07-13 18:45:48 +00:00
|
|
|
# pylint: disable=no-member
|
2022-04-26 09:04:00 +00:00
|
|
|
def __init__(self):
|
2022-07-02 15:45:24 +00:00
|
|
|
self.__dict__["dirty"] = list()
|
2022-04-26 09:04:00 +00:00
|
|
|
|
2022-07-02 15:45:24 +00:00
|
|
|
def init_config(self, session, secret_key, cli):
|
2019-07-13 18:45:48 +00:00
|
|
|
self._session = session
|
|
|
|
self._settings = None
|
|
|
|
self.db_configured = None
|
|
|
|
self.config_calibre_dir = None
|
2022-07-02 15:45:24 +00:00
|
|
|
self._fernet = Fernet(secret_key)
|
2022-04-26 09:04:00 +00:00
|
|
|
self.cli = cli
|
2022-07-02 15:45:24 +00:00
|
|
|
self.load()
|
2019-07-13 18:45:48 +00:00
|
|
|
|
2020-05-09 10:04:00 +00:00
|
|
|
change = False
|
2023-11-02 16:05:02 +00:00
|
|
|
|
|
|
|
if self.config_binariesdir == None: # pylint: disable=access-member-before-definition
|
2020-05-09 10:04:00 +00:00
|
|
|
change = True
|
2023-11-02 16:05:02 +00:00
|
|
|
self.config_binariesdir = autodetect_calibre_binaries()
|
|
|
|
self.config_converterpath = autodetect_converter_binary(self.config_binariesdir)
|
2020-05-09 10:04:00 +00:00
|
|
|
|
2021-03-14 12:28:52 +00:00
|
|
|
if self.config_kepubifypath == None: # pylint: disable=access-member-before-definition
|
2020-05-09 10:04:00 +00:00
|
|
|
change = True
|
|
|
|
self.config_kepubifypath = autodetect_kepubify_binary()
|
|
|
|
|
2021-03-14 12:28:52 +00:00
|
|
|
if self.config_rarfile_location == None: # pylint: disable=access-member-before-definition
|
2020-05-09 10:04:00 +00:00
|
|
|
change = True
|
|
|
|
self.config_rarfile_location = autodetect_unrar_binary()
|
|
|
|
if change:
|
|
|
|
self.save()
|
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
def _read_from_storage(self):
|
|
|
|
if self._settings is None:
|
|
|
|
log.debug("_ConfigSQL._read_from_storage")
|
|
|
|
self._settings = self._session.query(_Settings).first()
|
|
|
|
return self._settings
|
|
|
|
|
|
|
|
def get_config_certfile(self):
|
2022-04-26 09:04:00 +00:00
|
|
|
if self.cli.certfilepath:
|
|
|
|
return self.cli.certfilepath
|
|
|
|
if self.cli.certfilepath == "":
|
2019-07-13 18:45:48 +00:00
|
|
|
return None
|
|
|
|
return self.config_certfile
|
|
|
|
|
|
|
|
def get_config_keyfile(self):
|
2022-04-26 09:04:00 +00:00
|
|
|
if self.cli.keyfilepath:
|
|
|
|
return self.cli.keyfilepath
|
|
|
|
if self.cli.certfilepath == "":
|
2019-07-13 18:45:48 +00:00
|
|
|
return None
|
|
|
|
return self.config_keyfile
|
|
|
|
|
2022-04-26 09:04:00 +00:00
|
|
|
def get_config_ipaddress(self):
|
|
|
|
return self.cli.ip_address or ""
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
def _has_role(self, role_flag):
|
|
|
|
return constants.has_flag(self.config_default_role, role_flag)
|
|
|
|
|
|
|
|
def role_admin(self):
|
|
|
|
return self._has_role(constants.ROLE_ADMIN)
|
|
|
|
|
|
|
|
def role_download(self):
|
|
|
|
return self._has_role(constants.ROLE_DOWNLOAD)
|
|
|
|
|
|
|
|
def role_viewer(self):
|
|
|
|
return self._has_role(constants.ROLE_VIEWER)
|
|
|
|
|
|
|
|
def role_upload(self):
|
|
|
|
return self._has_role(constants.ROLE_UPLOAD)
|
|
|
|
|
|
|
|
def role_edit(self):
|
|
|
|
return self._has_role(constants.ROLE_EDIT)
|
|
|
|
|
|
|
|
def role_passwd(self):
|
|
|
|
return self._has_role(constants.ROLE_PASSWD)
|
|
|
|
|
|
|
|
def role_edit_shelfs(self):
|
|
|
|
return self._has_role(constants.ROLE_EDIT_SHELFS)
|
|
|
|
|
|
|
|
def role_delete_books(self):
|
|
|
|
return self._has_role(constants.ROLE_DELETE_BOOKS)
|
|
|
|
|
|
|
|
def show_element_new_user(self, value):
|
|
|
|
return constants.has_flag(self.config_default_show, value)
|
|
|
|
|
|
|
|
def show_detail_random(self):
|
|
|
|
return self.show_element_new_user(constants.DETAIL_RANDOM)
|
|
|
|
|
2020-02-15 09:21:45 +00:00
|
|
|
def list_denied_tags(self):
|
2020-07-22 16:44:03 +00:00
|
|
|
mct = self.config_denied_tags or ""
|
|
|
|
return [t.strip() for t in mct.split(",")]
|
2019-07-13 18:45:48 +00:00
|
|
|
|
2020-01-01 16:26:47 +00:00
|
|
|
def list_allowed_tags(self):
|
2020-07-22 16:44:03 +00:00
|
|
|
mct = self.config_allowed_tags or ""
|
|
|
|
return [t.strip() for t in mct.split(",")]
|
2020-01-01 16:26:47 +00:00
|
|
|
|
2020-02-15 09:21:45 +00:00
|
|
|
def list_denied_column_values(self):
|
2020-07-22 16:44:03 +00:00
|
|
|
mct = self.config_denied_column_value or ""
|
|
|
|
return [t.strip() for t in mct.split(",")]
|
2020-01-01 16:26:47 +00:00
|
|
|
|
|
|
|
def list_allowed_column_values(self):
|
2020-07-22 16:44:03 +00:00
|
|
|
mct = self.config_allowed_column_value or ""
|
|
|
|
return [t.strip() for t in mct.split(",")]
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
def get_log_level(self):
|
|
|
|
return logger.get_level_name(self.config_log_level)
|
|
|
|
|
|
|
|
def get_mail_settings(self):
|
2022-05-21 20:52:59 +00:00
|
|
|
return {k: v for k, v in self.__dict__.items() if k.startswith('mail_')}
|
2019-07-13 18:45:48 +00:00
|
|
|
|
2019-12-28 15:18:21 +00:00
|
|
|
def get_mail_server_configured(self):
|
2021-03-27 15:36:24 +00:00
|
|
|
return bool((self.mail_server != constants.DEFAULT_MAIL_SERVER and self.mail_server_type == 0)
|
2021-03-28 12:50:55 +00:00
|
|
|
or (self.mail_gmail_token != {} and self.mail_server_type == 1))
|
2019-12-28 15:18:21 +00:00
|
|
|
|
2021-09-26 07:02:48 +00:00
|
|
|
def get_scheduled_task_settings(self):
|
2022-05-21 20:52:59 +00:00
|
|
|
return {k: v for k, v in self.__dict__.items() if k.startswith('schedule_')}
|
2019-12-28 15:18:21 +00:00
|
|
|
|
2020-04-05 15:31:41 +00:00
|
|
|
def set_from_dictionary(self, dictionary, field, convertor=None, default=None, encode=None):
|
2021-03-28 12:50:55 +00:00
|
|
|
"""Possibly updates a field of this object.
|
2019-07-13 18:45:48 +00:00
|
|
|
The new value, if present, is grabbed from the given dictionary, and optionally passed through a convertor.
|
|
|
|
|
|
|
|
:returns: `True` if the field has changed value
|
2021-03-28 12:50:55 +00:00
|
|
|
"""
|
2019-07-13 18:45:48 +00:00
|
|
|
new_value = dictionary.get(field, default)
|
|
|
|
if new_value is None:
|
|
|
|
return False
|
|
|
|
|
|
|
|
if field not in self.__dict__:
|
|
|
|
log.warning("_ConfigSQL trying to set unknown field '%s' = %r", field, new_value)
|
|
|
|
return False
|
|
|
|
|
|
|
|
if convertor is not None:
|
2020-04-05 15:31:41 +00:00
|
|
|
if encode:
|
|
|
|
new_value = convertor(new_value.encode(encode))
|
|
|
|
else:
|
|
|
|
new_value = convertor(new_value)
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
current_value = self.__dict__.get(field)
|
|
|
|
if current_value == new_value:
|
|
|
|
return False
|
|
|
|
|
|
|
|
setattr(self, field, new_value)
|
|
|
|
return True
|
|
|
|
|
2022-07-02 15:45:24 +00:00
|
|
|
def to_dict(self):
|
2020-12-12 09:02:11 +00:00
|
|
|
storage = {}
|
|
|
|
for k, v in self.__dict__.items():
|
2022-07-02 15:45:24 +00:00
|
|
|
if k[0] != '_' and not k.endswith("_e") and not k == "cli":
|
2020-12-12 09:02:11 +00:00
|
|
|
storage[k] = v
|
|
|
|
return storage
|
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
def load(self):
|
2022-05-21 20:52:59 +00:00
|
|
|
"""Load all configuration values from the underlying storage."""
|
2019-07-13 18:45:48 +00:00
|
|
|
s = self._read_from_storage() # type: _Settings
|
|
|
|
for k, v in s.__dict__.items():
|
|
|
|
if k[0] != '_':
|
|
|
|
if v is None:
|
|
|
|
# if the storage column has no value, apply the (possible) default
|
|
|
|
column = s.__class__.__dict__.get(k)
|
|
|
|
if column.default is not None:
|
|
|
|
v = column.default.arg
|
2022-07-02 15:45:24 +00:00
|
|
|
if k.endswith("_e") and v is not None:
|
|
|
|
try:
|
|
|
|
setattr(self, k, self._fernet.decrypt(v).decode())
|
|
|
|
except cryptography.fernet.InvalidToken:
|
|
|
|
setattr(self, k, "")
|
|
|
|
else:
|
|
|
|
setattr(self, k, v)
|
2019-07-13 18:45:48 +00:00
|
|
|
|
2019-07-14 11:43:40 +00:00
|
|
|
have_metadata_db = bool(self.config_calibre_dir)
|
|
|
|
if have_metadata_db:
|
2022-02-05 18:05:26 +00:00
|
|
|
db_file = os.path.join(self.config_calibre_dir, 'metadata.db')
|
|
|
|
have_metadata_db = os.path.isfile(db_file)
|
2019-07-14 11:43:40 +00:00
|
|
|
self.db_configured = have_metadata_db
|
2020-09-08 18:57:39 +00:00
|
|
|
constants.EXTENSIONS_UPLOAD = [x.lstrip().rstrip().lower() for x in self.config_upload_formats.split(',')]
|
2023-05-28 18:32:19 +00:00
|
|
|
from . import cli_param
|
2021-03-28 12:50:55 +00:00
|
|
|
if os.environ.get('FLASK_DEBUG'):
|
|
|
|
logfile = logger.setup(logger.LOG_TO_STDOUT, logger.logging.DEBUG)
|
|
|
|
else:
|
|
|
|
# pylint: disable=access-member-before-definition
|
2023-05-28 18:32:19 +00:00
|
|
|
logfile = logger.setup(cli_param.logpath or self.config_logfile, self.config_log_level)
|
|
|
|
if logfile != os.path.abspath(self.config_logfile):
|
|
|
|
if logfile != os.path.abspath(cli_param.logpath):
|
|
|
|
log.warning("Log path %s not valid, falling back to default", self.config_logfile)
|
2020-05-24 07:59:45 +00:00
|
|
|
self.config_logfile = logfile
|
2023-05-28 18:32:19 +00:00
|
|
|
s.config_logfile = logfile
|
2020-05-24 07:59:45 +00:00
|
|
|
self._session.merge(s)
|
2020-12-07 18:53:34 +00:00
|
|
|
try:
|
|
|
|
self._session.commit()
|
|
|
|
except OperationalError as e:
|
|
|
|
log.error('Database error: %s', e)
|
|
|
|
self._session.rollback()
|
2022-07-02 15:45:24 +00:00
|
|
|
self.__dict__["dirty"] = list()
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
def save(self):
|
2022-05-21 20:52:59 +00:00
|
|
|
"""Apply all configuration values to the underlying storage."""
|
2019-07-13 18:45:48 +00:00
|
|
|
s = self._read_from_storage() # type: _Settings
|
|
|
|
|
2022-07-02 15:45:24 +00:00
|
|
|
for k in self.dirty:
|
2019-07-13 18:45:48 +00:00
|
|
|
if k[0] == '_':
|
|
|
|
continue
|
2020-01-05 07:43:00 +00:00
|
|
|
if hasattr(s, k):
|
2022-07-02 15:45:24 +00:00
|
|
|
if k.endswith("_e"):
|
|
|
|
setattr(s, k, self._fernet.encrypt(self.__dict__[k].encode()))
|
|
|
|
else:
|
|
|
|
setattr(s, k, self.__dict__[k])
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
log.debug("_ConfigSQL updating storage")
|
|
|
|
self._session.merge(s)
|
2020-12-07 18:53:34 +00:00
|
|
|
try:
|
|
|
|
self._session.commit()
|
|
|
|
except OperationalError as e:
|
|
|
|
log.error('Database error: %s', e)
|
|
|
|
self._session.rollback()
|
2019-07-13 18:45:48 +00:00
|
|
|
self.load()
|
|
|
|
|
2020-04-13 20:23:58 +00:00
|
|
|
def invalidate(self, error=None):
|
|
|
|
if error:
|
|
|
|
log.error(error)
|
2019-07-13 18:45:48 +00:00
|
|
|
log.warning("invalidating configuration")
|
|
|
|
self.db_configured = False
|
|
|
|
self.save()
|
|
|
|
|
2023-11-07 18:30:13 +00:00
|
|
|
def get_book_path(self):
|
|
|
|
return self.config_calibre_split_dir if self.config_calibre_split_dir else self.config_calibre_dir
|
|
|
|
|
2022-02-06 13:22:55 +00:00
|
|
|
def store_calibre_uuid(self, calibre_db, Library_table):
|
|
|
|
try:
|
|
|
|
calibre_uuid = calibre_db.session.query(Library_table).one_or_none()
|
|
|
|
if self.config_calibre_uuid != calibre_uuid.uuid:
|
|
|
|
self.config_calibre_uuid = calibre_uuid.uuid
|
|
|
|
self.save()
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
2019-07-13 18:45:48 +00:00
|
|
|
|
2022-07-02 15:45:24 +00:00
|
|
|
def __setattr__(self, attr_name, attr_value):
|
|
|
|
super().__setattr__(attr_name, attr_value)
|
|
|
|
self.__dict__["dirty"].append(attr_name)
|
2022-05-21 20:52:59 +00:00
|
|
|
|
2022-07-02 15:45:24 +00:00
|
|
|
|
|
|
|
def _encrypt_fields(session, secret_key):
|
|
|
|
try:
|
|
|
|
session.query(exists().where(_Settings.mail_password_e)).scalar()
|
|
|
|
except OperationalError:
|
|
|
|
with session.bind.connect() as conn:
|
2023-04-12 16:56:21 +00:00
|
|
|
conn.execute(text("ALTER TABLE settings ADD column 'mail_password_e' String"))
|
|
|
|
conn.execute(text("ALTER TABLE settings ADD column 'config_goodreads_api_secret_e' String"))
|
|
|
|
conn.execute(text("ALTER TABLE settings ADD column 'config_ldap_serv_password_e' String"))
|
2022-07-02 15:45:24 +00:00
|
|
|
session.commit()
|
|
|
|
crypter = Fernet(secret_key)
|
|
|
|
settings = session.query(_Settings.mail_password, _Settings.config_goodreads_api_secret,
|
|
|
|
_Settings.config_ldap_serv_password).first()
|
|
|
|
if settings.mail_password:
|
|
|
|
session.query(_Settings).update(
|
|
|
|
{_Settings.mail_password_e: crypter.encrypt(settings.mail_password.encode())})
|
|
|
|
if settings.config_goodreads_api_secret:
|
|
|
|
session.query(_Settings).update(
|
|
|
|
{_Settings.config_goodreads_api_secret_e:
|
|
|
|
crypter.encrypt(settings.config_goodreads_api_secret.encode())})
|
|
|
|
if settings.config_ldap_serv_password:
|
|
|
|
session.query(_Settings).update(
|
|
|
|
{_Settings.config_ldap_serv_password_e:
|
|
|
|
crypter.encrypt(settings.config_ldap_serv_password.encode())})
|
|
|
|
session.commit()
|
|
|
|
|
|
|
|
|
|
|
|
def _migrate_table(session, orm_class, secret_key=None):
|
|
|
|
if secret_key:
|
|
|
|
_encrypt_fields(session, secret_key)
|
2019-07-13 18:45:48 +00:00
|
|
|
changed = False
|
|
|
|
|
|
|
|
for column_name, column in orm_class.__dict__.items():
|
|
|
|
if column_name[0] != '_':
|
|
|
|
try:
|
|
|
|
session.query(column).first()
|
2021-04-15 10:05:03 +00:00
|
|
|
except OperationalError as err:
|
2020-01-05 07:43:00 +00:00
|
|
|
log.debug("%s: %s", column_name, err.args[0])
|
|
|
|
if column.default is None:
|
|
|
|
column_default = ""
|
|
|
|
else:
|
|
|
|
if isinstance(column.default.arg, bool):
|
2021-04-15 15:42:39 +00:00
|
|
|
column_default = "DEFAULT {}".format(int(column.default.arg))
|
2020-01-05 07:43:00 +00:00
|
|
|
else:
|
2021-04-15 15:42:39 +00:00
|
|
|
column_default = "DEFAULT `{}`".format(column.default.arg)
|
2021-03-27 15:36:24 +00:00
|
|
|
if isinstance(column.type, JSON):
|
|
|
|
column_type = "JSON"
|
|
|
|
else:
|
|
|
|
column_type = column.type
|
2021-04-15 15:42:39 +00:00
|
|
|
alter_table = text("ALTER TABLE %s ADD COLUMN `%s` %s %s" % (orm_class.__tablename__,
|
2022-05-21 20:52:59 +00:00
|
|
|
column_name,
|
|
|
|
column_type,
|
|
|
|
column_default))
|
2019-07-14 11:43:40 +00:00
|
|
|
log.debug(alter_table)
|
2019-07-13 18:45:48 +00:00
|
|
|
session.execute(alter_table)
|
|
|
|
changed = True
|
2021-04-18 09:33:14 +00:00
|
|
|
except json.decoder.JSONDecodeError as e:
|
|
|
|
log.error("Database corrupt column: {}".format(column_name))
|
|
|
|
log.debug(e)
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
if changed:
|
2020-12-07 18:53:34 +00:00
|
|
|
try:
|
|
|
|
session.commit()
|
|
|
|
except OperationalError:
|
|
|
|
session.rollback()
|
2019-07-13 18:45:48 +00:00
|
|
|
|
2020-05-09 09:03:11 +00:00
|
|
|
|
2023-11-02 16:05:02 +00:00
|
|
|
def autodetect_calibre_binaries():
|
2019-07-14 11:20:40 +00:00
|
|
|
if sys.platform == "win32":
|
2023-11-02 16:05:02 +00:00
|
|
|
calibre_path = ["C:\\program files\\calibre\\",
|
|
|
|
"C:\\program files(x86)\\calibre\\",
|
|
|
|
"C:\\program files(x86)\\calibre2\\",
|
|
|
|
"C:\\program files\\calibre2\\"]
|
2019-07-14 11:20:40 +00:00
|
|
|
else:
|
2023-11-02 16:05:02 +00:00
|
|
|
calibre_path = ["/opt/calibre/"]
|
2019-07-14 11:20:40 +00:00
|
|
|
for element in calibre_path:
|
2023-11-02 16:05:02 +00:00
|
|
|
supported_binary_paths = [os.path.join(element, binary) for binary in constants.SUPPORTED_CALIBRE_BINARIES.values()]
|
|
|
|
if all(os.path.isfile(binary_path) and os.access(binary_path, os.X_OK) for binary_path in supported_binary_paths):
|
|
|
|
values = [process_wait([binary_path, "--version"], pattern='\(calibre (.*)\)') for binary_path in supported_binary_paths]
|
|
|
|
if all(values):
|
|
|
|
version = values[0].group(1)
|
|
|
|
log.debug("calibre version %s", version)
|
|
|
|
return element
|
|
|
|
return ""
|
|
|
|
|
|
|
|
|
|
|
|
def autodetect_converter_binary(calibre_path):
|
|
|
|
if sys.platform == "win32":
|
|
|
|
converter_path = os.path.join(calibre_path, "ebook-convert.exe")
|
|
|
|
else:
|
|
|
|
converter_path = os.path.join(calibre_path, "ebook-convert")
|
|
|
|
if calibre_path and os.path.isfile(converter_path) and os.access(converter_path, os.X_OK):
|
|
|
|
return converter_path
|
2020-05-09 09:03:11 +00:00
|
|
|
return ""
|
2019-07-14 11:20:40 +00:00
|
|
|
|
2021-09-26 07:02:48 +00:00
|
|
|
|
2020-05-09 09:03:11 +00:00
|
|
|
def autodetect_unrar_binary():
|
|
|
|
if sys.platform == "win32":
|
|
|
|
calibre_path = ["C:\\program files\\WinRar\\unRAR.exe",
|
|
|
|
"C:\\program files(x86)\\WinRar\\unRAR.exe"]
|
|
|
|
else:
|
|
|
|
calibre_path = ["/usr/bin/unrar"]
|
|
|
|
for element in calibre_path:
|
|
|
|
if os.path.isfile(element) and os.access(element, os.X_OK):
|
|
|
|
return element
|
|
|
|
return ""
|
2019-07-14 11:20:40 +00:00
|
|
|
|
2021-09-26 07:02:48 +00:00
|
|
|
|
2020-05-09 09:03:11 +00:00
|
|
|
def autodetect_kepubify_binary():
|
|
|
|
if sys.platform == "win32":
|
|
|
|
calibre_path = ["C:\\program files\\kepubify\\kepubify-windows-64Bit.exe",
|
|
|
|
"C:\\program files(x86)\\kepubify\\kepubify-windows-64Bit.exe"]
|
|
|
|
else:
|
|
|
|
calibre_path = ["/opt/kepubify/kepubify-linux-64bit", "/opt/kepubify/kepubify-linux-32bit"]
|
|
|
|
for element in calibre_path:
|
|
|
|
if os.path.isfile(element) and os.access(element, os.X_OK):
|
|
|
|
return element
|
|
|
|
return ""
|
2019-07-13 18:45:48 +00:00
|
|
|
|
2021-09-26 07:02:48 +00:00
|
|
|
|
2022-07-02 15:45:24 +00:00
|
|
|
def _migrate_database(session, secret_key):
|
2019-07-13 18:45:48 +00:00
|
|
|
# make sure the table is created, if it does not exist
|
|
|
|
_Base.metadata.create_all(session.bind)
|
2022-07-02 15:45:24 +00:00
|
|
|
_migrate_table(session, _Settings, secret_key)
|
2020-05-04 17:02:03 +00:00
|
|
|
_migrate_table(session, _Flask_Settings)
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
|
2022-07-02 15:45:24 +00:00
|
|
|
def load_configuration(session, secret_key):
|
|
|
|
_migrate_database(session, secret_key)
|
2019-07-13 18:45:48 +00:00
|
|
|
if not session.query(_Settings).count():
|
|
|
|
session.add(_Settings())
|
|
|
|
session.commit()
|
2020-05-04 17:02:03 +00:00
|
|
|
|
2022-05-21 20:52:59 +00:00
|
|
|
|
2022-02-06 13:22:55 +00:00
|
|
|
def get_flask_session_key(_session):
|
|
|
|
flask_settings = _session.query(_Flask_Settings).one_or_none()
|
2020-05-04 17:02:03 +00:00
|
|
|
if flask_settings == None:
|
|
|
|
flask_settings = _Flask_Settings(os.urandom(32))
|
2022-02-06 13:22:55 +00:00
|
|
|
_session.add(flask_settings)
|
|
|
|
_session.commit()
|
2020-05-04 17:02:03 +00:00
|
|
|
return flask_settings.flask_session_key
|
2022-07-02 15:45:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_encryption_key(key_path):
|
|
|
|
key_file = os.path.join(key_path, ".key")
|
|
|
|
generate = True
|
|
|
|
error = ""
|
2023-04-17 16:52:52 +00:00
|
|
|
if os.path.exists(key_file) and os.path.getsize(key_file) > 32:
|
2022-07-02 15:45:24 +00:00
|
|
|
with open(key_file, "rb") as f:
|
|
|
|
key = f.read()
|
|
|
|
try:
|
|
|
|
urlsafe_b64decode(key)
|
|
|
|
generate = False
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
if generate:
|
|
|
|
key = Fernet.generate_key()
|
|
|
|
try:
|
|
|
|
with open(key_file, "wb") as f:
|
|
|
|
f.write(key)
|
|
|
|
except PermissionError as e:
|
|
|
|
error = e
|
|
|
|
return key, error
|