Compare commits
197 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0d52a9fd52 | ||
|
|
08287deaa4 | ||
|
|
c43c5520a4 | ||
|
|
4e2b7886a9 | ||
|
|
05744e8472 | ||
|
|
9126973378 | ||
|
|
018e1069d5 | ||
|
|
92617b3263 | ||
|
|
d0172f11c3 | ||
|
|
638126b5f3 | ||
|
|
ef938bff77 | ||
|
|
50597f8d3a | ||
|
|
b84e26fa3b | ||
|
|
d8ac700ee7 | ||
|
|
5519e23e02 | ||
|
|
4d6da6a9b7 | ||
|
|
1385babea5 | ||
|
|
ed916e1b28 | ||
|
|
97c18a290a | ||
|
|
e53bb11566 | ||
|
|
1ecc19b8b5 | ||
|
|
7ef1520756 | ||
|
|
5a5ea147b1 | ||
|
|
5a15241cc1 | ||
|
|
4a8035949c | ||
|
|
ab57dbfbe7 | ||
|
|
302dd8eeb4 | ||
|
|
e983db84ba | ||
|
|
51c026e9d2 | ||
|
|
435b425ab6 | ||
|
|
0981a36fb5 | ||
|
|
3292f35310 | ||
|
|
f85c126d03 | ||
|
|
2cc753f6d0 | ||
|
|
cb26aff4b9 | ||
|
|
ac1be219fb | ||
|
|
b34d915597 | ||
|
|
a2db43bbe1 | ||
|
|
024a5f9b8c | ||
|
|
070c667605 | ||
|
|
6e2ff07a48 | ||
|
|
46f40d29be | ||
|
|
71f2674b38 | ||
|
|
bd2551559b | ||
|
|
85bf7296ef | ||
|
|
64254ccf5f | ||
|
|
48b973d384 | ||
|
|
c17b696d61 | ||
|
|
8837fea654 | ||
|
|
7fe0bcee2f | ||
|
|
b8589ea026 | ||
|
|
de838ebb4d | ||
|
|
74af82f34f | ||
|
|
307b78f60d | ||
|
|
8ed52f62b8 | ||
|
|
93fcbb9084 | ||
|
|
2b017a2ae3 | ||
|
|
d32375d4e4 | ||
|
|
a965032b08 | ||
|
|
3a26a12807 | ||
|
|
0867e4020f | ||
|
|
f132cdfbdf | ||
|
|
4927cdf6ce | ||
|
|
cff6e5aab9 | ||
|
|
85007f3e91 | ||
|
|
5b6ef8202f | ||
|
|
0c07f638f9 | ||
|
|
5faa6a1e8f | ||
|
|
51b325d63c | ||
|
|
a4715be0ed | ||
|
|
c2c88cb397 | ||
|
|
a72a37ddfa | ||
|
|
512d9fc8cb | ||
|
|
6284154a39 | ||
|
|
d78991dc03 | ||
|
|
287edf3d12 | ||
|
|
7b6bd25189 | ||
|
|
b3ed9ea89c | ||
|
|
de9471c2d5 | ||
|
|
4ad5aad64e | ||
|
|
28d68ca569 | ||
|
|
db34b954c5 | ||
|
|
a22ec367a5 | ||
|
|
fd66034ba9 | ||
|
|
c2e1d062e8 | ||
|
|
17e42d7088 | ||
|
|
6858acd0b1 | ||
|
|
ea0845880b | ||
|
|
a9cc87f64f | ||
|
|
7bbe90a968 | ||
|
|
d970863a17 | ||
|
|
10b10f695a | ||
|
|
8272f48cba | ||
|
|
409befce5e | ||
|
|
105680e38a | ||
|
|
0ddf18ec26 | ||
|
|
46b69059e8 | ||
|
|
09a4e70bb0 | ||
|
|
cf5de3ca89 | ||
|
|
8134527a7d | ||
|
|
33aea2a194 | ||
|
|
772ea3ca0c | ||
|
|
5c77b1711b | ||
|
|
602fe39c00 | ||
|
|
6160afa7f0 | ||
|
|
68532e361e | ||
|
|
e0a315f77c | ||
|
|
bcce467bcb | ||
|
|
f5cff52588 | ||
|
|
3472ca7844 | ||
|
|
5372507acd | ||
|
|
01fb6479f3 | ||
|
|
90c3faadcf | ||
|
|
e594206759 | ||
|
|
80b21371a4 | ||
|
|
af575a87a2 | ||
|
|
a6aa8d8e95 | ||
|
|
aa436feae7 | ||
|
|
abce84999f | ||
|
|
74bb3f2362 | ||
|
|
abcbbc86bc | ||
|
|
d859202fb1 | ||
|
|
d71c235ebc | ||
|
|
49583d3407 | ||
|
|
a7f71562b4 | ||
|
|
f93269ac8b | ||
|
|
79f85f362a | ||
|
|
bf9ae0b9e1 | ||
|
|
678562525f | ||
|
|
abb3baf94b | ||
|
|
59b4f48897 | ||
|
|
dca59e02dd | ||
|
|
9bc92a7346 | ||
|
|
53264da6bd | ||
|
|
53e328ac2d | ||
|
|
59c2d73c05 | ||
|
|
fc729218ba | ||
|
|
15778eb3e4 | ||
|
|
ad1a6560fa | ||
|
|
7fa203eaae | ||
|
|
74d8cd3f94 | ||
|
|
fd29532cf1 | ||
|
|
1dc169d14b | ||
|
|
870f95a51f | ||
|
|
72ab94291c | ||
|
|
8de33fbd9a | ||
|
|
fd9bc45fb1 | ||
|
|
e356b87494 | ||
|
|
4371e1a641 | ||
|
|
95da605cb9 | ||
|
|
6dfa551b97 | ||
|
|
6a3b919f5f | ||
|
|
cac8e7c721 | ||
|
|
410aa01ac9 | ||
|
|
a8ed8b29e5 | ||
|
|
5a04e4f0c7 | ||
|
|
a6d9df7dec | ||
|
|
8cf370c79d | ||
|
|
35925dbc6e | ||
|
|
d0e79b0abb | ||
|
|
aba0c206f8 | ||
|
|
037b42a5b4 | ||
|
|
8a71c4040c | ||
|
|
e685f136e1 | ||
|
|
b8b40e8cb0 | ||
|
|
7e9f446079 | ||
|
|
13c3c98c63 | ||
|
|
1b70259ea7 | ||
|
|
a840fb7233 | ||
|
|
1ba54c1237 | ||
|
|
412335c0f1 | ||
|
|
6b91c43655 | ||
|
|
4b4865b6ed | ||
|
|
d5931138e3 | ||
|
|
3d1385da6e | ||
|
|
5630feba36 | ||
|
|
64a301eda1 | ||
|
|
044ab1ab26 | ||
|
|
d6260e3433 | ||
|
|
fb2eb62a98 | ||
|
|
d7c6b0e7ab | ||
|
|
94922f3926 | ||
|
|
a580b1eb6d | ||
|
|
cd7b8afb29 | ||
|
|
e634893ff3 | ||
|
|
fadc7ddc34 | ||
|
|
ed5dc25d94 | ||
|
|
dd11e8c5ad | ||
|
|
2db2b8cff4 | ||
|
|
4d3661b758 | ||
|
|
891b1e4fe8 | ||
|
|
d588b16885 | ||
|
|
a0e4651607 | ||
|
|
c21b8ffa0e | ||
|
|
f174617f33 | ||
|
|
2de9ad0edf | ||
|
|
a6592f2f8d |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,5 +1,4 @@
|
||||
/node_modules
|
||||
/server/.inpx-web*
|
||||
/server/inpx-web-filter.json
|
||||
/dist
|
||||
dev*.sh
|
||||
78
CHANGELOG.md
Normal file
78
CHANGELOG.md
Normal file
@@ -0,0 +1,78 @@
|
||||
1.5.2 / 2023-02-05
|
||||
|
||||
- Исправление проблемы чтения каталога opds для koreader
|
||||
|
||||
1.5.1 / 2023-01-28
|
||||
------------------
|
||||
|
||||
- Настройки веб-интерфейса и опции командной строки "--lib-dir", "--inpx" вынесены в конфиг (#6)
|
||||
- Уменьшена длина имени файла при скачивании (#7)
|
||||
- OPDS: добавлен раздел "Жанры", в поиск добавлен раздел "Поиск книг в жанре" (#9)
|
||||
- Исправление проблем скроллинга панели инструментов (#12)
|
||||
- Улучшено отображение веб-интерфейса на мобильных устройствах
|
||||
|
||||
1.4.1 / 2022-12-21
|
||||
------------------
|
||||
|
||||
- Добавлена возможность поиска по регулярному выражению (префикс "~")
|
||||
- Заплатка для исправления (#10)
|
||||
|
||||
1.4.0 / 2022-12-07
|
||||
------------------
|
||||
|
||||
- Добавлена возможность расширенного поиска (раздел "</>"). Поиск не оптимизирован и может сильно нагружать сервер.
|
||||
Отключить можно в конфиге, параметр extendedSearch
|
||||
- Улучшение поддержки reverse-proxy, в конфиг добавлены параметры server.root и opds.root для встраивания inpx-web в уже существующий веб-сервер
|
||||
- В настройки веб-интерфейса добавлена опция "Скачивать книги в виде zip-архива"
|
||||
- Исправлен баг "Android-читалки не очень хорошо работают с OPDS" (#4)
|
||||
- Добавлена сборка релизов для Linux arm64
|
||||
- В readme добавлена ссылка для донатов: [отблагодарить автора проекта](https://donatty.com/liberama)
|
||||
|
||||
1.3.3 / 2022-11-28
|
||||
------------------
|
||||
|
||||
- Исправление выявленных недочетов
|
||||
|
||||
1.3.2 / 2022-11-27
|
||||
------------------
|
||||
|
||||
- Изменения механизма ограничения доступа по паролю:
|
||||
- появилась возможность выхода из сессии
|
||||
- в конфиг добавлена настройка таймаута для автозавершения сессии
|
||||
- Добавлено отображение количества книг в серии в разделе "Авторы"
|
||||
|
||||
1.3.1 / 2022-11-25
|
||||
------------------
|
||||
|
||||
- Улучшена кроссплатформенность приложения
|
||||
|
||||
1.3.0 / 2022-11-24
|
||||
------------------
|
||||
|
||||
- Добавлен OPDS-сервер для inpx-коллекции
|
||||
- Произведена небольшая оптимизация поисковой БД
|
||||
- Добавлен релиз для macos, без тестирования
|
||||
|
||||
1.2.4 / 2022-11-14
|
||||
------------------
|
||||
|
||||
- Добавлена возможность посмотреть обложку в увеличении
|
||||
- Исправление выявленных недочетов
|
||||
|
||||
1.2.3 / 2022-11-12
|
||||
------------------
|
||||
|
||||
- Добавлено диалоговое окно "Информация о книге"
|
||||
- Небольшие изменения интерфейса, добавлена кнопка "Клонировать поиск"
|
||||
|
||||
1.1.4 / 2022-11-03
|
||||
------------------
|
||||
|
||||
- Исправлен баг "Не качает книги #1"
|
||||
|
||||
1.1.2 / 2022-10-31
|
||||
------------------
|
||||
|
||||
- Добавлены разделы "Серии" и "Книги"
|
||||
- Расширена форма поиска: добавлен поиск по датам поступления и оценкам
|
||||
|
||||
106
README.md
106
README.md
@@ -2,19 +2,26 @@ inpx-web
|
||||
========
|
||||
|
||||
Веб-сервер для поиска по .inpx-коллекции.
|
||||
Выглядит это так: https://lib.omnireader.ru
|
||||
|
||||
Выглядит следующим образом: [https://lib.omnireader.ru](https://lib.omnireader.ru)
|
||||
|
||||
.inpx - индексный файл для импорта\экспорта информации из базы данных сетевых библиотек
|
||||
в базу каталогизатора [MyHomeLib](https://alex80.github.io/mhl/)
|
||||
или [freeLib](http://sourceforge.net/projects/freelibdesign)
|
||||
или [LightLib](https://lightlib.azurewebsites.net)
|
||||
|
||||
Просто поместите приложение `inpx-web` в папку с .inpx-файлом и файлами библиотеки (zip-архивами) и запустите.
|
||||
Сервер будет доступен по адресу http://127.0.0.1:12380
|
||||
[Установка](#usage): просто поместить приложение `inpx-web` в папку с .inpx-файлом и файлами библиотеки (zip-архивами) и запустить.
|
||||
|
||||
После открытия веб-приложения в бразуере, для быстрого понимания того, как работает поиск, воспользуйтесь памяткой (кнопка со знаком вопроса).
|
||||
По умолчанию, веб-сервер будет доступен по адресу [http://127.0.0.1:12380](http://127.0.0.1:12380)
|
||||
|
||||
##
|
||||
OPDS-сервер доступен по адресу [http://127.0.0.1:12380/opds](http://127.0.0.1:12380/opds)
|
||||
|
||||
Для указания местоположения .inpx-файла или папки с файлами библиотеки, воспользуйтесь [параметрами командной строки](#cli).
|
||||
Дополнительные параметры сервера настраиваются в [конфигурационном файле](#config).
|
||||
|
||||
[Отблагодарить автора проекта](https://donatty.com/liberama)
|
||||
|
||||
##
|
||||
* [Возможности программы](#capabilities)
|
||||
* [Использование](#usage)
|
||||
* [Параметры командной строки](#cli)
|
||||
@@ -28,6 +35,7 @@ inpx-web
|
||||
<a id="capabilities" />
|
||||
|
||||
## Возможности программы
|
||||
- веб-интерфейс и OPDS-сервер
|
||||
- поиск по автору, серии, названию и пр.
|
||||
- скачивание книги, копирование ссылки или открытие в читалке
|
||||
- возможность указать рабочий каталог при запуске, а также расположение .inpx и файлов библиотеки
|
||||
@@ -36,7 +44,7 @@ inpx-web
|
||||
- фильтр авторов и книг при создании поисковой БД для создания своей коллекции "на лету"
|
||||
- подхват изменений .inpx-файла (периодическая проверка), автоматическое пересоздание поисковой БД
|
||||
- мощная оптимизация, хорошая скорость поиска
|
||||
- релизы под Linux и Windows
|
||||
- релизы под Linux, MacOS и Windows
|
||||
|
||||
<a id="usage" />
|
||||
|
||||
@@ -45,7 +53,9 @@ inpx-web
|
||||
Там же, при первом запуске, будет создана рабочая директория `.inpx-web`, в которой хранится
|
||||
конфигурационный файл `config.json`, файлы базы данных, журналы и прочее.
|
||||
|
||||
По умолчанию сервер будет доступен по адресу http://127.0.0.1:12380
|
||||
По умолчанию веб-интерфейс будет доступен по адресу [http://127.0.0.1:12380](http://127.0.0.1:12380)
|
||||
|
||||
OPDS-сервер доступен по адресу [http://127.0.0.1:12380/opds](http://127.0.0.1:12380/opds)
|
||||
|
||||
<a id="cli" />
|
||||
|
||||
@@ -70,9 +80,28 @@ Options:
|
||||
При первом запуске в рабочей директории будет создан конфигурационный файл `config.json`:
|
||||
```js
|
||||
{
|
||||
// директория библиотеки (с zip-архивами), аналог параметра командной строки --lib-dir
|
||||
// пустая строка: использовать значение по умолчанию - директорию файла приложения
|
||||
"libDir": "",
|
||||
|
||||
// путь к файлу .inpx, аналог параметра командной строки --inpx
|
||||
// пустая строка: использовать значение по умолчанию - inpx-файл, что найдется в директории библиотеки
|
||||
"inpx": "",
|
||||
|
||||
// пароль для ограничения доступа к веб-интерфейсу сервера
|
||||
// пустое значение - доступ без ограничений
|
||||
"accessPassword": "",
|
||||
|
||||
// таймаут автозавершения сессии доступа к веб-интерфейсу (если задан accessPassword),
|
||||
// при неактивности в течение указанного времени (в минутах), пароль будет запрошен заново
|
||||
// 0 - отключить таймаут, время доступа по паролю не ограничено
|
||||
"accessTimeout": 0,
|
||||
|
||||
// включить(true)/выключить(false) возможность расширенного поиска (раздел "</>")
|
||||
// расширенный поиск не оптимизирован, поэтому может сильно нагружать сервер
|
||||
// чтобы ускорить поиск, увеличьте параметр dbCacheSize
|
||||
"extendedSearch": true,
|
||||
|
||||
// содержимое кнопки-ссылки "(читать)", если не задано - кнопка "(читать)" не показывается
|
||||
// пример: "https://omnireader.ru/#/reader?url=${DOWNLOAD_LINK}"
|
||||
// на место ${DOWNLOAD_LINK} будет подставлена ссылка на скачивание файла книги
|
||||
@@ -85,13 +114,21 @@ Options:
|
||||
// если надо кешировать всю БД, можно поставить значение от 1000 и больше
|
||||
"dbCacheSize": 5,
|
||||
|
||||
// максимальный размер в байтах директории закешированных файлов в <раб.дир>/public/files
|
||||
// максимальный размер в байтах директории закешированных файлов в <раб.дир>/public-files
|
||||
// чистка каждый час
|
||||
"maxFilesDirSize": 1073741824,
|
||||
|
||||
// включить(true)/выключить(false) кеширование запросов на сервере
|
||||
// включить(true)/выключить(false) серверное кеширование запросов на диске и в памяти
|
||||
"queryCacheEnabled": true,
|
||||
|
||||
// размер кеша запросов в оперативной памяти (количество)
|
||||
// 0 - отключить кеширование запросов в оперативной памяти
|
||||
"queryCacheMemSize": 50,
|
||||
|
||||
// размер кеша запросов на диске (количество)
|
||||
// 0 - отключить кеширование запросов на диске
|
||||
"queryCacheDiskSize": 500,
|
||||
|
||||
// периодичность чистки кеша запросов на сервере, в минутах
|
||||
// 0 - отключить чистку
|
||||
"cacheCleanInterval": 60,
|
||||
@@ -118,9 +155,40 @@ Options:
|
||||
"remoteLib": false,
|
||||
|
||||
// настройки веб-сервера
|
||||
// парамертр root указывает путь для кореневой страницы inpx-web
|
||||
// например для "root": "/library", веб-интерфейс будет доступен по адресу http://127.0.0.1:12380/library
|
||||
// root необходим при настройке reverse-proxy и встраивании inpx-web в уже существующий сервер
|
||||
"server": {
|
||||
"host": "0.0.0.0",
|
||||
"port": "12380"
|
||||
"port": "12380",
|
||||
"root": ""
|
||||
},
|
||||
|
||||
// настройки opds-сервера
|
||||
// user, password используются для Basic HTTP authentication
|
||||
// параметр root задает путь для доступа к opds-серверу
|
||||
"opds": {
|
||||
"enabled": true,
|
||||
"user": "",
|
||||
"password": "",
|
||||
"root": "/opds"
|
||||
},
|
||||
|
||||
// настройки по умолчанию для веб-интерфейса
|
||||
// устанавливаются при первой загрузке страницы в браузере
|
||||
// дальнейшие изменения настроек с помощью веб-интерфейса уже сохраняются в самом браузере
|
||||
"uiDefaults": {
|
||||
"limit": 20, // результатов на странице
|
||||
"downloadAsZip": false, // скачивать книги в виде zip-архива
|
||||
"showCounts": true, // показывать количество
|
||||
"showRates": true, // показывать оценки
|
||||
"showInfo": true, // показывать кнопку (инфо)
|
||||
"showGenres": true, // показывать жанры
|
||||
"showDates": false, // показывать даты поступления
|
||||
"showDeleted": false, // показывать удаленные
|
||||
"abCacheEnabled": true, // кешировать запросы
|
||||
"langDefault": "", // язык по умолчанию (например "ru,en")
|
||||
"showJson": false // показывать JSON (в расширенном поиске)
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -161,14 +229,13 @@ Options:
|
||||
|
||||
### Фильтр по авторам и книгам
|
||||
|
||||
При создании поисковой БД во время загрузки и парсинга .inpx-файла, имеется возможность
|
||||
При создании поисковой БД, во время загрузки и парсинга .inpx-файла, имеется возможность
|
||||
отфильтровать авторов и книги, задав определенные критерии. Для этого небходимо создать
|
||||
в рабочей директории (там же, где `config.json`) файл `filter.json` следующего вида:
|
||||
```json
|
||||
{
|
||||
"info": {
|
||||
"collection": "Новое название коллекции",
|
||||
"structure": "",
|
||||
"version": "1.0.0"
|
||||
},
|
||||
"filter": "(r) => r.del == 0",
|
||||
@@ -176,7 +243,7 @@ Options:
|
||||
"excludeAuthors": ["Имя автора"]
|
||||
}
|
||||
```
|
||||
При создании поисковой БД, авторы и книги из `includeAuthors` будут добавлены, а из `excludeAuthors` исключены.
|
||||
При фильтрации, авторы и их книги из `includeAuthors` будут оставлены, а из `excludeAuthors` исключены.
|
||||
Использование совместно `includeAuthors` и `excludeAuthors` имеет мало смысла, поэтому для включения
|
||||
определенных авторов можно использовать только `includeAuthors`:
|
||||
```json
|
||||
@@ -250,23 +317,20 @@ sudo service nginx reload
|
||||
Сборка только в среде Linux.
|
||||
Необходима версия node.js не ниже 16.
|
||||
|
||||
Для сборки linux-arm64 необходимо предварительно установить [QEMU](https://wiki.debian.org/QemuUserEmulation).
|
||||
|
||||
```sh
|
||||
git clone https://github.com/bookpauk/inpx-web
|
||||
cd inpx-web
|
||||
npm i
|
||||
```
|
||||
|
||||
#### Для платформы Windows
|
||||
#### Релизы
|
||||
```sh
|
||||
npm run build:win
|
||||
npm run release
|
||||
```
|
||||
|
||||
#### Для платформы Linux
|
||||
```sh
|
||||
npm run build:linux
|
||||
```
|
||||
|
||||
Результат сборки будет доступен в каталоге `dist/linux|win` в виде исполнимого (standalone) файла.
|
||||
Результат сборки будет доступен в каталоге `dist/release`
|
||||
|
||||
<a id="development" />
|
||||
|
||||
|
||||
1
build/appdir.js
Normal file
1
build/appdir.js
Normal file
@@ -0,0 +1 @@
|
||||
module.exports = 'app0b58f8bd9fbfa95504ba';
|
||||
@@ -2,6 +2,8 @@ const fs = require('fs-extra');
|
||||
const path = require('path');
|
||||
const { execSync } = require('child_process');
|
||||
|
||||
const showdown = require('showdown');
|
||||
|
||||
const platform = process.argv[2];
|
||||
|
||||
const distDir = path.resolve(__dirname, '../dist');
|
||||
@@ -10,11 +12,17 @@ const publicDir = `${tmpDir}/public`;
|
||||
const outDir = `${distDir}/${platform}`;
|
||||
|
||||
async function build() {
|
||||
if (platform != 'linux' && platform != 'win')
|
||||
throw new Error(`Unknown platform: ${platform}`);
|
||||
if (!platform)
|
||||
throw new Error(`Please set platform`);
|
||||
|
||||
await fs.emptyDir(outDir);
|
||||
|
||||
//добавляем readme в релиз
|
||||
let readme = await fs.readFile(path.resolve(__dirname, '../README.md'), 'utf-8');
|
||||
const converter = new showdown.Converter();
|
||||
readme = converter.makeHtml(readme);
|
||||
await fs.writeFile(`${outDir}/readme.html`, readme);
|
||||
|
||||
// перемещаем public на место
|
||||
if (await fs.pathExists(publicDir)) {
|
||||
|
||||
|
||||
@@ -22,6 +22,8 @@ async function main() {
|
||||
await fs.emptyDir(outDir);
|
||||
await makeRelease('win');
|
||||
await makeRelease('linux');
|
||||
await makeRelease('linux-arm64');
|
||||
await makeRelease('macos');
|
||||
} catch(e) {
|
||||
console.error(e);
|
||||
process.exit(1);
|
||||
|
||||
@@ -2,6 +2,8 @@ const path = require('path');
|
||||
const DefinePlugin = require('webpack').DefinePlugin;
|
||||
const { VueLoaderPlugin } = require('vue-loader');
|
||||
|
||||
const appdir = require('./appdir');
|
||||
|
||||
const clientDir = path.resolve(__dirname, '../client');
|
||||
|
||||
module.exports = {
|
||||
@@ -12,7 +14,7 @@ module.exports = {
|
||||
},
|
||||
entry: [`${clientDir}/main.js`],
|
||||
output: {
|
||||
publicPath: '/app/',
|
||||
publicPath: `/${appdir}/`,
|
||||
clean: true
|
||||
},
|
||||
|
||||
|
||||
@@ -16,9 +16,8 @@ module.exports = merge(baseWpConfig, {
|
||||
mode: 'development',
|
||||
devtool: 'inline-source-map',
|
||||
output: {
|
||||
path: `${publicDir}/app`,
|
||||
path: `${publicDir}${baseWpConfig.output.publicPath}`,
|
||||
filename: 'bundle.js',
|
||||
clean: true
|
||||
},
|
||||
|
||||
module: {
|
||||
|
||||
@@ -18,9 +18,8 @@ fs.emptyDirSync(publicDir);
|
||||
module.exports = merge(baseWpConfig, {
|
||||
mode: 'production',
|
||||
output: {
|
||||
path: `${publicDir}/app`,
|
||||
path: `${publicDir}${baseWpConfig.output.publicPath}`,
|
||||
filename: 'bundle.[contenthash].js',
|
||||
clean: true
|
||||
},
|
||||
module: {
|
||||
rules: [
|
||||
|
||||
@@ -60,10 +60,21 @@ const componentOptions = {
|
||||
settings() {
|
||||
this.loadSettings();
|
||||
},
|
||||
modelValue(newValue) {
|
||||
this.accessGranted = newValue;
|
||||
},
|
||||
accessGranted(newValue) {
|
||||
this.$emit('update:modelValue', newValue);
|
||||
}
|
||||
},
|
||||
};
|
||||
class Api {
|
||||
_options = componentOptions;
|
||||
_props = {
|
||||
modelValue: Boolean,
|
||||
};
|
||||
accessGranted = false;
|
||||
|
||||
busyDialogVisible = false;
|
||||
mainMessage = '';
|
||||
jobMessage = '';
|
||||
@@ -98,10 +109,6 @@ class Api {
|
||||
}
|
||||
}
|
||||
|
||||
get config() {
|
||||
return this.$store.state.config;
|
||||
}
|
||||
|
||||
get settings() {
|
||||
return this.$store.state.settings;
|
||||
}
|
||||
@@ -123,7 +130,13 @@ class Api {
|
||||
});
|
||||
|
||||
if (result && result.value) {
|
||||
const accessToken = utils.toHex(cryptoUtils.sha256(result.value));
|
||||
//получим свежую соль
|
||||
const response = await wsc.message(await wsc.send({}), 10);
|
||||
let salt = '';
|
||||
if (response && response.error == 'need_access_token' && response.salt)
|
||||
salt = response.salt;
|
||||
|
||||
const accessToken = utils.toHex(cryptoUtils.sha256(result.value + salt));
|
||||
this.commit('setSettings', {accessToken});
|
||||
}
|
||||
} finally {
|
||||
@@ -192,10 +205,13 @@ class Api {
|
||||
const response = await wsc.message(await wsc.send(params), timeoutSecs);
|
||||
|
||||
if (response && response.error == 'need_access_token') {
|
||||
this.accessGranted = false;
|
||||
await this.showPasswordDialog();
|
||||
} else if (response && response.error == 'server_busy') {
|
||||
this.accessGranted = true;
|
||||
await this.showBusyDialog();
|
||||
} else {
|
||||
this.accessGranted = true;
|
||||
if (response.error) {
|
||||
throw new Error(response.error);
|
||||
}
|
||||
@@ -219,10 +235,18 @@ class Api {
|
||||
return await this.request({action: 'search', from, query}, 30);
|
||||
}
|
||||
|
||||
async bookSearch(query) {
|
||||
return await this.request({action: 'bookSearch', query}, 30);
|
||||
}
|
||||
|
||||
async getAuthorBookList(authorId) {
|
||||
return await this.request({action: 'get-author-book-list', authorId});
|
||||
}
|
||||
|
||||
async getAuthorSeriesList(authorId) {
|
||||
return await this.request({action: 'get-author-series-list', authorId});
|
||||
}
|
||||
|
||||
async getSeriesBookList(series) {
|
||||
return await this.request({action: 'get-series-book-list', series});
|
||||
}
|
||||
@@ -231,17 +255,23 @@ class Api {
|
||||
return await this.request({action: 'get-genre-tree'});
|
||||
}
|
||||
|
||||
async getBookLink(bookId) {
|
||||
return await this.request({action: 'get-book-link', bookId}, 120);
|
||||
async getBookLink(bookUid) {
|
||||
return await this.request({action: 'get-book-link', bookUid}, 120);
|
||||
}
|
||||
|
||||
async getBookInfo(bookId) {
|
||||
return await this.request({action: 'get-book-info', bookId}, 120);
|
||||
async getBookInfo(bookUid) {
|
||||
return await this.request({action: 'get-book-info', bookUid}, 120);
|
||||
}
|
||||
|
||||
async getConfig() {
|
||||
return await this.request({action: 'get-config'});
|
||||
}
|
||||
|
||||
async logout() {
|
||||
await this.request({action: 'logout'});
|
||||
this.accessGranted = false;
|
||||
await this.request({action: 'test'});
|
||||
}
|
||||
}
|
||||
|
||||
export default vueComponent(Api);
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
import WebSocketConnection from '../../../server/core/WebSocketConnection';
|
||||
|
||||
export default new WebSocketConnection();
|
||||
const protocol = (window.location.protocol == 'https:' ? 'wss:' : 'ws:');
|
||||
let url = `${protocol}//${window.location.host}${window.location.pathname}`;
|
||||
url += (url[url.length - 1] === '/' ? 'ws' : '/ws');
|
||||
|
||||
export default new WebSocketConnection(url);
|
||||
@@ -1,10 +1,10 @@
|
||||
<template>
|
||||
<div class="fit row">
|
||||
<Api ref="api" />
|
||||
<Api ref="api" v-model="accessGranted" />
|
||||
<Notify ref="notify" />
|
||||
<StdDialog ref="stdDialog" />
|
||||
|
||||
<router-view v-slot="{ Component }">
|
||||
<router-view v-if="accessGranted" v-slot="{ Component }">
|
||||
<keep-alive>
|
||||
<component :is="Component" class="col" />
|
||||
</keep-alive>
|
||||
@@ -37,6 +37,7 @@ const componentOptions = {
|
||||
};
|
||||
class App {
|
||||
_options = componentOptions;
|
||||
accessGranted = false;
|
||||
|
||||
created() {
|
||||
this.commit = this.$store.commit;
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
|
||||
<div class="q-ml-sm text-bold" style="color: #555">
|
||||
{{ getBookCount(item) }}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div v-if="item.bookLoading" class="book-row row items-center">
|
||||
@@ -54,6 +54,10 @@
|
||||
<div class="clickable2 q-ml-xs q-py-sm text-bold" @click="selectSeries(book.series)">
|
||||
Серия: {{ book.series }}
|
||||
</div>
|
||||
|
||||
<div class="q-ml-sm text-bold" style="color: #555">
|
||||
{{ getSeriesBookCount(item, book) }}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div v-if="isExpandedSeries(book) && book.seriesBooks">
|
||||
@@ -184,6 +188,26 @@ class AuthorList extends BaseList {
|
||||
return `(${result})`;
|
||||
}
|
||||
|
||||
getSeriesBookCount(item, book) {
|
||||
let result = '';
|
||||
if (!this.showCounts || book.type != 'series')
|
||||
return result;
|
||||
|
||||
let count = book.seriesBooks.length;
|
||||
result = `${count}`;
|
||||
if (item.seriesLoaded) {
|
||||
const rec = item.seriesLoaded[book.series];
|
||||
// заплатка для исправления https://github.com/bookpauk/inpx-web/issues/10
|
||||
// по невыясненным причинам rec иногда равен undefined
|
||||
if (rec) {
|
||||
const totalCount = (this.showDeleted ? rec.bookCount + rec.bookDelCount : rec.bookCount);
|
||||
result += `/${totalCount}`;
|
||||
}
|
||||
}
|
||||
|
||||
return `(${result})`;
|
||||
}
|
||||
|
||||
async expandAuthor(item) {
|
||||
this.$emit('listEvent', {action: 'ignoreScroll'});
|
||||
|
||||
@@ -209,6 +233,19 @@ class AuthorList extends BaseList {
|
||||
}
|
||||
}
|
||||
|
||||
async getAuthorSeries(item) {
|
||||
if (item.seriesLoaded)
|
||||
return;
|
||||
|
||||
const series = await this.loadAuthorSeries(item.key);
|
||||
const loaded = {};
|
||||
for (const s of series) {
|
||||
loaded[s.series] = {bookCount: s.bookCount, bookDelCount: s.bookDelCount};
|
||||
}
|
||||
|
||||
item.seriesLoaded = loaded;
|
||||
}
|
||||
|
||||
async getAuthorBooks(item) {
|
||||
if (item.books) {
|
||||
if (item.count > this.maxItemCount) {
|
||||
@@ -238,6 +275,13 @@ class AuthorList extends BaseList {
|
||||
const booksToFilter = await this.loadAuthorBooks(item.key);
|
||||
const filtered = this.filterBooks(booksToFilter);
|
||||
|
||||
if (!filtered.length && this.list.totalFound == 1) {
|
||||
this.list.queryFound = 0;
|
||||
this.list.totalFound = 0;
|
||||
this.searchResult.found = [];
|
||||
return false;
|
||||
}
|
||||
|
||||
const prepareBook = (book) => {
|
||||
return Object.assign(
|
||||
{
|
||||
@@ -303,6 +347,7 @@ class AuthorList extends BaseList {
|
||||
}
|
||||
|
||||
item.booksLoaded = books;
|
||||
this.getAuthorSeries(item);//no await
|
||||
this.showMore(item);
|
||||
|
||||
await this.$nextTick();
|
||||
@@ -335,6 +380,7 @@ class AuthorList extends BaseList {
|
||||
name: rec.author.replace(/,/g, ', '),
|
||||
count,
|
||||
booksLoaded: false,
|
||||
seriesLoaded: false,
|
||||
books: false,
|
||||
bookLoading: false,
|
||||
showMore: false,
|
||||
@@ -345,7 +391,10 @@ class AuthorList extends BaseList {
|
||||
if (authors.length > 1 || item.count > this.maxItemCount)
|
||||
this.getAuthorBooks(item);//no await
|
||||
else
|
||||
await this.getAuthorBooks(item);
|
||||
if (await this.getAuthorBooks(item) === false) {
|
||||
this.tableData = [];
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
result.push(item);
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import axios from 'axios';
|
||||
import dayjs from 'dayjs';
|
||||
import _ from 'lodash';
|
||||
|
||||
@@ -20,13 +21,16 @@ const componentOptions = {
|
||||
this.loadSettings();
|
||||
},
|
||||
search: {
|
||||
handler(newValue) {
|
||||
this.limit = newValue.limit;
|
||||
|
||||
if (this.pageCount > 1)
|
||||
this.prevPage = this.search.page;
|
||||
|
||||
this.refresh();
|
||||
handler() {
|
||||
if (!this.isExtendedSearch)
|
||||
this.refresh();
|
||||
},
|
||||
deep: true,
|
||||
},
|
||||
extSearch: {
|
||||
handler() {
|
||||
if (this.isExtendedSearch)
|
||||
this.refresh();
|
||||
},
|
||||
deep: true,
|
||||
},
|
||||
@@ -40,6 +44,7 @@ export default class BaseList {
|
||||
_props = {
|
||||
list: Object,
|
||||
search: Object,
|
||||
extSearch: Object,
|
||||
genreMap: Object,
|
||||
};
|
||||
|
||||
@@ -50,6 +55,7 @@ export default class BaseList {
|
||||
expandedAuthor = [];
|
||||
expandedSeries = [];
|
||||
|
||||
downloadAsZip = false;
|
||||
showCounts = true;
|
||||
showRates = true;
|
||||
showGenres = true;
|
||||
@@ -66,6 +72,7 @@ export default class BaseList {
|
||||
tableData = [];
|
||||
|
||||
created() {
|
||||
this.isExtendedSearch = false;
|
||||
this.commit = this.$store.commit;
|
||||
this.api = this.$root.api;
|
||||
|
||||
@@ -81,6 +88,7 @@ export default class BaseList {
|
||||
|
||||
this.expandedAuthor = _.cloneDeep(settings.expandedAuthor);
|
||||
this.expandedSeries = _.cloneDeep(settings.expandedSeries);
|
||||
this.downloadAsZip = settings.downloadAsZip;
|
||||
this.showCounts = settings.showCounts;
|
||||
this.showRates = settings.showRates;
|
||||
this.showGenres = settings.showGenres;
|
||||
@@ -105,16 +113,19 @@ export default class BaseList {
|
||||
}
|
||||
|
||||
selectAuthor(author) {
|
||||
this.search.author = `=${author}`;
|
||||
const search = (this.isExtendedSearch ? this.extSearch : this.search);
|
||||
search.author = `=${author}`;
|
||||
this.scrollToTop();
|
||||
}
|
||||
|
||||
selectSeries(series) {
|
||||
this.search.series = `=${series}`;
|
||||
const search = (this.isExtendedSearch ? this.extSearch : this.search);
|
||||
search.series = `=${series}`;
|
||||
}
|
||||
|
||||
selectTitle(title) {
|
||||
this.search.title = `=${title}`;
|
||||
const search = (this.isExtendedSearch ? this.extSearch : this.search);
|
||||
search.title = `=${title}`;
|
||||
}
|
||||
|
||||
async download(book, action) {
|
||||
@@ -130,16 +141,23 @@ export default class BaseList {
|
||||
|
||||
try {
|
||||
//подготовка
|
||||
const response = await this.api.getBookLink(book.id);
|
||||
const response = await this.api.getBookLink(book._uid);
|
||||
|
||||
const link = response.link;
|
||||
const href = `${window.location.origin}${link}`;
|
||||
let href = `${window.location.origin}${link}`;
|
||||
|
||||
//downloadAsZip
|
||||
if (this.downloadAsZip && (action == 'download' || action == 'copyLink')) {
|
||||
href += '/zip';
|
||||
//подожлем формирования zip-файла
|
||||
await axios.head(href);
|
||||
}
|
||||
|
||||
//action
|
||||
if (action == 'download') {
|
||||
//скачивание
|
||||
const d = this.$refs.download;
|
||||
d.href = href;
|
||||
d.download = response.downFileName;
|
||||
|
||||
d.click();
|
||||
} else if (action == 'copyLink') {
|
||||
@@ -164,7 +182,7 @@ export default class BaseList {
|
||||
}
|
||||
} else if (action == 'bookInfo') {
|
||||
//информация о книге
|
||||
const response = await this.api.getBookInfo(book.id);
|
||||
const response = await this.api.getBookInfo(book._uid);
|
||||
this.$emit('listEvent', {action: 'bookInfo', data: response.bookInfo});
|
||||
}
|
||||
} catch(e) {
|
||||
@@ -253,7 +271,30 @@ export default class BaseList {
|
||||
result = await this.api.getAuthorBookList(authorId);
|
||||
}
|
||||
|
||||
return (result.books ? JSON.parse(result.books) : []);
|
||||
return result.books;
|
||||
} catch (e) {
|
||||
this.$root.stdDialog.alert(e.message, 'Ошибка');
|
||||
}
|
||||
}
|
||||
|
||||
async loadAuthorSeries(authorId) {
|
||||
try {
|
||||
let result;
|
||||
|
||||
if (this.abCacheEnabled) {
|
||||
const key = `author-${authorId}-series-${this.list.inpxHash}`;
|
||||
const data = await authorBooksStorage.getData(key);
|
||||
if (data) {
|
||||
result = JSON.parse(data);
|
||||
} else {
|
||||
result = await this.api.getAuthorSeriesList(authorId);
|
||||
await authorBooksStorage.setData(key, JSON.stringify(result));
|
||||
}
|
||||
} else {
|
||||
result = await this.api.getAuthorSeriesList(authorId);
|
||||
}
|
||||
|
||||
return result.series;
|
||||
} catch (e) {
|
||||
this.$root.stdDialog.alert(e.message, 'Ошибка');
|
||||
}
|
||||
@@ -276,7 +317,7 @@ export default class BaseList {
|
||||
result = await this.api.getSeriesBookList(series);
|
||||
}
|
||||
|
||||
return (result.books ? JSON.parse(result.books) : []);
|
||||
return result.books;
|
||||
} catch (e) {
|
||||
this.$root.stdDialog.alert(e.message, 'Ошибка');
|
||||
}
|
||||
@@ -344,7 +385,14 @@ export default class BaseList {
|
||||
} else if (searchValue[0] == '#') {
|
||||
|
||||
searchValue = searchValue.substring(1);
|
||||
return !bookValue || (bookValue !== emptyFieldValue && !enru.has(bookValue[0]) && bookValue.indexOf(searchValue) >= 0);
|
||||
if (!bookValue)
|
||||
return false;
|
||||
return bookValue !== emptyFieldValue && !enru.has(bookValue[0]) && bookValue.indexOf(searchValue) >= 0;
|
||||
} else if (searchValue[0] == '~') {//RegExp
|
||||
|
||||
searchValue = searchValue.substring(1);
|
||||
const re = new RegExp(searchValue, 'i');
|
||||
return re.test(bookValue);
|
||||
} else {
|
||||
//where = `@dirtyIndexLR('value', ${db.esc(a)}, ${db.esc(a + maxUtf8Char)})`;
|
||||
return bookValue.localeCompare(searchValue) >= 0 && bookValue.localeCompare(searchValue + maxUtf8Char) <= 0;
|
||||
@@ -483,9 +531,9 @@ export default class BaseList {
|
||||
}
|
||||
|
||||
getQuery() {
|
||||
let newQuery = _.cloneDeep(this.search);
|
||||
newQuery = newQuery.setDefaults(newQuery);
|
||||
delete newQuery.setDefaults;
|
||||
const search = (this.isExtendedSearch ? this.extSearch : this.search);
|
||||
const newQuery = {};
|
||||
search.setDefaults(newQuery, search);
|
||||
|
||||
//дата
|
||||
if (newQuery.date) {
|
||||
@@ -496,8 +544,8 @@ export default class BaseList {
|
||||
newQuery.offset = (newQuery.page - 1)*newQuery.limit;
|
||||
|
||||
//del
|
||||
if (!this.showDeleted)
|
||||
newQuery.del = 0;
|
||||
if (!newQuery.del && !this.showDeleted)
|
||||
newQuery.del = '0';
|
||||
|
||||
return newQuery;
|
||||
}
|
||||
|
||||
@@ -17,10 +17,12 @@
|
||||
</div>
|
||||
|
||||
<div class="row q-mt-sm no-wrap">
|
||||
<div class="column justify-center" style="height: 300px; width: 200px; min-width: 100px">
|
||||
<img v-if="coverSrc" :src="coverSrc" class="fit row justify-center items-center" style="object-fit: contain" @error="coverSrc = ''" />
|
||||
<div v-if="!coverSrc" class="fit row justify-center items-center text-grey-5" style="border: 1px solid #ccc; font-size: 300%">
|
||||
<i>{{ book.ext }}</i>
|
||||
<div class="poster-size">
|
||||
<div class="column justify-center items-center" :class="{'poster': coverSrc, 'no-poster': !coverSrc}" @click.stop.prevent="posterClick">
|
||||
<img v-if="coverSrc" :src="coverSrc" class="fit row justify-center items-center" style="object-fit: contain" @error="coverSrc = ''" />
|
||||
<div v-if="!coverSrc" class="fit row justify-center items-center text-grey-5" style="border: 1px solid #ccc; font-size: 300%">
|
||||
<i>{{ book.ext }}</i>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -71,6 +73,19 @@
|
||||
OK
|
||||
</q-btn>
|
||||
</template>
|
||||
|
||||
|
||||
<Dialog v-model="posterDialogVisible">
|
||||
<template #header>
|
||||
<div class="row items-center">
|
||||
<div style="font-size: 110%">
|
||||
Обложка
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<img :src="coverSrc" class="fit q-pb-sm" style="height: 100%; max-height: calc(100vh - 140px); object-fit: contain" />
|
||||
</Dialog>
|
||||
</Dialog>
|
||||
</template>
|
||||
|
||||
@@ -106,6 +121,7 @@ class BookInfoDialog {
|
||||
};
|
||||
|
||||
dialogVisible = false;
|
||||
posterDialogVisible = false;
|
||||
selectedTab = 'fb2';
|
||||
|
||||
//info props
|
||||
@@ -158,7 +174,6 @@ class BookInfoDialog {
|
||||
{name: 'fileInfo', label: 'Информация о файле', value: [
|
||||
{name: 'folder', label: 'Папка'},
|
||||
{name: 'file', label: 'Файл'},
|
||||
{name: 'ext', label: 'Тип'},
|
||||
{name: 'size', label: 'Размер'},
|
||||
{name: 'date', label: 'Добавлен'},
|
||||
{name: 'del', label: 'Удален'},
|
||||
@@ -177,7 +192,10 @@ class BookInfoDialog {
|
||||
]},
|
||||
];
|
||||
|
||||
const valueToString = (value, nodePath) => {//eslint-disable-line no-unused-vars
|
||||
const valueToString = (value, nodePath, b) => {//eslint-disable-line no-unused-vars
|
||||
if (nodePath == 'fileInfo/file')
|
||||
return `${value}.${b.ext}`;
|
||||
|
||||
if (nodePath == 'fileInfo/size')
|
||||
return `${this.formatSize(value)} (${value.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1 ')} Bytes)`;
|
||||
|
||||
@@ -185,7 +203,10 @@ class BookInfoDialog {
|
||||
return utils.sqlDateFormat(value);
|
||||
|
||||
if (nodePath == 'fileInfo/del')
|
||||
return (value ? 'Да' : '');
|
||||
return (value ? 'Да' : null);
|
||||
|
||||
if (nodePath == 'fileInfo/insno')
|
||||
return (value ? value : null);
|
||||
|
||||
if (nodePath == 'titleInfo/author')
|
||||
return value.split(',').join(', ');
|
||||
@@ -211,7 +232,7 @@ class BookInfoDialog {
|
||||
const subItemOut = {
|
||||
name: subItem.name,
|
||||
label: subItem.label,
|
||||
value: valueToString(book[subItem.name], `${item.name}/${subItem.name}`)
|
||||
value: valueToString(book[subItem.name], `${item.name}/${subItem.name}`, book)
|
||||
};
|
||||
if (subItemOut.value)
|
||||
itemOut.value.push(subItemOut);
|
||||
@@ -273,6 +294,13 @@ class BookInfoDialog {
|
||||
this.book = bookInfo.book;
|
||||
}
|
||||
|
||||
posterClick() {
|
||||
if (!this.coverSrc)
|
||||
return;
|
||||
|
||||
this.posterDialogVisible = true;
|
||||
}
|
||||
|
||||
okClick() {
|
||||
this.dialogVisible = false;
|
||||
}
|
||||
@@ -283,6 +311,26 @@ export default vueComponent(BookInfoDialog);
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.poster-size {
|
||||
height: 300px;
|
||||
width: 200px;
|
||||
min-width: 100px;
|
||||
}
|
||||
|
||||
.poster, .no-poster {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
.poster:hover {
|
||||
position: relative;
|
||||
top: -1%;
|
||||
left: -1%;
|
||||
width: 102%;
|
||||
height: 102%;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
</style>
|
||||
|
||||
<style>
|
||||
|
||||
@@ -33,8 +33,8 @@
|
||||
</div>
|
||||
|
||||
<div class="q-ml-sm column">
|
||||
<div v-if="(mode == 'series' || mode == 'title') && bookAuthor" class="row">
|
||||
<div class="clickable2 text-green-10" @click="emit('authorClick')">
|
||||
<div v-if="(mode == 'series' || mode == 'title' || mode == 'extended') && bookAuthor" class="row">
|
||||
<div class="clickable2 text-green-10" @click.stop.prevent="emit('authorClick')">
|
||||
{{ bookAuthor }}
|
||||
</div>
|
||||
</div>
|
||||
@@ -43,10 +43,10 @@
|
||||
<div v-if="book.serno" class="q-mr-xs">
|
||||
{{ book.serno }}.
|
||||
</div>
|
||||
<div class="clickable2" :class="titleColor" @click="emit('titleClick')">
|
||||
<div class="clickable2" :class="titleColor" @click.stop.prevent="emit('titleClick')">
|
||||
{{ book.title }}
|
||||
</div>
|
||||
<div v-if="mode == 'title' && bookSeries" class="q-ml-xs clickable2" @click="emit('seriesClick')">
|
||||
<div v-if="(mode == 'title' || mode == 'extended') && bookSeries" class="q-ml-xs clickable2" @click.stop.prevent="emit('seriesClick')">
|
||||
{{ bookSeries }}
|
||||
</div>
|
||||
|
||||
@@ -55,19 +55,19 @@
|
||||
{{ bookSize }}, {{ book.ext }}
|
||||
</div>
|
||||
|
||||
<div v-if="showInfo" class="q-ml-sm clickable" @click="emit('bookInfo')">
|
||||
<div v-if="showInfo" class="q-ml-sm clickable" @click.stop.prevent="emit('bookInfo')">
|
||||
(инфо)
|
||||
</div>
|
||||
|
||||
<div class="q-ml-sm clickable" @click="emit('download')">
|
||||
<div class="q-ml-sm clickable" @click.stop.prevent="emit('download')">
|
||||
(скачать)
|
||||
</div>
|
||||
|
||||
<div class="q-ml-sm clickable" @click="emit('copyLink')">
|
||||
<div class="q-ml-sm clickable" @click.stop.prevent="emit('copyLink')">
|
||||
<q-icon name="la la-copy" size="20px" />
|
||||
</div>
|
||||
|
||||
<div v-if="showReadLink" class="q-ml-sm clickable" @click="emit('readBook')">
|
||||
<div v-if="showReadLink" class="q-ml-sm clickable" @click.stop.prevent="emit('readBook')">
|
||||
(читать)
|
||||
</div>
|
||||
|
||||
@@ -79,10 +79,10 @@
|
||||
{{ bookDate }}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div v-show="false">
|
||||
{{ book }}
|
||||
<div v-show="showJson && mode == 'extended'">
|
||||
<pre style="font-size: 80%; white-space: pre-wrap;">{{ book }}</pre>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
@@ -117,6 +117,7 @@ class BookView {
|
||||
showGenres = true;
|
||||
showDeleted = false;
|
||||
showDates = false;
|
||||
showJson = false;
|
||||
|
||||
created() {
|
||||
this.loadSettings();
|
||||
@@ -130,6 +131,7 @@ class BookView {
|
||||
this.showGenres = settings.showGenres;
|
||||
this.showDates = settings.showDates;
|
||||
this.showDeleted = settings.showDeleted;
|
||||
this.showJson = settings.showJson;
|
||||
}
|
||||
|
||||
get settings() {
|
||||
|
||||
128
client/components/Search/ExtendedList/ExtendedList.vue
Normal file
128
client/components/Search/ExtendedList/ExtendedList.vue
Normal file
@@ -0,0 +1,128 @@
|
||||
<template>
|
||||
<div>
|
||||
<a ref="download" style="display: none;"></a>
|
||||
|
||||
<LoadingMessage :message="loadingMessage" z-index="2" />
|
||||
<LoadingMessage :message="loadingMessage2" z-index="1" />
|
||||
|
||||
<!-- Формирование списка ------------------------------------------------------------------------>
|
||||
<div v-for="item in tableData" :key="item.key" class="column" :class="{'odd-item': item.num % 2}" style="font-size: 120%">
|
||||
<BookView
|
||||
class="q-ml-md"
|
||||
:book="item.book" mode="extended" :genre-map="genreMap" :show-read-link="showReadLink" @book-event="bookEvent"
|
||||
/>
|
||||
</div>
|
||||
<!-- Формирование списка конец ------------------------------------------------------------------>
|
||||
|
||||
<div v-if="!refreshing && !tableData.length" class="row items-center q-ml-md" style="font-size: 120%">
|
||||
<q-icon class="la la-meh q-mr-xs" size="28px" />
|
||||
Поиск не дал результатов
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
//-----------------------------------------------------------------------------
|
||||
import vueComponent from '../../vueComponent.js';
|
||||
import { reactive } from 'vue';
|
||||
|
||||
import BaseList from '../BaseList';
|
||||
|
||||
import * as utils from '../../../share/utils';
|
||||
|
||||
import _ from 'lodash';
|
||||
|
||||
class ExtendedList extends BaseList {
|
||||
created() {
|
||||
super.created();
|
||||
this.isExtendedSearch = true;
|
||||
}
|
||||
|
||||
get foundCountMessage() {
|
||||
return `${this.list.totalFound} ссыл${utils.wordEnding(this.list.totalFound, 5)} на файл(ы)`;
|
||||
}
|
||||
|
||||
async updateTableData() {
|
||||
let result = [];
|
||||
|
||||
const books = this.searchResult.found;
|
||||
if (!books)
|
||||
return;
|
||||
|
||||
let num = 0;
|
||||
for (const book of books) {
|
||||
const item = reactive({
|
||||
num: num++,
|
||||
book,
|
||||
});
|
||||
|
||||
result.push(item);
|
||||
}
|
||||
|
||||
this.tableData = result;
|
||||
}
|
||||
|
||||
async refresh() {
|
||||
//параметры запроса
|
||||
const newQuery = this.getQuery();
|
||||
if (_.isEqual(newQuery, this.prevQuery))
|
||||
return;
|
||||
this.prevQuery = newQuery;
|
||||
|
||||
this.queryExecute = newQuery;
|
||||
|
||||
if (this.refreshing)
|
||||
return;
|
||||
|
||||
this.refreshing = true;
|
||||
|
||||
(async() => {
|
||||
await utils.sleep(500);
|
||||
if (this.refreshing)
|
||||
this.loadingMessage = 'Поиск книг...';
|
||||
})();
|
||||
|
||||
try {
|
||||
while (this.queryExecute) {
|
||||
const query = this.queryExecute;
|
||||
this.queryExecute = null;
|
||||
|
||||
try {
|
||||
const response = await this.api.bookSearch(query);
|
||||
|
||||
this.list.queryFound = response.found.length;
|
||||
this.list.totalFound = response.totalFound;
|
||||
this.list.inpxHash = response.inpxHash;
|
||||
|
||||
this.searchResult = response;
|
||||
|
||||
await utils.sleep(1);
|
||||
if (!this.queryExecute) {
|
||||
await this.updateTableData();
|
||||
this.scrollToTop();
|
||||
this.highlightPageScroller(query);
|
||||
}
|
||||
} catch (e) {
|
||||
this.$root.stdDialog.alert(e.message, 'Ошибка');
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
this.refreshing = false;
|
||||
this.loadingMessage = '';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default vueComponent(ExtendedList);
|
||||
//-----------------------------------------------------------------------------
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.clickable2 {
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.odd-item {
|
||||
background-color: #e8e8e8;
|
||||
}
|
||||
</style>
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,205 @@
|
||||
<template>
|
||||
<Dialog ref="dialog" v-model="dialogVisible">
|
||||
<template #header>
|
||||
<div class="row items-center">
|
||||
<div style="font-size: 110%">
|
||||
Расширенный поиск
|
||||
</div>
|
||||
|
||||
<DivBtn class="q-ml-sm text-grey-5 bg-yellow-1" :size="28" :icon-size="24" icon="la la-question" round @click.stop.prevent="showSearchHelp">
|
||||
<template #tooltip>
|
||||
<q-tooltip :delay="1500" anchor="bottom middle" content-style="font-size: 80%" max-width="400px">
|
||||
Памятка
|
||||
</q-tooltip>
|
||||
</template>
|
||||
</DivBtn>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<div ref="box" class="column q-mt-xs overflow-auto" style="max-width: 660px; padding: 0px 10px 10px 10px;">
|
||||
<div class="row">
|
||||
<div v-for="f in recStruct" :key="f.field" class="row">
|
||||
<div class="q-mx-xs" />
|
||||
<q-input
|
||||
v-model="search[f.field]" :maxlength="5000"
|
||||
class="q-mt-xs" style="width: 150px;" :label="`(${f.type}) ${f.field}`"
|
||||
:bg-color="bgColor[f.field] || 'white'"
|
||||
stack-label outlined dense clearable
|
||||
@keydown="onKeyDown"
|
||||
>
|
||||
<q-tooltip v-if="search[f.field]" :delay="500" anchor="bottom middle" content-style="font-size: 80%" max-width="400px">
|
||||
{{ search[f.field] }}
|
||||
</q-tooltip>
|
||||
</q-input>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row q-mt-xs q-ml-sm" style="color: red" v-html="error" />
|
||||
</div>
|
||||
|
||||
<template #footer>
|
||||
<q-btn class="q-px-md q-ml-sm" color="primary" dense no-caps :disabled="error !== ''" @click="apply">
|
||||
Применить
|
||||
</q-btn>
|
||||
</template>
|
||||
</Dialog>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
//-----------------------------------------------------------------------------
|
||||
import vueComponent from '../../vueComponent.js';
|
||||
|
||||
import Dialog from '../../share/Dialog.vue';
|
||||
import DivBtn from '../../share/DivBtn.vue';
|
||||
|
||||
import _ from 'lodash';
|
||||
|
||||
const componentOptions = {
|
||||
components: {
|
||||
Dialog,
|
||||
DivBtn,
|
||||
},
|
||||
watch: {
|
||||
modelValue(newValue) {
|
||||
this.dialogVisible = newValue;
|
||||
},
|
||||
dialogVisible(newValue) {
|
||||
this.$emit('update:modelValue', newValue);
|
||||
},
|
||||
extSearch: {
|
||||
handler(newValue) {
|
||||
this.search = _.cloneDeep(newValue);
|
||||
},
|
||||
deep: true,
|
||||
},
|
||||
search: {
|
||||
handler() {
|
||||
this.validate();
|
||||
},
|
||||
deep: true,
|
||||
},
|
||||
}
|
||||
};
|
||||
class SelectExtSearchDialog {
|
||||
_options = componentOptions;
|
||||
_props = {
|
||||
modelValue: Boolean,
|
||||
extSearch: Object,
|
||||
};
|
||||
|
||||
dialogVisible = false;
|
||||
search = {};
|
||||
bgColor = {};
|
||||
error = '';
|
||||
|
||||
created() {
|
||||
this.commit = this.$store.commit;
|
||||
}
|
||||
|
||||
mounted() {
|
||||
}
|
||||
|
||||
get config() {
|
||||
return this.$store.state.config;
|
||||
}
|
||||
|
||||
get recStruct() {
|
||||
if (this.config.dbConfig && this.config.dbConfig.inpxInfo.recStruct)
|
||||
return this.config.dbConfig.inpxInfo.recStruct;
|
||||
else
|
||||
return [];
|
||||
}
|
||||
|
||||
validate() {
|
||||
const validNumValue = (n) => {
|
||||
const validChars = new Set('0123456789.'.split(''));
|
||||
for (const c of n.split(''))
|
||||
if (!validChars.has(c))
|
||||
return false;
|
||||
|
||||
const v = n.split('..');
|
||||
if ( isNaN(parseInt(v[0] || '0', 10)) || isNaN(parseInt(v[1] || '0', 10)) )
|
||||
return false;
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
let error = [];
|
||||
const s = this.search;
|
||||
for (const f of this.recStruct) {
|
||||
if (f.type == 'N' && s[f.field] && !validNumValue(s[f.field])) {
|
||||
error.push(`Недопустимое значение поля ${f.field}`);
|
||||
this.bgColor[f.field] = 'red-2';
|
||||
} else {
|
||||
this.bgColor[f.field] = '';//default
|
||||
}
|
||||
}
|
||||
|
||||
this.error = error.join('<br>');
|
||||
}
|
||||
|
||||
showSearchHelp() {
|
||||
let info = `<div style="min-width: 250px" />`;
|
||||
info += `
|
||||
<p>
|
||||
Расширенный поиск ведется непосредственно по значениям атрибутов записей описания книг.
|
||||
Атрибуты можно увидеть, если включить опцию "Показывать JSON".
|
||||
Названия атрибутов (кроме "_uid" и "id") соответствуют названиям полей струкутры записей из inpx-файла.
|
||||
На поисковые значения действуют те же правила, что и для разделов "Авторы", "Серии", "Книги".
|
||||
<br>
|
||||
Для строковых значений (S):
|
||||
<ul>
|
||||
<li>
|
||||
без префикса: значение трактуется, как "начинается с"
|
||||
</li>
|
||||
<li>
|
||||
префикс "=": поиск по точному совпадению
|
||||
</li>
|
||||
<li>
|
||||
префикс "*": поиск подстроки в строке
|
||||
</li>
|
||||
<li>
|
||||
префикс "#": поиск подстроки в строке, но только среди начинающихся не с латинского или кириллического символа
|
||||
</li>
|
||||
<li>
|
||||
префикс "~": поиск по регулярному выражению
|
||||
</li>
|
||||
<li>
|
||||
префикс "?": поиск пустых значений или тех, что начинаются с этого символа
|
||||
</li>
|
||||
</ul>
|
||||
Для числовых значений (N):
|
||||
<ul>
|
||||
<li>
|
||||
число N: поиск по точному совпадению
|
||||
</li>
|
||||
<li>
|
||||
диапазон N..M: поиск по диапазону числовых значений, включая N и M. Например, поисковое значение 1024..2048 в поле "size"
|
||||
найдет все ссылки на файлы размером от 1КБ до 2КБ.
|
||||
</li>
|
||||
</ul>
|
||||
</p>
|
||||
`;
|
||||
|
||||
this.$root.stdDialog.alert(info, 'Памятка', {iconName: 'la la-info-circle'});
|
||||
}
|
||||
|
||||
onKeyDown(event) {
|
||||
if (event.code == 'Enter')
|
||||
this.apply();
|
||||
}
|
||||
|
||||
apply() {
|
||||
this.validate();
|
||||
if (!this.error) {
|
||||
this.$emit('update:extSearch', _.cloneDeep(this.search));
|
||||
this.dialogVisible = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default vueComponent(SelectExtSearchDialog);
|
||||
//-----------------------------------------------------------------------------
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
</style>
|
||||
157
client/components/Search/SettingsDialog/SettingsDialog.vue
Normal file
157
client/components/Search/SettingsDialog/SettingsDialog.vue
Normal file
@@ -0,0 +1,157 @@
|
||||
<template>
|
||||
<Dialog ref="dialog" v-model="dialogVisible">
|
||||
<template #header>
|
||||
<div class="row items-center" style="font-size: 110%">
|
||||
<q-icon class="q-mr-sm text-green" name="la la-cog" size="28px"></q-icon>
|
||||
Настройки
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<div class="q-mx-md column" style="min-width: 300px; font-size: 120%;">
|
||||
<div class="row items-center q-ml-sm">
|
||||
<div class="q-mr-sm">
|
||||
Результатов на странице
|
||||
</div>
|
||||
<q-select
|
||||
v-model="limit" :options="limitOptions" class="bg-white"
|
||||
dropdown-icon="la la-angle-down la-sm"
|
||||
outlined dense emit-value map-options
|
||||
/>
|
||||
</div>
|
||||
|
||||
<q-checkbox v-model="downloadAsZip" size="36px" label="Скачивать книги в виде zip-архива" />
|
||||
<q-checkbox v-model="showCounts" size="36px" label="Показывать количество" />
|
||||
<q-checkbox v-model="showRates" size="36px" label="Показывать оценки" />
|
||||
<q-checkbox v-model="showInfo" size="36px" label="Показывать кнопку (инфо)" />
|
||||
<q-checkbox v-model="showGenres" size="36px" label="Показывать жанры" />
|
||||
<q-checkbox v-model="showDates" size="36px" label="Показывать даты поступления" />
|
||||
<q-checkbox v-model="showDeleted" size="36px" label="Показывать удаленные" />
|
||||
<q-checkbox v-model="abCacheEnabled" size="36px" label="Кешировать запросы" />
|
||||
</div>
|
||||
|
||||
<template #footer>
|
||||
<q-btn class="q-px-md q-ml-sm" color="primary" dense no-caps @click="okClick">
|
||||
OK
|
||||
</q-btn>
|
||||
</template>
|
||||
</Dialog>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
//-----------------------------------------------------------------------------
|
||||
import vueComponent from '../../vueComponent.js';
|
||||
|
||||
import Dialog from '../../share/Dialog.vue';
|
||||
|
||||
const componentOptions = {
|
||||
components: {
|
||||
Dialog
|
||||
},
|
||||
watch: {
|
||||
modelValue(newValue) {
|
||||
this.dialogVisible = newValue;
|
||||
},
|
||||
dialogVisible(newValue) {
|
||||
this.$emit('update:modelValue', newValue);
|
||||
},
|
||||
|
||||
settings() {
|
||||
this.loadSettings();
|
||||
},
|
||||
limit(newValue) {
|
||||
this.commit('setSettings', {'limit': newValue});
|
||||
},
|
||||
downloadAsZip(newValue) {
|
||||
this.commit('setSettings', {'downloadAsZip': newValue});
|
||||
},
|
||||
showCounts(newValue) {
|
||||
this.commit('setSettings', {'showCounts': newValue});
|
||||
},
|
||||
showRates(newValue) {
|
||||
this.commit('setSettings', {'showRates': newValue});
|
||||
},
|
||||
showInfo(newValue) {
|
||||
this.commit('setSettings', {'showInfo': newValue});
|
||||
},
|
||||
showGenres(newValue) {
|
||||
this.commit('setSettings', {'showGenres': newValue});
|
||||
},
|
||||
showDates(newValue) {
|
||||
this.commit('setSettings', {'showDates': newValue});
|
||||
},
|
||||
showDeleted(newValue) {
|
||||
this.commit('setSettings', {'showDeleted': newValue});
|
||||
},
|
||||
abCacheEnabled(newValue) {
|
||||
this.commit('setSettings', {'abCacheEnabled': newValue});
|
||||
},
|
||||
}
|
||||
};
|
||||
class SettingsDialog {
|
||||
_options = componentOptions;
|
||||
_props = {
|
||||
modelValue: Boolean,
|
||||
};
|
||||
|
||||
dialogVisible = false;
|
||||
|
||||
//settings
|
||||
limit = 20;
|
||||
downloadAsZip = false;
|
||||
showCounts = true;
|
||||
showRates = true;
|
||||
showInfo = true;
|
||||
showGenres = true;
|
||||
showDates = true;
|
||||
showDeleted = false;
|
||||
abCacheEnabled = true;
|
||||
|
||||
limitOptions = [
|
||||
{label: '10', value: 10},
|
||||
{label: '20', value: 20},
|
||||
{label: '50', value: 50},
|
||||
{label: '100', value: 100},
|
||||
{label: '200', value: 200},
|
||||
{label: '500', value: 500},
|
||||
{label: '1000', value: 1000},
|
||||
];
|
||||
|
||||
created() {
|
||||
this.commit = this.$store.commit;
|
||||
|
||||
this.loadSettings();
|
||||
}
|
||||
|
||||
mounted() {
|
||||
}
|
||||
|
||||
get settings() {
|
||||
return this.$store.state.settings;
|
||||
}
|
||||
|
||||
loadSettings() {
|
||||
const settings = this.settings;
|
||||
|
||||
this.limit = settings.limit;
|
||||
|
||||
this.downloadAsZip = settings.downloadAsZip;
|
||||
this.showCounts = settings.showCounts;
|
||||
this.showRates = settings.showRates;
|
||||
this.showInfo = settings.showInfo;
|
||||
this.showGenres = settings.showGenres;
|
||||
this.showDates = settings.showDates;
|
||||
this.showDeleted = settings.showDeleted;
|
||||
this.abCacheEnabled = settings.abCacheEnabled;
|
||||
}
|
||||
|
||||
okClick() {
|
||||
this.dialogVisible = false;
|
||||
}
|
||||
}
|
||||
|
||||
export default vueComponent(SettingsDialog);
|
||||
//-----------------------------------------------------------------------------
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
</style>
|
||||
@@ -8,6 +8,8 @@ const abStore = localForage.createInstance({
|
||||
name: 'authorBooksStorage'
|
||||
});
|
||||
|
||||
const storageVersion = '1';
|
||||
|
||||
class AuthorBooksStorage {
|
||||
constructor() {
|
||||
}
|
||||
@@ -17,6 +19,8 @@ class AuthorBooksStorage {
|
||||
}
|
||||
|
||||
async setData(key, data) {
|
||||
key += storageVersion;
|
||||
|
||||
if (typeof data !== 'string')
|
||||
throw new Error('AuthorBooksStorage: data must be a string');
|
||||
|
||||
@@ -25,6 +29,8 @@ class AuthorBooksStorage {
|
||||
}
|
||||
|
||||
async getData(key) {
|
||||
key += storageVersion;
|
||||
|
||||
const item = await abStore.getItem(key);
|
||||
|
||||
//обновим addTime
|
||||
@@ -34,9 +40,9 @@ class AuthorBooksStorage {
|
||||
return item;
|
||||
}
|
||||
|
||||
async removeData(key) {
|
||||
await abStore.removeItem(key);
|
||||
await abStore.removeItem(`addTime-${key}`);
|
||||
async _removeData(fullKey) {
|
||||
await abStore.removeItem(fullKey);
|
||||
await abStore.removeItem(`addTime-${fullKey}`);
|
||||
}
|
||||
|
||||
async cleanStorage() {
|
||||
@@ -62,7 +68,7 @@ class AuthorBooksStorage {
|
||||
}
|
||||
|
||||
if (size > maxDataSize && toDel) {
|
||||
await this.removeData(toDel);
|
||||
await this._removeData(toDel);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<template>
|
||||
<div ref="btn" class="button clickable row justify-center items-center" @click="clickEffect">
|
||||
<div ref="btn" class="button clickable row justify-center items-center" :class="{disabled}" @click.stop.prevent="clickEffect">
|
||||
<div class="row justify-center items-center no-wrap" :class="{'button-pressed': pressed}">
|
||||
<i :class="icon" :style="`font-size: ${iconSize}px; margin-top: ${imt}px`" />
|
||||
<slot></slot>
|
||||
@@ -29,8 +29,10 @@ class DivBtn {
|
||||
height: { type: Number, default: 0 },
|
||||
icon: { type: String, default: '' },
|
||||
iconSize: { type: Number, default: 14 },
|
||||
round: { type: Boolean },
|
||||
round: Boolean,
|
||||
imt: { type: Number, default: 0 },// icon margin top
|
||||
disabled: Boolean,
|
||||
noShadow: Boolean,
|
||||
};
|
||||
|
||||
pressed = false;
|
||||
@@ -55,9 +57,17 @@ class DivBtn {
|
||||
style.borderRadius = `${this.size}px`;
|
||||
else
|
||||
style.borderRadius = `${this.size/10}px`;
|
||||
|
||||
if (!this.noShadow)
|
||||
style.boxShadow = '0.5px 1px 3px #333333';
|
||||
}
|
||||
|
||||
async clickEffect() {
|
||||
async clickEffect(event) {
|
||||
if (this.disabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.$emit('meClick', event);
|
||||
this.pressed = true;
|
||||
await utils.sleep(100);
|
||||
this.pressed = false;
|
||||
@@ -71,7 +81,6 @@ export default vueComponent(DivBtn);
|
||||
<style scoped>
|
||||
.button {
|
||||
position: relative;
|
||||
box-shadow: 0.5px 1px 3px #333333;
|
||||
}
|
||||
|
||||
.button:hover {
|
||||
|
||||
@@ -74,7 +74,8 @@ const componentOptions = {
|
||||
this.checkErrorAndEmit(true);
|
||||
},
|
||||
modelValue(newValue) {
|
||||
this.filteredValue = newValue;
|
||||
if (this.ready)//исправление бага TypeError: Cannot read properties of null (reading 'emitsOptions')
|
||||
this.filteredValue = newValue;
|
||||
},
|
||||
min() {
|
||||
this.checkErrorAndEmit();
|
||||
@@ -102,7 +103,8 @@ class NumInput {
|
||||
filteredValue = 0;
|
||||
error = false;
|
||||
|
||||
created() {
|
||||
mounted() {
|
||||
this.ready = true;
|
||||
this.filteredValue = this.modelValue;
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ const myRoutes = [
|
||||
['/author', Search],
|
||||
['/series', Search],
|
||||
['/title', Search],
|
||||
['/extended', Search],
|
||||
['/:pathMatch(.*)*', null, null, '/'],
|
||||
];
|
||||
|
||||
|
||||
@@ -36,14 +36,14 @@ export function keyEventToCode(event) {
|
||||
|
||||
export function wordEnding(num, type = 0) {
|
||||
const endings = [
|
||||
['ов', '', 'а', 'а', 'а', 'ов', 'ов', 'ов', 'ов', 'ов'],
|
||||
['й', 'я', 'и', 'и', 'и', 'й', 'й', 'й', 'й', 'й'],
|
||||
['о', '', 'о', 'о', 'о', 'о', 'о', 'о', 'о', 'о'],
|
||||
['ий', 'ие', 'ия', 'ия', 'ия', 'ий', 'ий', 'ий', 'ий', 'ий'],
|
||||
['о', 'а', 'о', 'о', 'о', 'о', 'о', 'о', 'о', 'о'],
|
||||
['ок', 'ка', 'ки', 'ки', 'ки', 'ок', 'ок', 'ок', 'ок', 'ок'],
|
||||
['ых', 'ое', 'ых', 'ых', 'ых', 'ых', 'ых', 'ых', 'ых', 'ых'],
|
||||
['о', 'о', 'о', 'о', 'о', 'о', 'о', 'о', 'о', 'о'],
|
||||
['ов', '', 'а', 'а', 'а', 'ов', 'ов', 'ов', 'ов', 'ов'],//0
|
||||
['й', 'я', 'и', 'и', 'и', 'й', 'й', 'й', 'й', 'й'],//1
|
||||
['о', '', 'о', 'о', 'о', 'о', 'о', 'о', 'о', 'о'],//2
|
||||
['ий', 'ие', 'ия', 'ия', 'ия', 'ий', 'ий', 'ий', 'ий', 'ий'],//3
|
||||
['о', 'а', 'о', 'о', 'о', 'о', 'о', 'о', 'о', 'о'],//4
|
||||
['ок', 'ка', 'ки', 'ки', 'ки', 'ок', 'ок', 'ок', 'ок', 'ок'],//5
|
||||
['ых', 'ое', 'ых', 'ых', 'ых', 'ых', 'ых', 'ых', 'ых', 'ых'],//6
|
||||
['о', 'о', 'о', 'о', 'о', 'о', 'о', 'о', 'о', 'о'],//7
|
||||
];
|
||||
const deci = num % 100;
|
||||
if (deci > 10 && deci < 20) {
|
||||
|
||||
@@ -4,9 +4,14 @@ const state = {
|
||||
settings: {
|
||||
accessToken: '',
|
||||
extendedParams: false,
|
||||
limit: 20,
|
||||
expandedAuthor: [],
|
||||
expandedSeries: [],
|
||||
|
||||
defaultsSet: false,
|
||||
|
||||
//uiDefaults
|
||||
limit: 20,
|
||||
downloadAsZip: false,
|
||||
showCounts: true,
|
||||
showRates: true,
|
||||
showInfo: true,
|
||||
@@ -15,6 +20,7 @@ const state = {
|
||||
showDeleted: false,
|
||||
abCacheEnabled: true,
|
||||
langDefault: '',
|
||||
showJson: false,
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
3895
package-lock.json
generated
3895
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
62
package.json
62
package.json
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "inpx-web",
|
||||
"version": "1.2.1",
|
||||
"version": "1.5.2",
|
||||
"author": "Book Pauk <bookpauk@gmail.com>",
|
||||
"license": "CC0-1.0",
|
||||
"repository": "bookpauk/inpx-web",
|
||||
@@ -11,9 +11,11 @@
|
||||
"dev": "nodemon --inspect --ignore server/.inpx-web --ignore client --exec 'node --expose-gc server --lib-dir=server/.inpx-web/lib'",
|
||||
"build:client": "webpack --config build/webpack.prod.config.js",
|
||||
"build:linux": "npm run build:client && node build/prepkg.js linux && pkg -t node16-linux-x64 -C GZip --options max-old-space-size=4096,expose-gc -o dist/linux/inpx-web .",
|
||||
"build:linux-arm64": "npm run build:client && node build/prepkg.js linux-arm64 && pkg -t node16-linuxstatic-arm64 -C GZip --options max-old-space-size=4096,expose-gc -o dist/linux-arm64/inpx-web .",
|
||||
"build:win": "npm run build:client && node build/prepkg.js win && pkg -t node16-win-x64 -C GZip --options max-old-space-size=4096,expose-gc -o dist/win/inpx-web .",
|
||||
"build:macos": "npm run build:client && node build/prepkg.js macos && pkg -t node16-macos-x64 -C GZip --options max-old-space-size=4096,expose-gc -o dist/macos/inpx-web .",
|
||||
"build:client-dev": "webpack --config build/webpack.dev.config.js",
|
||||
"build:all": "npm run build:linux && npm run build:win",
|
||||
"build:all": "npm run build:linux && npm run build:win && npm run build:macos && npm run build:linux-arm64",
|
||||
"release": "npm run build:all && node build/release.js",
|
||||
"postinstall": "npm run build:client-dev"
|
||||
},
|
||||
@@ -23,50 +25,54 @@
|
||||
"assets": "dist/public.json"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.18.9",
|
||||
"@babel/eslint-parser": "^7.18.9",
|
||||
"@babel/eslint-plugin": "^7.17.7",
|
||||
"@babel/plugin-proposal-decorators": "^7.18.9",
|
||||
"@babel/preset-env": "^7.18.9",
|
||||
"@babel/core": "^7.20.5",
|
||||
"@babel/eslint-parser": "^7.19.1",
|
||||
"@babel/eslint-plugin": "^7.19.1",
|
||||
"@babel/plugin-proposal-decorators": "^7.20.5",
|
||||
"@babel/preset-env": "^7.20.2",
|
||||
"@vue/compiler-sfc": "^3.2.22",
|
||||
"babel-loader": "^8.2.5",
|
||||
"babel-loader": "^9.1.0",
|
||||
"copy-webpack-plugin": "^11.0.0",
|
||||
"css-loader": "^6.7.1",
|
||||
"css-minimizer-webpack-plugin": "^4.0.0",
|
||||
"eslint": "^8.20.0",
|
||||
"eslint-plugin-vue": "^9.3.0",
|
||||
"css-loader": "^6.7.2",
|
||||
"css-minimizer-webpack-plugin": "^4.2.2",
|
||||
"eslint": "^8.28.0",
|
||||
"eslint-plugin-vue": "^9.8.0",
|
||||
"html-webpack-plugin": "^5.5.0",
|
||||
"mini-css-extract-plugin": "^2.6.1",
|
||||
"mini-css-extract-plugin": "^2.7.1",
|
||||
"pkg": "^5.8.0",
|
||||
"terser-webpack-plugin": "^5.3.3",
|
||||
"vue-eslint-parser": "^9.0.3",
|
||||
"vue-loader": "^17.0.0",
|
||||
"showdown": "^2.1.0",
|
||||
"terser-webpack-plugin": "^5.3.6",
|
||||
"vue-eslint-parser": "^9.1.0",
|
||||
"vue-loader": "^17.0.1",
|
||||
"vue-style-loader": "^4.1.3",
|
||||
"webpack": "^5.74.0",
|
||||
"webpack-cli": "^4.10.0",
|
||||
"webpack-dev-middleware": "^5.3.3",
|
||||
"webpack-hot-middleware": "^2.25.1",
|
||||
"webpack": "^5.75.0",
|
||||
"webpack-cli": "^5.0.0",
|
||||
"webpack-dev-middleware": "^6.0.1",
|
||||
"webpack-hot-middleware": "^2.25.3",
|
||||
"webpack-merge": "^5.8.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"@quasar/extras": "^1.15.0",
|
||||
"@quasar/extras": "^1.15.6",
|
||||
"axios": "^0.27.2",
|
||||
"chardet": "^1.5.0",
|
||||
"dayjs": "^1.11.6",
|
||||
"express": "^4.18.1",
|
||||
"express": "^4.18.2",
|
||||
"express-basic-auth": "^1.2.1",
|
||||
"fs-extra": "^10.1.0",
|
||||
"he": "^1.2.0",
|
||||
"iconv-lite": "^0.6.3",
|
||||
"jembadb": "^5.0.2",
|
||||
"jembadb": "^5.1.7",
|
||||
"localforage": "^1.10.0",
|
||||
"lodash": "^4.17.21",
|
||||
"minimist": "^1.2.6",
|
||||
"minimist": "^1.2.7",
|
||||
"node-stream-zip": "^1.15.0",
|
||||
"quasar": "^2.7.5",
|
||||
"quasar": "^2.10.2",
|
||||
"safe-buffer": "^5.2.1",
|
||||
"vue": "^3.2.37",
|
||||
"vue-router": "^4.1.2",
|
||||
"vuex": "^4.0.2",
|
||||
"vue-router": "^4.1.6",
|
||||
"vuex": "^4.1.0",
|
||||
"vuex-persist": "^3.1.3",
|
||||
"ws": "^8.8.1"
|
||||
"ws": "^8.11.0",
|
||||
"yazl": "^2.5.1"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,25 +9,31 @@ module.exports = {
|
||||
name: pckg.name,
|
||||
|
||||
execDir,
|
||||
libDir: '',
|
||||
inpx: '',
|
||||
|
||||
accessPassword: '',
|
||||
accessTimeout: 0,
|
||||
extendedSearch: true,
|
||||
bookReadLink: '',
|
||||
loggingEnabled: true,
|
||||
|
||||
//поправить в случае, если были критические изменения в DbCreator или InpxParser
|
||||
//иначе будет рассинхронизация между сервером и клиентом на уровне БД
|
||||
dbVersion: '6',
|
||||
dbVersion: '11',
|
||||
dbCacheSize: 5,
|
||||
|
||||
maxPayloadSize: 500,//in MB
|
||||
maxFilesDirSize: 1024*1024*1024,//1Gb
|
||||
queryCacheEnabled: true,
|
||||
queryCacheMemSize: 50,
|
||||
queryCacheDiskSize: 500,
|
||||
cacheCleanInterval: 60,//minutes
|
||||
inpxCheckInterval: 60,//minutes
|
||||
lowMemoryMode: false,
|
||||
fullOptimization: false,
|
||||
|
||||
webConfigParams: ['name', 'version', 'branch', 'bookReadLink', 'dbVersion'],
|
||||
webConfigParams: ['name', 'version', 'branch', 'bookReadLink', 'dbVersion', 'extendedSearch', 'uiDefaults'],
|
||||
|
||||
allowRemoteLib: false,
|
||||
remoteLib: false,
|
||||
@@ -42,6 +48,27 @@ module.exports = {
|
||||
server: {
|
||||
host: '0.0.0.0',
|
||||
port: '22380',
|
||||
root: '',
|
||||
},
|
||||
//opds: false,
|
||||
opds: {
|
||||
enabled: true,
|
||||
user: '',
|
||||
password: '',
|
||||
root: '/opds',
|
||||
},
|
||||
uiDefaults: {
|
||||
limit: 20,
|
||||
downloadAsZip: false,
|
||||
showCounts: true,
|
||||
showRates: true,
|
||||
showInfo: true,
|
||||
showGenres: true,
|
||||
showDates: false,
|
||||
showDeleted: false,
|
||||
abCacheEnabled: true,
|
||||
langDefault: '',
|
||||
showJson: false,
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@@ -5,12 +5,18 @@ const fs = require('fs-extra');
|
||||
const branchFilename = __dirname + '/application_env';
|
||||
|
||||
const propsToSave = [
|
||||
'libDir',
|
||||
'inpx',
|
||||
'accessPassword',
|
||||
'accessTimeout',
|
||||
'extendedSearch',
|
||||
'bookReadLink',
|
||||
'loggingEnabled',
|
||||
'dbCacheSize',
|
||||
'maxFilesDirSize',
|
||||
'queryCacheEnabled',
|
||||
'queryCacheMemSize',
|
||||
'queryCacheDiskSize',
|
||||
'cacheCleanInterval',
|
||||
'inpxCheckInterval',
|
||||
'lowMemoryMode',
|
||||
@@ -18,6 +24,8 @@ const propsToSave = [
|
||||
'allowRemoteLib',
|
||||
'remoteLib',
|
||||
'server',
|
||||
'opds',
|
||||
'uiDefaults',
|
||||
];
|
||||
|
||||
let instance = null;
|
||||
|
||||
@@ -11,6 +11,7 @@ module.exports = Object.assign({}, base, {
|
||||
server: {
|
||||
host: '0.0.0.0',
|
||||
port: '12380',
|
||||
root: '',
|
||||
},
|
||||
|
||||
});
|
||||
|
||||
@@ -10,12 +10,11 @@ const cleanPeriod = 1*60*1000;//1 минута
|
||||
const closeSocketOnIdle = 5*60*1000;//5 минут
|
||||
|
||||
class WebSocketController {
|
||||
constructor(wss, config) {
|
||||
constructor(wss, webAccess, config) {
|
||||
this.config = config;
|
||||
this.isDevelopment = (config.branch == 'development');
|
||||
this.accessToken = '';
|
||||
if (config.accessPassword)
|
||||
this.accessToken = utils.getBufHash(config.accessPassword, 'sha256', 'hex');
|
||||
|
||||
this.webAccess = webAccess;
|
||||
|
||||
this.workerState = new WorkerState();
|
||||
this.webWorker = new WebWorker(config);
|
||||
@@ -32,19 +31,25 @@ class WebSocketController {
|
||||
});
|
||||
});
|
||||
|
||||
setTimeout(() => { this.periodicClean(); }, cleanPeriod);
|
||||
this.periodicClean();//no await
|
||||
}
|
||||
|
||||
periodicClean() {
|
||||
try {
|
||||
const now = Date.now();
|
||||
this.wss.clients.forEach((ws) => {
|
||||
if (!ws.lastActivity || now - ws.lastActivity > closeSocketOnIdle - 50) {
|
||||
ws.terminate();
|
||||
}
|
||||
});
|
||||
} finally {
|
||||
setTimeout(() => { this.periodicClean(); }, cleanPeriod);
|
||||
async periodicClean() {
|
||||
while (1) {//eslint-disable-line no-constant-condition
|
||||
try {
|
||||
const now = Date.now();
|
||||
|
||||
//почистим ws-клиентов
|
||||
this.wss.clients.forEach((ws) => {
|
||||
if (!ws.lastActivity || now - ws.lastActivity > closeSocketOnIdle - 50) {
|
||||
ws.terminate();
|
||||
}
|
||||
});
|
||||
} catch(e) {
|
||||
log(LM_ERR, `WebSocketController.periodicClean error: ${e.message}`);
|
||||
}
|
||||
|
||||
await utils.sleep(cleanPeriod);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,22 +67,32 @@ class WebSocketController {
|
||||
//pong for WebSocketConnection
|
||||
this.send({_rok: 1}, req, ws);
|
||||
|
||||
if (this.accessToken && req.accessToken !== this.accessToken) {
|
||||
await utils.sleep(1000);
|
||||
throw new Error('need_access_token');
|
||||
//access
|
||||
if (!await this.webAccess.hasAccess(req.accessToken)) {
|
||||
await utils.sleep(500);
|
||||
const salt = this.webAccess.newToken();
|
||||
this.send({error: 'need_access_token', salt}, req, ws);
|
||||
return;
|
||||
}
|
||||
|
||||
//api
|
||||
switch (req.action) {
|
||||
case 'test':
|
||||
await this.test(req, ws); break;
|
||||
case 'logout':
|
||||
await this.logout(req, ws); break;
|
||||
case 'get-config':
|
||||
await this.getConfig(req, ws); break;
|
||||
case 'get-worker-state':
|
||||
await this.getWorkerState(req, ws); break;
|
||||
case 'search':
|
||||
await this.search(req, ws); break;
|
||||
case 'bookSearch':
|
||||
await this.bookSearch(req, ws); break;
|
||||
case 'get-author-book-list':
|
||||
await this.getAuthorBookList(req, ws); break;
|
||||
case 'get-author-series-list':
|
||||
await this.getAuthorSeriesList(req, ws); break;
|
||||
case 'get-series-book-list':
|
||||
await this.getSeriesBookList(req, ws); break;
|
||||
case 'get-genre-tree':
|
||||
@@ -120,9 +135,15 @@ class WebSocketController {
|
||||
this.send({message: `${this.config.name} project is awesome`}, req, ws);
|
||||
}
|
||||
|
||||
async logout(req, ws) {
|
||||
await this.webAccess.deleteAccess(req.accessToken);
|
||||
this.send({success: true}, req, ws);
|
||||
}
|
||||
|
||||
async getConfig(req, ws) {
|
||||
const config = _.pick(this.config, this.config.webConfigParams);
|
||||
config.dbConfig = await this.webWorker.dbConfig();
|
||||
config.freeAccess = this.webAccess.freeAccess;
|
||||
|
||||
this.send(config, req, ws);
|
||||
}
|
||||
@@ -146,12 +167,29 @@ class WebSocketController {
|
||||
this.send(result, req, ws);
|
||||
}
|
||||
|
||||
async bookSearch(req, ws) {
|
||||
if (!this.config.extendedSearch)
|
||||
throw new Error('config.extendedSearch disabled');
|
||||
if (!req.query)
|
||||
throw new Error(`query is empty`);
|
||||
|
||||
const result = await this.webWorker.bookSearch(req.query);
|
||||
|
||||
this.send(result, req, ws);
|
||||
}
|
||||
|
||||
async getAuthorBookList(req, ws) {
|
||||
const result = await this.webWorker.getAuthorBookList(req.authorId);
|
||||
|
||||
this.send(result, req, ws);
|
||||
}
|
||||
|
||||
async getAuthorSeriesList(req, ws) {
|
||||
const result = await this.webWorker.getAuthorSeriesList(req.authorId);
|
||||
|
||||
this.send(result, req, ws);
|
||||
}
|
||||
|
||||
async getSeriesBookList(req, ws) {
|
||||
const result = await this.webWorker.getSeriesBookList(req.series);
|
||||
|
||||
@@ -165,19 +203,19 @@ class WebSocketController {
|
||||
}
|
||||
|
||||
async getBookLink(req, ws) {
|
||||
if (!utils.hasProp(req, 'bookId'))
|
||||
throw new Error(`bookId is empty`);
|
||||
if (!utils.hasProp(req, 'bookUid'))
|
||||
throw new Error(`bookUid is empty`);
|
||||
|
||||
const result = await this.webWorker.getBookLink(req.bookId);
|
||||
const result = await this.webWorker.getBookLink(req.bookUid);
|
||||
|
||||
this.send(result, req, ws);
|
||||
}
|
||||
|
||||
async getBookInfo(req, ws) {
|
||||
if (!utils.hasProp(req, 'bookId'))
|
||||
throw new Error(`bookId is empty`);
|
||||
if (!utils.hasProp(req, 'bookUid'))
|
||||
throw new Error(`bookUid is empty`);
|
||||
|
||||
const result = await this.webWorker.getBookInfo(req.bookId);
|
||||
const result = await this.webWorker.getBookInfo(req.bookUid);
|
||||
|
||||
this.send(result, req, ws);
|
||||
}
|
||||
|
||||
@@ -65,6 +65,8 @@ class DbCreator {
|
||||
let librateMap = new Map();//оценка
|
||||
let librateArr = [];
|
||||
|
||||
let uidSet = new Set();//уникальные идентификаторы
|
||||
|
||||
//stats
|
||||
let authorCount = 0;
|
||||
let bookCount = 0;
|
||||
@@ -221,13 +223,14 @@ class DbCreator {
|
||||
let filtered = false;
|
||||
for (const rec of chunk) {
|
||||
//сначала фильтр
|
||||
if (!filter(rec)) {
|
||||
if (!filter(rec) || uidSet.has(rec._uid)) {
|
||||
rec.id = 0;
|
||||
filtered = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
rec.id = ++id;
|
||||
uidSet.add(rec._uid);
|
||||
|
||||
if (!rec.del) {
|
||||
bookCount++;
|
||||
@@ -258,7 +261,7 @@ class DbCreator {
|
||||
|
||||
//парсинг
|
||||
const parser = new InpxParser();
|
||||
await parser.parse(config.inpxFile, readFileCallback, parsedCallback);
|
||||
await parser.parse(config.inpxFile, readFileCallback, parsedCallback);
|
||||
|
||||
//чистка памяти, ибо жрет как не в себя
|
||||
authorMap = null;
|
||||
@@ -269,6 +272,7 @@ class DbCreator {
|
||||
delMap = null;
|
||||
dateMap = null;
|
||||
librateMap = null;
|
||||
uidSet = null;
|
||||
|
||||
await db.close({table: 'book'});
|
||||
await db.freeMemory();
|
||||
@@ -333,7 +337,7 @@ class DbCreator {
|
||||
//сохраним поисковые таблицы
|
||||
const chunkSize = 10000;
|
||||
|
||||
const saveTable = async(table, arr, nullArr, indexType = 'string') => {
|
||||
const saveTable = async(table, arr, nullArr, indexType = 'string', delEmpty = false) => {
|
||||
|
||||
if (indexType == 'string')
|
||||
arr.sort((a, b) => a.value.localeCompare(b.value));
|
||||
@@ -362,6 +366,13 @@ class DbCreator {
|
||||
callback({progress: i/arr.length});
|
||||
}
|
||||
|
||||
if (delEmpty) {
|
||||
const delResult = await db.delete({table, where: `@@indexLR('value', '?', '?')`});
|
||||
const statField = `${table}Count`;
|
||||
if (stats[statField])
|
||||
stats[statField] -= delResult.deleted;
|
||||
}
|
||||
|
||||
nullArr();
|
||||
await db.close({table});
|
||||
utils.freeMemory();
|
||||
@@ -374,7 +385,7 @@ class DbCreator {
|
||||
|
||||
//series
|
||||
callback({job: 'series save', jobMessage: 'Сохранение индекса серий', jobStep: 4, progress: 0});
|
||||
await saveTable('series', seriesArr, () => {seriesArr = null});
|
||||
await saveTable('series', seriesArr, () => {seriesArr = null}, 'string', true);
|
||||
|
||||
//title
|
||||
callback({job: 'title save', jobMessage: 'Сохранение индекса названий', jobStep: 5, progress: 0});
|
||||
@@ -442,8 +453,16 @@ class DbCreator {
|
||||
table: 'config'
|
||||
});
|
||||
|
||||
const inpxInfo = parser.info;
|
||||
if (inpxFilter && inpxFilter.info) {
|
||||
if (inpxFilter.info.collection)
|
||||
inpxInfo.collection = inpxFilter.info.collection;
|
||||
if (inpxFilter.info.version)
|
||||
inpxInfo.version = inpxFilter.info.version;
|
||||
}
|
||||
|
||||
await db.insert({table: 'config', rows: [
|
||||
{id: 'inpxInfo', value: (inpxFilter && inpxFilter.info ? inpxFilter.info : parser.info)},
|
||||
{id: 'inpxInfo', value: inpxInfo},
|
||||
{id: 'stats', value: stats},
|
||||
{id: 'inpxHash', value: await inpxHashCreator.getHash()},
|
||||
]});
|
||||
@@ -455,7 +474,6 @@ class DbCreator {
|
||||
const config = this.config;
|
||||
|
||||
const to = `${from}_book`;
|
||||
const toId = `${from}_id`;
|
||||
|
||||
await db.open({table: from});
|
||||
await db.create({table: to});
|
||||
@@ -544,7 +562,7 @@ class DbCreator {
|
||||
await saveChunk(chunk);
|
||||
|
||||
processed += chunk.length;
|
||||
callback({progress: 0.5*processed/fromLength});
|
||||
callback({progress: 0.9*processed/fromLength});
|
||||
} else
|
||||
break;
|
||||
|
||||
@@ -558,24 +576,18 @@ class DbCreator {
|
||||
await db.close({table: to});
|
||||
await db.close({table: from});
|
||||
|
||||
await db.create({table: toId});
|
||||
|
||||
const chunkSize = 50000;
|
||||
let idRows = [];
|
||||
let proc = 0;
|
||||
const idMap = {arr: [], map: []};
|
||||
for (const [id, value] of bookId2RecId) {
|
||||
idRows.push({id, value});
|
||||
if (idRows.length >= chunkSize) {
|
||||
await db.insert({table: toId, rows: idRows});
|
||||
idRows = [];
|
||||
|
||||
proc += chunkSize;
|
||||
callback({progress: 0.5 + 0.5*proc/bookId2RecId.size});
|
||||
if (value.length > 1) {
|
||||
idMap.map.push([id, value]);
|
||||
idMap.arr[id] = 0;
|
||||
} else {
|
||||
idMap.arr[id] = value[0];
|
||||
}
|
||||
}
|
||||
if (idRows.length)
|
||||
await db.insert({table: toId, rows: idRows});
|
||||
await db.close({table: toId});
|
||||
|
||||
callback({progress: 1});
|
||||
await fs.writeFile(`${this.config.dataDir}/db/${from}_id.map`, JSON.stringify(idMap));
|
||||
|
||||
bookId2RecId = null;
|
||||
utils.freeMemory();
|
||||
@@ -624,6 +636,12 @@ class DbCreator {
|
||||
stats.filesDelCount = res.filesDelCount;
|
||||
}
|
||||
|
||||
//заодно добавим нужный индекс
|
||||
await db.create({
|
||||
in: 'book',
|
||||
hash: {field: '_uid', type: 'string', depth: 100, unique: true},
|
||||
});
|
||||
|
||||
countDone = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
const fs = require('fs-extra');
|
||||
//const _ = require('lodash');
|
||||
const LockQueue = require('./LockQueue');
|
||||
const utils = require('./utils');
|
||||
|
||||
const maxMemCacheSize = 100;
|
||||
const maxLimit = 1000;
|
||||
|
||||
const emptyFieldValue = '?';
|
||||
@@ -14,9 +13,13 @@ const enruArr = (ruAlphabet + enAlphabet).split('');
|
||||
class DbSearcher {
|
||||
constructor(config, db) {
|
||||
this.config = config;
|
||||
this.queryCacheMemSize = this.config.queryCacheMemSize;
|
||||
this.queryCacheDiskSize = this.config.queryCacheDiskSize;
|
||||
this.queryCacheEnabled = this.config.queryCacheEnabled
|
||||
&& (this.queryCacheMemSize > 0 || this.queryCacheDiskSize > 0);
|
||||
|
||||
this.db = db;
|
||||
|
||||
this.lock = new LockQueue();
|
||||
this.searchFlag = 0;
|
||||
this.timer = null;
|
||||
this.closed = false;
|
||||
@@ -25,11 +28,22 @@ class DbSearcher {
|
||||
this.bookIdMap = {};
|
||||
|
||||
this.periodicCleanCache();//no await
|
||||
this.fillBookIdMapAll();//no await
|
||||
}
|
||||
|
||||
async init() {
|
||||
await this.fillBookIdMap('author');
|
||||
await this.fillBookIdMap('series');
|
||||
await this.fillBookIdMap('title');
|
||||
await this.fillDbConfig();
|
||||
}
|
||||
|
||||
queryKey(q) {
|
||||
return JSON.stringify([q.author, q.series, q.title, q.genre, q.lang, q.del, q.date, q.librate]);
|
||||
const result = [];
|
||||
for (const f of this.recStruct) {
|
||||
result.push(q[f.field]);
|
||||
}
|
||||
|
||||
return JSON.stringify(result);
|
||||
}
|
||||
|
||||
getWhere(a) {
|
||||
@@ -49,8 +63,18 @@ class DbSearcher {
|
||||
a = a.substring(1);
|
||||
where = `@indexIter('value', (v) => {
|
||||
const enru = new Set(${db.esc(enruArr)});
|
||||
return !v || (v !== ${db.esc(emptyFieldValue)} && !enru.has(v[0]) && v.indexOf(${db.esc(a)}) >= 0);
|
||||
if (!v)
|
||||
return false;
|
||||
return v !== ${db.esc(emptyFieldValue)} && !enru.has(v[0]) && v.indexOf(${db.esc(a)}) >= 0;
|
||||
})`;
|
||||
} else if (a[0] == '~') {//RegExp
|
||||
a = a.substring(1);
|
||||
where = `
|
||||
await (async() => {
|
||||
const re = new RegExp(${db.esc(a)}, 'i');
|
||||
@@indexIter('value', (v) => re.test(v) );
|
||||
})()
|
||||
`;
|
||||
} else {
|
||||
where = `@dirtyIndexLR('value', ${db.esc(a)}, ${db.esc(a + maxUtf8Char)})`;
|
||||
}
|
||||
@@ -77,7 +101,7 @@ class DbSearcher {
|
||||
result.add(bookId);
|
||||
}
|
||||
|
||||
return Array.from(result);
|
||||
return new Uint32Array(result);
|
||||
`
|
||||
});
|
||||
|
||||
@@ -85,7 +109,7 @@ class DbSearcher {
|
||||
};
|
||||
|
||||
//авторы
|
||||
if (query.author && query.author !== '*') {
|
||||
if (query.author) {
|
||||
const key = `book-ids-author-${query.author}`;
|
||||
let ids = await this.getCached(key);
|
||||
|
||||
@@ -99,7 +123,7 @@ class DbSearcher {
|
||||
}
|
||||
|
||||
//серии
|
||||
if (query.series && query.series !== '*') {
|
||||
if (query.series) {
|
||||
const key = `book-ids-series-${query.series}`;
|
||||
let ids = await this.getCached(key);
|
||||
|
||||
@@ -113,7 +137,7 @@ class DbSearcher {
|
||||
}
|
||||
|
||||
//названия
|
||||
if (query.title && query.title !== '*') {
|
||||
if (query.title) {
|
||||
const key = `book-ids-title-${query.title}`;
|
||||
let ids = await this.getCached(key);
|
||||
|
||||
@@ -151,7 +175,7 @@ class DbSearcher {
|
||||
result.add(bookId);
|
||||
}
|
||||
|
||||
return Array.from(result);
|
||||
return new Uint32Array(result);
|
||||
`
|
||||
});
|
||||
|
||||
@@ -187,7 +211,7 @@ class DbSearcher {
|
||||
result.add(bookId);
|
||||
}
|
||||
|
||||
return Array.from(result);
|
||||
return new Uint32Array(result);
|
||||
`
|
||||
});
|
||||
|
||||
@@ -199,12 +223,13 @@ class DbSearcher {
|
||||
}
|
||||
|
||||
//удаленные
|
||||
if (query.del !== undefined) {
|
||||
const key = `book-ids-del-${query.del}`;
|
||||
if (query.del) {
|
||||
const del = parseInt(query.del, 10) || 0;
|
||||
const key = `book-ids-del-${del}`;
|
||||
let ids = await this.getCached(key);
|
||||
|
||||
if (ids === null) {
|
||||
ids = await tableBookIds('del', `@indexLR('value', ${db.esc(query.del)}, ${db.esc(query.del)})`);
|
||||
ids = await tableBookIds('del', `@indexLR('value', ${db.esc(del)}, ${db.esc(del)})`);
|
||||
|
||||
await this.putCached(key, ids);
|
||||
}
|
||||
@@ -252,7 +277,7 @@ class DbSearcher {
|
||||
result.add(bookId);
|
||||
}
|
||||
|
||||
return Array.from(result);
|
||||
return new Uint32Array(result);
|
||||
`
|
||||
});
|
||||
|
||||
@@ -285,7 +310,7 @@ class DbSearcher {
|
||||
inter = newInter;
|
||||
}
|
||||
|
||||
return Array.from(inter);
|
||||
return new Uint32Array(inter);
|
||||
} else if (idsArr.length == 1) {
|
||||
return idsArr[0];
|
||||
} else {
|
||||
@@ -293,53 +318,36 @@ class DbSearcher {
|
||||
}
|
||||
}
|
||||
|
||||
async fillBookIdMap(from) {
|
||||
if (this.bookIdMap[from])
|
||||
return this.bookIdMap[from];
|
||||
|
||||
await this.lock.get();
|
||||
try {
|
||||
const db = this.db;
|
||||
const map = new Map();
|
||||
const table = `${from}_id`;
|
||||
|
||||
await db.open({table});
|
||||
let rows = await db.select({table});
|
||||
await db.close({table});
|
||||
async fillDbConfig() {
|
||||
if (!this.dbConfig) {
|
||||
const rows = await this.db.select({table: 'config'});
|
||||
const config = {};
|
||||
|
||||
for (const row of rows) {
|
||||
if (!row.value.length)
|
||||
continue;
|
||||
|
||||
if (row.value.length > 1)
|
||||
map.set(row.id, row.value);
|
||||
else
|
||||
map.set(row.id, row.value[0]);
|
||||
config[row.id] = row.value;
|
||||
}
|
||||
|
||||
this.bookIdMap[from] = map;
|
||||
|
||||
rows = null;
|
||||
await db.freeMemory();
|
||||
utils.freeMemory();
|
||||
|
||||
return this.bookIdMap[from];
|
||||
} finally {
|
||||
this.lock.ret();
|
||||
this.dbConfig = config;
|
||||
this.recStruct = config.inpxInfo.recStruct;
|
||||
}
|
||||
}
|
||||
|
||||
async fillBookIdMapAll() {
|
||||
await this.fillBookIdMap('author');
|
||||
await this.fillBookIdMap('series');
|
||||
await this.fillBookIdMap('title');
|
||||
async fillBookIdMap(from) {
|
||||
const data = await fs.readFile(`${this.config.dataDir}/db/${from}_id.map`, 'utf-8');
|
||||
|
||||
const idMap = JSON.parse(data);
|
||||
idMap.arr = new Uint32Array(idMap.arr);
|
||||
idMap.map = new Map(idMap.map);
|
||||
|
||||
this.bookIdMap[from] = idMap;
|
||||
}
|
||||
|
||||
async filterTableIds(tableIds, from, query) {
|
||||
let result = tableIds;
|
||||
|
||||
//т.к. авторы у книги идут списком, то дополнительно фильтруем
|
||||
if (from == 'author' && query.author && query.author !== '*') {
|
||||
async tableIdsFilter(from, query) {
|
||||
//т.к. авторы у книги идут списком (т.е. одна книга относиться сразу к нескольким авторам),
|
||||
//то в выборку по bookId могут попасть авторы, которые отсутствуют в критерии query.author,
|
||||
//поэтому дополнительно фильтруем
|
||||
let result = null;
|
||||
if (from == 'author' && query.author) {
|
||||
const key = `filter-ids-author-${query.author}`;
|
||||
let authorIds = await this.getCached(key);
|
||||
|
||||
@@ -347,7 +355,7 @@ class DbSearcher {
|
||||
const rows = await this.db.select({
|
||||
table: 'author',
|
||||
rawResult: true,
|
||||
where: `return Array.from(${this.getWhere(query.author)})`
|
||||
where: `return new Uint32Array(${this.getWhere(query.author)})`
|
||||
});
|
||||
|
||||
authorIds = rows[0].rawResult;
|
||||
@@ -355,12 +363,7 @@ class DbSearcher {
|
||||
await this.putCached(key, authorIds);
|
||||
}
|
||||
|
||||
//пересечение tableIds и authorIds
|
||||
result = [];
|
||||
const authorIdsSet = new Set(authorIds);
|
||||
for (const id of tableIds)
|
||||
if (authorIdsSet.has(id))
|
||||
result.push(id);
|
||||
result = new Set(authorIds);
|
||||
}
|
||||
|
||||
return result;
|
||||
@@ -381,24 +384,30 @@ class DbSearcher {
|
||||
await this.putCached(bookKey, bookIds);
|
||||
}
|
||||
|
||||
//id книг (bookIds) нашли, теперь надо их смаппировать в id таблицы from (авторов, серий, названий)
|
||||
if (bookIds) {
|
||||
//т.к. авторы у книги идут списком, то дополнительно фильтруем
|
||||
const filter = await this.tableIdsFilter(from, query);
|
||||
|
||||
const tableIdsSet = new Set();
|
||||
const bookIdMap = await this.fillBookIdMap(from);
|
||||
const idMap = this.bookIdMap[from];
|
||||
let proc = 0;
|
||||
let nextProc = 0;
|
||||
for (const bookId of bookIds) {
|
||||
const tableIdValue = bookIdMap.get(bookId);
|
||||
if (!tableIdValue)
|
||||
continue;
|
||||
|
||||
if (Array.isArray(tableIdValue)) {
|
||||
for (const tableId of tableIdValue) {
|
||||
const tableId = idMap.arr[bookId];
|
||||
if (tableId) {
|
||||
if (!filter || filter.has(tableId))
|
||||
tableIdsSet.add(tableId);
|
||||
proc++;
|
||||
}
|
||||
} else {
|
||||
tableIdsSet.add(tableIdValue);
|
||||
proc++;
|
||||
} else {
|
||||
const tableIdArr = idMap.map.get(bookId);
|
||||
if (tableIdArr) {
|
||||
for (const tableId of tableIdArr) {
|
||||
if (!filter || filter.has(tableId))
|
||||
tableIdsSet.add(tableId);
|
||||
proc++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//прерываемся иногда, чтобы не блокировать Event Loop
|
||||
@@ -408,19 +417,19 @@ class DbSearcher {
|
||||
}
|
||||
}
|
||||
|
||||
tableIds = Array.from(tableIdsSet);
|
||||
} else {
|
||||
tableIds = new Uint32Array(tableIdsSet);
|
||||
} else {//bookIds пустой - критерии не заданы, значит берем все id из from
|
||||
const rows = await db.select({
|
||||
table: from,
|
||||
rawResult: true,
|
||||
where: `return Array.from(@all())`
|
||||
where: `return new Uint32Array(@all())`
|
||||
});
|
||||
|
||||
tableIds = rows[0].rawResult;
|
||||
}
|
||||
|
||||
tableIds = await this.filterTableIds(tableIds, from, query);
|
||||
|
||||
//сортируем по id
|
||||
//порядок id соответствует ASC-сортировке по строковому значению из from (имя автора, назание серии, название книги)
|
||||
tableIds.sort((a, b) => a - b);
|
||||
|
||||
await this.putCached(tableKey, tableIds);
|
||||
@@ -509,11 +518,13 @@ class DbSearcher {
|
||||
limit = (limit > maxLimit ? maxLimit : limit);
|
||||
const offset = (query.offset ? query.offset : 0);
|
||||
|
||||
const slice = ids.slice(offset, offset + limit);
|
||||
|
||||
//выборка найденных значений
|
||||
const found = await db.select({
|
||||
table: from,
|
||||
map: `(r) => ({id: r.id, ${from}: r.name, bookCount: r.bookCount, bookDelCount: r.bookDelCount})`,
|
||||
where: `@@id(${db.esc(ids.slice(offset, offset + limit))})`
|
||||
where: `@@id(${db.esc(Array.from(slice))})`
|
||||
});
|
||||
|
||||
//для title восстановим books
|
||||
@@ -537,28 +548,292 @@ class DbSearcher {
|
||||
}
|
||||
}
|
||||
|
||||
async getAuthorBookList(authorId) {
|
||||
async bookSearchIds(query) {
|
||||
const queryKey = this.queryKey(query);
|
||||
const bookKey = `book-search-ids-${queryKey}`;
|
||||
let bookIds = await this.getCached(bookKey);
|
||||
|
||||
if (bookIds === null) {
|
||||
const db = this.db;
|
||||
const filterBySearch = (bookField, searchValue) => {
|
||||
searchValue = searchValue.toLowerCase();
|
||||
//особая обработка префиксов
|
||||
if (searchValue == emptyFieldValue) {
|
||||
return `(row.${bookField} === '' || row.${bookField}.indexOf(${db.esc(emptyFieldValue)}) === 0)`;
|
||||
} else if (searchValue[0] == '=') {
|
||||
|
||||
searchValue = searchValue.substring(1);
|
||||
return `(row.${bookField}.toLowerCase().localeCompare(${db.esc(searchValue)}) === 0)`;
|
||||
} else if (searchValue[0] == '*') {
|
||||
|
||||
searchValue = searchValue.substring(1);
|
||||
return `(row.${bookField} && row.${bookField}.toLowerCase().indexOf(${db.esc(searchValue)}) >= 0)`;
|
||||
} else if (searchValue[0] == '#') {
|
||||
|
||||
searchValue = searchValue.substring(1);
|
||||
return `(row.${bookField} === '' || (!enru.has(row.${bookField}.toLowerCase()[0]) && row.${bookField}.toLowerCase().indexOf(${db.esc(searchValue)}) >= 0))`;
|
||||
} else if (searchValue[0] == '~') {//RegExp
|
||||
searchValue = searchValue.substring(1);
|
||||
|
||||
return `
|
||||
(() => {
|
||||
const re = new RegExp(${db.esc(searchValue)}, 'i');
|
||||
return re.test(row.${bookField});
|
||||
})()
|
||||
`;
|
||||
} else {
|
||||
|
||||
return `(row.${bookField}.toLowerCase().localeCompare(${db.esc(searchValue)}) >= 0 ` +
|
||||
`&& row.${bookField}.toLowerCase().localeCompare(${db.esc(searchValue)} + ${db.esc(maxUtf8Char)}) <= 0)`;
|
||||
}
|
||||
};
|
||||
|
||||
const checks = ['true'];
|
||||
for (const f of this.recStruct) {
|
||||
if (query[f.field]) {
|
||||
let searchValue = query[f.field];
|
||||
if (f.type === 'S') {
|
||||
checks.push(filterBySearch(f.field, searchValue));
|
||||
} if (f.type === 'N') {
|
||||
const v = searchValue.split('..');
|
||||
|
||||
if (v.length == 1) {
|
||||
searchValue = parseInt(searchValue, 10);
|
||||
if (isNaN(searchValue))
|
||||
throw new Error(`Wrong query param, ${f.field}=${searchValue}`);
|
||||
|
||||
checks.push(`row.${f.field} === ${searchValue}`);
|
||||
} else {
|
||||
const from = parseInt(v[0] || '0', 10);
|
||||
const to = parseInt(v[1] || '0', 10);
|
||||
if (isNaN(from) || isNaN(to))
|
||||
throw new Error(`Wrong query param, ${f.field}=${searchValue}`);
|
||||
|
||||
checks.push(`row.${f.field} >= ${from} && row.${f.field} <= ${to}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const rows = await db.select({
|
||||
table: 'book',
|
||||
rawResult: true,
|
||||
where: `
|
||||
const enru = new Set(${db.esc(enruArr)});
|
||||
|
||||
const checkBook = (row) => {
|
||||
return ${checks.join(' && ')};
|
||||
};
|
||||
|
||||
const result = [];
|
||||
for (const id of @all()) {
|
||||
const row = @unsafeRow(id);
|
||||
if (checkBook(row))
|
||||
result.push(row);
|
||||
}
|
||||
|
||||
result.sort((a, b) => {
|
||||
let cmp = a.author.localeCompare(b.author);
|
||||
if (cmp === 0 && (a.series || b.series)) {
|
||||
cmp = (a.series && b.series ? a.series.localeCompare(b.series) : (a.series ? -1 : 1));
|
||||
}
|
||||
if (cmp === 0)
|
||||
cmp = a.serno - b.serno;
|
||||
if (cmp === 0)
|
||||
cmp = a.title.localeCompare(b.title);
|
||||
|
||||
return cmp;
|
||||
});
|
||||
|
||||
return new Uint32Array(result.map(row => row.id));
|
||||
`
|
||||
});
|
||||
|
||||
bookIds = rows[0].rawResult;
|
||||
|
||||
await this.putCached(bookKey, bookIds);
|
||||
}
|
||||
|
||||
return bookIds;
|
||||
}
|
||||
|
||||
//неоптимизированный поиск по всем книгам, по всем полям
|
||||
async bookSearch(query) {
|
||||
if (this.closed)
|
||||
throw new Error('DbSearcher closed');
|
||||
|
||||
if (!authorId)
|
||||
return {author: '', books: ''};
|
||||
|
||||
this.searchFlag++;
|
||||
|
||||
try {
|
||||
//выборка книг автора по authorId
|
||||
const rows = await this.restoreBooks('author', [authorId])
|
||||
const db = this.db;
|
||||
|
||||
let author = '';
|
||||
let books = '';
|
||||
const ids = await this.bookSearchIds(query);
|
||||
|
||||
const totalFound = ids.length;
|
||||
let limit = (query.limit ? query.limit : 100);
|
||||
limit = (limit > maxLimit ? maxLimit : limit);
|
||||
const offset = (query.offset ? query.offset : 0);
|
||||
|
||||
const slice = ids.slice(offset, offset + limit);
|
||||
|
||||
//выборка найденных значений
|
||||
const found = await db.select({
|
||||
table: 'book',
|
||||
where: `@@id(${db.esc(Array.from(slice))})`
|
||||
});
|
||||
|
||||
return {found, totalFound};
|
||||
} finally {
|
||||
this.searchFlag--;
|
||||
}
|
||||
}
|
||||
|
||||
async opdsQuery(from, query) {
|
||||
if (this.closed)
|
||||
throw new Error('DbSearcher closed');
|
||||
|
||||
if (!['author', 'series', 'title'].includes(from))
|
||||
throw new Error(`Unknown value for param 'from'`);
|
||||
|
||||
this.searchFlag++;
|
||||
|
||||
try {
|
||||
const db = this.db;
|
||||
|
||||
const depth = query.depth || 1;
|
||||
const queryKey = this.queryKey(query);
|
||||
const opdsKey = `${from}-opds-d${depth}-${queryKey}`;
|
||||
let result = await this.getCached(opdsKey);
|
||||
|
||||
if (result === null) {
|
||||
const ids = await this.selectTableIds(from, query);
|
||||
|
||||
const totalFound = ids.length;
|
||||
|
||||
//группировка по name длиной depth
|
||||
const found = await db.select({
|
||||
table: from,
|
||||
rawResult: true,
|
||||
where: `
|
||||
const depth = ${db.esc(depth)};
|
||||
const group = new Map();
|
||||
|
||||
const ids = ${db.esc(Array.from(ids))};
|
||||
for (const id of ids) {
|
||||
const row = @unsafeRow(id);
|
||||
const s = row.value.substring(0, depth);
|
||||
let g = group.get(s);
|
||||
if (!g) {
|
||||
g = {id: row.id, name: row.name, value: s, count: 0, bookCount: 0};
|
||||
group.set(s, g);
|
||||
}
|
||||
g.count++;
|
||||
g.bookCount += row.bookCount;
|
||||
}
|
||||
|
||||
const result = Array.from(group.values());
|
||||
result.sort((a, b) => a.value.localeCompare(b.value));
|
||||
|
||||
return result;
|
||||
`
|
||||
});
|
||||
|
||||
result = {found: found[0].rawResult, totalFound};
|
||||
|
||||
await this.putCached(opdsKey, result);
|
||||
}
|
||||
|
||||
return result;
|
||||
} finally {
|
||||
this.searchFlag--;
|
||||
}
|
||||
}
|
||||
|
||||
async getAuthorBookList(authorId, author) {
|
||||
if (this.closed)
|
||||
throw new Error('DbSearcher closed');
|
||||
|
||||
if (!authorId && !author)
|
||||
return {author: '', books: []};
|
||||
|
||||
this.searchFlag++;
|
||||
|
||||
try {
|
||||
const db = this.db;
|
||||
|
||||
if (!authorId) {
|
||||
//восстановим authorId
|
||||
authorId = 0;
|
||||
author = author.toLowerCase();
|
||||
|
||||
const rows = await db.select({
|
||||
table: 'author',
|
||||
rawResult: true,
|
||||
where: `return Array.from(@dirtyIndexLR('value', ${db.esc(author)}, ${db.esc(author)}))`
|
||||
});
|
||||
|
||||
if (rows.length && rows[0].rawResult.length)
|
||||
authorId = rows[0].rawResult[0];
|
||||
}
|
||||
|
||||
//выборка книг автора по authorId
|
||||
const rows = await this.restoreBooks('author', [authorId]);
|
||||
|
||||
let authorName = '';
|
||||
let books = [];
|
||||
|
||||
if (rows.length) {
|
||||
author = rows[0].name;
|
||||
authorName = rows[0].name;
|
||||
books = rows[0].books;
|
||||
}
|
||||
|
||||
return {author, books: (books && books.length ? JSON.stringify(books) : '')};
|
||||
return {author: authorName, books};
|
||||
} finally {
|
||||
this.searchFlag--;
|
||||
}
|
||||
}
|
||||
|
||||
async getAuthorSeriesList(authorId) {
|
||||
if (this.closed)
|
||||
throw new Error('DbSearcher closed');
|
||||
|
||||
if (!authorId)
|
||||
return {author: '', series: []};
|
||||
|
||||
this.searchFlag++;
|
||||
|
||||
try {
|
||||
const db = this.db;
|
||||
|
||||
//выборка книг автора по authorId
|
||||
const bookList = await this.getAuthorBookList(authorId);
|
||||
const books = bookList.books;
|
||||
const seriesSet = new Set();
|
||||
for (const book of books) {
|
||||
if (book.series)
|
||||
seriesSet.add(book.series.toLowerCase());
|
||||
}
|
||||
|
||||
let series = [];
|
||||
if (seriesSet.size) {
|
||||
//выборка серий по названиям
|
||||
series = await db.select({
|
||||
table: 'series',
|
||||
map: `(r) => ({id: r.id, series: r.name, bookCount: r.bookCount, bookDelCount: r.bookDelCount})`,
|
||||
where: `
|
||||
const seriesArr = ${db.esc(Array.from(seriesSet))};
|
||||
const ids = new Set();
|
||||
for (const value of seriesArr) {
|
||||
for (const id of @dirtyIndexLR('value', value, value))
|
||||
ids.add(id);
|
||||
}
|
||||
|
||||
return ids;
|
||||
`
|
||||
});
|
||||
}
|
||||
|
||||
return {author: bookList.author, series};
|
||||
} finally {
|
||||
this.searchFlag--;
|
||||
}
|
||||
@@ -569,7 +844,7 @@ class DbSearcher {
|
||||
throw new Error('DbSearcher closed');
|
||||
|
||||
if (!series)
|
||||
return {books: ''};
|
||||
return {books: []};
|
||||
|
||||
this.searchFlag++;
|
||||
|
||||
@@ -585,7 +860,7 @@ class DbSearcher {
|
||||
where: `return Array.from(@dirtyIndexLR('value', ${db.esc(series)}, ${db.esc(series)}))`
|
||||
});
|
||||
|
||||
let books;
|
||||
let books = [];
|
||||
if (rows.length && rows[0].rawResult.length) {
|
||||
//выборка книг серии
|
||||
const bookRows = await this.restoreBooks('series', [rows[0].rawResult[0]])
|
||||
@@ -594,14 +869,14 @@ class DbSearcher {
|
||||
books = bookRows[0].books;
|
||||
}
|
||||
|
||||
return {books: (books && books.length ? JSON.stringify(books) : '')};
|
||||
return {books};
|
||||
} finally {
|
||||
this.searchFlag--;
|
||||
}
|
||||
}
|
||||
|
||||
async getCached(key) {
|
||||
if (!this.config.queryCacheEnabled)
|
||||
if (!this.queryCacheEnabled)
|
||||
return null;
|
||||
|
||||
let result = null;
|
||||
@@ -609,13 +884,13 @@ class DbSearcher {
|
||||
const db = this.db;
|
||||
const memCache = this.memCache;
|
||||
|
||||
if (memCache.has(key)) {//есть в недавних
|
||||
if (this.queryCacheMemSize > 0 && memCache.has(key)) {//есть в недавних
|
||||
result = memCache.get(key);
|
||||
|
||||
//изменим порядок ключей, для последующей правильной чистки старых
|
||||
memCache.delete(key);
|
||||
memCache.set(key, result);
|
||||
} else {//смотрим в таблице
|
||||
} else if (this.queryCacheDiskSize > 0) {//смотрим в таблице
|
||||
const rows = await db.select({table: 'query_cache', where: `@@id(${db.esc(key)})`});
|
||||
|
||||
if (rows.length) {//нашли в кеше
|
||||
@@ -626,13 +901,17 @@ class DbSearcher {
|
||||
});
|
||||
|
||||
result = rows[0].value;
|
||||
memCache.set(key, result);
|
||||
|
||||
if (memCache.size > maxMemCacheSize) {
|
||||
//удаляем самый старый ключ-значение
|
||||
for (const k of memCache.keys()) {
|
||||
memCache.delete(k);
|
||||
break;
|
||||
//заполняем кеш в памяти
|
||||
if (this.queryCacheMemSize > 0) {
|
||||
memCache.set(key, result);
|
||||
|
||||
if (memCache.size > this.queryCacheMemSize) {
|
||||
//удаляем самый старый ключ-значение
|
||||
for (const k of memCache.keys()) {
|
||||
memCache.delete(k);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -642,40 +921,44 @@ class DbSearcher {
|
||||
}
|
||||
|
||||
async putCached(key, value) {
|
||||
if (!this.config.queryCacheEnabled)
|
||||
if (!this.queryCacheEnabled)
|
||||
return;
|
||||
|
||||
const db = this.db;
|
||||
|
||||
const memCache = this.memCache;
|
||||
memCache.set(key, value);
|
||||
if (this.queryCacheMemSize > 0) {
|
||||
const memCache = this.memCache;
|
||||
memCache.set(key, value);
|
||||
|
||||
if (memCache.size > maxMemCacheSize) {
|
||||
//удаляем самый старый ключ-значение
|
||||
for (const k of memCache.keys()) {
|
||||
memCache.delete(k);
|
||||
break;
|
||||
if (memCache.size > this.queryCacheMemSize) {
|
||||
//удаляем самый старый ключ-значение
|
||||
for (const k of memCache.keys()) {
|
||||
memCache.delete(k);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//кладем в таблицу асинхронно
|
||||
(async() => {
|
||||
try {
|
||||
await db.insert({
|
||||
table: 'query_cache',
|
||||
replace: true,
|
||||
rows: [{id: key, value}],
|
||||
});
|
||||
if (this.queryCacheDiskSize > 0) {
|
||||
//кладем в таблицу асинхронно
|
||||
(async() => {
|
||||
try {
|
||||
await db.insert({
|
||||
table: 'query_cache',
|
||||
replace: true,
|
||||
rows: [{id: key, value}],
|
||||
});
|
||||
|
||||
await db.insert({
|
||||
table: 'query_time',
|
||||
replace: true,
|
||||
rows: [{id: key, time: Date.now()}],
|
||||
});
|
||||
} catch(e) {
|
||||
console.error(`putCached: ${e.message}`);
|
||||
}
|
||||
})();
|
||||
await db.insert({
|
||||
table: 'query_time',
|
||||
replace: true,
|
||||
rows: [{id: key, time: Date.now()}],
|
||||
});
|
||||
} catch(e) {
|
||||
console.error(`putCached: ${e.message}`);
|
||||
}
|
||||
})();
|
||||
}
|
||||
}
|
||||
|
||||
async periodicCleanCache() {
|
||||
@@ -685,21 +968,37 @@ class DbSearcher {
|
||||
return;
|
||||
|
||||
try {
|
||||
if (!this.queryCacheEnabled || this.queryCacheDiskSize <= 0)
|
||||
return;
|
||||
|
||||
const db = this.db;
|
||||
|
||||
const oldThres = Date.now() - cleanInterval;
|
||||
let rows = await db.select({table: 'query_time', count: true});
|
||||
const delCount = rows[0].count - this.queryCacheDiskSize;
|
||||
|
||||
//выберем всех кандидатов на удаление
|
||||
const rows = await db.select({
|
||||
if (delCount < 1)
|
||||
return;
|
||||
|
||||
//выберем delCount кандидатов на удаление
|
||||
rows = await db.select({
|
||||
table: 'query_time',
|
||||
rawResult: true,
|
||||
where: `
|
||||
@@iter(@all(), (r) => (r.time < ${db.esc(oldThres)}));
|
||||
const delCount = ${delCount};
|
||||
const rows = [];
|
||||
|
||||
@unsafeIter(@all(), (r) => {
|
||||
rows.push(r);
|
||||
return false;
|
||||
});
|
||||
|
||||
rows.sort((a, b) => a.time - b.time);
|
||||
|
||||
return rows.slice(0, delCount).map(r => r.id);
|
||||
`
|
||||
});
|
||||
|
||||
const ids = [];
|
||||
for (const row of rows)
|
||||
ids.push(row.id);
|
||||
const ids = rows[0].rawResult;
|
||||
|
||||
//удаляем
|
||||
await db.delete({table: 'query_cache', where: `@@id(${db.esc(ids)})`});
|
||||
|
||||
@@ -14,6 +14,7 @@ class FileDownloader {
|
||||
|
||||
let options = {
|
||||
headers: {
|
||||
'accept-encoding': 'gzip, compress, deflate',
|
||||
'user-agent': userAgent,
|
||||
timeout: 300*1000,
|
||||
},
|
||||
|
||||
@@ -1,11 +1,32 @@
|
||||
const path = require('path');
|
||||
const crypto = require('crypto');
|
||||
const ZipReader = require('./ZipReader');
|
||||
const utils = require('./utils');
|
||||
|
||||
const collectionInfo = 'collection.info';
|
||||
const structureInfo = 'structure.info';
|
||||
const versionInfo = 'version.info';
|
||||
|
||||
const defaultStructure = 'AUTHOR;GENRE;TITLE;SERIES;SERNO;FILE;SIZE;LIBID;DEL;EXT;DATE;LANG;LIBRATE;KEYWORDS';
|
||||
//'AUTHOR;GENRE;TITLE;SERIES;SERNO;FILE;SIZE;LIBID;DEL;EXT;DATE;INSNO;FOLDER;LANG;LIBRATE;KEYWORDS;'
|
||||
const recStructType = {
|
||||
author: 'S',
|
||||
genre: 'S',
|
||||
title: 'S',
|
||||
series: 'S',
|
||||
serno: 'N',
|
||||
file: 'S',
|
||||
size: 'N',
|
||||
libid: 'S',
|
||||
del: 'N',
|
||||
ext: 'S',
|
||||
date: 'S',
|
||||
insno: 'N',
|
||||
folder: 'S',
|
||||
lang: 'S',
|
||||
librate: 'N',
|
||||
keywords: 'S',
|
||||
}
|
||||
|
||||
class InpxParser {
|
||||
constructor() {
|
||||
@@ -23,6 +44,21 @@ class InpxParser {
|
||||
return result;
|
||||
}
|
||||
|
||||
getRecStruct(structure) {
|
||||
const result = [];
|
||||
let struct = structure;
|
||||
//folder есть всегда
|
||||
if (!struct.includes('folder'))
|
||||
struct = struct.concat(['folder']);
|
||||
|
||||
for (const field of struct) {
|
||||
if (utils.hasProp(recStructType, field))
|
||||
result.push({field, type: recStructType[field]});
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
async parse(inpxFile, readFileCallback, parsedCallback) {
|
||||
if (!readFileCallback)
|
||||
readFileCallback = async() => {};
|
||||
@@ -60,11 +96,11 @@ class InpxParser {
|
||||
info.version = await this.safeExtractToString(zipReader, versionInfo);
|
||||
|
||||
//структура
|
||||
let inpxStructure = info.structure;
|
||||
if (!inpxStructure)
|
||||
inpxStructure = defaultStructure;
|
||||
inpxStructure = inpxStructure.toLowerCase();
|
||||
const structure = inpxStructure.split(';');
|
||||
if (!info.structure)
|
||||
info.structure = defaultStructure;
|
||||
const structure = info.structure.toLowerCase().split(';');
|
||||
|
||||
info.recStruct = this.getRecStruct(structure);
|
||||
|
||||
//парсим inp-файлы
|
||||
this.chunk = [];
|
||||
@@ -98,9 +134,13 @@ class InpxParser {
|
||||
if (line[line.length - 1] == '\x0D')
|
||||
line = line.substring(0, line.length - 1);
|
||||
|
||||
const rec = {};
|
||||
//уникальный идентификатор записи
|
||||
const sha256 = crypto.createHash('sha256');
|
||||
rec._uid = sha256.update(line).digest('base64');
|
||||
|
||||
//парсим запись
|
||||
const parts = line.split('\x04');
|
||||
const rec = {};
|
||||
|
||||
const len = (parts.length > structLen ? structLen : parts.length);
|
||||
for (let i = 0; i < len; i++) {
|
||||
|
||||
@@ -16,8 +16,6 @@ class RemoteLib {
|
||||
this.config = config;
|
||||
|
||||
this.wsc = new WebSocketConnection(config.remoteLib.url, 10, 30, {rejectUnauthorized: false});
|
||||
if (config.remoteLib.accessPassword)
|
||||
this.accessToken = utils.getBufHash(config.remoteLib.accessPassword, 'sha256', 'hex');
|
||||
|
||||
this.remoteHost = config.remoteLib.url.replace(/^ws:\/\//, 'http://').replace(/^wss:\/\//, 'https://');
|
||||
|
||||
@@ -31,7 +29,7 @@ class RemoteLib {
|
||||
return instance;
|
||||
}
|
||||
|
||||
async wsRequest(query) {
|
||||
async wsRequest(query, recurse = false) {
|
||||
if (this.accessToken)
|
||||
query.accessToken = this.accessToken;
|
||||
|
||||
@@ -40,6 +38,11 @@ class RemoteLib {
|
||||
120
|
||||
);
|
||||
|
||||
if (!recurse && response && response.error == 'need_access_token' && this.config.remoteLib.accessPassword) {
|
||||
this.accessToken = utils.getBufHash(this.config.remoteLib.accessPassword + response.salt, 'sha256', 'hex');
|
||||
return await this.wsRequest(query, true);
|
||||
}
|
||||
|
||||
if (response.error)
|
||||
throw new Error(response.error);
|
||||
|
||||
@@ -58,14 +61,15 @@ class RemoteLib {
|
||||
}
|
||||
}
|
||||
|
||||
async downloadBook(bookPath, downFileName) {
|
||||
async downloadBook(bookUid) {
|
||||
try {
|
||||
const response = await await this.wsRequest({action: 'get-book-link', bookPath, downFileName});
|
||||
const response = await await this.wsRequest({action: 'get-book-link', bookUid});
|
||||
const link = response.link;
|
||||
|
||||
const buf = await this.down.load(`${this.remoteHost}${link}`, {decompress: false});
|
||||
|
||||
const publicPath = `${this.config.publicFilesDir}${link}`;
|
||||
const hash = path.basename(link);
|
||||
const publicPath = `${this.config.bookDir}/${hash}`;
|
||||
|
||||
await fs.writeFile(publicPath, buf);
|
||||
|
||||
|
||||
154
server/core/WebAccess.js
Normal file
154
server/core/WebAccess.js
Normal file
@@ -0,0 +1,154 @@
|
||||
const { JembaDbThread } = require('jembadb');
|
||||
const utils = require('../core/utils');
|
||||
const log = new (require('../core/AppLogger'))().log;//singleton
|
||||
const asyncExit = new (require('./AsyncExit'))();
|
||||
|
||||
const cleanPeriod = 1*60*1000;//1 минута
|
||||
const cleanUnusedTokenTimeout = 5*60*1000;//5 минут
|
||||
|
||||
class WebAccess {
|
||||
constructor(config) {
|
||||
this.config = config;
|
||||
|
||||
this.freeAccess = (config.accessPassword === '');
|
||||
this.accessTimeout = config.accessTimeout*60*1000;
|
||||
this.accessMap = new Map();
|
||||
|
||||
asyncExit.add(this.closeDb.bind(this));
|
||||
|
||||
setTimeout(() => { this.periodicClean(); }, cleanPeriod);
|
||||
}
|
||||
|
||||
async init() {
|
||||
const config = this.config;
|
||||
const dbPath = `${config.dataDir}/web-access`;
|
||||
const db = new JembaDbThread();//в отдельном потоке
|
||||
await db.lock({
|
||||
dbPath,
|
||||
create: true,
|
||||
softLock: true,
|
||||
|
||||
tableDefaults: {
|
||||
cacheSize: config.dbCacheSize,
|
||||
},
|
||||
});
|
||||
|
||||
try {
|
||||
//открываем таблицы
|
||||
await db.openAll();
|
||||
} catch(e) {
|
||||
if (
|
||||
e.message.indexOf('corrupted') >= 0
|
||||
|| e.message.indexOf('Unexpected token') >= 0
|
||||
|| e.message.indexOf('invalid stored block lengths') >= 0
|
||||
) {
|
||||
log(LM_ERR, `DB ${dbPath} corrupted`);
|
||||
log(`Open "${dbPath}" with auto repair`);
|
||||
await db.openAll({autoRepair: true});
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
await db.create({table: 'access', quietIfExists: true});
|
||||
//проверим, нужно ли обнулить таблицу access
|
||||
const pass = utils.getBufHash(this.config.accessPassword, 'sha256', 'hex');
|
||||
await db.create({table: 'config', quietIfExists: true});
|
||||
let rows = await db.select({table: 'config', where: `@@id('pass')`});
|
||||
|
||||
if (!rows.length || rows[0].value !== pass) {
|
||||
//пароль сменился в конфиге, обнуляем токены
|
||||
await db.truncate({table: 'access'});
|
||||
await db.insert({table: 'config', replace: true, rows: [{id: 'pass', value: pass}]});
|
||||
}
|
||||
|
||||
//загрузим токены сессий
|
||||
rows = await db.select({table: 'access'});
|
||||
for (const row of rows)
|
||||
this.accessMap.set(row.id, row.value);
|
||||
|
||||
this.db = db;
|
||||
}
|
||||
|
||||
async closeDb() {
|
||||
if (this.db) {
|
||||
await this.db.unlock();
|
||||
this.db = null;
|
||||
}
|
||||
}
|
||||
|
||||
async periodicClean() {
|
||||
while (1) {//eslint-disable-line no-constant-condition
|
||||
try {
|
||||
const now = Date.now();
|
||||
|
||||
//почистим accessMap
|
||||
if (!this.freeAccess) {
|
||||
for (const [accessToken, accessRec] of this.accessMap) {
|
||||
if ( !(accessRec.used > 0 || now - accessRec.time < cleanUnusedTokenTimeout)
|
||||
|| !(this.accessTimeout === 0 || now - accessRec.time < this.accessTimeout)
|
||||
) {
|
||||
await this.deleteAccess(accessToken);
|
||||
} else if (!accessRec.saved) {
|
||||
await this.saveAccess(accessToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} catch(e) {
|
||||
log(LM_ERR, `WebAccess.periodicClean error: ${e.message}`);
|
||||
}
|
||||
|
||||
await utils.sleep(cleanPeriod);
|
||||
}
|
||||
}
|
||||
|
||||
async hasAccess(accessToken) {
|
||||
if (this.freeAccess)
|
||||
return true;
|
||||
|
||||
const accessRec = this.accessMap.get(accessToken);
|
||||
if (accessRec) {
|
||||
const now = Date.now();
|
||||
|
||||
if (this.accessTimeout === 0 || now - accessRec.time < this.accessTimeout) {
|
||||
accessRec.used++;
|
||||
accessRec.time = now;
|
||||
accessRec.saved = false;
|
||||
if (accessRec.used === 1)
|
||||
await this.saveAccess(accessToken);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
async deleteAccess(accessToken) {
|
||||
await this.db.delete({table: 'access', where: `@@id(${this.db.esc(accessToken)})`});
|
||||
this.accessMap.delete(accessToken);
|
||||
}
|
||||
|
||||
async saveAccess(accessToken) {
|
||||
const value = this.accessMap.get(accessToken);
|
||||
if (!value || value.saved)
|
||||
return;
|
||||
|
||||
value.saved = true;
|
||||
await this.db.insert({
|
||||
table: 'access',
|
||||
replace: true,
|
||||
rows: [{id: accessToken, value}]
|
||||
});
|
||||
}
|
||||
|
||||
newToken() {
|
||||
const salt = utils.randomHexString(32);
|
||||
const accessToken = utils.getBufHash(this.config.accessPassword + salt, 'sha256', 'hex');
|
||||
this.accessMap.set(accessToken, {time: Date.now(), used: 0});
|
||||
|
||||
return salt;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = WebAccess;
|
||||
@@ -11,7 +11,7 @@ const DbSearcher = require('./DbSearcher');
|
||||
const InpxHashCreator = require('./InpxHashCreator');
|
||||
const RemoteLib = require('./RemoteLib');//singleton
|
||||
|
||||
const ayncExit = new (require('./AsyncExit'))();
|
||||
const asyncExit = new (require('./AsyncExit'))();
|
||||
const log = new (require('./AppLogger'))().log;//singleton
|
||||
const utils = require('./utils');
|
||||
const genreTree = require('./genres');
|
||||
@@ -53,14 +53,14 @@ class WebWorker {
|
||||
this.db = null;
|
||||
this.dbSearcher = null;
|
||||
|
||||
ayncExit.add(this.closeDb.bind(this));
|
||||
asyncExit.add(this.closeDb.bind(this));
|
||||
|
||||
this.loadOrCreateDb();//no await
|
||||
this.periodicLogServerStats();//no await
|
||||
|
||||
const dirConfig = [
|
||||
{
|
||||
dir: config.filesDir,
|
||||
dir: config.bookDir,
|
||||
maxSize: config.maxFilesDirSize,
|
||||
},
|
||||
];
|
||||
@@ -210,6 +210,7 @@ class WebWorker {
|
||||
|
||||
//поисковый движок
|
||||
this.dbSearcher = new DbSearcher(config, db);
|
||||
await this.dbSearcher.init();
|
||||
|
||||
//stuff
|
||||
db.wwCache = {};
|
||||
@@ -221,7 +222,7 @@ class WebWorker {
|
||||
this.logServerStats();
|
||||
} catch (e) {
|
||||
log(LM_FATAL, e.message);
|
||||
ayncExit.exit(1);
|
||||
asyncExit.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -267,10 +268,33 @@ class WebWorker {
|
||||
return result;
|
||||
}
|
||||
|
||||
async getAuthorBookList(authorId) {
|
||||
async bookSearch(query) {
|
||||
this.checkMyState();
|
||||
|
||||
return await this.dbSearcher.getAuthorBookList(authorId);
|
||||
const result = await this.dbSearcher.bookSearch(query);
|
||||
|
||||
const config = await this.dbConfig();
|
||||
result.inpxHash = (config.inpxHash ? config.inpxHash : '');
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
async opdsQuery(from, query) {
|
||||
this.checkMyState();
|
||||
|
||||
return await this.dbSearcher.opdsQuery(from, query);
|
||||
}
|
||||
|
||||
async getAuthorBookList(authorId, author) {
|
||||
this.checkMyState();
|
||||
|
||||
return await this.dbSearcher.getAuthorBookList(authorId, author);
|
||||
}
|
||||
|
||||
async getAuthorSeriesList(authorId) {
|
||||
this.checkMyState();
|
||||
|
||||
return await this.dbSearcher.getAuthorSeriesList(authorId);
|
||||
}
|
||||
|
||||
async getSeriesBookList(series) {
|
||||
@@ -354,7 +378,7 @@ class WebWorker {
|
||||
}
|
||||
}
|
||||
|
||||
async restoreBook(bookPath, downFileName) {
|
||||
async restoreBook(bookUid, bookPath, downFileName) {
|
||||
const db = this.db;
|
||||
|
||||
let extractedFile = '';
|
||||
@@ -364,11 +388,11 @@ class WebWorker {
|
||||
extractedFile = await this.extractBook(bookPath);
|
||||
hash = await utils.getFileHash(extractedFile, 'sha256', 'hex');
|
||||
} else {
|
||||
hash = await this.remoteLib.downloadBook(bookPath, downFileName);
|
||||
hash = await this.remoteLib.downloadBook(bookUid);
|
||||
}
|
||||
|
||||
const link = `${this.config.filesPathStatic}/${hash}`;
|
||||
const bookFile = `${this.config.filesDir}/${hash}`;
|
||||
const link = `${this.config.bookPathStatic}/${hash}`;
|
||||
const bookFile = `${this.config.bookDir}/${hash}`;
|
||||
const bookFileDesc = `${bookFile}.d.json`;
|
||||
|
||||
if (!await fs.pathExists(bookFile) || !await fs.pathExists(bookFileDesc)) {
|
||||
@@ -402,27 +426,35 @@ class WebWorker {
|
||||
return link;
|
||||
}
|
||||
|
||||
async getBookLink(bookId) {
|
||||
async getBookLink(bookUid) {
|
||||
this.checkMyState();
|
||||
|
||||
try {
|
||||
const db = this.db;
|
||||
let link = '';
|
||||
|
||||
//найдем bookPath и downFileName
|
||||
let rows = await db.select({table: 'book', where: `@@id(${db.esc(bookId)})`});
|
||||
//найдем downFileName и bookPath
|
||||
let rows = await db.select({table: 'book', where: `@@hash('_uid', ${db.esc(bookUid)})`});
|
||||
if (!rows.length)
|
||||
throw new Error('404 Файл не найден');
|
||||
|
||||
const book = rows[0];
|
||||
const book = rows[0];
|
||||
let downFileName = book.file;
|
||||
const author = book.author.split(',');
|
||||
const at = [author[0], book.title];
|
||||
downFileName = utils.makeValidFileNameOrEmpty(at.filter(r => r).join(' - '))
|
||||
const authors = book.author.split(',');
|
||||
let author = authors[0];
|
||||
author = author.split(' ').filter(r => r.trim());
|
||||
for (let i = 1; i < author.length; i++)
|
||||
author[i] = `${(i === 1 ? ' ' : '')}${author[i][0]}.`;
|
||||
if (authors.length > 1)
|
||||
author.push(' и др.');
|
||||
|
||||
const at = [author.join(''), (book.title ? `_${book.title}` : '')];
|
||||
downFileName = utils.makeValidFileNameOrEmpty(at.filter(r => r).join(''))
|
||||
|| utils.makeValidFileNameOrEmpty(at[0])
|
||||
|| utils.makeValidFileNameOrEmpty(at[1])
|
||||
|| downFileName;
|
||||
downFileName = downFileName.substring(0, 100);
|
||||
if (downFileName.length > 50)
|
||||
downFileName = `${downFileName.substring(0, 50)}_`;
|
||||
|
||||
const ext = `.${book.ext}`;
|
||||
if (downFileName.substring(downFileName.length - ext.length) != ext)
|
||||
@@ -434,16 +466,16 @@ class WebWorker {
|
||||
rows = await db.select({table: 'file_hash', where: `@@id(${db.esc(bookPath)})`});
|
||||
if (rows.length) {//хеш найден по bookPath
|
||||
const hash = rows[0].hash;
|
||||
const bookFile = `${this.config.filesDir}/${hash}`;
|
||||
const bookFile = `${this.config.bookDir}/${hash}`;
|
||||
const bookFileDesc = `${bookFile}.d.json`;
|
||||
|
||||
if (await fs.pathExists(bookFile) && await fs.pathExists(bookFileDesc)) {
|
||||
link = `${this.config.filesPathStatic}/${hash}`;
|
||||
link = `${this.config.bookPathStatic}/${hash}`;
|
||||
}
|
||||
}
|
||||
|
||||
if (!link) {
|
||||
link = await this.restoreBook(bookPath, downFileName)
|
||||
link = await this.restoreBook(bookUid, bookPath, downFileName)
|
||||
}
|
||||
|
||||
if (!link)
|
||||
@@ -458,23 +490,25 @@ class WebWorker {
|
||||
}
|
||||
}
|
||||
|
||||
async getBookInfo(bookId) {
|
||||
async getBookInfo(bookUid) {
|
||||
this.checkMyState();
|
||||
|
||||
try {
|
||||
const db = this.db;
|
||||
|
||||
let bookInfo = await this.getBookLink(bookId);
|
||||
let bookInfo = await this.getBookLink(bookUid);
|
||||
const hash = path.basename(bookInfo.link);
|
||||
const bookFile = `${this.config.filesDir}/${hash}`;
|
||||
const bookFile = `${this.config.bookDir}/${hash}`;
|
||||
const bookFileInfo = `${bookFile}.i.json`;
|
||||
|
||||
let rows = await db.select({table: 'book', where: `@@hash('_uid', ${db.esc(bookUid)})`});
|
||||
if (!rows.length)
|
||||
throw new Error('404 Файл не найден');
|
||||
const book = rows[0];
|
||||
|
||||
const restoreBookInfo = async(info) => {
|
||||
const result = {};
|
||||
|
||||
const rows = await db.select({table: 'book', where: `@@id(${db.esc(bookId)})`});
|
||||
const book = rows[0];
|
||||
|
||||
result.book = book;
|
||||
result.cover = '';
|
||||
result.fb2 = false;
|
||||
@@ -486,12 +520,13 @@ class WebWorker {
|
||||
result.fb2 = fb2.rawNodes;
|
||||
|
||||
if (cover) {
|
||||
result.cover = `${this.config.filesPathStatic}/${hash}${coverExt}`;
|
||||
result.cover = `${this.config.bookPathStatic}/${hash}${coverExt}`;
|
||||
await fs.writeFile(`${bookFile}${coverExt}`, cover);
|
||||
}
|
||||
}
|
||||
|
||||
Object.assign(info ,result);
|
||||
Object.assign(info, result);
|
||||
|
||||
await fs.writeFile(bookFileInfo, JSON.stringify(info));
|
||||
|
||||
if (this.config.branch === 'development') {
|
||||
@@ -511,7 +546,7 @@ class WebWorker {
|
||||
if (tmpInfo.cover)
|
||||
coverFile = `${this.config.publicFilesDir}${tmpInfo.cover}`;
|
||||
|
||||
if (coverFile && !await fs.pathExists(coverFile)) {
|
||||
if (book.id != tmpInfo.book.id || (coverFile && !await fs.pathExists(coverFile))) {
|
||||
await restoreBookInfo(bookInfo);
|
||||
} else {
|
||||
bookInfo = tmpInfo;
|
||||
@@ -619,7 +654,7 @@ class WebWorker {
|
||||
}
|
||||
} catch (e) {
|
||||
log(LM_FATAL, e.message);
|
||||
ayncExit.exit(1);
|
||||
asyncExit.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
const StreamZip = require('node-stream-zip');
|
||||
const StreamUnzip = require('node-stream-zip');
|
||||
|
||||
class ZipReader {
|
||||
constructor() {
|
||||
@@ -14,7 +14,7 @@ class ZipReader {
|
||||
if (this.zip)
|
||||
throw new Error('Zip file is already open');
|
||||
|
||||
const zip = new StreamZip.async({file: zipFile, skipEntryNameValidation: true});
|
||||
const zip = new StreamUnzip.async({file: zipFile, skipEntryNameValidation: true});
|
||||
|
||||
if (zipEntries)
|
||||
this.zipEntries = await zip.entries();
|
||||
|
||||
@@ -70,9 +70,6 @@ class Fb2Helper {
|
||||
if (coverImage.count) {
|
||||
const coverAttrs = coverImage.attrs();
|
||||
const href = coverAttrs[`${parser.xlinkNS}:href`];
|
||||
let coverType = coverAttrs['content-type'];
|
||||
coverType = (coverType == 'image/jpg' || coverType == 'application/octet-stream' ? 'image/jpeg' : coverType);
|
||||
coverExt = (coverType == 'image/png' ? '.png' : '.jpg');
|
||||
|
||||
if (href) {
|
||||
const binaryId = (href[0] == '#' ? href.substring(1) : href);
|
||||
@@ -84,6 +81,10 @@ class Fb2Helper {
|
||||
return;
|
||||
|
||||
if (attrs.id === binaryId) {
|
||||
let coverType = attrs['content-type'];
|
||||
coverType = (coverType == 'image/jpg' || coverType == 'application/octet-stream' ? 'image/jpeg' : coverType);
|
||||
coverExt = (coverType == 'image/png' ? '.png' : '.jpg');
|
||||
|
||||
const base64 = node.text();
|
||||
cover = (base64 ? Buffer.from(base64, 'base64') : null);
|
||||
}
|
||||
|
||||
@@ -283,7 +283,7 @@ class Fb2Parser extends XmlParser {
|
||||
};
|
||||
|
||||
for (const [tag, s] of Object.entries(substs)) {
|
||||
const r = new RegExp(`${tag}`, 'g');
|
||||
const r = new RegExp(tag, 'g');
|
||||
xmlString = xmlString.replace(r, s);
|
||||
}
|
||||
|
||||
|
||||
203
server/core/opds/AuthorPage.js
Normal file
203
server/core/opds/AuthorPage.js
Normal file
@@ -0,0 +1,203 @@
|
||||
const BasePage = require('./BasePage');
|
||||
const utils = require('../utils');
|
||||
|
||||
class AuthorPage extends BasePage {
|
||||
constructor(config) {
|
||||
super(config);
|
||||
|
||||
this.id = 'author';
|
||||
this.title = 'Авторы';
|
||||
}
|
||||
|
||||
sortBooks(bookList) {
|
||||
//схлопывание серий
|
||||
const books = [];
|
||||
const seriesMap = new Map();
|
||||
for (const book of bookList) {
|
||||
if (book.series) {
|
||||
let seriesIndex = seriesMap.get(book.series);
|
||||
if (seriesIndex === undefined) {
|
||||
seriesIndex = books.length;
|
||||
books.push({
|
||||
type: 'series',
|
||||
book,
|
||||
bookCount: 0,
|
||||
});
|
||||
|
||||
seriesMap.set(book.series, seriesIndex);
|
||||
}
|
||||
|
||||
books[seriesIndex].bookCount++;
|
||||
} else {
|
||||
books.push({
|
||||
type: 'book',
|
||||
book
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
//сортировка
|
||||
books.sort((a, b) => {
|
||||
if (a.type == 'series') {
|
||||
return (b.type == 'series' ? a.book.series.localeCompare(b.book.series) : -1);
|
||||
} else {
|
||||
return (b.type == 'book' ? a.book.title.localeCompare(b.book.title) : 1);
|
||||
}
|
||||
});
|
||||
|
||||
return books;
|
||||
}
|
||||
|
||||
sortSeriesBooks(seriesBooks) {
|
||||
seriesBooks.sort((a, b) => {
|
||||
const dserno = (a.serno || Number.MAX_VALUE) - (b.serno || Number.MAX_VALUE);
|
||||
const dtitle = a.title.localeCompare(b.title);
|
||||
const dext = a.ext.localeCompare(b.ext);
|
||||
return (dserno ? dserno : (dtitle ? dtitle : dext));
|
||||
});
|
||||
|
||||
return seriesBooks;
|
||||
}
|
||||
|
||||
async body(req) {
|
||||
const result = {};
|
||||
|
||||
const query = {
|
||||
author: req.query.author || '',
|
||||
series: req.query.series || '',
|
||||
genre: req.query.genre || '',
|
||||
del: '0',
|
||||
|
||||
all: req.query.all || '',
|
||||
depth: 0,
|
||||
};
|
||||
query.depth = query.author.length + 1;
|
||||
|
||||
if (query.author == '___others') {
|
||||
query.author = '';
|
||||
query.depth = 1;
|
||||
query.others = true;
|
||||
}
|
||||
|
||||
const entry = [];
|
||||
if (query.series) {
|
||||
//книги по серии
|
||||
const bookList = await this.webWorker.getSeriesBookList(query.series);
|
||||
|
||||
if (bookList.books) {
|
||||
let books = bookList.books;
|
||||
const booksAll = this.filterBooks(books);
|
||||
const filtered = (query.all ? booksAll : this.filterBooks(books, query));
|
||||
const sorted = this.sortSeriesBooks(filtered);
|
||||
|
||||
if (booksAll.length > filtered.length) {
|
||||
entry.push(
|
||||
this.makeEntry({
|
||||
id: 'all_series_books',
|
||||
title: '[Все книги серии]',
|
||||
link: this.navLink({
|
||||
href: `/${this.id}?author=${encodeURIComponent(query.author)}` +
|
||||
`&series=${encodeURIComponent(query.series)}&all=1`}),
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
for (const book of sorted) {
|
||||
const title = `${book.serno ? `${book.serno}. `: ''}${book.title || 'Без названия'} (${book.ext})`;
|
||||
|
||||
entry.push(
|
||||
this.makeEntry({
|
||||
id: book._uid,
|
||||
title,
|
||||
link: this.acqLink({href: `/book?uid=${encodeURIComponent(book._uid)}`}),
|
||||
content: {
|
||||
'*ATTRS': {type: 'text'},
|
||||
'*TEXT': this.bookAuthor(book.author),
|
||||
},
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
} else if (query.author && query.author[0] == '=') {
|
||||
//книги по автору
|
||||
const bookList = await this.webWorker.getAuthorBookList(0, query.author.substring(1));
|
||||
|
||||
if (bookList.books) {
|
||||
let books = bookList.books;
|
||||
books = this.sortBooks(this.filterBooks(books, query));
|
||||
|
||||
for (const b of books) {
|
||||
if (b.type == 'series') {
|
||||
entry.push(
|
||||
this.makeEntry({
|
||||
id: b.book._uid,
|
||||
title: `Серия: ${b.book.series}`,
|
||||
link: this.navLink({
|
||||
href: `/${this.id}?author=${encodeURIComponent(query.author)}` +
|
||||
`&series=${encodeURIComponent(b.book.series)}&genre=${encodeURIComponent(query.genre)}`}),
|
||||
content: {
|
||||
'*ATTRS': {type: 'text'},
|
||||
'*TEXT': `${b.bookCount} книг${utils.wordEnding(b.bookCount, 8)} по автору${(query.genre ? ' (в выбранном жанре)' : '')}`,
|
||||
},
|
||||
})
|
||||
);
|
||||
} else {
|
||||
const title = `${b.book.title || 'Без названия'} (${b.book.ext})`;
|
||||
entry.push(
|
||||
this.makeEntry({
|
||||
id: b.book._uid,
|
||||
title,
|
||||
link: this.acqLink({href: `/book?uid=${encodeURIComponent(b.book._uid)}`}),
|
||||
content: {
|
||||
'*ATTRS': {type: 'text'},
|
||||
'*TEXT': this.bookAuthor(b.book.author),
|
||||
},
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (query.depth == 1 && !query.genre && !query.others) {
|
||||
entry.push(
|
||||
this.makeEntry({
|
||||
id: 'select_genre',
|
||||
title: '[Выбрать жанр]',
|
||||
link: this.navLink({href: `/genre?from=${this.id}`}),
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
//навигация по каталогу
|
||||
const queryRes = await this.opdsQuery('author', query, '[Остальные авторы]');
|
||||
|
||||
for (const rec of queryRes) {
|
||||
const e = {
|
||||
id: rec.id,
|
||||
title: this.bookAuthor(rec.title),
|
||||
link: this.navLink({href: `/${this.id}?author=${rec.q}&genre=${encodeURIComponent(query.genre)}`}),
|
||||
};
|
||||
|
||||
let countStr = '';
|
||||
if (rec.count)
|
||||
countStr = `${rec.count} автор${utils.wordEnding(rec.count, 0)}${(query.genre ? ' (в выбранном жанре)' : '')}`;
|
||||
if (!countStr && rec.bookCount && !query.genre)
|
||||
countStr = `${rec.bookCount} книг${utils.wordEnding(rec.bookCount, 8)}`;
|
||||
|
||||
if (countStr) {
|
||||
e.content = {
|
||||
'*ATTRS': {type: 'text'},
|
||||
'*TEXT': countStr,
|
||||
};
|
||||
}
|
||||
|
||||
entry.push(this.makeEntry(e));
|
||||
}
|
||||
}
|
||||
|
||||
result.entry = entry;
|
||||
return this.makeBody(result, req);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = AuthorPage;
|
||||
373
server/core/opds/BasePage.js
Normal file
373
server/core/opds/BasePage.js
Normal file
@@ -0,0 +1,373 @@
|
||||
const _ = require('lodash');
|
||||
const he = require('he');
|
||||
|
||||
const WebWorker = require('../WebWorker');//singleton
|
||||
const XmlParser = require('../xml/XmlParser');
|
||||
|
||||
const spaceChar = String.fromCodePoint(0x00B7);
|
||||
const emptyFieldValue = '?';
|
||||
const maxUtf8Char = String.fromCodePoint(0xFFFFF);
|
||||
const ruAlphabet = 'абвгдеёжзийклмнопрстуфхцчшщъыьэюя';
|
||||
const enAlphabet = 'abcdefghijklmnopqrstuvwxyz';
|
||||
const enruArr = (ruAlphabet + enAlphabet).split('');
|
||||
const enru = new Set(enruArr);
|
||||
const ruOnly = new Set(ruAlphabet.split(''));
|
||||
|
||||
class BasePage {
|
||||
constructor(config) {
|
||||
this.config = config;
|
||||
|
||||
this.webWorker = new WebWorker(config);
|
||||
this.rootTag = 'feed';
|
||||
this.opdsRoot = config.opdsRoot;
|
||||
|
||||
this.showDeleted = false;
|
||||
}
|
||||
|
||||
escape(s) {
|
||||
//костыль для koreader, не понимает hex-экранирование вида '
|
||||
return he.escape(s).replace(/'/g, ''').replace(/`/g, '`');
|
||||
}
|
||||
|
||||
makeEntry(entry = {}) {
|
||||
if (!entry.id)
|
||||
throw new Error('makeEntry: no id');
|
||||
if (!entry.title)
|
||||
throw new Error('makeEntry: no title');
|
||||
|
||||
entry.title = this.escape(entry.title);
|
||||
|
||||
const result = {
|
||||
updated: (new Date()).toISOString().substring(0, 19) + 'Z',
|
||||
};
|
||||
|
||||
return Object.assign(result, entry);
|
||||
}
|
||||
|
||||
myEntry() {
|
||||
return this.makeEntry({
|
||||
id: this.id,
|
||||
title: this.title,
|
||||
link: this.navLink({href: `/${this.id}`}),
|
||||
});
|
||||
}
|
||||
|
||||
makeLink(attrs) {
|
||||
attrs.href = this.escape(attrs.href);
|
||||
return {'*ATTRS': attrs};
|
||||
}
|
||||
|
||||
navLink(attrs) {
|
||||
return this.makeLink({
|
||||
href: (attrs.hrefAsIs ? attrs.href : `${this.opdsRoot}${attrs.href || ''}`),
|
||||
rel: attrs.rel || 'subsection',
|
||||
type: 'application/atom+xml;profile=opds-catalog;kind=navigation',
|
||||
});
|
||||
}
|
||||
|
||||
acqLink(attrs) {
|
||||
return this.makeLink({
|
||||
href: (attrs.hrefAsIs ? attrs.href : `${this.opdsRoot}${attrs.href || ''}`),
|
||||
rel: attrs.rel || 'subsection',
|
||||
type: 'application/atom+xml;profile=opds-catalog;kind=acquisition',
|
||||
});
|
||||
}
|
||||
|
||||
downLink(attrs) {
|
||||
if (!attrs.href)
|
||||
throw new Error('downLink: no href');
|
||||
if (!attrs.type)
|
||||
throw new Error('downLink: no type');
|
||||
|
||||
return this.makeLink({
|
||||
href: attrs.href,
|
||||
rel: 'http://opds-spec.org/acquisition',
|
||||
type: attrs.type,
|
||||
});
|
||||
}
|
||||
|
||||
imgLink(attrs) {
|
||||
if (!attrs.href)
|
||||
throw new Error('imgLink: no href');
|
||||
|
||||
return this.makeLink({
|
||||
href: attrs.href,
|
||||
rel: `http://opds-spec.org/image${attrs.thumb ? '/thumbnail' : ''}`,
|
||||
type: attrs.type || 'image/jpeg',
|
||||
});
|
||||
}
|
||||
|
||||
baseLinks(req, selfAcq = false) {
|
||||
const result = [
|
||||
this.makeLink({href: `${this.opdsRoot}/opensearch`, rel: 'search', type: 'application/opensearchdescription+xml'}),
|
||||
this.makeLink({href: `${this.opdsRoot}/search?term={searchTerms}`, rel: 'search', type: 'application/atom+xml'}),
|
||||
|
||||
this.navLink({rel: 'start'}),
|
||||
];
|
||||
|
||||
if (selfAcq) {
|
||||
result.push(this.acqLink({rel: 'self', href: req.originalUrl, hrefAsIs: true}));
|
||||
} else {
|
||||
result.push(this.navLink({rel: 'self', href: req.originalUrl, hrefAsIs: true}));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
makeBody(content, req) {
|
||||
const base = this.makeEntry({id: this.id, title: this.title});
|
||||
base['*ATTRS'] = {
|
||||
'xmlns': 'http://www.w3.org/2005/Atom',
|
||||
'xmlns:dc': 'http://purl.org/dc/terms/',
|
||||
'xmlns:opds': 'http://opds-spec.org/2010/catalog',
|
||||
};
|
||||
|
||||
if (!content.link)
|
||||
base.link = this.baseLinks(req);
|
||||
|
||||
const xml = new XmlParser();
|
||||
const xmlObject = {};
|
||||
xmlObject[this.rootTag] = Object.assign(base, content);
|
||||
|
||||
xml.fromObject(xmlObject);
|
||||
|
||||
return xml.toString({format: true});
|
||||
}
|
||||
|
||||
async body() {
|
||||
throw new Error('Body not implemented');
|
||||
}
|
||||
|
||||
// -- stuff -------------------------------------------
|
||||
async search(from, query) {
|
||||
const result = [];
|
||||
const queryRes = await this.webWorker.search(from, query);
|
||||
|
||||
for (const row of queryRes.found) {
|
||||
const rec = {
|
||||
id: row.id,
|
||||
title: (row[from] || 'Без автора'),
|
||||
q: `=${encodeURIComponent(row[from])}`,
|
||||
bookCount: row.bookCount,
|
||||
};
|
||||
|
||||
result.push(rec);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
async opdsQuery(from, query, otherTitle = '[Другие]', prevLen = 0) {
|
||||
const queryRes = await this.webWorker.opdsQuery(from, query);
|
||||
let count = 0;
|
||||
for (const row of queryRes.found)
|
||||
count += row.count;
|
||||
|
||||
const others = [];
|
||||
let result = [];
|
||||
if (count <= 50) {
|
||||
//конец навигации
|
||||
return await this.search(from, query);
|
||||
} else {
|
||||
let len = 0;
|
||||
const enResult = [];
|
||||
for (const row of queryRes.found) {
|
||||
const value = row.value;
|
||||
len += value.length;
|
||||
|
||||
let rec;
|
||||
if (row.count == 1) {
|
||||
rec = {
|
||||
id: row.id,
|
||||
title: row.name,
|
||||
q: `=${encodeURIComponent(row.name)}`,
|
||||
bookCount: row.bookCount,
|
||||
};
|
||||
} else {
|
||||
rec = {
|
||||
id: row.id,
|
||||
title: `${value.toUpperCase().replace(/ /g, spaceChar)}~`,
|
||||
q: encodeURIComponent(value),
|
||||
count: row.count,
|
||||
};
|
||||
}
|
||||
if (query.depth > 1 || enru.has(value[0]) ) {
|
||||
//такой костыль из-за проблем с локалями в pkg
|
||||
//русский язык всегда идет первым!
|
||||
if (ruOnly.has(value[0]))
|
||||
result.push(rec)
|
||||
else
|
||||
enResult.push(rec);
|
||||
} else {
|
||||
others.push(rec);
|
||||
}
|
||||
}
|
||||
|
||||
result = result.concat(enResult);
|
||||
|
||||
if (query[from] && query.depth > 1 && result.length < 10 && len > prevLen) {
|
||||
//рекурсия, с увеличением глубины, для облегчения навигации
|
||||
const newQuery = _.cloneDeep(query);
|
||||
newQuery.depth++;
|
||||
return await this.opdsQuery(from, newQuery, otherTitle, len);
|
||||
}
|
||||
}
|
||||
|
||||
if (!query.others && others.length)
|
||||
result.unshift({id: 'other', title: otherTitle, q: '___others', count: others.length});
|
||||
|
||||
return (!query.others ? result : others);
|
||||
}
|
||||
|
||||
//скопировано из BaseList.js, часть функционала не используется
|
||||
filterBooks(books, query = {}) {
|
||||
const s = query;
|
||||
|
||||
const splitAuthor = (author) => {
|
||||
if (!author) {
|
||||
author = emptyFieldValue;
|
||||
}
|
||||
|
||||
const result = author.split(',');
|
||||
if (result.length > 1)
|
||||
result.push(author);
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
const filterBySearch = (bookValue, searchValue) => {
|
||||
if (!searchValue)
|
||||
return true;
|
||||
|
||||
if (!bookValue)
|
||||
bookValue = emptyFieldValue;
|
||||
|
||||
bookValue = bookValue.toLowerCase();
|
||||
searchValue = searchValue.toLowerCase();
|
||||
|
||||
//особая обработка префиксов
|
||||
if (searchValue[0] == '=') {
|
||||
|
||||
searchValue = searchValue.substring(1);
|
||||
return bookValue.localeCompare(searchValue) == 0;
|
||||
} else if (searchValue[0] == '*') {
|
||||
|
||||
searchValue = searchValue.substring(1);
|
||||
return bookValue !== emptyFieldValue && bookValue.indexOf(searchValue) >= 0;
|
||||
} else if (searchValue[0] == '#') {
|
||||
|
||||
searchValue = searchValue.substring(1);
|
||||
if (!bookValue)
|
||||
return false;
|
||||
return bookValue !== emptyFieldValue && !enru.has(bookValue[0]) && bookValue.indexOf(searchValue) >= 0;
|
||||
} else if (searchValue[0] == '~') {//RegExp
|
||||
|
||||
searchValue = searchValue.substring(1);
|
||||
const re = new RegExp(searchValue, 'i');
|
||||
return re.test(bookValue);
|
||||
} else {
|
||||
//where = `@dirtyIndexLR('value', ${db.esc(a)}, ${db.esc(a + maxUtf8Char)})`;
|
||||
return bookValue.localeCompare(searchValue) >= 0 && bookValue.localeCompare(searchValue + maxUtf8Char) <= 0;
|
||||
}
|
||||
};
|
||||
|
||||
return books.filter((book) => {
|
||||
//author
|
||||
let authorFound = false;
|
||||
const authors = splitAuthor(book.author);
|
||||
for (const a of authors) {
|
||||
if (filterBySearch(a, s.author)) {
|
||||
authorFound = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//genre
|
||||
let genreFound = !s.genre;
|
||||
if (!genreFound) {
|
||||
const searchGenres = new Set(s.genre.split(','));
|
||||
const bookGenres = book.genre.split(',');
|
||||
|
||||
for (let g of bookGenres) {
|
||||
if (!g)
|
||||
g = emptyFieldValue;
|
||||
|
||||
if (searchGenres.has(g)) {
|
||||
genreFound = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//lang
|
||||
let langFound = !s.lang;
|
||||
if (!langFound) {
|
||||
const searchLang = new Set(s.lang.split(','));
|
||||
langFound = searchLang.has(book.lang || emptyFieldValue);
|
||||
}
|
||||
|
||||
//date
|
||||
let dateFound = !s.date;
|
||||
if (!dateFound) {
|
||||
const date = this.queryDate(s.date).split(',');
|
||||
let [from = '0000-00-00', to = '9999-99-99'] = date;
|
||||
|
||||
dateFound = (book.date >= from && book.date <= to);
|
||||
}
|
||||
|
||||
//librate
|
||||
let librateFound = !s.librate;
|
||||
if (!librateFound) {
|
||||
const searchLibrate = new Set(s.librate.split(',').map(n => parseInt(n, 10)).filter(n => !isNaN(n)));
|
||||
librateFound = searchLibrate.has(book.librate);
|
||||
}
|
||||
|
||||
return (this.showDeleted || !book.del)
|
||||
&& authorFound
|
||||
&& filterBySearch(book.series, s.series)
|
||||
&& filterBySearch(book.title, s.title)
|
||||
&& genreFound
|
||||
&& langFound
|
||||
&& dateFound
|
||||
&& librateFound
|
||||
;
|
||||
});
|
||||
}
|
||||
|
||||
bookAuthor(author) {
|
||||
if (author) {
|
||||
let a = author.split(',');
|
||||
return a.slice(0, 3).join(', ') + (a.length > 3 ? ' и др.' : '');
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
async getGenres() {
|
||||
let result;
|
||||
if (!this.genres) {
|
||||
const res = await this.webWorker.getGenreTree();
|
||||
|
||||
result = {
|
||||
genreTree: res.genreTree,
|
||||
genreMap: new Map(),
|
||||
genreSection: new Map(),
|
||||
};
|
||||
|
||||
for (const section of result.genreTree) {
|
||||
result.genreSection.set(section.name, section.value);
|
||||
|
||||
for (const g of section.value)
|
||||
result.genreMap.set(g.value, g.name);
|
||||
}
|
||||
|
||||
this.genres = result;
|
||||
} else {
|
||||
result = this.genres;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = BasePage;
|
||||
217
server/core/opds/BookPage.js
Normal file
217
server/core/opds/BookPage.js
Normal file
@@ -0,0 +1,217 @@
|
||||
const path = require('path');
|
||||
const _ = require('lodash');
|
||||
const dayjs = require('dayjs');
|
||||
|
||||
const BasePage = require('./BasePage');
|
||||
const Fb2Parser = require('../fb2/Fb2Parser');
|
||||
|
||||
class BookPage extends BasePage {
|
||||
constructor(config) {
|
||||
super(config);
|
||||
|
||||
this.id = 'book';
|
||||
this.title = 'Книга';
|
||||
|
||||
}
|
||||
|
||||
formatSize(size) {
|
||||
size = size/1024;
|
||||
let unit = 'KB';
|
||||
if (size > 1024) {
|
||||
size = size/1024;
|
||||
unit = 'MB';
|
||||
}
|
||||
return `${size.toFixed(1)} ${unit}`;
|
||||
}
|
||||
|
||||
inpxInfo(bookRec) {
|
||||
const mapping = [
|
||||
{name: 'fileInfo', label: 'Информация о файле', value: [
|
||||
{name: 'folder', label: 'Папка'},
|
||||
{name: 'file', label: 'Файл'},
|
||||
{name: 'size', label: 'Размер'},
|
||||
{name: 'date', label: 'Добавлен'},
|
||||
{name: 'del', label: 'Удален'},
|
||||
{name: 'libid', label: 'LibId'},
|
||||
{name: 'insno', label: 'InsideNo'},
|
||||
]},
|
||||
|
||||
{name: 'titleInfo', label: 'Общая информация', value: [
|
||||
{name: 'author', label: 'Автор(ы)'},
|
||||
{name: 'title', label: 'Название'},
|
||||
{name: 'series', label: 'Серия'},
|
||||
{name: 'genre', label: 'Жанр'},
|
||||
{name: 'librate', label: 'Оценка'},
|
||||
{name: 'lang', label: 'Язык книги'},
|
||||
{name: 'keywords', label: 'Ключевые слова'},
|
||||
]},
|
||||
];
|
||||
|
||||
const valueToString = (value, nodePath, b) => {//eslint-disable-line no-unused-vars
|
||||
if (nodePath == 'fileInfo/file')
|
||||
return `${value}.${b.ext}`;
|
||||
|
||||
if (nodePath == 'fileInfo/size')
|
||||
return `${this.formatSize(value)} (${value.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1 ')} Bytes)`;
|
||||
|
||||
if (nodePath == 'fileInfo/date')
|
||||
return dayjs(value, 'YYYY-MM-DD').format('DD.MM.YYYY');
|
||||
|
||||
if (nodePath == 'fileInfo/del')
|
||||
return (value ? 'Да' : null);
|
||||
|
||||
if (nodePath == 'fileInfo/insno')
|
||||
return (value ? value : null);
|
||||
|
||||
if (nodePath == 'titleInfo/author')
|
||||
return value.split(',').join(', ');
|
||||
|
||||
if (nodePath == 'titleInfo/librate' && !value)
|
||||
return null;
|
||||
|
||||
if (typeof(value) === 'string') {
|
||||
return value;
|
||||
}
|
||||
|
||||
return (value.toString ? value.toString() : '');
|
||||
};
|
||||
|
||||
let result = [];
|
||||
const book = _.cloneDeep(bookRec);
|
||||
book.series = [book.series, book.serno].filter(v => v).join(' #');
|
||||
|
||||
for (const item of mapping) {
|
||||
const itemOut = {name: item.name, label: item.label, value: []};
|
||||
|
||||
for (const subItem of item.value) {
|
||||
const subItemOut = {
|
||||
name: subItem.name,
|
||||
label: subItem.label,
|
||||
value: valueToString(book[subItem.name], `${item.name}/${subItem.name}`, book)
|
||||
};
|
||||
if (subItemOut.value)
|
||||
itemOut.value.push(subItemOut);
|
||||
}
|
||||
|
||||
if (itemOut.value.length)
|
||||
result.push(itemOut);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
htmlInfo(title, infoList) {
|
||||
let info = '';
|
||||
for (const part of infoList) {
|
||||
if (part.value.length)
|
||||
info += `<h3>${part.label}</h3>`;
|
||||
for (const rec of part.value)
|
||||
info += `<p>${rec.label}: ${rec.value}</p>`;
|
||||
}
|
||||
|
||||
if (info)
|
||||
info = `<h2>${title}</h2>${info}`;
|
||||
|
||||
return info;
|
||||
}
|
||||
|
||||
async body(req) {
|
||||
const result = {};
|
||||
|
||||
result.link = this.baseLinks(req, true);
|
||||
|
||||
const bookUid = req.query.uid;
|
||||
const entry = [];
|
||||
if (bookUid) {
|
||||
const {bookInfo} = await this.webWorker.getBookInfo(bookUid);
|
||||
|
||||
if (bookInfo) {
|
||||
const {genreMap} = await this.getGenres();
|
||||
|
||||
//format
|
||||
const ext = bookInfo.book.ext;
|
||||
let fileFormat = `${ext}+zip`;
|
||||
let downHref = bookInfo.link;
|
||||
|
||||
if (ext === 'mobi') {
|
||||
fileFormat = 'x-mobipocket-ebook';
|
||||
} else if (ext == 'epub') {
|
||||
//
|
||||
} else {
|
||||
downHref = `${bookInfo.link}/zip`;
|
||||
}
|
||||
|
||||
//entry
|
||||
const e = this.makeEntry({
|
||||
id: bookUid,
|
||||
title: bookInfo.book.title || 'Без названия',
|
||||
});
|
||||
|
||||
e['dc:language'] = bookInfo.book.lang;
|
||||
e['dc:format'] = fileFormat;
|
||||
|
||||
//genre
|
||||
const genre = bookInfo.book.genre.split(',');
|
||||
for (const g of genre) {
|
||||
const genreName = genreMap.get(g);
|
||||
if (genreName) {
|
||||
if (!e.category)
|
||||
e.category = [];
|
||||
e.category.push({
|
||||
'*ATTRS': {term: genreName, label: genreName},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
let content = '';
|
||||
let ann = '';
|
||||
let info = '';
|
||||
//fb2 info
|
||||
if (bookInfo.fb2) {
|
||||
const parser = new Fb2Parser(bookInfo.fb2);
|
||||
const infoObj = parser.bookInfo();
|
||||
|
||||
if (infoObj.titleInfo) {
|
||||
if (infoObj.titleInfo.author.length) {
|
||||
e.author = infoObj.titleInfo.author.map(a => ({name: a}));
|
||||
}
|
||||
|
||||
ann = infoObj.titleInfo.annotationHtml || '';
|
||||
const infoList = parser.bookInfoList(infoObj);
|
||||
info += this.htmlInfo('Fb2 инфо', infoList);
|
||||
}
|
||||
}
|
||||
|
||||
//content
|
||||
info += this.htmlInfo('Inpx инфо', this.inpxInfo(bookInfo.book));
|
||||
|
||||
content = `${ann}${info}`;
|
||||
if (content) {
|
||||
e.content = {
|
||||
'*ATTRS': {type: 'text/html'},
|
||||
'*TEXT': this.escape(content),
|
||||
};
|
||||
}
|
||||
|
||||
//links
|
||||
e.link = [ this.downLink({href: downHref, type: `application/${fileFormat}`}) ];
|
||||
if (bookInfo.cover) {
|
||||
let coverType = 'image/jpeg';
|
||||
if (path.extname(bookInfo.cover) == '.png')
|
||||
coverType = 'image/png';
|
||||
|
||||
e.link.push(this.imgLink({href: bookInfo.cover, type: coverType}));
|
||||
e.link.push(this.imgLink({href: bookInfo.cover, type: coverType, thumb: true}));
|
||||
}
|
||||
|
||||
entry.push(e);
|
||||
}
|
||||
}
|
||||
|
||||
result.entry = entry;
|
||||
|
||||
return this.makeBody(result, req);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = BookPage;
|
||||
76
server/core/opds/GenrePage.js
Normal file
76
server/core/opds/GenrePage.js
Normal file
@@ -0,0 +1,76 @@
|
||||
const BasePage = require('./BasePage');
|
||||
|
||||
class GenrePage extends BasePage {
|
||||
constructor(config) {
|
||||
super(config);
|
||||
|
||||
this.id = 'genre';
|
||||
this.title = 'Жанры';
|
||||
|
||||
}
|
||||
|
||||
async body(req) {
|
||||
const result = {};
|
||||
|
||||
const query = {
|
||||
from: req.query.from || 'search',
|
||||
term: req.query.term || '',
|
||||
section: req.query.section || '',
|
||||
};
|
||||
|
||||
let searchQuery = '';
|
||||
if (query.from == 'search')
|
||||
searchQuery = `&type=title&term=${encodeURIComponent(query.term)}`;
|
||||
|
||||
const entry = [];
|
||||
if (query.from) {
|
||||
if (query.section) {
|
||||
//выбираем подразделы
|
||||
const {genreSection} = await this.getGenres();
|
||||
const section = genreSection.get(query.section);
|
||||
|
||||
if (section) {
|
||||
let id = 0;
|
||||
const all = [];
|
||||
for (const g of section) {
|
||||
all.push(g.value);
|
||||
entry.push(
|
||||
this.makeEntry({
|
||||
id: ++id,
|
||||
title: g.name,
|
||||
link: this.navLink({href: `/${encodeURIComponent(query.from)}?genre=${encodeURIComponent(g.value)}${searchQuery}`}),
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
entry.unshift(
|
||||
this.makeEntry({
|
||||
id: 'whole_section',
|
||||
title: '[Весь раздел]',
|
||||
link: this.navLink({href: `/${encodeURIComponent(query.from)}?genre=${encodeURIComponent(all.join(','))}${searchQuery}`}),
|
||||
})
|
||||
);
|
||||
}
|
||||
} else {
|
||||
//выбираем разделы
|
||||
const {genreTree} = await this.getGenres();
|
||||
let id = 0;
|
||||
for (const section of genreTree) {
|
||||
entry.push(
|
||||
this.makeEntry({
|
||||
id: ++id,
|
||||
title: section.name,
|
||||
link: this.navLink({href: `/genre?from=${encodeURIComponent(query.from)}§ion=${encodeURIComponent(section.name)}${searchQuery}`}),
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result.entry = entry;
|
||||
|
||||
return this.makeBody(result, req);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = GenrePage;
|
||||
45
server/core/opds/OpensearchPage.js
Normal file
45
server/core/opds/OpensearchPage.js
Normal file
@@ -0,0 +1,45 @@
|
||||
const BasePage = require('./BasePage');
|
||||
const XmlParser = require('../xml/XmlParser');
|
||||
|
||||
class OpensearchPage extends BasePage {
|
||||
constructor(config) {
|
||||
super(config);
|
||||
|
||||
this.id = 'opensearch';
|
||||
this.title = 'opensearch';
|
||||
}
|
||||
|
||||
async body() {
|
||||
const xml = new XmlParser();
|
||||
const xmlObject = {};
|
||||
/*
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
|
||||
<ShortName>inpx-web</ShortName>
|
||||
<Description>Поиск по каталогу</Description>
|
||||
<InputEncoding>UTF-8</InputEncoding>
|
||||
<OutputEncoding>UTF-8</OutputEncoding>
|
||||
<Url type="application/atom+xml;profile=opds-catalog;kind=navigation" template="/opds/search?term={searchTerms}"/>
|
||||
</OpenSearchDescription>
|
||||
*/
|
||||
xmlObject['OpenSearchDescription'] = {
|
||||
'*ATTRS': {xmlns: 'http://a9.com/-/spec/opensearch/1.1/'},
|
||||
ShortName: 'inpx-web',
|
||||
Description: 'Поиск по каталогу',
|
||||
InputEncoding: 'UTF-8',
|
||||
OutputEncoding: 'UTF-8',
|
||||
Url: {
|
||||
'*ATTRS': {
|
||||
type: 'application/atom+xml;profile=opds-catalog;kind=navigation',
|
||||
template: `${this.opdsRoot}/search?term={searchTerms}`,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
xml.fromObject(xmlObject);
|
||||
|
||||
return xml.toString({format: true});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = OpensearchPage;
|
||||
42
server/core/opds/RootPage.js
Normal file
42
server/core/opds/RootPage.js
Normal file
@@ -0,0 +1,42 @@
|
||||
const BasePage = require('./BasePage');
|
||||
const AuthorPage = require('./AuthorPage');
|
||||
const SeriesPage = require('./SeriesPage');
|
||||
const TitlePage = require('./TitlePage');
|
||||
const GenrePage = require('./GenrePage');
|
||||
|
||||
class RootPage extends BasePage {
|
||||
constructor(config) {
|
||||
super(config);
|
||||
|
||||
this.id = 'root';
|
||||
this.title = '';
|
||||
|
||||
this.authorPage = new AuthorPage(config);
|
||||
this.seriesPage = new SeriesPage(config);
|
||||
this.titlePage = new TitlePage(config);
|
||||
this.genrePage = new GenrePage(config);
|
||||
}
|
||||
|
||||
async body(req) {
|
||||
const result = {};
|
||||
|
||||
if (!this.title) {
|
||||
const dbConfig = await this.webWorker.dbConfig();
|
||||
const collection = dbConfig.inpxInfo.collection.split('\n');
|
||||
this.title = collection[0].trim();
|
||||
if (!this.title)
|
||||
this.title = 'Неизвестная коллекция';
|
||||
}
|
||||
|
||||
result.entry = [
|
||||
this.authorPage.myEntry(),
|
||||
this.seriesPage.myEntry(),
|
||||
this.titlePage.myEntry(),
|
||||
this.genrePage.myEntry(),
|
||||
];
|
||||
|
||||
return this.makeBody(result, req);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = RootPage;
|
||||
60
server/core/opds/SearchHelpPage.js
Normal file
60
server/core/opds/SearchHelpPage.js
Normal file
@@ -0,0 +1,60 @@
|
||||
const BasePage = require('./BasePage');
|
||||
|
||||
class SearchHelpPage extends BasePage {
|
||||
constructor(config) {
|
||||
super(config);
|
||||
|
||||
this.id = 'search_help';
|
||||
this.title = 'Памятка по поиску';
|
||||
|
||||
}
|
||||
|
||||
async body(req) {
|
||||
const result = {};
|
||||
|
||||
result.link = this.baseLinks(req, true);
|
||||
|
||||
const content = `
|
||||
Формат поискового значения:
|
||||
<ul>
|
||||
<li>
|
||||
без префикса: значение трактуется, как "начинается с"
|
||||
</li>
|
||||
<li>
|
||||
префикс "=": поиск по точному совпадению
|
||||
</li>
|
||||
<li>
|
||||
префикс "*": поиск подстроки в строке
|
||||
</li>
|
||||
<li>
|
||||
префикс "#": поиск подстроки в строке, но только среди значений, начинающихся не с латинского или кириллического символа
|
||||
</li>
|
||||
<li>
|
||||
префикс "~": поиск по регулярному выражению
|
||||
</li>
|
||||
<li>
|
||||
префикс "?": поиск пустых значений или тех, что начинаются с этого символа
|
||||
</li>
|
||||
</ul>
|
||||
`;
|
||||
const entry = [
|
||||
this.makeEntry({
|
||||
id: 'help',
|
||||
title: this.title,
|
||||
content: {
|
||||
'*ATTRS': {type: 'text/html'},
|
||||
'*TEXT': this.escape(content),
|
||||
},
|
||||
link: [
|
||||
this.downLink({href: '/book/fake-link', type: `application/fb2+zip`})
|
||||
],
|
||||
})
|
||||
];
|
||||
|
||||
result.entry = entry;
|
||||
|
||||
return this.makeBody(result, req);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = SearchHelpPage;
|
||||
128
server/core/opds/SearchPage.js
Normal file
128
server/core/opds/SearchPage.js
Normal file
@@ -0,0 +1,128 @@
|
||||
const BasePage = require('./BasePage');
|
||||
const utils = require('../utils');
|
||||
|
||||
class SearchPage extends BasePage {
|
||||
constructor(config) {
|
||||
super(config);
|
||||
|
||||
this.id = 'search';
|
||||
this.title = 'Поиск';
|
||||
}
|
||||
|
||||
async body(req) {
|
||||
const result = {};
|
||||
|
||||
const query = {
|
||||
type: req.query.type || '',
|
||||
term: req.query.term || '',
|
||||
genre: req.query.genre || '',
|
||||
page: parseInt(req.query.page, 10) || 1,
|
||||
};
|
||||
|
||||
let entry = [];
|
||||
if (query.type) {
|
||||
if (['author', 'series', 'title'].includes(query.type)) {
|
||||
try {
|
||||
const from = query.type;
|
||||
const page = query.page;
|
||||
|
||||
const limit = 100;
|
||||
const offset = (page - 1)*limit;
|
||||
const queryRes = await this.webWorker.search(from, {[from]: query.term, genre: query.genre, del: '0', offset, limit});
|
||||
|
||||
const found = queryRes.found;
|
||||
|
||||
for (let i = 0; i < found.length; i++) {
|
||||
const row = found[i];
|
||||
if (!row.bookCount)
|
||||
continue;
|
||||
|
||||
entry.push(
|
||||
this.makeEntry({
|
||||
id: row.id,
|
||||
title: `${(from === 'series' ? 'Серия: ': '')}${from === 'author' ? this.bookAuthor(row[from]) : row[from]}`,
|
||||
link: this.navLink({href: `/${from}?${from}==${encodeURIComponent(row[from])}`}),
|
||||
content: {
|
||||
'*ATTRS': {type: 'text'},
|
||||
'*TEXT': `${row.bookCount} книг${utils.wordEnding(row.bookCount, 8)}`,
|
||||
},
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
if (queryRes.totalFound > offset + found.length) {
|
||||
entry.push(
|
||||
this.makeEntry({
|
||||
id: 'next_page',
|
||||
title: '[Следующая страница]',
|
||||
link: this.navLink({href: `/${this.id}?type=${from}&term=${encodeURIComponent(query.term)}&genre=${encodeURIComponent(query.genre)}&page=${page + 1}`}),
|
||||
})
|
||||
);
|
||||
}
|
||||
} catch(e) {
|
||||
entry.push(
|
||||
this.makeEntry({
|
||||
id: 'error',
|
||||
title: `Ошибка: ${e.message}`,
|
||||
link: this.navLink({href: `/fake-error-link`}),
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
//корневой раздел
|
||||
entry = [
|
||||
this.makeEntry({
|
||||
id: 'search_author',
|
||||
title: 'Поиск авторов',
|
||||
link: this.navLink({href: `/${this.id}?type=author&term=${encodeURIComponent(query.term)}`}),
|
||||
content: {
|
||||
'*ATTRS': {type: 'text'},
|
||||
'*TEXT': `Искать по именам авторов`,
|
||||
},
|
||||
}),
|
||||
this.makeEntry({
|
||||
id: 'search_series',
|
||||
title: 'Поиск серий',
|
||||
link: this.navLink({href: `/${this.id}?type=series&term=${encodeURIComponent(query.term)}`}),
|
||||
content: {
|
||||
'*ATTRS': {type: 'text'},
|
||||
'*TEXT': `Искать по названиям серий`,
|
||||
},
|
||||
}),
|
||||
this.makeEntry({
|
||||
id: 'search_title',
|
||||
title: 'Поиск книг',
|
||||
link: this.navLink({href: `/${this.id}?type=title&term=${encodeURIComponent(query.term)}`}),
|
||||
content: {
|
||||
'*ATTRS': {type: 'text'},
|
||||
'*TEXT': `Искать по названиям книг`,
|
||||
},
|
||||
}),
|
||||
this.makeEntry({
|
||||
id: 'search_genre',
|
||||
title: 'Поиск книг в жанре',
|
||||
link: this.navLink({href: `/genre?from=search&term=${encodeURIComponent(query.term)}`}),
|
||||
content: {
|
||||
'*ATTRS': {type: 'text'},
|
||||
'*TEXT': `Искать по названиям книг в выбранном жанре`,
|
||||
},
|
||||
}),
|
||||
this.makeEntry({
|
||||
id: 'search_help',
|
||||
title: '[Памятка по поиску]',
|
||||
link: this.acqLink({href: `/search-help`}),
|
||||
content: {
|
||||
'*ATTRS': {type: 'text'},
|
||||
'*TEXT': `Описание формата поискового значения`,
|
||||
},
|
||||
}),
|
||||
]
|
||||
}
|
||||
|
||||
result.entry = entry;
|
||||
return this.makeBody(result, req);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = SearchPage;
|
||||
123
server/core/opds/SeriesPage.js
Normal file
123
server/core/opds/SeriesPage.js
Normal file
@@ -0,0 +1,123 @@
|
||||
const BasePage = require('./BasePage');
|
||||
const utils = require('../utils');
|
||||
|
||||
class SeriesPage extends BasePage {
|
||||
constructor(config) {
|
||||
super(config);
|
||||
|
||||
this.id = 'series';
|
||||
this.title = 'Серии';
|
||||
}
|
||||
|
||||
sortSeriesBooks(seriesBooks) {
|
||||
seriesBooks.sort((a, b) => {
|
||||
const dserno = (a.serno || Number.MAX_VALUE) - (b.serno || Number.MAX_VALUE);
|
||||
const dtitle = a.title.localeCompare(b.title);
|
||||
const dext = a.ext.localeCompare(b.ext);
|
||||
return (dserno ? dserno : (dtitle ? dtitle : dext));
|
||||
});
|
||||
|
||||
return seriesBooks;
|
||||
}
|
||||
|
||||
async body(req) {
|
||||
const result = {};
|
||||
|
||||
const query = {
|
||||
series: req.query.series || '',
|
||||
genre: req.query.genre || '',
|
||||
del: '0',
|
||||
|
||||
all: req.query.all || '',
|
||||
depth: 0,
|
||||
};
|
||||
query.depth = query.series.length + 1;
|
||||
|
||||
if (query.series == '___others') {
|
||||
query.series = '';
|
||||
query.depth = 1;
|
||||
query.others = true;
|
||||
}
|
||||
|
||||
const entry = [];
|
||||
if (query.series && query.series[0] == '=') {
|
||||
//книги по серии
|
||||
const bookList = await this.webWorker.getSeriesBookList(query.series.substring(1));
|
||||
|
||||
if (bookList.books) {
|
||||
let books = bookList.books;
|
||||
const booksAll = this.filterBooks(books);
|
||||
const filtered = (query.all ? booksAll : this.filterBooks(books, query));
|
||||
const sorted = this.sortSeriesBooks(filtered);
|
||||
|
||||
if (booksAll.length > filtered.length) {
|
||||
entry.push(
|
||||
this.makeEntry({
|
||||
id: 'all_series_books',
|
||||
title: '[Все книги серии]',
|
||||
link: this.navLink({
|
||||
href: `/${this.id}?series=${encodeURIComponent(query.series)}&all=1`}),
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
for (const book of sorted) {
|
||||
const title = `${book.serno ? `${book.serno}. `: ''}${book.title || 'Без названия'} (${book.ext})`;
|
||||
|
||||
entry.push(
|
||||
this.makeEntry({
|
||||
id: book._uid,
|
||||
title,
|
||||
link: this.acqLink({href: `/book?uid=${encodeURIComponent(book._uid)}`}),
|
||||
content: {
|
||||
'*ATTRS': {type: 'text'},
|
||||
'*TEXT': this.bookAuthor(book.author),
|
||||
},
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (query.depth == 1 && !query.genre && !query.others) {
|
||||
entry.push(
|
||||
this.makeEntry({
|
||||
id: 'select_genre',
|
||||
title: '[Выбрать жанр]',
|
||||
link: this.navLink({href: `/genre?from=${this.id}`}),
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
//навигация по каталогу
|
||||
const queryRes = await this.opdsQuery('series', query, '[Остальные серии]');
|
||||
|
||||
for (const rec of queryRes) {
|
||||
const e = {
|
||||
id: rec.id,
|
||||
title: (rec.count ? rec.title : `Серия: ${rec.title}`),
|
||||
link: this.navLink({href: `/${this.id}?series=${rec.q}&genre=${encodeURIComponent(query.genre)}`}),
|
||||
};
|
||||
|
||||
let countStr = '';
|
||||
if (rec.count)
|
||||
countStr = `${rec.count} сери${utils.wordEnding(rec.count, 1)}${(query.genre ? ' (в выбранном жанре)' : '')}`;
|
||||
if (!countStr && rec.bookCount && !query.genre)
|
||||
countStr = `${rec.bookCount} книг${utils.wordEnding(rec.bookCount, 8)}`;
|
||||
|
||||
if (countStr) {
|
||||
e.content = {
|
||||
'*ATTRS': {type: 'text'},
|
||||
'*TEXT': countStr,
|
||||
};
|
||||
}
|
||||
|
||||
entry.push(this.makeEntry(e));
|
||||
}
|
||||
}
|
||||
|
||||
result.entry = entry;
|
||||
return this.makeBody(result, req);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = SeriesPage;
|
||||
98
server/core/opds/TitlePage.js
Normal file
98
server/core/opds/TitlePage.js
Normal file
@@ -0,0 +1,98 @@
|
||||
const BasePage = require('./BasePage');
|
||||
const utils = require('../utils');
|
||||
|
||||
class TitlePage extends BasePage {
|
||||
constructor(config) {
|
||||
super(config);
|
||||
|
||||
this.id = 'title';
|
||||
this.title = 'Книги';
|
||||
}
|
||||
|
||||
async body(req) {
|
||||
const result = {};
|
||||
|
||||
const query = {
|
||||
title: req.query.title || '',
|
||||
genre: req.query.genre || '',
|
||||
del: '0',
|
||||
|
||||
depth: 0,
|
||||
};
|
||||
query.depth = query.title.length + 1;
|
||||
|
||||
if (query.title == '___others') {
|
||||
query.title = '';
|
||||
query.depth = 1;
|
||||
query.others = true;
|
||||
}
|
||||
|
||||
const entry = [];
|
||||
if (query.title && query.title[0] == '=') {
|
||||
//книги по названию
|
||||
const res = await this.webWorker.search('title', query);
|
||||
|
||||
if (res.found.length) {
|
||||
const books = res.found[0].books || [];
|
||||
const filtered = this.filterBooks(books, query);
|
||||
|
||||
for (const book of filtered) {
|
||||
const title = `${book.serno ? `${book.serno}. `: ''}${book.title || 'Без названия'} (${book.ext})`;
|
||||
|
||||
entry.push(
|
||||
this.makeEntry({
|
||||
id: book._uid,
|
||||
title,
|
||||
link: this.acqLink({href: `/book?uid=${encodeURIComponent(book._uid)}`}),
|
||||
content: {
|
||||
'*ATTRS': {type: 'text'},
|
||||
'*TEXT': this.bookAuthor(book.author),
|
||||
},
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (query.depth == 1 && !query.genre && !query.others) {
|
||||
entry.push(
|
||||
this.makeEntry({
|
||||
id: 'select_genre',
|
||||
title: '[Выбрать жанр]',
|
||||
link: this.navLink({href: `/genre?from=${this.id}`}),
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
//навигация по каталогу
|
||||
const queryRes = await this.opdsQuery('title', query, '[Остальные названия]');
|
||||
|
||||
for (const rec of queryRes) {
|
||||
const e = {
|
||||
id: rec.id,
|
||||
title: rec.title,
|
||||
link: this.navLink({href: `/${this.id}?title=${rec.q}&genre=${encodeURIComponent(query.genre)}`}),
|
||||
};
|
||||
|
||||
let countStr = '';
|
||||
if (rec.count)
|
||||
countStr = `${rec.count} назван${utils.wordEnding(rec.count, 3)}${(query.genre ? ' (в выбранном жанре)' : '')}`;
|
||||
if (!countStr && rec.bookCount && !query.genre)
|
||||
countStr = `${rec.bookCount} книг${utils.wordEnding(rec.bookCount, 8)}`;
|
||||
|
||||
if (countStr) {
|
||||
e.content = {
|
||||
'*ATTRS': {type: 'text'},
|
||||
'*TEXT': countStr,
|
||||
};
|
||||
}
|
||||
|
||||
entry.push(this.makeEntry(e));
|
||||
}
|
||||
}
|
||||
|
||||
result.entry = entry;
|
||||
return this.makeBody(result, req);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = TitlePage;
|
||||
85
server/core/opds/index.js
Normal file
85
server/core/opds/index.js
Normal file
@@ -0,0 +1,85 @@
|
||||
const basicAuth = require('express-basic-auth');
|
||||
|
||||
const RootPage = require('./RootPage');
|
||||
const AuthorPage = require('./AuthorPage');
|
||||
const SeriesPage = require('./SeriesPage');
|
||||
const TitlePage = require('./TitlePage');
|
||||
const GenrePage = require('./GenrePage');
|
||||
const BookPage = require('./BookPage');
|
||||
|
||||
const OpensearchPage = require('./OpensearchPage');
|
||||
const SearchPage = require('./SearchPage');
|
||||
const SearchHelpPage = require('./SearchHelpPage');
|
||||
|
||||
const log = new (require('../AppLogger'))().log;//singleton
|
||||
|
||||
module.exports = function(app, config) {
|
||||
if (!config.opds || !config.opds.enabled)
|
||||
return;
|
||||
|
||||
const opdsRoot = config.opds.root || '/opds';
|
||||
config.opdsRoot = opdsRoot;
|
||||
|
||||
const root = new RootPage(config);
|
||||
const author = new AuthorPage(config);
|
||||
const series = new SeriesPage(config);
|
||||
const title = new TitlePage(config);
|
||||
const genre = new GenrePage(config);
|
||||
const book = new BookPage(config);
|
||||
|
||||
const opensearch = new OpensearchPage(config);
|
||||
const search = new SearchPage(config);
|
||||
const searchHelp = new SearchHelpPage(config);
|
||||
|
||||
const routes = [
|
||||
['', root],
|
||||
['/root', root],
|
||||
['/author', author],
|
||||
['/series', series],
|
||||
['/title', title],
|
||||
['/genre', genre],
|
||||
['/book', book],
|
||||
|
||||
['/opensearch', opensearch],
|
||||
['/search', search],
|
||||
['/search-help', searchHelp],
|
||||
];
|
||||
|
||||
const pages = new Map();
|
||||
for (const r of routes) {
|
||||
pages.set(`${opdsRoot}${r[0]}`, r[1]);
|
||||
}
|
||||
|
||||
const opds = async(req, res, next) => {
|
||||
try {
|
||||
const page = pages.get(req.path);
|
||||
|
||||
if (page) {
|
||||
res.set('Content-Type', 'application/atom+xml; charset=utf-8');
|
||||
|
||||
const result = await page.body(req, res);
|
||||
|
||||
if (result !== false)
|
||||
res.send(result);
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
} catch (e) {
|
||||
log(LM_ERR, `OPDS: ${e.message}, url: ${req.originalUrl}`);
|
||||
res.status(500).send({error: e.message});
|
||||
}
|
||||
};
|
||||
|
||||
const opdsPaths = [opdsRoot, `${opdsRoot}/*`];
|
||||
if (config.opds.password) {
|
||||
if (!config.opds.user)
|
||||
throw new Error('User must not be empty if password set');
|
||||
|
||||
app.use(opdsPaths, basicAuth({
|
||||
users: {[config.opds.user]: config.opds.password},
|
||||
challenge: true,
|
||||
}));
|
||||
}
|
||||
app.get(opdsPaths, opds);
|
||||
};
|
||||
|
||||
@@ -12,7 +12,7 @@ function processLoop() {
|
||||
}
|
||||
|
||||
function versionText(config) {
|
||||
return `${config.name} v${config.version}, Node.js ${process.version}`;
|
||||
return `${config.name} v${config.version}, Node.js ${process.version}, ${process.platform}`;
|
||||
}
|
||||
|
||||
async function findFiles(callback, dir, recursive = true) {
|
||||
@@ -109,9 +109,10 @@ function gzipFile(inputFile, outputFile, level = 1) {
|
||||
.pipe(gzip).on('error', reject)
|
||||
.pipe(output).on('error', reject)
|
||||
.on('finish', (err) => {
|
||||
if (err) reject(err);
|
||||
else resolve();
|
||||
});
|
||||
if (err) reject(err);
|
||||
else resolve();
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -125,9 +126,10 @@ function gunzipFile(inputFile, outputFile) {
|
||||
.pipe(gzip).on('error', reject)
|
||||
.pipe(output).on('error', reject)
|
||||
.on('finish', (err) => {
|
||||
if (err) reject(err);
|
||||
else resolve();
|
||||
});
|
||||
if (err) reject(err);
|
||||
else resolve();
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -174,6 +176,26 @@ function makeValidFileNameOrEmpty(fileName) {
|
||||
}
|
||||
}
|
||||
|
||||
function wordEnding(num, type = 0) {
|
||||
const endings = [
|
||||
['ов', '', 'а', 'а', 'а', 'ов', 'ов', 'ов', 'ов', 'ов'],//0
|
||||
['й', 'я', 'и', 'и', 'и', 'й', 'й', 'й', 'й', 'й'],//1
|
||||
['о', '', 'о', 'о', 'о', 'о', 'о', 'о', 'о', 'о'],//2
|
||||
['ий', 'ие', 'ия', 'ия', 'ия', 'ий', 'ий', 'ий', 'ий', 'ий'],//3
|
||||
['о', 'а', 'о', 'о', 'о', 'о', 'о', 'о', 'о', 'о'],//4
|
||||
['ок', 'ка', 'ки', 'ки', 'ки', 'ок', 'ок', 'ок', 'ок', 'ок'],//5
|
||||
['ых', 'ое', 'ых', 'ых', 'ых', 'ых', 'ых', 'ых', 'ых', 'ых'],//6
|
||||
['о', 'о', 'о', 'о', 'о', 'о', 'о', 'о', 'о', 'о'],//7
|
||||
['', 'а', 'и', 'и', 'и', '', '', '', '', ''],//8
|
||||
];
|
||||
const deci = num % 100;
|
||||
if (deci > 10 && deci < 20) {
|
||||
return endings[type][0];
|
||||
} else {
|
||||
return endings[type][num % 10];
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
sleep,
|
||||
processLoop,
|
||||
@@ -193,4 +215,5 @@ module.exports = {
|
||||
toUnixPath,
|
||||
makeValidFileName,
|
||||
makeValidFileNameOrEmpty,
|
||||
wordEnding,
|
||||
};
|
||||
@@ -734,8 +734,8 @@ class XmlParser extends NodeBase {
|
||||
} else if (tag === '*ATTRS') {
|
||||
//пропускаем
|
||||
} else {
|
||||
if (typeof(objNode) === 'string') {
|
||||
result.push(this.createNode(tag, null, [this.createText(objNode).raw]).raw);
|
||||
if (typeof(objNode) === 'string' || typeof(objNode) === 'number') {
|
||||
result.push(this.createNode(tag, null, [this.createText(objNode.toString()).raw]).raw);
|
||||
} else if (Array.isArray(objNode)) {
|
||||
for (const n of objNode) {
|
||||
if (typeof(n) === 'string') {
|
||||
@@ -747,6 +747,8 @@ class XmlParser extends NodeBase {
|
||||
|
||||
} else if (typeof(objNode) === 'object') {
|
||||
result.push(this.createNode(tag, (objNode['*ATTRS'] ? Object.entries(objNode['*ATTRS']) : null), objectToNodes(objNode)).raw);
|
||||
} else {
|
||||
throw new Error(`Unknown node type "${typeof(objNode)}" of node: ${objNode}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ module.exports = async(config) => {
|
||||
|
||||
if (await fs.pathExists(verFile)) {
|
||||
const curPublicVersion = await fs.readFile(verFile, 'utf8');
|
||||
if (curPublicVersion == config.version)
|
||||
if (curPublicVersion == config.version + config.rootPathStatic)
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -26,6 +26,6 @@ module.exports = async(config) => {
|
||||
await zipReader.close();
|
||||
}
|
||||
|
||||
await fs.writeFile(verFile, config.version);
|
||||
await fs.writeFile(verFile, config.version + config.rootPathStatic);
|
||||
await fs.remove(zipFile);
|
||||
};
|
||||
@@ -16,16 +16,16 @@ let branch = '';
|
||||
const argvStrings = ['host', 'port', 'app-dir', 'lib-dir', 'inpx'];
|
||||
|
||||
function showHelp(defaultConfig) {
|
||||
console.log(utils.versionText(config));
|
||||
console.log(utils.versionText(defaultConfig));
|
||||
console.log(
|
||||
`Usage: ${config.name} [options]
|
||||
`Usage: ${defaultConfig.name} [options]
|
||||
|
||||
Options:
|
||||
--help Print ${config.name} command line options
|
||||
--help Print ${defaultConfig.name} command line options
|
||||
--host=<ip> Set web server host, default: ${defaultConfig.server.host}
|
||||
--port=<port> Set web server port, default: ${defaultConfig.server.port}
|
||||
--app-dir=<dirpath> Set application working directory, default: <execDir>/.${config.name}
|
||||
--lib-dir=<dirpath> Set library directory, default: the same as ${config.name} executable's
|
||||
--app-dir=<dirpath> Set application working directory, default: <execDir>/.${defaultConfig.name}
|
||||
--lib-dir=<dirpath> Set library directory, default: the same as ${defaultConfig.name} executable's
|
||||
--inpx=<filepath> Set INPX collection file, default: the one that found in library dir
|
||||
--recreate Force recreation of the search database on start
|
||||
`
|
||||
@@ -50,13 +50,14 @@ async function init() {
|
||||
config.logDir = `${config.dataDir}/log`;
|
||||
config.publicDir = `${config.dataDir}/public`;
|
||||
config.publicFilesDir = `${config.dataDir}/public-files`;
|
||||
config.filesPathStatic = `/book`;
|
||||
config.filesDir = `${config.publicFilesDir}${config.filesPathStatic}`;
|
||||
config.rootPathStatic = config.server.root || '';
|
||||
config.bookPathStatic = `${config.rootPathStatic}/book`;
|
||||
config.bookDir = `${config.publicFilesDir}/book`;
|
||||
|
||||
configManager.config = config;
|
||||
|
||||
await fs.ensureDir(config.dataDir);
|
||||
await fs.ensureDir(config.filesDir);
|
||||
await fs.ensureDir(config.bookDir);
|
||||
await fs.ensureDir(config.tempDir);
|
||||
await fs.emptyDir(config.tempDir);
|
||||
|
||||
@@ -83,7 +84,7 @@ async function init() {
|
||||
}
|
||||
|
||||
if (!config.remoteLib) {
|
||||
const libDir = argv['lib-dir'];
|
||||
const libDir = argv['lib-dir'] || config.libDir;
|
||||
if (libDir) {
|
||||
if (await fs.pathExists(libDir)) {
|
||||
config.libDir = libDir;
|
||||
@@ -94,11 +95,12 @@ async function init() {
|
||||
config.libDir = config.execDir;
|
||||
}
|
||||
|
||||
if (argv.inpx) {
|
||||
if (await fs.pathExists(argv.inpx)) {
|
||||
config.inpxFile = argv.inpx;
|
||||
const inpxFile = argv.inpx || config.inpx;
|
||||
if (inpxFile) {
|
||||
if (await fs.pathExists(inpxFile)) {
|
||||
config.inpxFile = inpxFile;
|
||||
} else {
|
||||
throw new Error(`File "${argv.inpx}" not found`);
|
||||
throw new Error(`File "${inpxFile}" not found`);
|
||||
}
|
||||
} else {
|
||||
const inpxFiles = [];
|
||||
@@ -154,10 +156,17 @@ async function main() {
|
||||
if (devModule)
|
||||
devModule.logQueries(app);
|
||||
|
||||
const opds = require('./core/opds');
|
||||
opds(app, config);
|
||||
|
||||
const initStatic = require('./static');
|
||||
initStatic(app, config);
|
||||
|
||||
const webAccess = new (require('./core/WebAccess'))(config);
|
||||
await webAccess.init();
|
||||
|
||||
const { WebSocketController } = require('./controllers');
|
||||
new WebSocketController(wss, config);
|
||||
new WebSocketController(wss, webAccess, config);
|
||||
|
||||
if (devModule) {
|
||||
devModule.logErrors(app);
|
||||
@@ -174,63 +183,6 @@ async function main() {
|
||||
});
|
||||
}
|
||||
|
||||
function initStatic(app, config) {
|
||||
/*
|
||||
publicFilesDir = `${config.dataDir}/public-files`;
|
||||
filesPathStatic = `/book`;
|
||||
filesDir = `${config.publicFilesDir}${config.filesPathStatic}`;
|
||||
*/
|
||||
const filesPath = `${config.filesPathStatic}/`;
|
||||
//загрузка или восстановление файлов в /files, при необходимости
|
||||
app.use(async(req, res, next) => {
|
||||
if ((req.method !== 'GET' && req.method !== 'HEAD') ||
|
||||
!(req.path.indexOf(filesPath) === 0)
|
||||
) {
|
||||
return next();
|
||||
}
|
||||
|
||||
if (path.extname(req.path) == '') {
|
||||
const bookFile = `${config.publicFilesDir}${req.path}`;
|
||||
const bookFileDesc = `${bookFile}.d.json`;
|
||||
|
||||
let downFileName = '';
|
||||
//восстановим из json-файла описания
|
||||
try {
|
||||
if (await fs.pathExists(bookFile) && await fs.pathExists(bookFileDesc)) {
|
||||
await utils.touchFile(bookFile);
|
||||
await utils.touchFile(bookFileDesc);
|
||||
|
||||
let desc = await fs.readFile(bookFileDesc, 'utf8');
|
||||
desc = JSON.parse(desc);
|
||||
downFileName = desc.downFileName;
|
||||
} else {
|
||||
await fs.remove(bookFile);
|
||||
await fs.remove(bookFileDesc);
|
||||
}
|
||||
} catch(e) {
|
||||
log(LM_ERR, e.message);
|
||||
}
|
||||
|
||||
if (downFileName)
|
||||
res.downFileName = downFileName;
|
||||
}
|
||||
|
||||
return next();
|
||||
});
|
||||
|
||||
//заголовки при отдаче
|
||||
app.use(config.filesPathStatic, express.static(config.filesDir, {
|
||||
setHeaders: (res) => {
|
||||
if (res.downFileName) {
|
||||
res.set('Content-Encoding', 'gzip');
|
||||
res.set('Content-Disposition', `inline; filename*=UTF-8''${encodeURIComponent(res.downFileName)}`);
|
||||
}
|
||||
},
|
||||
}));
|
||||
|
||||
app.use(express.static(config.publicDir));
|
||||
}
|
||||
|
||||
(async() => {
|
||||
try {
|
||||
await init();
|
||||
|
||||
127
server/static.js
Normal file
127
server/static.js
Normal file
@@ -0,0 +1,127 @@
|
||||
const fs = require('fs-extra');
|
||||
const path = require('path');
|
||||
const yazl = require('yazl');
|
||||
|
||||
const express = require('express');
|
||||
const utils = require('./core/utils');
|
||||
const webAppDir = require('../build/appdir');
|
||||
|
||||
const log = new (require('./core/AppLogger'))().log;//singleton
|
||||
|
||||
function generateZip(zipFile, dataFile, dataFileInZip) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const zip = new yazl.ZipFile();
|
||||
zip.addFile(dataFile, dataFileInZip);
|
||||
zip.outputStream
|
||||
.pipe(fs.createWriteStream(zipFile)).on('error', reject)
|
||||
.on('finish', (err) => {
|
||||
if (err) reject(err);
|
||||
else resolve();
|
||||
}
|
||||
);
|
||||
zip.end();
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = (app, config) => {
|
||||
/*
|
||||
config.bookPathStatic = `${config.rootPathStatic}/book`;
|
||||
config.bookDir = `${config.publicFilesDir}/book`;
|
||||
*/
|
||||
//загрузка или восстановление файлов в /public-files, при необходимости
|
||||
app.use([`${config.bookPathStatic}/:fileName/:fileType`, `${config.bookPathStatic}/:fileName`], async(req, res, next) => {
|
||||
if (req.method !== 'GET' && req.method !== 'HEAD') {
|
||||
return next();
|
||||
}
|
||||
|
||||
try {
|
||||
const fileName = req.params.fileName;
|
||||
const fileType = req.params.fileType;
|
||||
|
||||
if (path.extname(fileName) === '') {//восстановление файлов {hash}.raw, {hash}.zip
|
||||
let bookFile = `${config.bookDir}/${fileName}`;
|
||||
const bookFileDesc = `${bookFile}.d.json`;
|
||||
|
||||
//восстановим из json-файла описания
|
||||
if (await fs.pathExists(bookFile) && await fs.pathExists(bookFileDesc)) {
|
||||
await utils.touchFile(bookFile);
|
||||
await utils.touchFile(bookFileDesc);
|
||||
|
||||
let desc = await fs.readFile(bookFileDesc, 'utf8');
|
||||
let downFileName = (JSON.parse(desc)).downFileName;
|
||||
let gzipped = true;
|
||||
|
||||
if (!req.acceptsEncodings('gzip') || fileType) {
|
||||
const rawFile = `${bookFile}.raw`;
|
||||
//не принимает gzip, тогда распакуем
|
||||
if (!await fs.pathExists(rawFile))
|
||||
await utils.gunzipFile(bookFile, rawFile);
|
||||
|
||||
gzipped = false;
|
||||
|
||||
if (fileType === undefined || fileType === 'raw') {
|
||||
bookFile = rawFile;
|
||||
} else if (fileType === 'zip') {
|
||||
//создаем zip-файл
|
||||
bookFile += '.zip';
|
||||
if (!await fs.pathExists(bookFile))
|
||||
await generateZip(bookFile, rawFile, downFileName);
|
||||
downFileName += '.zip';
|
||||
} else {
|
||||
throw new Error(`Unsupported file type: ${fileType}`);
|
||||
}
|
||||
}
|
||||
|
||||
//отдача файла
|
||||
if (gzipped)
|
||||
res.set('Content-Encoding', 'gzip');
|
||||
res.set('Content-Disposition', `inline; filename*=UTF-8''${encodeURIComponent(downFileName)}`);
|
||||
res.sendFile(bookFile);
|
||||
return;
|
||||
} else {
|
||||
await fs.remove(bookFile);
|
||||
await fs.remove(bookFileDesc);
|
||||
}
|
||||
}
|
||||
} catch(e) {
|
||||
log(LM_ERR, e.message);
|
||||
}
|
||||
|
||||
return next();
|
||||
});
|
||||
|
||||
//иначе просто отдаем запрошенный файл из /public-files
|
||||
app.use(config.bookPathStatic, express.static(config.bookDir));
|
||||
|
||||
if (config.rootPathStatic) {
|
||||
//подмена rootPath в файлах статики WebApp при необходимости
|
||||
app.use(config.rootPathStatic, async(req, res, next) => {
|
||||
if (req.method !== 'GET' && req.method !== 'HEAD') {
|
||||
return next();
|
||||
}
|
||||
|
||||
try {
|
||||
const reqPath = (req.path == '/' ? '/index.html' : req.path);
|
||||
const ext = path.extname(reqPath);
|
||||
if (ext == '.html' || ext == '.js' || ext == '.css') {
|
||||
const reqFile = `${config.publicDir}${reqPath}`;
|
||||
const flagFile = `${reqFile}.replaced`;
|
||||
|
||||
if (!await fs.pathExists(flagFile) && await fs.pathExists(reqFile)) {
|
||||
const content = await fs.readFile(reqFile, 'utf8');
|
||||
const re = new RegExp(`/${webAppDir}`, 'g');
|
||||
await fs.writeFile(reqFile, content.replace(re, `${config.rootPathStatic}/${webAppDir}`));
|
||||
await fs.writeFile(flagFile, '');
|
||||
}
|
||||
}
|
||||
} catch(e) {
|
||||
log(LM_ERR, e.message);
|
||||
}
|
||||
|
||||
return next();
|
||||
});
|
||||
}
|
||||
|
||||
//статика файлов WebApp
|
||||
app.use(config.rootPathStatic, express.static(config.publicDir));
|
||||
};
|
||||
Reference in New Issue
Block a user