| 1 |
#!/usr/bin/env python |
| 2 |
# License: GPLv3 Copyright: 2015, Kovid Goyal <kovid at kovidgoyal.net> |
| 3 |
|
| 4 |
import json |
| 5 |
from functools import partial |
| 6 |
from importlib import import_module |
| 7 |
from threading import Lock |
| 8 |
from typing import Any, Protocol |
| 9 |
|
| 10 |
from calibre.srv.auth import AuthController |
| 11 |
from calibre.srv.errors import HTTPForbidden |
| 12 |
from calibre.srv.library_broker import LibraryBroker, path_for_db |
| 13 |
from calibre.srv.routes import Router |
| 14 |
from calibre.srv.users import UserManager |
| 15 |
from calibre.utils.date import utcnow |
| 16 |
from calibre.utils.search_query_parser import ParseException |
| 17 |
|
| 18 |
|
| 19 |
class UrlForCallable(Protocol): |
| 20 |
def __call__(self, route: str | None, **kwargs: Any) -> str: ... |
| 21 |
|
| 22 |
|
| 23 |
class Context: |
| 24 |
log = None |
| 25 |
url_for: UrlForCallable = lambda route, **kwargs: '' |
| 26 |
jobs_manager = None |
| 27 |
CATEGORY_CACHE_SIZE = 25 |
| 28 |
SEARCH_CACHE_SIZE = 100 |
| 29 |
|
| 30 |
def __init__(self, libraries, opts, testing=False, notify_changes=None): |
| 31 |
self.opts = opts |
| 32 |
self.library_broker = libraries if isinstance(libraries, LibraryBroker) else LibraryBroker(libraries) |
| 33 |
self.testing = testing |
| 34 |
self.lock = Lock() |
| 35 |
self.user_manager = UserManager(opts.userdb) |
| 36 |
self.ignored_fields = frozenset(filter(None, (x.strip() for x in (opts.ignored_fields or '').split(',')))) |
| 37 |
self.displayed_fields = frozenset(filter(None, (x.strip() for x in (opts.displayed_fields or '').split(',')))) |
| 38 |
self._notify_changes = notify_changes |
| 39 |
|
| 40 |
def notify_changes(self, library_path, change_event): |
| 41 |
if self._notify_changes is not None: |
| 42 |
self._notify_changes(library_path, change_event) |
| 43 |
|
| 44 |
def start_job(self, name, module, func, args=(), kwargs=None, job_done_callback=None, job_data=None): |
| 45 |
assert self.jobs_manager is not None |
| 46 |
return self.jobs_manager.start_job(name, module, func, args, kwargs, job_done_callback, job_data) |
| 47 |
|
| 48 |
def job_status(self, job_id): |
| 49 |
assert self.jobs_manager is not None |
| 50 |
return self.jobs_manager.job_status(job_id) |
| 51 |
|
| 52 |
def abort_job(self, job_id): |
| 53 |
assert self.jobs_manager is not None |
| 54 |
return self.jobs_manager.abort_job(job_id) |
| 55 |
|
| 56 |
def is_field_displayable(self, field): |
| 57 |
if self.displayed_fields and field not in self.displayed_fields: |
| 58 |
return False |
| 59 |
return field not in self.ignored_fields |
| 60 |
|
| 61 |
def init_session(self, endpoint, data): |
| 62 |
pass |
| 63 |
|
| 64 |
def finalize_session(self, endpoint, data, output): |
| 65 |
pass |
| 66 |
|
| 67 |
def get_library(self, request_data, library_id=None): |
| 68 |
if not request_data.username: |
| 69 |
return self.library_broker.get(library_id) |
| 70 |
lf = partial(self.user_manager.allowed_library_names, request_data.username) |
| 71 |
allowed_libraries = self.library_broker.allowed_libraries(lf) |
| 72 |
if not allowed_libraries: |
| 73 |
raise HTTPForbidden(f'The user {request_data.username} is not allowed to access any libraries on this server') |
| 74 |
library_id = library_id or next(iter(allowed_libraries)) |
| 75 |
if library_id in allowed_libraries: |
| 76 |
return self.library_broker.get(library_id) |
| 77 |
raise HTTPForbidden(f'The user {request_data.username} is not allowed to access the library {library_id}') |
| 78 |
|
| 79 |
def library_info(self, request_data): |
| 80 |
if not request_data.username: |
| 81 |
return self.library_broker.library_map, self.library_broker.default_library |
| 82 |
lf = partial(self.user_manager.allowed_library_names, request_data.username) |
| 83 |
allowed_libraries = self.library_broker.allowed_libraries(lf) |
| 84 |
if not allowed_libraries: |
| 85 |
raise HTTPForbidden(f'The user {request_data.username} is not allowed to access any libraries on this server') |
| 86 |
return dict(allowed_libraries), next(iter(allowed_libraries)) |
| 87 |
|
| 88 |
def restriction_for(self, request_data, db): |
| 89 |
return self.user_manager.library_restriction(request_data.username, path_for_db(db)) |
| 90 |
|
| 91 |
def has_id(self, request_data, db, book_id): |
| 92 |
restriction = self.restriction_for(request_data, db) |
| 93 |
if restriction: |
| 94 |
try: |
| 95 |
return book_id in db.search('', restriction=restriction, allow_templates=False) |
| 96 |
except ParseException: |
| 97 |
return False |
| 98 |
return db.has_id(book_id) |
| 99 |
|
| 100 |
def newest_book_ids(self, request_data, db, count=5): |
| 101 |
restriction = self.restriction_for(request_data, db) |
| 102 |
allowed_book_ids = None |
| 103 |
if restriction: |
| 104 |
allowed_book_ids = db.search('', restriction=restriction, allow_templates=False) |
| 105 |
return db.newly_added_book_ids(count=count, book_ids=allowed_book_ids) |
| 106 |
|
| 107 |
def get_allowed_book_ids_from_restriction(self, request_data, db): |
| 108 |
restriction = self.restriction_for(request_data, db) |
| 109 |
return frozenset(db.search('', restriction=restriction, allow_templates=False)) if restriction else None |
| 110 |
|
| 111 |
def allowed_book_ids(self, request_data, db): |
| 112 |
try: |
| 113 |
ans = self.get_allowed_book_ids_from_restriction(request_data, db) |
| 114 |
except ParseException: |
| 115 |
return frozenset() |
| 116 |
if ans is None: |
| 117 |
ans = db.all_book_ids() |
| 118 |
return ans |
| 119 |
|
| 120 |
def check_for_write_access(self, request_data): |
| 121 |
if not request_data.username: |
| 122 |
if request_data.is_trusted_ip: |
| 123 |
return |
| 124 |
raise HTTPForbidden('Anonymous users are not allowed to make changes') |
| 125 |
if self.user_manager.is_readonly(request_data.username): |
| 126 |
raise HTTPForbidden(f'The user {request_data.username} does not have permission to make changes') |
| 127 |
|
| 128 |
def get_effective_book_ids(self, db, request_data, vl, report_parse_errors=False): |
| 129 |
try: |
| 130 |
return db.books_in_virtual_library(vl, self.restriction_for(request_data, db)) |
| 131 |
except ParseException: |
| 132 |
if report_parse_errors: |
| 133 |
raise |
| 134 |
return frozenset() |
| 135 |
|
| 136 |
def get_categories(self, request_data, db, sort='name', first_letter_sort=True, vl='', report_parse_errors=False): |
| 137 |
restrict_to_ids = self.get_effective_book_ids(db, request_data, vl, report_parse_errors=report_parse_errors) |
| 138 |
key = restrict_to_ids, sort, first_letter_sort |
| 139 |
with self.lock: |
| 140 |
cache = self.library_broker.category_caches[db.server_library_id] |
| 141 |
old = cache.pop(key, None) |
| 142 |
if old is None or old[0] <= db.last_modified(): |
| 143 |
categories = db.get_categories(book_ids=restrict_to_ids, sort=sort, first_letter_sort=first_letter_sort) |
| 144 |
cache[key] = old = (utcnow(), categories) |
| 145 |
if len(cache) > self.CATEGORY_CACHE_SIZE: |
| 146 |
cache.popitem(last=False) |
| 147 |
else: |
| 148 |
cache[key] = old |
| 149 |
return old[1] |
| 150 |
|
| 151 |
def get_tag_browser(self, request_data, db, opts, render, vl=''): |
| 152 |
restrict_to_ids = self.get_effective_book_ids(db, request_data, vl) |
| 153 |
key = restrict_to_ids, opts |
| 154 |
with self.lock: |
| 155 |
cache = self.library_broker.category_caches[db.server_library_id] |
| 156 |
old = cache.pop(key, None) |
| 157 |
if old is None or old[0] <= db.last_modified(): |
| 158 |
categories = db.get_categories(book_ids=restrict_to_ids, sort=opts.sort_by, first_letter_sort=opts.collapse_model == 'first letter') |
| 159 |
data = json.dumps(render(db, categories), ensure_ascii=False) |
| 160 |
if isinstance(data, str): |
| 161 |
data = data.encode('utf-8') |
| 162 |
cache[key] = old = (utcnow(), data) |
| 163 |
if len(cache) > self.CATEGORY_CACHE_SIZE: |
| 164 |
cache.popitem(last=False) |
| 165 |
else: |
| 166 |
cache[key] = old |
| 167 |
return old[1] |
| 168 |
|
| 169 |
def search(self, request_data, db, query, vl='', report_restriction_errors=False): |
| 170 |
try: |
| 171 |
restrict_to_ids = self.get_effective_book_ids(db, request_data, vl, report_parse_errors=report_restriction_errors) |
| 172 |
except ParseException: |
| 173 |
try: |
| 174 |
self.get_allowed_book_ids_from_restriction(request_data, db) |
| 175 |
except ParseException as e: |
| 176 |
return frozenset(), e |
| 177 |
return frozenset(), None |
| 178 |
query = query or '' |
| 179 |
key = query, restrict_to_ids |
| 180 |
with self.lock: |
| 181 |
cache = self.library_broker.search_caches[db.server_library_id] |
| 182 |
old = cache.pop(key, None) |
| 183 |
if old is None or old[0] < db.clear_search_cache_count: |
| 184 |
matches = db.search(query, book_ids=restrict_to_ids, allow_templates=False) |
| 185 |
cache[key] = old = (db.clear_search_cache_count, matches) |
| 186 |
if len(cache) > self.SEARCH_CACHE_SIZE: |
| 187 |
cache.popitem(last=False) |
| 188 |
else: |
| 189 |
cache[key] = old |
| 190 |
if report_restriction_errors: |
| 191 |
return old[1], None |
| 192 |
return old[1] |
| 193 |
|
| 194 |
|
| 195 |
SRV_MODULES = ('ajax', 'books', 'cdb', 'code', 'content', 'legacy', 'opds', 'users_api', 'convert', 'fts') |
| 196 |
|
| 197 |
|
| 198 |
class Handler: |
| 199 |
def __init__(self, libraries, opts, testing=False, notify_changes=None): |
| 200 |
ctx = Context(libraries, opts, testing=testing, notify_changes=notify_changes) |
| 201 |
self.auth_controller = None |
| 202 |
if opts.auth: |
| 203 |
has_ssl = opts.ssl_certfile is not None and opts.ssl_keyfile is not None |
| 204 |
prefer_basic_auth = {'auto': has_ssl, 'basic': True}.get(opts.auth_mode, False) |
| 205 |
self.auth_controller = AuthController( |
| 206 |
user_credentials=ctx.user_manager, |
| 207 |
prefer_basic_auth=prefer_basic_auth, |
| 208 |
ban_time_in_minutes=opts.ban_for, |
| 209 |
ban_after=opts.ban_after, |
| 210 |
) |
| 211 |
self.router = Router(ctx=ctx, url_prefix=opts.url_prefix, auth_controller=self.auth_controller) |
| 212 |
for module in SRV_MODULES: |
| 213 |
module = import_module('calibre.srv.' + module) |
| 214 |
self.router.load_routes(vars(module).values()) |
| 215 |
self._load_content_server_plugin_routes() |
| 216 |
self.router.finalize() |
| 217 |
assert self.router.ctx is not None |
| 218 |
self.router.ctx.url_for = self.router.url_for |
| 219 |
self.dispatch = self.router.dispatch |
| 220 |
|
| 221 |
def _load_content_server_plugin_routes(self): |
| 222 |
from calibre.customize.ui import content_server_plugins |
| 223 |
|
| 224 |
for plugin in content_server_plugins(): |
| 225 |
try: |
| 226 |
endpoints = plugin.content_server_endpoints() |
| 227 |
except Exception: |
| 228 |
continue |
| 229 |
for ep in endpoints: |
| 230 |
try: |
| 231 |
self.router.add(ep) |
| 232 |
except ValueError: |
| 233 |
# Route key already exists (built-in or another plugin). |
| 234 |
# Plugins do not override existing routes. |
| 235 |
pass |
| 236 |
|
| 237 |
def set_log(self, log): |
| 238 |
assert self.router.ctx is not None |
| 239 |
self.router.ctx.log = log |
| 240 |
if self.auth_controller is not None: |
| 241 |
self.auth_controller.log = log |
| 242 |
|
| 243 |
def set_jobs_manager(self, jobs_manager): |
| 244 |
assert self.router.ctx is not None |
| 245 |
self.router.ctx.jobs_manager = jobs_manager |
| 246 |
|
| 247 |
def close(self): |
| 248 |
assert self.router is not None |
| 249 |
assert self.router.ctx is not None |
| 250 |
self.router.ctx.library_broker.close() |
| 251 |
|
| 252 |
@property |
| 253 |
def ctx(self): |
| 254 |
return self.router.ctx |