136 Commits
1.1.2 ... 1.3.2

Author SHA1 Message Date
Book Pauk
d71c235ebc Merge branch 'release/1.3.2' 2022-11-27 21:05:09 +07:00
Book Pauk
49583d3407 Поправки багов 2022-11-27 21:03:12 +07:00
Book Pauk
a7f71562b4 Версия 1.3.2 2022-11-27 20:29:13 +07:00
Book Pauk
f93269ac8b К предыдущему 2022-11-27 20:25:56 +07:00
Book Pauk
79f85f362a Добавлено отображение количества книг в серии (раздел "Авторы") 2022-11-27 20:23:43 +07:00
Book Pauk
bf9ae0b9e1 Работа с токенами сессий вынесена в отдельный класс 2022-11-27 19:57:35 +07:00
Book Pauk
678562525f Добавлено описание параметра accessTimeout 2022-11-27 19:43:27 +07:00
Book Pauk
abb3baf94b Изменения механизма ограничения доступа 2022-11-27 18:34:01 +07:00
Book Pauk
59b4f48897 Изменения механизма ограничения доступа 2022-11-27 18:12:59 +07:00
Book Pauk
dca59e02dd Merge tag '1.3.1' into develop
1.3.1
2022-11-26 01:03:18 +07:00
Book Pauk
9bc92a7346 Merge branch 'release/1.3.1' 2022-11-26 01:03:13 +07:00
Book Pauk
53264da6bd Версия 1.3.1 2022-11-26 01:02:44 +07:00
Book Pauk
53e328ac2d Поправлен versionText 2022-11-26 00:05:35 +07:00
Book Pauk
59c2d73c05 Merge tag '1.3.0' into develop
1.3.0
2022-11-24 21:11:10 +07:00
Book Pauk
fc729218ba Merge branch 'release/1.3.0' 2022-11-24 21:11:05 +07:00
Book Pauk
15778eb3e4 Версия 1.3.0 2022-11-24 20:59:43 +07:00
Book Pauk
ad1a6560fa Добавлены цели для сборки релиза macos 2022-11-24 20:58:33 +07:00
Book Pauk
7fa203eaae Поправлен readme 2022-11-24 20:50:04 +07:00
Book Pauk
74d8cd3f94 Добавлен basic-auth для opds 2022-11-24 20:37:35 +07:00
Book Pauk
fd29532cf1 + "express-basic-auth": "^1.2.1" 2022-11-24 20:14:07 +07:00
Book Pauk
1dc169d14b Работа над opds 2022-11-24 19:52:51 +07:00
Book Pauk
870f95a51f Работа над opds 2022-11-24 19:08:34 +07:00
Book Pauk
72ab94291c Рефакторинг 2022-11-24 18:29:14 +07:00
Book Pauk
8de33fbd9a Работа над opds 2022-11-24 17:50:35 +07:00
Book Pauk
fd9bc45fb1 Работа над opds 2022-11-24 17:20:11 +07:00
Book Pauk
e356b87494 Работа над opds 2022-11-24 16:54:56 +07:00
Book Pauk
4371e1a641 Работа над opds 2022-11-24 16:36:40 +07:00
Book Pauk
95da605cb9 Рефакторинг 2022-11-24 16:04:27 +07:00
Book Pauk
6dfa551b97 Работа над opds 2022-11-23 20:57:41 +07:00
Book Pauk
6a3b919f5f Работа над opds 2022-11-23 19:17:08 +07:00
Book Pauk
cac8e7c721 Поправки багов 2022-11-23 18:08:31 +07:00
Book Pauk
410aa01ac9 Поправки багов 2022-11-23 17:31:47 +07:00
Book Pauk
a8ed8b29e5 Работа над opds 2022-11-23 17:03:33 +07:00
Book Pauk
5a04e4f0c7 Работа над opds 2022-11-23 14:59:29 +07:00
Book Pauk
a6d9df7dec Работа над opds 2022-11-23 14:38:23 +07:00
Book Pauk
8cf370c79d Работа над opds 2022-11-23 01:21:29 +07:00
Book Pauk
35925dbc6e Работа над opds 2022-11-22 20:09:00 +07:00
Book Pauk
d0e79b0abb + "he": "^1.2.0" 2022-11-22 19:55:54 +07:00
Book Pauk
aba0c206f8 Работа над opds 2022-11-20 19:52:10 +07:00
Book Pauk
037b42a5b4 Работа над opds 2022-11-20 19:22:54 +07:00
Book Pauk
8a71c4040c Начата работа над opds 2022-11-20 17:47:15 +07:00
Book Pauk
e685f136e1 Поправка мелкого бага 2022-11-18 20:45:38 +07:00
Book Pauk
b8b40e8cb0 Поправлен readme 2022-11-17 18:00:05 +07:00
Book Pauk
7e9f446079 В релиз добавлен readme.html 2022-11-17 17:56:46 +07:00
Book Pauk
13c3c98c63 Поправил readme 2022-11-17 17:53:47 +07:00
Book Pauk
1b70259ea7 "showdown": "^2.1.0" 2022-11-17 17:53:28 +07:00
Book Pauk
a840fb7233 Поправка отображения Inpx инфо 2022-11-17 17:08:06 +07:00
Book Pauk
1ba54c1237 Добавлено описание параметров конфига queryCacheMemSize, queryCacheDiskSize 2022-11-17 16:58:09 +07:00
Book Pauk
412335c0f1 В конфиг добавлены параметры queryCacheMemSize, queryCacheDiskSize 2022-11-17 16:51:12 +07:00
Book Pauk
6b91c43655 "jembadb": "^5.1.3" 2022-11-17 14:01:40 +07:00
Book Pauk
4b4865b6ed Переход на Uint32Array 2022-11-16 20:37:18 +07:00
Book Pauk
d5931138e3 Переход на Uint32Array 2022-11-16 20:27:53 +07:00
Book Pauk
3d1385da6e "jembadb": "^5.1.2" 2022-11-16 20:08:28 +07:00
Book Pauk
5630feba36 "jembadb": "^5.1.1" 2022-11-16 19:32:51 +07:00
Book Pauk
64a301eda1 "jembadb": "^5.1.0" 2022-11-16 18:56:46 +07:00
Book Pauk
044ab1ab26 Поправлена обработка ошибок 2022-11-15 00:16:45 +07:00
Book Pauk
d6260e3433 Оптимизация использования памяти при загрузке маппингов 2022-11-15 00:03:10 +07:00
Book Pauk
fb2eb62a98 Merge tag '1.2.4' into develop
1.2.4
2022-11-14 16:39:53 +07:00
Book Pauk
d7c6b0e7ab Merge branch 'release/1.2.4' 2022-11-14 16:39:48 +07:00
Book Pauk
94922f3926 Версия 1.2.4 2022-11-14 16:39:24 +07:00
Book Pauk
a580b1eb6d Добавлено отображение постера в отдельном окне 2022-11-14 16:37:11 +07:00
Book Pauk
cd7b8afb29 Рефакторинг 2022-11-14 15:09:13 +07:00
Book Pauk
e634893ff3 Добавил .stop.prevent для событий @click 2022-11-14 14:46:23 +07:00
Book Pauk
fadc7ddc34 Merge tag '1.2.3' into develop
1.2.3
2022-11-13 02:04:13 +07:00
Book Pauk
ed5dc25d94 Merge branch 'release/1.2.3' 2022-11-13 02:04:01 +07:00
Book Pauk
dd11e8c5ad Версия 1.2.3 2022-11-13 02:03:25 +07:00
Book Pauk
2db2b8cff4 Решение проблемы скачивания файлов в режиме "Удаленная библиотека"
(запрашивался не тот файл из-за несовпадения bookId)
2022-11-13 01:59:42 +07:00
Book Pauk
4d3661b758 Мелкая поправка 2022-11-13 00:11:26 +07:00
Book Pauk
891b1e4fe8 Поправка мелкого бага 2022-11-12 23:56:50 +07:00
Book Pauk
d588b16885 Merge tag '1.2.2' into develop
1.2.2

Исправлен баг при скачивании в режиме "Удаленная библиотека"
2022-11-12 17:24:52 +07:00
Book Pauk
a0e4651607 Merge branch 'release/1.2.2' 2022-11-12 17:24:41 +07:00
Book Pauk
c21b8ffa0e 1.2.2 2022-11-12 17:24:19 +07:00
Book Pauk
f174617f33 Исправлен баг при скачивании в режиме "Удаленная библиотека" 2022-11-12 17:23:21 +07:00
Book Pauk
2de9ad0edf Мелкая поправка разметки 2022-11-12 17:11:24 +07:00
Book Pauk
a6592f2f8d Merge tag '1.2.1' into develop
1.2.1

Добавлено диалоговое окно "Информация о книге".
Небольшие изменения интерфейса, добавлена кнопка "Клонировать поиск".
2022-11-12 16:56:49 +07:00
Book Pauk
b4da07e924 Merge branch 'release/1.2.1' 2022-11-12 16:56:36 +07:00
Book Pauk
110d145b91 Версия 1.2.1 2022-11-12 16:56:05 +07:00
Book Pauk
fc3d391aa0 Замена moment на dayjs 2022-11-12 16:54:03 +07:00
Book Pauk
4d01901463 Мелкие поправки разметки 2022-11-12 16:38:01 +07:00
Book Pauk
2d380bd98f Убрал дебаг 2022-11-11 22:16:13 +07:00
Book Pauk
2dd67487dc Работа над BookInfoDialog 2022-11-11 22:12:13 +07:00
Book Pauk
a3190e4af3 Добавлены методы XML Inspector 2022-11-11 20:31:41 +07:00
Book Pauk
3d28beddac Merge tag '1.2.0' into develop
1.2.0
2022-11-10 20:27:53 +07:00
Book Pauk
c11e949316 Merge branch 'release/1.2.0' 2022-11-10 20:27:45 +07:00
Book Pauk
4bbaf659b8 Небольшая поправка разметки 2022-11-10 20:26:30 +07:00
Book Pauk
caf3adf884 Версия 1.2.0 2022-11-10 20:14:49 +07:00
Book Pauk
6dfb3f6db9 Работа над BookInfoDialog 2022-11-10 20:10:43 +07:00
Book Pauk
e39611098a Работа над BookInfoDialog 2022-11-10 19:45:26 +07:00
Book Pauk
d7d04fcda8 Работа над BookInfoDialog 2022-11-10 18:57:20 +07:00
Book Pauk
7b2171c269 Работа над BookInfoDialog 2022-11-10 18:05:53 +07:00
Book Pauk
0bb434d415 Работа над BookInfoDialog 2022-11-10 17:55:16 +07:00
Book Pauk
391fb3aa70 Работа над BookInfoDialog 2022-11-10 17:17:56 +07:00
Book Pauk
81d8b476a5 Работа над BookInfoDialog 2022-11-10 14:56:25 +07:00
Book Pauk
79e6ca2d27 Работа над BookInfoDialog 2022-11-10 00:59:47 +07:00
Book Pauk
1d99472ca1 Реструктуризация 2022-11-10 00:51:18 +07:00
Book Pauk
ec6b72868b Переименования 2022-11-10 00:40:44 +07:00
Book Pauk
8ee1b98a12 Поправлена отдача статики 2022-11-09 23:58:08 +07:00
Book Pauk
ffc65ab944 Работа над BookInfoDialog 2022-11-09 19:25:59 +07:00
Book Pauk
7b5061df5f Работа над BookInfoDialog 2022-11-09 18:26:29 +07:00
Book Pauk
2fa48cdde6 Поправлен баг 2022-11-09 18:26:19 +07:00
Book Pauk
28963116c3 Работа над XmlParser 2022-11-09 17:22:18 +07:00
Book Pauk
18da23530b Рефакторинг 2022-11-09 16:47:36 +07:00
Book Pauk
b64c5de5a3 Работа над XmlParser 2022-11-09 16:37:10 +07:00
Book Pauk
f1db203027 Перемещение файлов 2022-11-09 14:47:55 +07:00
Book Pauk
927dade502 Работа над Fb2Parser 2022-11-09 14:45:40 +07:00
Book Pauk
04a8ba8426 Работа над XmlParser 2022-11-09 14:44:00 +07:00
Book Pauk
40f72d17e6 Работа над BookInfoDialog 2022-11-08 22:07:11 +07:00
Book Pauk
4b5949e3bc Работа над XmlParser 2022-11-08 22:02:35 +07:00
Book Pauk
f7994fd9e9 Работа над XmlParser 2022-11-08 16:12:46 +07:00
Book Pauk
9cf9530447 Работа над XmlParser 2022-11-08 15:01:52 +07:00
Book Pauk
98fac2bf11 Работа над XmlParser 2022-11-08 14:42:28 +07:00
Book Pauk
e755ddbbef Работа над XmlParser 2022-11-08 14:07:17 +07:00
Book Pauk
b5c7219e09 Работа над XmlParser 2022-11-08 03:52:00 +07:00
Book Pauk
6a640ba2cd Работа над XmlParser 2022-11-08 02:42:11 +07:00
Book Pauk
d2484659e7 Работа над XmlParser 2022-11-08 02:21:34 +07:00
Book Pauk
8f4dec510c Работа над XmlParser 2022-11-07 21:50:38 +07:00
Book Pauk
e4571faf39 Работа над XmlParser 2022-11-07 21:13:15 +07:00
Book Pauk
a40d9e25b0 Работа над XmlParser 2022-11-07 19:52:29 +07:00
Book Pauk
02f276ca6b Работа над карточкой "Информация о книге" 2022-11-07 16:24:52 +07:00
Book Pauk
2ae7f21bc8 Дополнительные пакеты 2022-11-07 16:24:07 +07:00
Book Pauk
55239159ba Перенос на сервер работы с именами файлов при скачивании 2022-11-06 18:03:03 +07:00
Book Pauk
d9f1912ea2 Работа над BookInfoDialog 2022-11-06 17:15:45 +07:00
Book Pauk
351abe9401 Поправка размера шрифта 2022-11-06 16:39:39 +07:00
Book Pauk
4cde00b337 Поправки интерфейса, работа над информацией о файле 2022-11-06 16:38:01 +07:00
Book Pauk
ba5d7b10b8 Поправлено положение элементов интерфейса,
добавлена кнопка "Клонировать поиск"
2022-11-06 15:47:42 +07:00
Book Pauk
0360098b53 Поправлен баг скроллинга 2022-11-06 14:49:27 +07:00
Book Pauk
32c2d6fef9 Merge tag '1.1.4' into develop
1.1.4
2022-11-03 22:32:13 +07:00
Book Pauk
2c3172d2a9 Merge branch 'release/1.1.4' 2022-11-03 22:32:07 +07:00
Book Pauk
f43a0bde45 1.1.4 2022-11-03 22:31:57 +07:00
Book Pauk
0f7ac5c387 Версия БД: '6' 2022-11-03 22:30:50 +07:00
Book Pauk
a4aa4ae2f0 Рефакторинг 2022-11-03 21:50:08 +07:00
Book Pauk
6e9ff3787e Merge tag '1.1.3' into develop
1.1.3
2022-11-03 21:27:14 +07:00
Book Pauk
cd35acb60e Merge branch 'release/1.1.3' 2022-11-03 21:27:08 +07:00
Book Pauk
94bec6ed42 Верия 1.1.3 2022-11-03 21:26:45 +07:00
Book Pauk
42436fabd3 Исправлен баг "Не качает книги #1", fixed #1 2022-11-03 21:25:10 +07:00
Book Pauk
36c50fd699 Merge tag '1.1.2' into develop
1.1.2
2022-11-01 02:13:49 +07:00
47 changed files with 4712 additions and 477 deletions

View File

@@ -2,19 +2,24 @@ 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).
##
* [Возможности программы](#capabilities)
* [Использование](#usage)
* [Параметры командной строки](#cli)
@@ -28,6 +33,7 @@ inpx-web
<a id="capabilities" />
## Возможности программы
- веб-интерфейс и OPDS-сервер
- поиск по автору, серии, названию и пр.
- скачивание книги, копирование ссылки или открытие в читалке
- возможность указать рабочий каталог при запуске, а также расположение .inpx и файлов библиотеки
@@ -36,7 +42,7 @@ inpx-web
- фильтр авторов и книг при создании поисковой БД для создания своей коллекции "на лету"
- подхват изменений .inpx-файла (периодическая проверка), автоматическое пересоздание поисковой БД
- мощная оптимизация, хорошая скорость поиска
- релизы под Linux и Windows
- релизы под Linux, MacOS и Windows
<a id="usage" />
@@ -45,7 +51,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" />
@@ -71,8 +79,14 @@ Options:
```js
{
// пароль для ограничения доступа к веб-интерфейсу сервера
// пустое значение - доступ без ограничений
"accessPassword": "",
// таймаут автозавершения сессии доступа к веб-интерфейсу (если задан accessPassword),
// при неактивности в течение указанного времени (в минутах), пароль будет запрошен заново
// 0 - отключить таймаут, время доступа по паролю не ограничено
"accessTimeout": 0,
// содержимое кнопки-ссылки "(читать)", если не задано - кнопка "(читать)" не показывается
// пример: "https://omnireader.ru/#/reader?url=${DOWNLOAD_LINK}"
// на место ${DOWNLOAD_LINK} будет подставлена ссылка на скачивание файла книги
@@ -89,9 +103,17 @@ Options:
// чистка каждый час
"maxFilesDirSize": 1073741824,
// включить(true)/выключить(false) кеширование запросов на сервере
// включить(true)/выключить(false) серверное кеширование запросов на диске и в памяти
"queryCacheEnabled": true,
// размер кеша запросов в оперативной памяти (количество)
// 0 - отключить кеширование запросов в оперативной памяти
"queryCacheMemSize": 50,
// размер кеша запросов на диске (количество)
// 0 - отключить кеширование запросов на диске
"queryCacheDiskSize": 500,
// периодичность чистки кеша запросов на сервере, в минутах
// 0 - отключить чистку
"cacheCleanInterval": 60,
@@ -121,6 +143,14 @@ Options:
"server": {
"host": "0.0.0.0",
"port": "12380"
},
// настройки opds-сервера
// user, password используются для Basic HTTP authentication
"opds": {
"enabled": true,
"user": "",
"password": ""
}
}
```
@@ -161,7 +191,7 @@ Options:
### Фильтр по авторам и книгам
При создании поисковой БД во время загрузки и парсинга .inpx-файла, имеется возможность
При создании поисковой БД, во время загрузки и парсинга .inpx-файла, имеется возможность
отфильтровать авторов и книги, задав определенные критерии. Для этого небходимо создать
в рабочей директории (там же, где `config.json`) файл `filter.json` следующего вида:
```json
@@ -176,7 +206,7 @@ Options:
"excludeAuthors": ["Имя автора"]
}
```
При создании поисковой БД, авторы и книги из `includeAuthors` будут добавлены, а из `excludeAuthors` исключены.
При фильтрации, авторы и их книги из `includeAuthors` будут оставлены, а из `excludeAuthors` исключены.
Использование совместно `includeAuthors` и `excludeAuthors` имеет мало смысла, поэтому для включения
определенных авторов можно использовать только `includeAuthors`:
```json
@@ -256,17 +286,12 @@ 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" />

View File

@@ -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')
if (platform != 'linux' && platform != 'win' && platform != 'macos')
throw new Error(`Unknown platform: ${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)) {

View File

@@ -22,6 +22,7 @@ async function main() {
await fs.emptyDir(outDir);
await makeRelease('win');
await makeRelease('linux');
await makeRelease('macos');
} catch(e) {
console.error(e);
process.exit(1);

View File

@@ -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);
}
@@ -231,13 +247,22 @@ class Api {
return await this.request({action: 'get-genre-tree'});
}
async getBookLink(params) {
return await this.request(Object.assign({action: 'get-book-link'}, params), 120);
async getBookLink(bookUid) {
return await this.request({action: 'get-book-link', bookUid}, 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'});
await this.request({action: 'test'});
}
}
export default vueComponent(Api);

View File

@@ -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;
@@ -133,6 +134,15 @@ body, html, #app {
animation: rotating 2s linear infinite;
}
.q-dialog__inner--minimized {
padding: 10px !important;
}
.q-dialog__inner--minimized > div {
max-height: 100% !important;
max-width: 800px !important;
}
@keyframes rotating {
from {
transform: rotate(0deg);

View File

@@ -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(book) }}
</div>
</div>
<div v-if="isExpandedSeries(book) && book.seriesBooks">
@@ -184,6 +188,20 @@ class AuthorList extends BaseList {
return `(${result})`;
}
getSeriesBookCount(book) {
let result = '';
if (!this.showCounts || book.type != 'series')
return result;
let count = book.seriesBooks.length;
result = `${count}`;
if (book.allBooksLoaded) {
result += `/${book.allBooksLoaded.length}`;
}
return `(${result})`;
}
async expandAuthor(item) {
this.$emit('listEvent', {action: 'ignoreScroll'});
@@ -238,6 +256,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(
{
@@ -345,7 +370,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);

View File

@@ -1,4 +1,4 @@
import moment from 'moment';
import dayjs from 'dayjs';
import _ from 'lodash';
import authorBooksStorage from './authorBooksStorage';
@@ -129,31 +129,8 @@ export default class BaseList {
})();
try {
const makeValidFilenameOrEmpty = (s) => {
try {
return utils.makeValidFilename(s);
} catch(e) {
return '';
}
};
//имя файла
let downFileName = 'default-name';
const author = book.author.split(',');
const at = [author[0], book.title];
downFileName = makeValidFilenameOrEmpty(at.filter(r => r).join(' - '))
|| makeValidFilenameOrEmpty(at[0])
|| makeValidFilenameOrEmpty(at[1])
|| downFileName;
downFileName = downFileName.substring(0, 100);
const ext = `.${book.ext}`;
if (downFileName.substring(downFileName.length - ext.length) != ext)
downFileName += ext;
const bookPath = `${book.folder}/${book.file}${ext}`;
//подготовка
const response = await this.api.getBookLink({bookPath, downFileName});
const response = await this.api.getBookLink(book._uid);
const link = response.link;
const href = `${window.location.origin}${link}`;
@@ -162,7 +139,7 @@ export default class BaseList {
//скачивание
const d = this.$refs.download;
d.href = href;
d.download = downFileName;
d.download = response.downFileName;
d.click();
} else if (action == 'copyLink') {
@@ -185,6 +162,10 @@ export default class BaseList {
const url = this.config.bookReadLink.replace('${DOWNLOAD_LINK}', href);
window.open(url, '_blank');
}
} else if (action == 'bookInfo') {
//информация о книге
const response = await this.api.getBookInfo(book._uid);
this.$emit('listEvent', {action: 'bookInfo', data: response.bookInfo});
}
} catch(e) {
this.$root.stdDialog.alert(e.message, 'Ошибка');
@@ -208,6 +189,7 @@ export default class BaseList {
case 'download':
case 'copyLink':
case 'readBook':
case 'bookInfo':
this.download(event.book, event.action);//no await
break;
}
@@ -485,13 +467,13 @@ export default class BaseList {
*/
const sqlFormat = 'YYYY-MM-DD';
switch (date) {
case 'today': date = utils.dateFormat(moment(), sqlFormat); break;
case '3days': date = utils.dateFormat(moment().subtract(3, 'days'), sqlFormat); break;
case 'week': date = utils.dateFormat(moment().subtract(1, 'weeks'), sqlFormat); break;
case '2weeks': date = utils.dateFormat(moment().subtract(2, 'weeks'), sqlFormat); break;
case 'month': date = utils.dateFormat(moment().subtract(1, 'months'), sqlFormat); break;
case '2months': date = utils.dateFormat(moment().subtract(2, 'months'), sqlFormat); break;
case '3months': date = utils.dateFormat(moment().subtract(3, 'months'), sqlFormat); break;
case 'today': date = utils.dateFormat(dayjs(), sqlFormat); break;
case '3days': date = utils.dateFormat(dayjs().subtract(3, 'days'), sqlFormat); break;
case 'week': date = utils.dateFormat(dayjs().subtract(1, 'weeks'), sqlFormat); break;
case '2weeks': date = utils.dateFormat(dayjs().subtract(2, 'weeks'), sqlFormat); break;
case 'month': date = utils.dateFormat(dayjs().subtract(1, 'months'), sqlFormat); break;
case '2months': date = utils.dateFormat(dayjs().subtract(2, 'months'), sqlFormat); break;
case '3months': date = utils.dateFormat(dayjs().subtract(3, 'months'), sqlFormat); break;
default:
date = '';
}

View File

@@ -0,0 +1,348 @@
<template>
<Dialog ref="dialog" v-model="dialogVisible">
<template #header>
<div class="row items-center">
<div style="font-size: 110%">
Информация о книге
</div>
</div>
</template>
<div ref="box" class="fit column q-mt-xs overflow-auto no-wrap" style="padding: 0px 10px 10px 10px;">
<div class="text-green-10">
{{ bookAuthor }}
</div>
<div>
<b>{{ book.title }}</b>
</div>
<div class="row q-mt-sm no-wrap">
<div class="poster-size">
<div class="poster-size column justify-center items-center" :class="{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>
<div class="col column q-ml-sm" style="min-width: 400px; border: 1px solid #ccc">
<div class="bg-grey-3 row">
<q-tabs
v-model="selectedTab"
active-color="black"
active-bg-color="white"
indicator-color="white"
dense
no-caps
inline-label
class="bg-grey-4 text-grey-7"
>
<q-tab v-if="fb2.length" name="fb2" label="Fb2 инфо" />
<q-tab name="inpx" label="Inpx инфо" />
</q-tabs>
</div>
<div class="overflow-auto full-width" style="height: 262px">
<div v-for="item in info" :key="item.name">
<div class="row q-ml-sm q-mt-sm items-center">
<div class="text-blue" style="font-size: 90%">
{{ item.label }}
</div>
<div class="col q-mx-xs" style="height: 0px; border-top: 1px solid #ccc"></div>
</div>
<div v-for="subItem in item.value" :key="subItem.name" class="row q-ml-md">
<div style="width: 100px">
{{ subItem.label }}
</div>
<div class="q-ml-sm" v-html="subItem.value" />
</div>
</div>
<div class="q-mt-xs"></div>
</div>
</div>
</div>
<div class="q-mt-md" v-html="annotation" />
</div>
<template #footer>
<q-btn class="q-px-md q-ml-sm" color="primary" dense no-caps @click="okClick">
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>
<script>
//-----------------------------------------------------------------------------
import vueComponent from '../../vueComponent.js';
import Dialog from '../../share/Dialog.vue';
import Fb2Parser from '../../../../server/core/fb2/Fb2Parser';
import * as utils from '../../../share/utils';
import _ from 'lodash';
const componentOptions = {
components: {
Dialog
},
watch: {
modelValue(newValue) {
this.dialogVisible = newValue;
if (newValue)
this.init();
},
dialogVisible(newValue) {
this.$emit('update:modelValue', newValue);
},
}
};
class BookInfoDialog {
_options = componentOptions;
_props = {
modelValue: Boolean,
bookInfo: Object,
};
dialogVisible = false;
posterDialogVisible = false;
selectedTab = 'fb2';
//info props
coverSrc = '';
annotation = '';
fb2 = [];
book = {};
created() {
this.commit = this.$store.commit;
}
mounted() {
}
init() {
//defaults
this.coverSrc = '';
this.annotation = '';
this.fb2 = [];
this.book = {};
this.parseBookInfo();
if (!this.fb2.length)
this.selectedTab = 'inpx';
}
get bookAuthor() {
if (this.book.author) {
let a = this.book.author.split(',');
return a.slice(0, 3).join(', ') + (a.length > 3 ? ' и др.' : '');
}
return '';
}
formatSize(size) {
size = size/1024;
let unit = 'KB';
if (size > 1024) {
size = size/1024;
unit = 'MB';
}
return `${size.toFixed(1)} ${unit}`;
}
get inpx() {
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 utils.sqlDateFormat(value);
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(this.book);
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;
}
get info() {
let result = [];
switch (this.selectedTab) {
case 'fb2':
return this.fb2;
case 'inpx':
return this.inpx;
}
return result;
}
parseBookInfo() {
const bookInfo = this.bookInfo;
//cover
if (bookInfo.cover)
this.coverSrc = bookInfo.cover;
//fb2
if (bookInfo.fb2) {
const parser = new Fb2Parser(bookInfo.fb2);
const infoObj = parser.bookInfo();
if (infoObj.titleInfo) {
let ann = infoObj.titleInfo.annotationHtml;
if (ann) {
ann = ann.replace(/<p>/g, `<p class="p-annotation">`);
this.annotation = ann;
}
}
this.fb2 = parser.bookInfoList(infoObj, {
valueToString(value, nodePath, origVTS) {//eslint-disable-line no-unused-vars
if (nodePath == 'documentInfo/historyHtml' && value)
return value.replace(/<p>/g, `<p class="p-history">`);
return origVTS(value, nodePath);
},
});
}
//book
if (bookInfo.book)
this.book = bookInfo.book;
}
posterClick() {
if (!this.coverSrc)
return;
this.posterDialogVisible = true;
}
okClick() {
this.dialogVisible = false;
}
}
export default vueComponent(BookInfoDialog);
//-----------------------------------------------------------------------------
</script>
<style scoped>
.poster-size {
height: 300px;
width: 200px;
min-width: 100px;
}
.poster {
width: 100%;
height: 100%;
}
.poster:hover {
position: relative;
top: -1%;
left: -1%;
width: 102%;
height: 102%;
cursor: pointer;
}
</style>
<style>
.p-annotation {
text-indent: 20px;
text-align: justify;
padding: 0;
margin: 0;
}
.p-history {
padding: 0;
margin: 0;
}
</style>

View File

@@ -33,18 +33,20 @@
</div>
<div class="q-ml-sm column">
<div v-if="(mode == 'series' || mode == 'title') && bookAuthor" class="row items-center clickable2 text-green-10" @click="selectAuthor">
{{ bookAuthor }}
<div v-if="(mode == 'series' || mode == 'title') && bookAuthor" class="row">
<div class="clickable2 text-green-10" @click.stop.prevent="emit('authorClick')">
{{ bookAuthor }}
</div>
</div>
<div class="row items-center">
<div v-if="book.serno" class="q-mr-xs">
{{ book.serno }}.
</div>
<div class="clickable2" :class="titleColor" @click="selectTitle">
<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="selectSeries">
<div v-if="mode == 'title' && bookSeries" class="q-ml-xs clickable2" @click.stop.prevent="emit('seriesClick')">
{{ bookSeries }}
</div>
@@ -53,15 +55,19 @@
{{ bookSize }}, {{ book.ext }}
</div>
<div class="q-ml-sm clickable" @click="download">
<div v-if="showInfo" class="q-ml-sm clickable" @click.stop.prevent="emit('bookInfo')">
(инфо)
</div>
<div class="q-ml-sm clickable" @click.stop.prevent="emit('download')">
(скачать)
</div>
<div class="q-ml-sm clickable" @click="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="readBook">
<div v-if="showReadLink" class="q-ml-sm clickable" @click.stop.prevent="emit('readBook')">
(читать)
</div>
@@ -107,6 +113,7 @@ class BookView {
};
showRates = true;
showInfo = true;
showGenres = true;
showDeleted = false;
showDates = false;
@@ -119,6 +126,7 @@ class BookView {
const settings = this.settings;
this.showRates = settings.showRates;
this.showInfo = settings.showInfo;
this.showGenres = settings.showGenres;
this.showDates = settings.showDates;
this.showDeleted = settings.showDeleted;
@@ -183,28 +191,8 @@ class BookView {
return utils.sqlDateFormat(this.book.date);
}
selectAuthor() {
this.$emit('bookEvent', {action: 'authorClick', book: this.book});
}
selectSeries() {
this.$emit('bookEvent', {action: 'seriesClick', book: this.book});
}
selectTitle() {
this.$emit('bookEvent', {action: 'titleClick', book: this.book});
}
download() {
this.$emit('bookEvent', {action: 'download', book: this.book});
}
copyLink() {
this.$emit('bookEvent', {action: 'copyLink', book: this.book});
}
readBook() {
this.$emit('bookEvent', {action: 'readBook', book: this.book});
emit(action) {
this.$emit('bookEvent', {action, book: this.book});
}
}

View File

@@ -3,24 +3,16 @@
<div ref="scroller" class="col fit column no-wrap" style="overflow: auto; position: relative" @scroll="onScroll">
<div ref="toolPanel" class="tool-panel q-pb-xs column bg-cyan-2" style="position: sticky; top: 0; z-index: 10;">
<div class="header q-mx-md q-mb-xs q-mt-sm row items-center">
<a :href="newSearchLink" style="height: 33px">
<a :href="newSearchLink" style="height: 33px; width: 34px">
<img src="./assets/logo.png" />
<q-tooltip :delay="1500" anchor="bottom middle" content-style="font-size: 80%" max-width="400px">
Новый поиск
</q-tooltip>
</a>
<div class="row items-center q-ml-sm" style="font-size: 150%;">
<div class="q-mr-xs">
Коллекция
</div>
<div class="clickable" @click="showCollectionInfo">
{{ collection }}
</div>
</div>
<q-btn-toggle
v-model="selectedList"
class="q-ml-md"
class="q-ml-sm"
toggle-color="primary"
:options="listOptions"
push
@@ -28,15 +20,18 @@
rounded
/>
<DivBtn class="q-ml-md text-white bg-secondary" :size="30" :icon-size="24" :imt="1" icon="la la-cog" round @click="settingsDialogVisible = true">
<template #tooltip>
<q-tooltip :delay="1500" anchor="bottom middle" content-style="font-size: 80%" max-width="400px">
Настройки
</q-tooltip>
</template>
</DivBtn>
<div class="row items-center q-ml-sm" style="font-size: 150%;">
<div class="q-mr-xs">
Коллекция
</div>
<div class="clickable" @click.stop.prevent="showCollectionInfo">
{{ collection }}
</div>
</div>
<DivBtn class="q-ml-sm text-white bg-secondary" :size="30" :icon-size="24" icon="la la-question" round @click="showSearchHelp">
<div class="col"></div>
<DivBtn class="q-ml-md text-white bg-secondary" :size="30" :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">
Памятка
@@ -44,12 +39,35 @@
</template>
</DivBtn>
<div class="col"></div>
<div class="q-px-sm q-py-xs bg-green-12 clickable2" style="border: 1px solid #aaaaaa; border-radius: 6px" @click="openReleasePage">
{{ projectName }}
</div>
<DivBtn class="q-ml-sm text-white bg-secondary" :size="30" :icon-size="24" :imt="1" icon="la la-cog" round @click.stop.prevent="settingsDialogVisible = true">
<template #tooltip>
<q-tooltip :delay="1500" anchor="bottom middle" content-style="font-size: 80%" max-width="400px">
Настройки
</q-tooltip>
</template>
</DivBtn>
<DivBtn v-if="!config.freeAccess" class="q-ml-sm text-white bg-secondary" :size="30" :icon-size="24" :imt="1" icon="la la-sign-out-alt" round @click.stop.prevent="logout">
<template #tooltip>
<q-tooltip :delay="1500" anchor="bottom middle" content-style="font-size: 80%" max-width="400px">
Выход
</q-tooltip>
</template>
</DivBtn>
</div>
<div class="row q-mx-md q-mb-xs items-center">
<DivBtn
class="text-grey-5 bg-yellow-1 q-mt-xs" :size="34" :icon-size="24" round
:icon="(extendedParams ? 'la la-angle-double-up' : 'la la-angle-double-down')"
@click.stop.prevent="extendedParams = !extendedParams"
>
<template #tooltip>
<q-tooltip :delay="1500" anchor="bottom middle" content-style="font-size: 80%" max-width="400px">
{{ `${(extendedParams ? 'Скрыть' : 'Показать')} дополнительные критерии поиска` }}
</q-tooltip>
</template>
</DivBtn>
<div class="q-mx-xs" />
<q-input
ref="authorInput" v-model="search.author" :maxlength="5000" :debounce="inputDebounce"
class="q-mt-xs" :bg-color="inputBgColor('author')" style="width: 200px;" label="Автор" stack-label outlined dense clearable
@@ -79,8 +97,8 @@
<div class="q-mx-xs" />
<q-input
v-model="search.lang" :maxlength="inputMaxLength" :debounce="inputDebounce"
class="q-mt-xs" :bg-color="inputBgColor()" input-style="cursor: pointer" style="width: 100px;" label="Язык" stack-label outlined dense clearable readonly
@click="selectLang"
class="q-mt-xs" :bg-color="inputBgColor()" input-style="cursor: pointer" style="width: 90px;" label="Язык" stack-label outlined dense clearable readonly
@click.stop.prevent="selectLang"
>
<template v-if="search.lang" #append>
<q-icon name="la la-times-circle" class="q-field__focusable-action" @click.stop.prevent="search.lang = ''" />
@@ -90,25 +108,26 @@
{{ search.lang }}
</q-tooltip>
</q-input>
<div class="q-mx-xs" />
<DivBtn
class="text-grey-5 bg-yellow-1 q-mt-xs" :size="34" :icon-size="24" round
:icon="(extendedParams ? 'la la-angle-double-up' : 'la la-angle-double-down')"
@click="extendedParams = !extendedParams"
class="text-grey-8 bg-yellow-1 q-mt-xs" :size="34" :icon-size="24" round
icon="la la-level-up-alt"
@click.stop.prevent="cloneSearch"
>
<template #tooltip>
<q-tooltip :delay="1500" anchor="bottom middle" content-style="font-size: 80%" max-width="400px">
{{ `${(extendedParams ? 'Скрыть' : 'Показать')} дополнительные критерии поиска` }}
Клонировать поиск
</q-tooltip>
</template>
</DivBtn>
</div>
<div v-show="extendedParams" class="row q-mx-md q-mb-xs items-center">
<div style="width: 34px" />
<div class="q-mx-xs" />
<q-input
v-model="genreNames" :maxlength="inputMaxLength" :debounce="inputDebounce"
class="q-mt-xs" :bg-color="inputBgColor()" input-style="cursor: pointer" style="width: 200px;" label="Жанр" stack-label outlined dense clearable readonly
@click="selectGenre"
@click.stop.prevent="selectGenre"
>
<template v-if="genreNames" #append>
<q-icon name="la la-times-circle" class="q-field__focusable-action" @click.stop.prevent="search.genre = ''" />
@@ -140,7 +159,7 @@
</template>
<template #option="scope">
<q-item v-bind="scope.itemProps" @click="dateSelectItemClick(scope.opt.value)">
<q-item v-bind="scope.itemProps" @click.stop.prevent="dateSelectItemClick(scope.opt.value)">
<q-item-section>
<q-item-label>
{{ scope.opt.label }}
@@ -153,8 +172,8 @@
<div class="q-mx-xs" />
<q-input
v-model="librateNames" :maxlength="inputMaxLength" :debounce="inputDebounce"
class="q-mt-xs" :bg-color="inputBgColor()" input-style="cursor: pointer" style="width: 100px;" label="Оценка" stack-label outlined dense clearable readonly
@click="selectLibRate"
class="q-mt-xs" :bg-color="inputBgColor()" input-style="cursor: pointer" style="width: 90px;" label="Оценка" stack-label outlined dense clearable readonly
@click.stop.prevent="selectLibRate"
>
<template v-if="librateNames" #append>
<q-icon name="la la-times-circle" class="q-field__focusable-action" @click.stop.prevent="search.librate = ''" />
@@ -165,7 +184,7 @@
</q-tooltip>
</q-input>
</div>
<div v-show="!extendedParams && extendedParamsMessage" class="row q-mx-md items-center clickable" @click="extendedParams = true">
<div v-show="!extendedParams && extendedParamsMessage" class="row q-mx-md items-center clickable" @click.stop.prevent="extendedParams = true">
+{{ extendedParamsMessage }}
</div>
</div>
@@ -191,47 +210,20 @@
<div class="row q-ml-lg q-mb-sm">
<PageScroller v-show="pageCount > 1" v-model="search.page" :page-count="pageCount" />
</div>
<div class="row justify-center">
<div class="q-mb-lg q-px-sm q-py-xs bg-cyan-2 clickable2" style="border: 1px solid #aaaaaa; border-radius: 6px; white-space: nowrap;" @click.stop.prevent="openReleasePage">
{{ projectName }}
</div>
</div>
</div>
<Dialog v-model="settingsDialogVisible">
<template #header>
<div class="row items-center" style="font-size: 130%">
<q-icon class="q-mr-sm" 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="showCounts" size="36px" label="Показывать количество" />
<q-checkbox v-model="showRates" 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="settingsDialogVisible = false">
OK
</q-btn>
</template>
</Dialog>
<SettingsDialog v-model="settingsDialogVisible" />
<SelectGenreDialog v-model="selectGenreDialogVisible" v-model:genre="search.genre" :genre-tree="genreTree" />
<SelectLangDialog v-model="selectLangDialogVisible" v-model:lang="search.lang" :lang-list="langList" :lang-default="langDefault" />
<SelectLibRateDialog v-model="selectLibRateDialogVisible" v-model:librate="search.librate" />
<SelectDateDialog v-model="selectDateDialogVisible" v-model:date="search.date" />
<BookInfoDialog v-model="bookInfoDialogVisible" :book-info="bookInfo" />
</div>
</template>
@@ -244,10 +236,12 @@ import SeriesList from './SeriesList/SeriesList.vue';
import TitleList from './TitleList/TitleList.vue';
import PageScroller from './PageScroller/PageScroller.vue';
import SettingsDialog from './SettingsDialog/SettingsDialog.vue';
import SelectGenreDialog from './SelectGenreDialog/SelectGenreDialog.vue';
import SelectLangDialog from './SelectLangDialog/SelectLangDialog.vue';
import SelectLibRateDialog from './SelectLibRateDialog/SelectLibRateDialog.vue';
import SelectDateDialog from './SelectDateDialog/SelectDateDialog.vue';
import BookInfoDialog from './BookInfoDialog/BookInfoDialog.vue';
import authorBooksStorage from './authorBooksStorage';
import DivBtn from '../share/DivBtn.vue';
@@ -270,10 +264,12 @@ const componentOptions = {
SeriesList,
TitleList,
PageScroller,
SettingsDialog,
SelectGenreDialog,
SelectLangDialog,
SelectLibRateDialog,
SelectDateDialog,
BookInfoDialog,
Dialog,
DivBtn
},
@@ -307,24 +303,6 @@ const componentOptions = {
this.updatePageCount();
},
showCounts(newValue) {
this.setSetting('showCounts', newValue);
},
showRates(newValue) {
this.setSetting('showRates', newValue);
},
showGenres(newValue) {
this.setSetting('showGenres', newValue);
},
showDates(newValue) {
this.setSetting('showDates', newValue);
},
showDeleted(newValue) {
this.setSetting('showDeleted', newValue);
},
abCacheEnabled(newValue) {
this.setSetting('abCacheEnabled', newValue);
},
$route(to) {
this.updateListFromRoute(to);
this.updateSearchFromRouteQuery(to);
@@ -383,6 +361,7 @@ class Search {
selectLangDialogVisible = false;
selectLibRateDialogVisible = false;
selectDateDialogVisible = false;
bookInfoDialogVisible = false;
pageCount = 1;
@@ -411,11 +390,6 @@ class Search {
prevManualDate = '';
//settings
showCounts = true;
showRates = true;
showGenres = true;
showDates = true;
showDeleted = false;
abCacheEnabled = true;
langDefault = '';
limit = 20;
@@ -436,15 +410,7 @@ class Search {
genreTreeInpxHash = '';
showTooltips = 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},
];
bookInfo = {};
searchDateOptions = [
{label: 'сегодня', value: 'today'},
@@ -469,6 +435,8 @@ class Search {
mounted() {
(async() => {
await this.api.updateConfig();
//для встраивания в liberama
window.addEventListener('message', (event) => {
if (!_.isObject(event.data) || event.data.from != 'ExternalLibs')
@@ -502,11 +470,6 @@ class Search {
this.extendedParams = settings.extendedParams;
this.expanded = _.cloneDeep(settings.expanded);
this.expandedSeries = _.cloneDeep(settings.expandedSeries);
this.showCounts = settings.showCounts;
this.showRates = settings.showRates;
this.showGenres = settings.showGenres;
this.showDates = settings.showDates;
this.showDeleted = settings.showDeleted;
this.abCacheEnabled = settings.abCacheEnabled;
this.langDefault = settings.langDefault;
}
@@ -786,6 +749,8 @@ class Search {
if (this.ignoreScrolling) {
this.lastScrollTop = curScrollTop;
if (this.$refs.toolPanel.offsetTop > curScrollTop)
this.$refs.toolPanel.style.top = `${curScrollTop}px`;
return;
}
@@ -854,6 +819,10 @@ class Search {
case 'submitUrl':
this.sendMessage({type: 'submitUrl', data: event.data});
break;
case 'bookInfo':
this.bookInfo = event.data;
this.bookInfoDialogVisible = true;
break;
}
}
@@ -1016,6 +985,14 @@ class Search {
this.selectDateDialogVisible = true
}
}
cloneSearch() {
window.open(window.location.href, '_blank');
}
async logout() {
await this.api.logout();
}
}
export default vueComponent(Search);

View File

@@ -2,7 +2,7 @@
<Dialog ref="dialog" v-model="dialogVisible">
<template #header>
<div class="row items-center">
<div style="font-size: 130%">
<div style="font-size: 110%">
Выбрать даты
</div>
</div>

View File

@@ -2,7 +2,7 @@
<Dialog ref="dialog" v-model="dialogVisible">
<template #header>
<div class="row items-center">
<div style="font-size: 130%">
<div style="font-size: 110%">
Выбрать жанры
</div>
</div>

View File

@@ -2,7 +2,7 @@
<Dialog ref="dialog" v-model="dialogVisible">
<template #header>
<div class="row items-center">
<div style="font-size: 130%">
<div style="font-size: 110%">
Выбрать языки
</div>
</div>

View File

@@ -2,7 +2,7 @@
<Dialog ref="dialog" v-model="dialogVisible">
<template #header>
<div class="row items-center">
<div style="font-size: 130%">
<div style="font-size: 110%">
Выбрать оценки
</div>
</div>

View File

@@ -0,0 +1,151 @@
<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="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});
},
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;
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.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>

View File

@@ -17,7 +17,7 @@ import {QBtn} from 'quasar/src/components/btn';
import {QBtnToggle} from 'quasar/src/components/btn-toggle';
import {QIcon} from 'quasar/src/components/icon';
//import {QSlider} from 'quasar/src/components/slider';
//import {QTabs, QTab} from 'quasar/src/components/tabs';
import {QTabs, QTab} from 'quasar/src/components/tabs';
//import {QTabPanels, QTabPanel} from 'quasar/src/components/tab-panels';
//import {QSeparator} from 'quasar/src/components/separator';
//import {QList} from 'quasar/src/components/item';
@@ -52,7 +52,7 @@ const components = {
QBtnToggle,
QIcon,
//QSlider,
//QTabs, QTab,
QTabs, QTab,
//QTabPanels, QTabPanel,
//QSeparator,
//QList,

View File

@@ -1,4 +1,4 @@
import moment from 'moment';
import dayjs from 'dayjs';
import {Buffer} from 'safe-buffer';
//import _ from 'lodash';
@@ -87,18 +87,6 @@ export async function copyTextToClipboard(text) {
return result;
}
export function makeValidFilename(filename, repl = '_') {
let f = filename.replace(/[\x00\\/:*"<>|]/g, repl); // eslint-disable-line no-control-regex
f = f.trim();
while (f.length && (f[f.length - 1] == '.' || f[f.length - 1] == '_')) {
f = f.substring(0, f.length - 1);
}
if (f)
return f;
else
throw new Error('Invalid filename');
}
/*
export function formatDate(d, format = 'normal') {
switch (format) {
@@ -133,11 +121,11 @@ export function isDigit(c) {
}
export function dateFormat(date, format = 'DD.MM.YYYY') {
return moment(date).format(format);
return dayjs(date).format(format);
}
export function sqlDateFormat(date, format = 'DD.MM.YYYY') {
return moment(date, 'YYYY-MM-DD').format(format);
return dayjs(date, 'YYYY-MM-DD').format(format);
}
export function isManualDate(date) {

View File

@@ -9,6 +9,7 @@ const state = {
expandedSeries: [],
showCounts: true,
showRates: true,
showInfo: true,
showGenres: true,
showDates: false,
showDeleted: false,

205
package-lock.json generated
View File

@@ -1,24 +1,28 @@
{
"name": "inpx-web",
"version": "1.1.2",
"version": "1.3.2",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "inpx-web",
"version": "1.1.2",
"version": "1.3.2",
"hasInstallScript": true,
"license": "CC0-1.0",
"dependencies": {
"@quasar/extras": "^1.15.0",
"axios": "^0.27.2",
"chardet": "^1.5.0",
"dayjs": "^1.11.6",
"express": "^4.18.1",
"express-basic-auth": "^1.2.1",
"fs-extra": "^10.1.0",
"jembadb": "^5.0.2",
"he": "^1.2.0",
"iconv-lite": "^0.6.3",
"jembadb": "^5.1.4",
"localforage": "^1.10.0",
"lodash": "^4.17.21",
"minimist": "^1.2.6",
"moment": "^2.29.4",
"node-stream-zip": "^1.15.0",
"quasar": "^2.7.5",
"safe-buffer": "^5.2.1",
@@ -47,6 +51,7 @@
"html-webpack-plugin": "^5.5.0",
"mini-css-extract-plugin": "^2.6.1",
"pkg": "^5.8.0",
"showdown": "^2.1.0",
"terser-webpack-plugin": "^5.3.3",
"vue-eslint-parser": "^9.0.3",
"vue-loader": "^17.0.0",
@@ -2572,6 +2577,22 @@
}
]
},
"node_modules/basic-auth": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/basic-auth/-/basic-auth-2.0.1.tgz",
"integrity": "sha512-NF+epuEdnUYVlGuhaxbbq+dvJttwLnGY+YixlXlME5KpQ5W3CnXA5cVTneY3SPbPDRkcjMbifrwmFYcClgOZeg==",
"dependencies": {
"safe-buffer": "5.1.2"
},
"engines": {
"node": ">= 0.8"
}
},
"node_modules/basic-auth/node_modules/safe-buffer": {
"version": "5.1.2",
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz",
"integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g=="
},
"node_modules/big.js": {
"version": "5.2.2",
"resolved": "https://registry.npmjs.org/big.js/-/big.js-5.2.2.tgz",
@@ -2645,6 +2666,17 @@
"ms": "2.0.0"
}
},
"node_modules/body-parser/node_modules/iconv-lite": {
"version": "0.4.24",
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz",
"integrity": "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==",
"dependencies": {
"safer-buffer": ">= 2.1.2 < 3"
},
"engines": {
"node": ">=0.10.0"
}
},
"node_modules/body-parser/node_modules/ms": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
@@ -2809,6 +2841,11 @@
"node": ">=4"
}
},
"node_modules/chardet": {
"version": "1.5.0",
"resolved": "https://registry.npmjs.org/chardet/-/chardet-1.5.0.tgz",
"integrity": "sha512-Nj3VehbbFs/1ZnJJJaL3ztEf3Nu5Fs6YV/NBs6lyz/iDDHUU+X9QNk5QgPy1/5Rjtb/cGVf+NyazP7kVEJqKRg=="
},
"node_modules/chownr": {
"version": "1.1.4",
"resolved": "https://registry.npmjs.org/chownr/-/chownr-1.1.4.tgz",
@@ -3415,6 +3452,11 @@
"resolved": "https://registry.npmjs.org/csstype/-/csstype-2.6.20.tgz",
"integrity": "sha512-/WwNkdXfckNgw6S5R125rrW8ez139lBHWouiBvX8dfMFtcn6V81REDqnH7+CRpRipfYlyU1CmOnOxrmGcFOjeA=="
},
"node_modules/dayjs": {
"version": "1.11.6",
"resolved": "https://registry.npmjs.org/dayjs/-/dayjs-1.11.6.tgz",
"integrity": "sha512-zZbY5giJAinCG+7AGaw0wIhNZ6J8AhWuSXKvuc1KAyMiRsvGQWqh4L+MomvhdAYjN+lqvVCMq1I41e3YHvXkyQ=="
},
"node_modules/debug": {
"version": "4.3.4",
"resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz",
@@ -4186,6 +4228,14 @@
"node": ">= 0.10.0"
}
},
"node_modules/express-basic-auth": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/express-basic-auth/-/express-basic-auth-1.2.1.tgz",
"integrity": "sha512-L6YQ1wQ/mNjVLAmK3AG1RK6VkokA1BIY6wmiH304Xtt/cLTps40EusZsU1Uop+v9lTDPxdtzbFmdXfFO3KEnwA==",
"dependencies": {
"basic-auth": "^2.0.1"
}
},
"node_modules/express/node_modules/debug": {
"version": "2.6.9",
"resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
@@ -4678,7 +4728,6 @@
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/he/-/he-1.2.0.tgz",
"integrity": "sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw==",
"dev": true,
"bin": {
"he": "bin/he"
}
@@ -4781,11 +4830,11 @@
}
},
"node_modules/iconv-lite": {
"version": "0.4.24",
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz",
"integrity": "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==",
"version": "0.6.3",
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.6.3.tgz",
"integrity": "sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==",
"dependencies": {
"safer-buffer": ">= 2.1.2 < 3"
"safer-buffer": ">= 2.1.2 < 3.0.0"
},
"engines": {
"node": ">=0.10.0"
@@ -5023,9 +5072,9 @@
}
},
"node_modules/jembadb": {
"version": "5.0.2",
"resolved": "https://registry.npmjs.org/jembadb/-/jembadb-5.0.2.tgz",
"integrity": "sha512-0309Qo4wSkyf154xTokxNl0DuBP5f2Q2MzWGUNX1JmMzlRypFsPY/9VDYV/htkxhT53f2prlQ2NUguQjG2lCRA==",
"version": "5.1.4",
"resolved": "https://registry.npmjs.org/jembadb/-/jembadb-5.1.4.tgz",
"integrity": "sha512-VGg800ZhEXDdWCJ1y2ZcgRqdBWo2g+/55LusBh3r9S3ruKJeRkwVv4nGqHXbtdW60QJNsSwUyQbzcku8zhzvXw==",
"engines": {
"node": ">=16.16.0"
}
@@ -5501,14 +5550,6 @@
"integrity": "sha512-gKLcREMhtuZRwRAfqP3RFW+TK4JqApVBtOIftVgjuABpAtpxhPGaDcfvbhNvD0B8iD1oUr/txX35NjcaY6Ns/A==",
"dev": true
},
"node_modules/moment": {
"version": "2.29.4",
"resolved": "https://registry.npmjs.org/moment/-/moment-2.29.4.tgz",
"integrity": "sha512-5LC9SOxjSc2HF6vO2CyuTDNivEdoz2IvyJJGj6X8DJ0eFyfszE0QiEd+iXmBvUP3WHxSjFH/vIsA0EN00cgr8w==",
"engines": {
"node": "*"
}
},
"node_modules/ms": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
@@ -7011,6 +7052,17 @@
"node": ">= 0.8"
}
},
"node_modules/raw-body/node_modules/iconv-lite": {
"version": "0.4.24",
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz",
"integrity": "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==",
"dependencies": {
"safer-buffer": ">= 2.1.2 < 3"
},
"engines": {
"node": ">=0.10.0"
}
},
"node_modules/rc": {
"version": "1.2.8",
"resolved": "https://registry.npmjs.org/rc/-/rc-1.2.8.tgz",
@@ -7451,6 +7503,31 @@
"node": ">=8"
}
},
"node_modules/showdown": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/showdown/-/showdown-2.1.0.tgz",
"integrity": "sha512-/6NVYu4U819R2pUIk79n67SYgJHWCce0a5xTP979WbNp0FL9MN1I1QK662IDU1b6JzKTvmhgI7T7JYIxBi3kMQ==",
"dev": true,
"dependencies": {
"commander": "^9.0.0"
},
"bin": {
"showdown": "bin/showdown.js"
},
"funding": {
"type": "individual",
"url": "https://www.paypal.me/tiviesantos"
}
},
"node_modules/showdown/node_modules/commander": {
"version": "9.4.1",
"resolved": "https://registry.npmjs.org/commander/-/commander-9.4.1.tgz",
"integrity": "sha512-5EEkTNyHNGFPD2H+c/dXXfQZYa/scCKasxWcXJaWnNJ99pnQN9Vnmqow+p+PlFPE63Q6mThaZws1T+HxfpgtPw==",
"dev": true,
"engines": {
"node": "^12.20.0 || >=14"
}
},
"node_modules/side-channel": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/side-channel/-/side-channel-1.0.4.tgz",
@@ -10656,6 +10733,21 @@
"integrity": "sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==",
"dev": true
},
"basic-auth": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/basic-auth/-/basic-auth-2.0.1.tgz",
"integrity": "sha512-NF+epuEdnUYVlGuhaxbbq+dvJttwLnGY+YixlXlME5KpQ5W3CnXA5cVTneY3SPbPDRkcjMbifrwmFYcClgOZeg==",
"requires": {
"safe-buffer": "5.1.2"
},
"dependencies": {
"safe-buffer": {
"version": "5.1.2",
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz",
"integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g=="
}
}
},
"big.js": {
"version": "5.2.2",
"resolved": "https://registry.npmjs.org/big.js/-/big.js-5.2.2.tgz",
@@ -10718,6 +10810,14 @@
"ms": "2.0.0"
}
},
"iconv-lite": {
"version": "0.4.24",
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz",
"integrity": "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==",
"requires": {
"safer-buffer": ">= 2.1.2 < 3"
}
},
"ms": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
@@ -10832,6 +10932,11 @@
"supports-color": "^5.3.0"
}
},
"chardet": {
"version": "1.5.0",
"resolved": "https://registry.npmjs.org/chardet/-/chardet-1.5.0.tgz",
"integrity": "sha512-Nj3VehbbFs/1ZnJJJaL3ztEf3Nu5Fs6YV/NBs6lyz/iDDHUU+X9QNk5QgPy1/5Rjtb/cGVf+NyazP7kVEJqKRg=="
},
"chownr": {
"version": "1.1.4",
"resolved": "https://registry.npmjs.org/chownr/-/chownr-1.1.4.tgz",
@@ -11282,6 +11387,11 @@
"resolved": "https://registry.npmjs.org/csstype/-/csstype-2.6.20.tgz",
"integrity": "sha512-/WwNkdXfckNgw6S5R125rrW8ez139lBHWouiBvX8dfMFtcn6V81REDqnH7+CRpRipfYlyU1CmOnOxrmGcFOjeA=="
},
"dayjs": {
"version": "1.11.6",
"resolved": "https://registry.npmjs.org/dayjs/-/dayjs-1.11.6.tgz",
"integrity": "sha512-zZbY5giJAinCG+7AGaw0wIhNZ6J8AhWuSXKvuc1KAyMiRsvGQWqh4L+MomvhdAYjN+lqvVCMq1I41e3YHvXkyQ=="
},
"debug": {
"version": "4.3.4",
"resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz",
@@ -11862,6 +11972,14 @@
}
}
},
"express-basic-auth": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/express-basic-auth/-/express-basic-auth-1.2.1.tgz",
"integrity": "sha512-L6YQ1wQ/mNjVLAmK3AG1RK6VkokA1BIY6wmiH304Xtt/cLTps40EusZsU1Uop+v9lTDPxdtzbFmdXfFO3KEnwA==",
"requires": {
"basic-auth": "^2.0.1"
}
},
"fast-deep-equal": {
"version": "3.1.3",
"resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz",
@@ -12239,8 +12357,7 @@
"he": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/he/-/he-1.2.0.tgz",
"integrity": "sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw==",
"dev": true
"integrity": "sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw=="
},
"html-entities": {
"version": "2.3.3",
@@ -12311,11 +12428,11 @@
}
},
"iconv-lite": {
"version": "0.4.24",
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz",
"integrity": "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==",
"version": "0.6.3",
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.6.3.tgz",
"integrity": "sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==",
"requires": {
"safer-buffer": ">= 2.1.2 < 3"
"safer-buffer": ">= 2.1.2 < 3.0.0"
}
},
"icss-utils": {
@@ -12477,9 +12594,9 @@
"dev": true
},
"jembadb": {
"version": "5.0.2",
"resolved": "https://registry.npmjs.org/jembadb/-/jembadb-5.0.2.tgz",
"integrity": "sha512-0309Qo4wSkyf154xTokxNl0DuBP5f2Q2MzWGUNX1JmMzlRypFsPY/9VDYV/htkxhT53f2prlQ2NUguQjG2lCRA=="
"version": "5.1.4",
"resolved": "https://registry.npmjs.org/jembadb/-/jembadb-5.1.4.tgz",
"integrity": "sha512-VGg800ZhEXDdWCJ1y2ZcgRqdBWo2g+/55LusBh3r9S3ruKJeRkwVv4nGqHXbtdW60QJNsSwUyQbzcku8zhzvXw=="
},
"jest-worker": {
"version": "27.5.1",
@@ -12837,11 +12954,6 @@
"integrity": "sha512-gKLcREMhtuZRwRAfqP3RFW+TK4JqApVBtOIftVgjuABpAtpxhPGaDcfvbhNvD0B8iD1oUr/txX35NjcaY6Ns/A==",
"dev": true
},
"moment": {
"version": "2.29.4",
"resolved": "https://registry.npmjs.org/moment/-/moment-2.29.4.tgz",
"integrity": "sha512-5LC9SOxjSc2HF6vO2CyuTDNivEdoz2IvyJJGj6X8DJ0eFyfszE0QiEd+iXmBvUP3WHxSjFH/vIsA0EN00cgr8w=="
},
"ms": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
@@ -13869,6 +13981,14 @@
"version": "3.1.2",
"resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.2.tgz",
"integrity": "sha512-/Nf7TyzTx6S3yRJObOAV7956r8cr2+Oj8AC5dt8wSP3BQAoeX58NoHyCU8P8zGkNXStjTSi6fzO6F0pBdcYbEg=="
},
"iconv-lite": {
"version": "0.4.24",
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz",
"integrity": "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==",
"requires": {
"safer-buffer": ">= 2.1.2 < 3"
}
}
}
},
@@ -14210,6 +14330,23 @@
"integrity": "sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A==",
"dev": true
},
"showdown": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/showdown/-/showdown-2.1.0.tgz",
"integrity": "sha512-/6NVYu4U819R2pUIk79n67SYgJHWCce0a5xTP979WbNp0FL9MN1I1QK662IDU1b6JzKTvmhgI7T7JYIxBi3kMQ==",
"dev": true,
"requires": {
"commander": "^9.0.0"
},
"dependencies": {
"commander": {
"version": "9.4.1",
"resolved": "https://registry.npmjs.org/commander/-/commander-9.4.1.tgz",
"integrity": "sha512-5EEkTNyHNGFPD2H+c/dXXfQZYa/scCKasxWcXJaWnNJ99pnQN9Vnmqow+p+PlFPE63Q6mThaZws1T+HxfpgtPw==",
"dev": true
}
}
},
"side-channel": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/side-channel/-/side-channel-1.0.4.tgz",

View File

@@ -1,6 +1,6 @@
{
"name": "inpx-web",
"version": "1.1.2",
"version": "1.3.2",
"author": "Book Pauk <bookpauk@gmail.com>",
"license": "CC0-1.0",
"repository": "bookpauk/inpx-web",
@@ -12,8 +12,9 @@
"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: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",
"release": "npm run build:all && node build/release.js",
"postinstall": "npm run build:client-dev"
},
@@ -38,6 +39,7 @@
"html-webpack-plugin": "^5.5.0",
"mini-css-extract-plugin": "^2.6.1",
"pkg": "^5.8.0",
"showdown": "^2.1.0",
"terser-webpack-plugin": "^5.3.3",
"vue-eslint-parser": "^9.0.3",
"vue-loader": "^17.0.0",
@@ -51,13 +53,17 @@
"dependencies": {
"@quasar/extras": "^1.15.0",
"axios": "^0.27.2",
"chardet": "^1.5.0",
"dayjs": "^1.11.6",
"express": "^4.18.1",
"express-basic-auth": "^1.2.1",
"fs-extra": "^10.1.0",
"jembadb": "^5.0.2",
"he": "^1.2.0",
"iconv-lite": "^0.6.3",
"jembadb": "^5.1.4",
"localforage": "^1.10.0",
"lodash": "^4.17.21",
"minimist": "^1.2.6",
"moment": "^2.29.4",
"node-stream-zip": "^1.15.0",
"quasar": "^2.7.5",
"safe-buffer": "^5.2.1",

View File

@@ -11,17 +11,20 @@ module.exports = {
execDir,
accessPassword: '',
accessTimeout: 0,
bookReadLink: '',
loggingEnabled: true,
//поправить в случае, если были критические изменения в DbCreator
//поправить в случае, если были критические изменения в DbCreator или InpxParser
//иначе будет рассинхронизация между сервером и клиентом на уровне БД
dbVersion: '5',
dbVersion: '8',
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,
@@ -43,5 +46,11 @@ module.exports = {
host: '0.0.0.0',
port: '22380',
},
//opds: false,
opds: {
enabled: true,
user: '',
password: '',
},
};

View File

@@ -6,11 +6,14 @@ const branchFilename = __dirname + '/application_env';
const propsToSave = [
'accessPassword',
'accessTimeout',
'bookReadLink',
'loggingEnabled',
'dbCacheSize',
'maxFilesDirSize',
'queryCacheEnabled',
'queryCacheMemSize',
'queryCacheDiskSize',
'cacheCleanInterval',
'inpxCheckInterval',
'lowMemoryMode',
@@ -18,6 +21,7 @@ const propsToSave = [
'allowRemoteLib',
'remoteLib',
'server',
'opds',
];
let instance = null;

View File

@@ -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,14 +67,20 @@ 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':
@@ -84,6 +95,8 @@ class WebSocketController {
await this.getGenreTree(req, ws); break;
case 'get-book-link':
await this.getBookLink(req, ws); break;
case 'get-book-info':
await this.getBookInfo(req, ws); break;
case 'get-inpx-file':
await this.getInpxFile(req, ws); break;
@@ -118,9 +131,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);
}
@@ -163,12 +182,19 @@ class WebSocketController {
}
async getBookLink(req, ws) {
if (!utils.hasProp(req, 'bookPath'))
throw new Error(`bookPath is empty`);
if (!utils.hasProp(req, 'downFileName'))
throw new Error(`downFileName is empty`);
if (!utils.hasProp(req, 'bookUid'))
throw new Error(`bookUid is empty`);
const result = await this.webWorker.getBookLink({bookPath: req.bookPath, downFileName: req.downFileName});
const result = await this.webWorker.getBookLink(req.bookUid);
this.send(result, req, ws);
}
async getBookInfo(req, ws) {
if (!utils.hasProp(req, 'bookUid'))
throw new Error(`bookUid is empty`);
const result = await this.webWorker.getBookInfo(req.bookUid);
this.send(result, req, ws);
}

View File

@@ -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++;
@@ -269,6 +272,7 @@ class DbCreator {
delMap = null;
dateMap = null;
librateMap = null;
uidSet = null;
await db.close({table: 'book'});
await db.freeMemory();
@@ -455,7 +459,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 +547,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 +561,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 +621,12 @@ class DbCreator {
stats.filesDelCount = res.filesDelCount;
}
//заодно добавим нужный индекс
await db.create({
in: 'book',
hash: {field: '_uid', type: 'string', depth: 100, unique: true},
});
countDone = true;
}
}

View File

@@ -1,8 +1,8 @@
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,6 +14,11 @@ 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();
@@ -77,7 +82,7 @@ class DbSearcher {
result.add(bookId);
}
return Array.from(result);
return new Uint32Array(result);
`
});
@@ -151,7 +156,7 @@ class DbSearcher {
result.add(bookId);
}
return Array.from(result);
return new Uint32Array(result);
`
});
@@ -187,7 +192,7 @@ class DbSearcher {
result.add(bookId);
}
return Array.from(result);
return new Uint32Array(result);
`
});
@@ -252,7 +257,7 @@ class DbSearcher {
result.add(bookId);
}
return Array.from(result);
return new Uint32Array(result);
`
});
@@ -285,7 +290,7 @@ class DbSearcher {
inter = newInter;
}
return Array.from(inter);
return new Uint32Array(inter);
} else if (idsArr.length == 1) {
return idsArr[0];
} else {
@@ -299,29 +304,13 @@ class DbSearcher {
await this.lock.get();
try {
const db = this.db;
const map = new Map();
const table = `${from}_id`;
const data = await fs.readFile(`${this.config.dataDir}/db/${from}_id.map`, 'utf-8');
await db.open({table});
let rows = await db.select({table});
await db.close({table});
const idMap = JSON.parse(data);
idMap.arr = new Uint32Array(idMap.arr);
idMap.map = new Map(idMap.map);
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]);
}
this.bookIdMap[from] = map;
rows = null;
await db.freeMemory();
utils.freeMemory();
this.bookIdMap[from] = idMap;
return this.bookIdMap[from];
} finally {
@@ -330,15 +319,20 @@ class DbSearcher {
}
async fillBookIdMapAll() {
await this.fillBookIdMap('author');
await this.fillBookIdMap('series');
await this.fillBookIdMap('title');
try {
await this.fillBookIdMap('author');
await this.fillBookIdMap('series');
await this.fillBookIdMap('title');
} catch (e) {
throw new Error(`DbSearcher.fillBookIdMapAll error: ${e.message}`)
}
}
async filterTableIds(tableIds, from, query) {
let result = tableIds;
//т.к. авторы у книги идут списком, то дополнительно фильтруем
async tableIdsFilter(from, query) {
//т.к. авторы у книги идут списком (т.е. одна книга относиться сразу к нескольким авторам),
//то в выборку по bookId могут попасть авторы, которые отсутствуют в критерии query.author,
//поэтому дополнительно фильтруем
let result = null;
if (from == 'author' && query.author && query.author !== '*') {
const key = `filter-ids-author-${query.author}`;
let authorIds = await this.getCached(key);
@@ -347,7 +341,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 +349,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 +370,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 = await this.fillBookIdMap(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 +403,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 +504,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 +534,105 @@ class DbSearcher {
}
}
async getAuthorBookList(authorId) {
async opdsQuery(from, query) {
if (this.closed)
throw new Error('DbSearcher closed');
if (!authorId)
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};
group.set(s, g);
}
g.count++;
}
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 {
//выборка книг автора по authorId
const rows = await this.restoreBooks('author', [authorId])
const db = this.db;
let author = '';
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: (books && books.length ? JSON.stringify(books) : '')};
} finally {
this.searchFlag--;
}
@@ -601,7 +675,7 @@ class DbSearcher {
}
async getCached(key) {
if (!this.config.queryCacheEnabled)
if (!this.queryCacheEnabled)
return null;
let result = null;
@@ -609,13 +683,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 +700,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 +720,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 +767,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)})`});

View File

@@ -1,4 +1,5 @@
const path = require('path');
const crypto = require('crypto');
const ZipReader = require('./ZipReader');
const collectionInfo = 'collection.info';
@@ -70,9 +71,8 @@ class InpxParser {
this.chunk = [];
for (const inpFile of inpFiles) {
await readFileCallback({fileName: inpFile, current: ++current});
const buf = await zipReader.extractToBuf(inpFile);
await this.parseInp(buf, structure, parsedCallback);
await this.parseInp(zipReader, inpFile, structure, parsedCallback);
}
if (this.chunk.length) {
@@ -84,10 +84,13 @@ class InpxParser {
}
}
async parseInp(inpBuf, structure, parsedCallback) {
const structLen = structure.length;
async parseInp(zipReader, inpFile, structure, parsedCallback) {
const inpBuf = await zipReader.extractToBuf(inpFile);
const rows = inpBuf.toString().split('\n');
const defaultFolder = `${path.basename(inpFile, '.inp')}.zip`;
const structLen = structure.length;
for (const row of rows) {
let line = row;
if (!line)
@@ -96,9 +99,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++) {
@@ -115,6 +122,9 @@ class InpxParser {
rec.genre = rec.genre.split(':').filter(s => s).join(',');
}
if (!rec.folder)
rec.folder = defaultFolder;
rec.serno = parseInt(rec.serno, 10) || 0;
rec.size = parseInt(rec.size, 10) || 0;
rec.del = parseInt(rec.del, 10) || 0;

View File

@@ -58,9 +58,9 @@ 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});

144
server/core/WebAccess.js Normal file
View File

@@ -0,0 +1,144 @@
const { JembaDbThread } = require('jembadb');
const utils = require('../core/utils');
const log = new (require('../core/AppLogger'))().log;//singleton
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();
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 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;

View File

@@ -15,6 +15,7 @@ const ayncExit = new (require('./AsyncExit'))();
const log = new (require('./AppLogger'))().log;//singleton
const utils = require('./utils');
const genreTree = require('./genres');
const Fb2Helper = require('./fb2/Fb2Helper');
//server states
const ssNormal = 'normal';
@@ -44,6 +45,7 @@ class WebWorker {
}
this.inpxHashCreator = new InpxHashCreator(config);
this.fb2Helper = new Fb2Helper();
this.inpxFileHash = '';
this.wState = this.workerState.getControl('server_state');
@@ -265,10 +267,16 @@ class WebWorker {
return result;
}
async getAuthorBookList(authorId) {
async opdsQuery(from, query) {
this.checkMyState();
return await this.dbSearcher.getAuthorBookList(authorId);
return await this.dbSearcher.opdsQuery(from, query);
}
async getAuthorBookList(authorId, author) {
this.checkMyState();
return await this.dbSearcher.getAuthorBookList(authorId, author);
}
async getSeriesBookList(series) {
@@ -352,7 +360,7 @@ class WebWorker {
}
}
async restoreBook(bookPath, downFileName) {
async restoreBook(bookUid, bookPath, downFileName) {
const db = this.db;
let extractedFile = '';
@@ -362,12 +370,12 @@ 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 bookFileDesc = `${bookFile}.json`;
const bookFileDesc = `${bookFile}.d.json`;
if (!await fs.pathExists(bookFile) || !await fs.pathExists(bookFileDesc)) {
if (!await fs.pathExists(bookFile) && extractedFile) {
@@ -400,21 +408,40 @@ class WebWorker {
return link;
}
async getBookLink(params) {
async getBookLink(bookUid) {
this.checkMyState();
const {bookPath, downFileName} = params;
try {
const db = this.db;
let link = '';
//найдем bookPath и downFileName
let rows = await db.select({table: 'book', where: `@@hash('_uid', ${db.esc(bookUid)})`});
if (!rows.length)
throw new Error('404 Файл не найден');
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(' - '))
|| utils.makeValidFileNameOrEmpty(at[0])
|| utils.makeValidFileNameOrEmpty(at[1])
|| downFileName;
downFileName = downFileName.substring(0, 100);
const ext = `.${book.ext}`;
if (downFileName.substring(downFileName.length - ext.length) != ext)
downFileName += ext;
const bookPath = `${book.folder}/${book.file}${ext}`;
//найдем хеш
const rows = await db.select({table: 'file_hash', where: `@@id(${db.esc(bookPath)})`});
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 bookFileDesc = `${bookFile}.json`;
const bookFileDesc = `${bookFile}.d.json`;
if (await fs.pathExists(bookFile) && await fs.pathExists(bookFileDesc)) {
link = `${this.config.filesPathStatic}/${hash}`;
@@ -422,13 +449,13 @@ class WebWorker {
}
if (!link) {
link = await this.restoreBook(bookPath, downFileName)
link = await this.restoreBook(bookUid, bookPath, downFileName)
}
if (!link)
throw new Error('404 Файл не найден');
return {link};
return {link, bookPath, downFileName};
} catch(e) {
log(LM_ERR, `getBookLink error: ${e.message}`);
if (e.message.indexOf('ENOENT') >= 0)
@@ -437,48 +464,78 @@ class WebWorker {
}
}
/*
async restoreBookFile(publicPath) {
async getBookInfo(bookUid) {
this.checkMyState();
try {
const db = this.db;
const hash = path.basename(publicPath);
//найдем bookPath и downFileName
const rows = await db.select({table: 'file_hash', where: `@@id(${db.esc(hash)})`});
if (rows.length) {//нашли по хешу
const rec = rows[0];
await this.restoreBook(rec.bookPath, rec.downFileName);
let bookInfo = await this.getBookLink(bookUid);
const hash = path.basename(bookInfo.link);
const bookFile = `${this.config.filesDir}/${hash}`;
const bookFileInfo = `${bookFile}.i.json`;
return rec.downFileName;
} else {//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 restoreBookInfo = async(info) => {
const result = {};
result.book = book;
result.cover = '';
result.fb2 = false;
let parser = null;
if (book.ext == 'fb2') {
const {fb2, cover, coverExt} = await this.fb2Helper.getDescAndCover(bookFile);
parser = fb2;
result.fb2 = fb2.rawNodes;
if (cover) {
result.cover = `${this.config.filesPathStatic}/${hash}${coverExt}`;
await fs.writeFile(`${bookFile}${coverExt}`, cover);
}
}
Object.assign(info, result);
await fs.writeFile(bookFileInfo, JSON.stringify(info));
if (this.config.branch === 'development') {
await fs.writeFile(`${bookFile}.dev`, `${JSON.stringify(info, null, 2)}\n\n${parser ? parser.toString({format: true}) : ''}`);
}
};
if (!await fs.pathExists(bookFileInfo)) {
await restoreBookInfo(bookInfo);
} else {
await utils.touchFile(bookFileInfo);
const info = await fs.readFile(bookFileInfo, 'utf-8');
const tmpInfo = JSON.parse(info);
//проверим существование файла обложки, восстановим если нету
let coverFile = '';
if (tmpInfo.cover)
coverFile = `${this.config.publicFilesDir}${tmpInfo.cover}`;
if (book.id != tmpInfo.book.id || (coverFile && !await fs.pathExists(coverFile))) {
await restoreBookInfo(bookInfo);
} else {
bookInfo = tmpInfo;
}
}
return {bookInfo};
} catch(e) {
log(LM_ERR, `restoreBookFile error: ${e.message}`);
log(LM_ERR, `getBookInfo error: ${e.message}`);
if (e.message.indexOf('ENOENT') >= 0)
throw new Error('404 Файл не найден');
throw e;
}
}
async getDownFileName(publicPath) {
this.checkMyState();
const db = this.db;
const hash = path.basename(publicPath);
//найдем downFileName
const rows = await db.select({table: 'file_hash', where: `@@id(${db.esc(hash)})`});
if (rows.length) {//downFileName найден по хешу
return rows[0].downFileName;
} else {//bookPath не найден
throw new Error('404 Файл не найден');
}
}
*/
async getInpxFile(params) {
let data = null;
if (params.inpxFileHash && this.inpxFileHash && params.inpxFileHash === this.inpxFileHash) {

View File

@@ -0,0 +1,99 @@
const fs = require('fs-extra');
const iconv = require('iconv-lite');
const textUtils = require('./textUtils');
const Fb2Parser = require('../fb2/Fb2Parser');
const utils = require('../utils');
class Fb2Helper {
checkEncoding(data) {
//Корректируем кодировку UTF-16
let encoding = textUtils.getEncoding(data);
if (encoding.indexOf('UTF-16') == 0) {
data = Buffer.from(iconv.decode(data, encoding));
encoding = 'utf-8';
}
//Корректируем пробелы, всякие файлы попадаются :(
if (data[0] == 32) {
data = Buffer.from(data.toString().trim());
}
//Окончательно корректируем кодировку
let result = data;
let left = data.indexOf('<?xml version="1.0"');
if (left < 0) {
left = data.indexOf('<?xml version=\'1.0\'');
}
if (left >= 0) {
const right = data.indexOf('?>', left);
if (right >= 0) {
const head = data.slice(left, right + 2).toString();
const m = head.match(/encoding=['"](.*?)['"]/);
if (m) {
let enc = m[1].toLowerCase();
if (enc != 'utf-8') {
//enc может не соответсвовать реальной кодировке файла, поэтому:
if (encoding.indexOf('ISO-8859') >= 0) {
encoding = enc;
}
result = iconv.decode(data, encoding);
result = Buffer.from(result.toString().replace(m[0], `encoding="utf-8"`));
}
}
}
}
return result;
}
async getDescAndCover(bookFile) {
let data = await fs.readFile(bookFile);
data = await utils.gunzipBuffer(data);
data = this.checkEncoding(data);
const parser = new Fb2Parser();
parser.fromString(data.toString(), {
lowerCase: true,
pickNode: route => route.indexOf('fictionbook/body') !== 0,
});
const coverImage = parser.$$('/description/title-info/coverpage/image');
let cover = null;
let coverExt = '';
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);
//найдем нужный image
for (const node of parser.$$array('/binary')) {
let attrs = node.attrs();
if (!attrs)
return;
if (attrs.id === binaryId) {
const base64 = node.text();
cover = (base64 ? Buffer.from(base64, 'base64') : null);
}
}
}
}
parser.remove('binary');
return {fb2: parser, cover, coverExt};
}
}
module.exports = Fb2Helper;

View File

@@ -0,0 +1,294 @@
const XmlParser = require('../xml/XmlParser');
class Fb2Parser extends XmlParser {
get xlinkNS() {
if (!this._xlinkNS) {
const rootAttrs = this.selectFirstSelf().attrs();
let ns = 'l';
for (const [key, value] of rootAttrs) {
if (value == 'http://www.w3.org/1999/xlink') {
ns = key.split(':')[1] || ns;
break;
}
}
this._xlinkNS = ns;
}
return this._xlinkNS;
}
bookInfo() {
const result = {};
const desc = this.$$('/description/');
if (!desc)
return result;
const parseAuthors = (node, tagName) => {
const authors = [];
for (const a of node.$$array(tagName)) {
let names = [];
names.push(a.text('/last-name'));
names.push(a.text('/first-name'));
names.push(a.text('/middle-name'));
names = names.filter(n => n);
if (!names.length)
names.push(a.text('/nickname'));
authors.push(names.join(' '));
}
return authors;
}
const parseSequence = (node, tagName) => {
const sequence = [];
for (const s of node.$$array(tagName)) {
const seqAttrs = s.attrs() || {};
const name = seqAttrs['name'] || null;
const num = seqAttrs['number'] || null;
const lang = seqAttrs['xml:lang'] || null;
sequence.push({name, num, lang});
}
return sequence;
}
const parseTitleInfo = (titleInfo) => {
const info = {};
info.genre = [];
for (const g of titleInfo.$$array('genre'))
info.genre.push(g.text());
info.author = parseAuthors(titleInfo, 'author');
info.bookTitle = titleInfo.text('book-title');
//annotation как Object
info.annotation = titleInfo.$('annotation') && titleInfo.$('annotation').value;
info.annotationXml = null;
info.annotationHtml = null;
if (info.annotation) {
//annotation как кусок xml
info.annotationXml = titleInfo.$$('annotation/').toString({noHeader: true});
//annotation как html
info.annotationHtml = this.toHtml(info.annotationXml);
}
info.keywords = titleInfo.text('keywords');
info.date = titleInfo.text('date');
info.coverpage = titleInfo.$('coverpage') && titleInfo.$('coverpage').value;
info.lang = titleInfo.text('lang');
info.srcLang = titleInfo.text('src-lang');
info.translator = parseAuthors(titleInfo, 'translator');
info.sequence = parseSequence(titleInfo, 'sequence');
return info;
}
//title-info
const titleInfo = desc.$$('title-info/');
if (titleInfo) {
result.titleInfo = parseTitleInfo(titleInfo);
}
//src-title-info
const srcTitleInfo = desc.$$('src-title-info/');
if (srcTitleInfo) {
result.srcTitleInfo = parseTitleInfo(srcTitleInfo);
}
//document-info
const documentInfo = desc.$$('document-info/');
if (documentInfo) {
const info = {};
info.author = parseAuthors(documentInfo, 'author');
info.programUsed = documentInfo.text('program-used');
info.date = documentInfo.text('date');
info.srcUrl = [];
for (const url of documentInfo.$$array('src-url'))
info.srcUrl.push(url.text());
info.srcOcr = documentInfo.text('src-ocr');
info.id = documentInfo.text('id');
info.version = documentInfo.text('version');
//аналогично annotation
info.history = documentInfo.$('history') && documentInfo.$('history').value;
info.historyXml = null;
info.historyHtml = null;
if (info.history) {
//history как кусок xml
info.historyXml = documentInfo.$$('history/').toString({noHeader: true});
//history как html
info.historyHtml = this.toHtml(info.historyXml);
}
info.publisher = parseAuthors(documentInfo, 'publisher');
result.documentInfo = info;
}
//publish-info
const publishInfo = desc.$$('publish-info/');
if (publishInfo) {
const info = {};
info.bookName = publishInfo.text('book-name');
info.publisher = publishInfo.text('publisher');
info.city = publishInfo.text('city');
info.year = publishInfo.text('year');
info.isbn = publishInfo.text('isbn');
info.sequence = parseSequence(publishInfo, 'sequence');
result.publishInfo = info;
}
return result;
}
bookInfoList(bookInfo, options = {}) {
let {
correctMapping = false,
valueToString = false,
} = options;
if (!correctMapping)
correctMapping = mapping => mapping;
const myValueToString = (value, nodePath, origVTS) => {//eslint-disable-line no-unused-vars
if (nodePath == 'titleInfo/sequence'
|| nodePath == 'srcTitleInfo/sequence'
|| nodePath == 'publishInfo/sequence')
return value.map(v => [v.name, v.num].filter(s => s).join(' #')).join(', ');
if (typeof(value) === 'string') {
return value;
} else if (Array.isArray(value)) {
return value.join(', ');
} else if (typeof(value) === 'object') {
return JSON.stringify(value);
}
return value;
};
if (!valueToString)
valueToString = myValueToString;
let mapping = [
{name: 'titleInfo', label: 'Общая информация', value: [
{name: 'author', label: 'Автор(ы)'},
{name: 'bookTitle', label: 'Название'},
{name: 'sequence', label: 'Серия'},
{name: 'genre', label: 'Жанр'},
{name: 'date', label: 'Дата'},
{name: 'lang', label: 'Язык книги'},
{name: 'srcLang', label: 'Язык оригинала'},
{name: 'translator', label: 'Переводчик(и)'},
{name: 'keywords', label: 'Ключевые слова'},
]},
{name: 'srcTitleInfo', label: 'Информация о произведении на языке оригинала', value: [
{name: 'author', label: 'Автор(ы)'},
{name: 'bookTitle', label: 'Название'},
{name: 'sequence', label: 'Серия'},
{name: 'genre', label: 'Жанр'},
{name: 'date', label: 'Дата'},
{name: 'lang', label: 'Язык книги'},
{name: 'srcLang', label: 'Язык оригинала'},
{name: 'translator', label: 'Переводчик(и)'},
{name: 'keywords', label: 'Ключевые слова'},
]},
{name: 'publishInfo', label: 'Издательская информация', value: [
{name: 'bookName', label: 'Название'},
{name: 'publisher', label: 'Издательство'},
{name: 'city', label: 'Город'},
{name: 'year', label: 'Год'},
{name: 'isbn', label: 'ISBN'},
{name: 'sequence', label: 'Серия'},
]},
{name: 'documentInfo', label: 'Информация о документе (OCR)', value: [
{name: 'author', label: 'Автор(ы)'},
{name: 'programUsed', label: 'Программа'},
{name: 'date', label: 'Дата'},
//srcUrl = []
{name: 'id', label: 'ID'},
{name: 'version', label: 'Версия'},
{name: 'srcOcr', label: 'Автор источника'},
{name: 'historyHtml', label: 'История'},
{name: 'publisher', label: 'Правообладатели'},
]},
];
mapping = correctMapping(mapping);
bookInfo = (bookInfo ? bookInfo : this.bookInfo());
//заполняем mapping
let result = [];
for (const item of mapping) {
const itemOut = {name: item.name, label: item.label, value: []};
const info = bookInfo[item.name];
if (!info)
continue;
for (const subItem of item.value) {
if (info[subItem.name] !== null) {
const subItemOut = {
name: subItem.name,
label: subItem.label,
value: valueToString(info[subItem.name], `${item.name}/${subItem.name}`, myValueToString),
};
if (subItemOut.value)
itemOut.value.push(subItemOut);
}
}
if (itemOut.value.length)
result.push(itemOut);
}
return result;
}
toHtml(xmlString) {
const substs = {
'<subtitle>': '<p><b>',
'</subtitle>': '</b></p>',
'<empty-line/>': '<br>',
'<strong>': '<b>',
'</strong>': '</b>',
'<emphasis>': '<i>',
'</emphasis>': '</i>',
'<stanza>': '<br>',
'</stanza>': '',
'<poem>': '<br>',
'</poem>': '',
'<cite>': '<i>',
'</cite>': '</i>',
'<table>': '<br>',
'</table>': '',
};
for (const [tag, s] of Object.entries(substs)) {
const r = new RegExp(`${tag}`, 'g');
xmlString = xmlString.replace(r, s);
}
return xmlString;
}
}
module.exports = Fb2Parser;

View File

@@ -0,0 +1,130 @@
const chardet = require('chardet');
function getEncoding(buf) {
let selected = getEncodingLite(buf);
if (selected == 'ISO-8859-5' && buf.length > 10) {
const charsetAll = chardet.analyse(buf.slice(0, 20000));
for (const charset of charsetAll) {
if (charset.name.indexOf('ISO-8859') < 0) {
selected = charset.name;
break;
}
}
}
return selected;
}
function getEncodingLite(buf, returnAll) {
const lowerCase = 3;
const upperCase = 1;
const codePage = {
'k': 'koi8-r',
'w': 'Windows-1251',
'd': 'cp866',
'i': 'ISO-8859-5',
'm': 'maccyrillic',
'u': 'utf-8',
};
let charsets = {
'k': 0,
'w': 0,
'd': 0,
'i': 0,
'm': 0,
'u': 0,
};
const len = buf.length;
const blockSize = (len > 5*3000 ? 3000 : len);
let counter = 0;
let i = 0;
let totalChecked = 0;
while (i < len) {
const char = buf[i];
const nextChar = (i < len - 1 ? buf[i + 1] : 0);
totalChecked++;
i++;
//non-russian characters
if (char < 128 || char > 256)
continue;
//UTF-8
if ((char == 208 || char == 209) && nextChar >= 128 && nextChar <= 190)
charsets['u'] += lowerCase;
else {
//CP866
if ((char > 159 && char < 176) || (char > 223 && char < 242)) charsets['d'] += lowerCase;
if ((char > 127 && char < 160)) charsets['d'] += upperCase;
//KOI8-R
if ((char > 191 && char < 223)) charsets['k'] += lowerCase;
if ((char > 222 && char < 256)) charsets['k'] += upperCase;
//WIN-1251
if (char > 223 && char < 256) charsets['w'] += lowerCase;
if (char > 191 && char < 224) charsets['w'] += upperCase;
//MAC
if (char > 221 && char < 255) charsets['m'] += lowerCase;
if (char > 127 && char < 160) charsets['m'] += upperCase;
//ISO-8859-5
if (char > 207 && char < 240) charsets['i'] += lowerCase;
if (char > 175 && char < 208) charsets['i'] += upperCase;
}
counter++;
if (counter > blockSize) {
counter = 0;
i += Math.round(len/2 - 2*blockSize);
}
}
let sorted = Object.keys(charsets).map(function(key) {
return { codePage: codePage[key], c: charsets[key], totalChecked };
});
sorted.sort((a, b) => b.c - a.c);
if (returnAll)
return sorted;
else if (sorted[0].c > 0 && sorted[0].c > sorted[0].totalChecked/2)
return sorted[0].codePage;
else
return 'ISO-8859-5';
}
function checkIfText(buf) {
const enc = getEncodingLite(buf, true);
if (enc[0].c > enc[0].totalChecked*0.9)
return true;
let spaceCount = 0;
let crCount = 0;
let lfCount = 0;
for (let i = 0; i < buf.length; i++) {
if (buf[i] == 32)
spaceCount++;
if (buf[i] == 13)
crCount++;
if (buf[i] == 10)
lfCount++;
}
const spaceFreq = spaceCount/(buf.length + 1);
const crFreq = crCount/(buf.length + 1);
const lfFreq = lfCount/(buf.length + 1);
return (buf.length < 1000 || spaceFreq > 0.1 || crFreq > 0.03 || lfFreq > 0.03);
}
module.exports = {
getEncoding,
getEncodingLite,
checkIfText,
}

View File

@@ -0,0 +1,181 @@
const BasePage = require('./BasePage');
class AuthorPage extends BasePage {
constructor(config) {
super(config);
this.id = 'author';
this.title = 'Авторы';
}
sortBooks(bookList) {
//схлопывание серий
const books = [];
const seriesSet = new Set();
for (const book of bookList) {
if (book.series) {
if (!seriesSet.has(book.series)) {
books.push({
type: 'series',
book
});
seriesSet.add(book.series);
}
} 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 = JSON.parse(bookList.books);
const booksAll = this.filterBooks(books, {del: 0});
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})`;
const e = {
id: book._uid,
title,
link: this.acqLink({href: `/book?uid=${encodeURIComponent(book._uid)}`}),
};
if (query.all) {
e.content = {
'*ATTRS': {type: 'text'},
'*TEXT': this.bookAuthor(book.author),
}
}
entry.push(
this.makeEntry(e)
);
}
}
} else if (query.author && query.author[0] == '=') {
//книги по автору
const bookList = await this.webWorker.getAuthorBookList(0, query.author.substring(1));
if (bookList.books) {
let books = JSON.parse(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)}`}),
})
);
} 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)}`}),
})
);
}
}
}
} 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) {
entry.push(
this.makeEntry({
id: rec.id,
title: this.bookAuthor(rec.title),
link: this.navLink({href: `/${this.id}?author=${rec.q}&genre=${encodeURIComponent(query.genre)}`}),
})
);
}
}
result.entry = entry;
return this.makeBody(result, req);
}
}
module.exports = AuthorPage;

View File

@@ -0,0 +1,347 @@
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);
class BasePage {
constructor(config) {
this.config = config;
this.webWorker = new WebWorker(config);
this.rootTag = 'feed';
this.opdsRoot = config.opdsRoot;
}
makeEntry(entry = {}) {
if (!entry.id)
throw new Error('makeEntry: no id');
if (!entry.title)
throw new Error('makeEntry: no title');
entry.title = he.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 = he.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])}`,
};
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;
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)}`,
};
} else {
rec = {
id: row.id,
title: `${value.toUpperCase().replace(/ /g, spaceChar)}~`,
q: encodeURIComponent(value),
};
}
if (query.depth > 1 || enru.has(value[0])) {
result.push(rec);
} else {
others.push(rec);
}
}
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'});
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);
return !bookValue || (bookValue !== emptyFieldValue && !enru.has(bookValue[0]) && bookValue.indexOf(searchValue) >= 0);
} 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;

View File

@@ -0,0 +1,206 @@
const path = require('path');
const _ = require('lodash');
const he = require('he');
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();
const fileFormat = `${bookInfo.book.ext}+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': he.escape(content),
};
}
//links
e.link = [ this.downLink({href: bookInfo.link, 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;

View File

@@ -0,0 +1,72 @@
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 || '',
section: req.query.section || '',
};
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)}`}),
})
);
}
entry.unshift(
this.makeEntry({
id: 'whole_section',
title: '[Весь раздел]',
link: this.navLink({href: `/${encodeURIComponent(query.from)}?genre=${encodeURIComponent(all.join(','))}`}),
})
);
}
} 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)}&section=${encodeURIComponent(section.name)}`}),
})
);
}
}
}
result.entry = entry;
return this.makeBody(result, req);
}
}
module.exports = GenrePage;

View 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;

View File

@@ -0,0 +1,39 @@
const BasePage = require('./BasePage');
const AuthorPage = require('./AuthorPage');
const SeriesPage = require('./SeriesPage');
const TitlePage = require('./TitlePage');
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);
}
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(),
];
return this.makeBody(result, req);
}
}
module.exports = RootPage;

View File

@@ -0,0 +1,83 @@
const BasePage = require('./BasePage');
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 || '',
page: parseInt(req.query.page, 10) || 1,
};
let entry = [];
if (query.type) {
if (['author', 'series', 'title'].includes(query.type)) {
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, del: 0, offset, limit});
const found = queryRes.found;
for (let i = 0; i < found.length; i++) {
if (i >= limit)
break;
const row = found[i];
entry.push(
this.makeEntry({
id: row.id,
title: row[from],
link: this.navLink({href: `/${from}?${from}==${encodeURIComponent(row[from])}`}),
}),
);
}
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)}&page=${page + 1}`}),
}),
);
}
}
} else {
//корневой раздел
entry = [
this.makeEntry({
id: 'search_author',
title: 'Поиск авторов',
link: this.navLink({href: `/${this.id}?type=author&term=${encodeURIComponent(query.term)}`}),
}),
this.makeEntry({
id: 'search_series',
title: 'Поиск серий',
link: this.navLink({href: `/${this.id}?type=series&term=${encodeURIComponent(query.term)}`}),
}),
this.makeEntry({
id: 'search_title',
title: 'Поиск книг',
link: this.navLink({href: `/${this.id}?type=title&term=${encodeURIComponent(query.term)}`}),
}),
]
}
result.entry = entry;
return this.makeBody(result, req);
}
}
module.exports = SearchPage;

View File

@@ -0,0 +1,114 @@
const BasePage = require('./BasePage');
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 = JSON.parse(bookList.books);
const booksAll = this.filterBooks(books, {del: 0});
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})`;
const e = {
id: book._uid,
title,
link: this.acqLink({href: `/book?uid=${encodeURIComponent(book._uid)}`}),
};
if (query.all) {
e.content = {
'*ATTRS': {type: 'text'},
'*TEXT': this.bookAuthor(book.author),
}
}
entry.push(
this.makeEntry(e)
);
}
}
} 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) {
entry.push(
this.makeEntry({
id: rec.id,
title: rec.title,
link: this.navLink({href: `/${this.id}?series=${rec.q}&genre=${encodeURIComponent(query.genre)}`}),
})
);
}
}
result.entry = entry;
return this.makeBody(result, req);
}
}
module.exports = SeriesPage;

View File

@@ -0,0 +1,84 @@
const BasePage = require('./BasePage');
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) {
entry.push(
this.makeEntry({
id: rec.id,
title: rec.title,
link: this.navLink({href: `/${this.id}?title=${rec.q}&genre=${encodeURIComponent(query.genre)}`}),
})
);
}
}
result.entry = entry;
return this.makeBody(result, req);
}
}
module.exports = TitlePage;

82
server/core/opds/index.js Normal file
View File

@@ -0,0 +1,82 @@
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');
module.exports = function(app, config) {
if (!config.opds || !config.opds.enabled)
return;
const opdsRoot = '/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 routes = [
['', root],
['/root', root],
['/author', author],
['/series', series],
['/title', title],
['/genre', genre],
['/book', book],
['/opensearch', opensearch],
['/search', search],
];
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) {
res.status(500).send({error: e.message});
if (config.branch == 'development') {
console.error({error: e.message, url: req.originalUrl});
}
}
};
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);
};

View File

@@ -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) {
@@ -115,10 +115,65 @@ function gzipFile(inputFile, outputFile, level = 1) {
});
}
function gunzipFile(inputFile, outputFile) {
return new Promise((resolve, reject) => {
const gzip = zlib.createGunzip();
const input = fs.createReadStream(inputFile);
const output = fs.createWriteStream(outputFile);
input.on('error', reject)
.pipe(gzip).on('error', reject)
.pipe(output).on('error', reject)
.on('finish', (err) => {
if (err) reject(err);
else resolve();
});
});
}
function gzipBuffer(buf) {
return new Promise((resolve, reject) => {
zlib.gzip(buf, {level: 1}, (err, result) => {
if (err) reject(err);
resolve(result);
});
});
}
function gunzipBuffer(buf) {
return new Promise((resolve, reject) => {
zlib.gunzip(buf, (err, result) => {
if (err) reject(err);
resolve(result);
});
});
}
function toUnixPath(dir) {
return dir.replace(/\\/g, '/');
}
function makeValidFileName(fileName, repl = '_') {
let f = fileName.replace(/[\x00\\/:*"<>|]/g, repl); // eslint-disable-line no-control-regex
f = f.trim();
while (f.length && (f[f.length - 1] == '.' || f[f.length - 1] == '_')) {
f = f.substring(0, f.length - 1);
}
if (f)
return f;
else
throw new Error('Invalid filename');
}
function makeValidFileNameOrEmpty(fileName) {
try {
return makeValidFileName(fileName);
} catch(e) {
return '';
}
}
module.exports = {
sleep,
processLoop,
@@ -132,5 +187,10 @@ module.exports = {
intersectSet,
randomHexString,
gzipFile,
gunzipFile,
gzipBuffer,
gunzipBuffer,
toUnixPath,
makeValidFileName,
makeValidFileNameOrEmpty,
};

View File

@@ -0,0 +1,105 @@
class ObjectInspector {
constructor(raw = null) {
this.raw = raw;
}
narrowSelector(selector) {
const result = [];
selector = selector.trim();
//последний индекс не учитывется, только если не задан явно
if (selector && selector[selector.length - 1] == ']')
selector += '/';
const levels = selector.split('/');
for (const level of levels) {
const [name, indexPart] = level.split('[');
let index = 0;
if (indexPart) {
const i = indexPart.indexOf(']');
index = parseInt(indexPart.substring(0, i), 10) || 0;
}
result.push({name, index});
}
if (result.length);
result[result.length - 1].last = true;
return result;
}
select(selector = '') {
selector = this.narrowSelector(selector);
let raw = this.raw;
for (const s of selector) {
if (s.name) {
if (typeof(raw) === 'object' && !Array.isArray(raw))
raw = raw[s.name];
else
raw = null;
}
if (raw !== null && !s.last) {
if (Array.isArray(raw))
raw = raw[s.index];
else if (s.index > 0)
raw = null;
}
if (raw === undefined || raw === null) {
return [];
}
}
raw = (Array.isArray(raw) ? raw : [raw]);
const result = [];
for (const r of raw)
result.push(new ObjectInspector(r));
return result;
}
$$(selector) {
return this.select(selector);
}
$(selector) {
const res = this.select(selector);
return (res !== null && res.length ? res[0] : null);
}
get value() {
return this.raw;
}
v(selector = '') {
const res = this.$(selector);
return (res ? res.value : null);
}
text(selector = '') {
const res = this.$(`${selector}/*TEXT`);
return (res ? res.value : null);
}
comment(selector = '') {
const res = this.$(`${selector}/*COMMENT`);
return (res ? res.value : null);
}
cdata(selector = '') {
const res = this.$(`${selector}/*CDATA`);
return (res ? res.value : null);
}
attrs(selector = '') {
const res = this.$(`${selector}/*ATTRS`);
return (res ? res.value : null);
}
}
module.exports = ObjectInspector;

View File

@@ -0,0 +1,896 @@
const sax = require('./sax');
//node types
const NODE = 1;
const TEXT = 2;
const CDATA = 3;
const COMMENT = 4;
const name2type = {
'NODE': NODE,
'TEXT': TEXT,
'CDATA': CDATA,
'COMMENT': COMMENT,
};
const type2name = {
[NODE]: 'NODE',
[TEXT]: 'TEXT',
[CDATA]: 'CDATA',
[COMMENT]: 'COMMENT',
};
class NodeBase {
wideSelector(selectorString) {
const result = {all: false, before: false, type: 0, name: ''};
if (selectorString === '') {
result.before = true;
} else if (selectorString === '*') {
result.all = true;
} else if (selectorString[0] === '*') {
const typeName = selectorString.substring(1);
result.type = name2type[typeName];
if (!result.type)
throw new Error(`Unknown selector type: ${typeName}`);
} else {
result.name = selectorString;
}
return result;
}
checkNode(rawNode, selectorObj) {
return selectorObj.all || selectorObj.before
|| (selectorObj.type && rawNode[0] === selectorObj.type)
|| (rawNode[0] === NODE && rawNode[1] === selectorObj.name);
}
findNodeIndex(nodes, selectorObj) {
for (let i = 0; i < nodes.length; i++)
if (this.checkNode(nodes[i], selectorObj))
return i;
}
rawAdd(nodes, rawNode, selectorObj) {
if (selectorObj.all) {
nodes.push(rawNode);
} else if (selectorObj.before) {
nodes.unshift(rawNode);
} else {
const index = this.findNodeIndex(nodes, selectorObj);
if (index >= 0)
nodes.splice(index, 0, rawNode);
else
nodes.push(rawNode);
}
}
rawRemove(nodes, selectorObj) {
if (selectorObj.before)
return;
for (let i = nodes.length - 1; i >= 0; i--) {
if (this.checkNode(nodes[i], selectorObj))
nodes.splice(i, 1);
}
}
}
class NodeObject extends NodeBase {
constructor(raw = null) {
super();
if (raw)
this.raw = raw;
else
this.raw = [];
}
get type() {
return this.raw[0] || null;
}
get name() {
if (this.type === NODE)
return this.raw[1] || null;
return null;
}
set name(value) {
if (this.type === NODE)
this.raw[1] = value;
}
attrs(key, value) {
if (this.type !== NODE)
return null;
let map = null;
if (key instanceof Map) {
map = key;
this.raw[2] = Array.from(map);
} else if (Array.isArray(this.raw[2])) {
map = new Map(this.raw[2]);
if (key) {
map.set(key, value);
this.raw[2] = Array.from(map);
}
}
return map;
}
get value() {
switch (this.type) {
case NODE:
return this.raw[3] || null;
case TEXT:
case CDATA:
case COMMENT:
return this.raw[1] || null;
}
return null;
}
set value(v) {
switch (this.type) {
case NODE:
this.raw[3] = v;
break;
case TEXT:
case CDATA:
case COMMENT:
this.raw[1] = v;
}
}
add(node, after = '*') {
if (this.type !== NODE)
return;
const selectorObj = this.wideSelector(after);
if (!Array.isArray(this.raw[3]))
this.raw[3] = [];
if (Array.isArray(node)) {
for (const node_ of node)
this.rawAdd(this.raw[3], node_.raw, selectorObj);
} else {
this.rawAdd(this.raw[3], node.raw, selectorObj);
}
return this;
}
remove(selector = '') {
if (this.type !== NODE || !this.raw[3])
return;
const selectorObj = this.wideSelector(selector);
this.rawRemove(this.raw[3], selectorObj);
if (!this.raw[3].length)
this.raw[3] = null;
return this;
}
each(callback) {
if (this.type !== NODE || !this.raw[3])
return;
for (const n of this.raw[3]) {
if (callback(new NodeObject(n)) === false)
break;
}
return this;
}
eachDeep(callback) {
if (this.type !== NODE || !this.raw[3])
return;
const deep = (nodes, route = '') => {
for (const n of nodes) {
const node = new NodeObject(n);
if (callback(node, route) === false)
return false;
if (node.type === NODE && node.value) {
if (deep(node.value, `${route}${route ? '/' : ''}${node.name}`) === false)
return false;
}
}
}
deep(this.raw[3]);
return this;
}
}
class XmlParser extends NodeBase {
constructor(rawNodes = []) {
super();
this.NODE = NODE;
this.TEXT = TEXT;
this.CDATA = CDATA;
this.COMMENT = COMMENT;
this.rawNodes = rawNodes;
}
get count() {
return this.rawNodes.length;
}
get nodes() {
const result = [];
for (const n of this.rawNodes)
result.push(new NodeObject(n));
return result;
}
nodeObject(node) {
return new NodeObject(node);
}
newParser(nodes) {
return new XmlParser(nodes);
}
checkType(type) {
if (!type2name[type])
throw new Error(`Invalid type: ${type}`);
}
createTypedNode(type, nameOrValue, attrs = null, value = null) {
this.checkType(type);
switch (type) {
case NODE:
if (!nameOrValue || typeof(nameOrValue) !== 'string')
throw new Error('Node name must be non-empty string');
return new NodeObject([type, nameOrValue, attrs, value]);
case TEXT:
case CDATA:
case COMMENT:
if (typeof(nameOrValue) !== 'string')
throw new Error('Node value must be of type string');
return new NodeObject([type, nameOrValue]);
}
}
createNode(name, attrs = null, value = null) {
return this.createTypedNode(NODE, name, attrs, value);
}
createText(value = null) {
return this.createTypedNode(TEXT, value);
}
createCdata(value = null) {
return this.createTypedNode(CDATA, value);
}
createComment(value = null) {
return this.createTypedNode(COMMENT, value);
}
add(node, after = '*') {
const selectorObj = this.wideSelector(after);
for (const n of this.rawNodes) {
if (n && n[0] === NODE) {
if (!Array.isArray(n[3]))
n[3] = [];
if (Array.isArray(node)) {
for (const node_ of node)
this.rawAdd(n[3], node_.raw, selectorObj);
} else {
this.rawAdd(n[3], node.raw, selectorObj);
}
}
}
return this;
}
addRoot(node, after = '*') {
const selectorObj = this.wideSelector(after);
if (Array.isArray(node)) {
for (const node_ of node)
this.rawAdd(this.rawNodes, node_.raw, selectorObj);
} else {
this.rawAdd(this.rawNodes, node.raw, selectorObj);
}
return this;
}
remove(selector = '') {
const selectorObj = this.wideSelector(selector);
for (const n of this.rawNodes) {
if (n && n[0] === NODE && Array.isArray(n[3])) {
this.rawRemove(n[3], selectorObj);
if (!n[3].length)
n[3] = null;
}
}
return this;
}
removeRoot(selector = '') {
const selectorObj = this.wideSelector(selector);
this.rawRemove(this.rawNodes, selectorObj);
return this;
}
each(callback, self = false) {
if (self) {
for (const n of this.rawNodes) {
if (callback(new NodeObject(n)) === false)
return this;
}
} else {
for (const n of this.rawNodes) {
if (n[0] === NODE && n[3]) {
for (const nn of n[3])
if (callback(new NodeObject(nn)) === false)
return this;
}
}
}
return this;
}
eachSelf(callback) {
return this.each(callback, true);
}
eachDeep(callback, self = false) {
const deep = (nodes, route = '') => {
for (const n of nodes) {
const node = new NodeObject(n);
if (callback(node, route) === false)
return false;
if (node.type === NODE && node.value) {
if (deep(node.value, `${route}${route ? '/' : ''}${node.name}`) === false)
return false;
}
}
}
if (self) {
deep(this.rawNodes);
} else {
for (const n of this.rawNodes) {
if (n[0] === NODE && n[3])
if (deep(n[3]) === false)
break;
}
}
return this;
}
eachDeepSelf(callback) {
return this.eachDeep(callback, true);
}
rawSelect(nodes, selectorObj, callback) {
for (const n of nodes)
if (this.checkNode(n, selectorObj))
callback(n);
return this;
}
select(selector = '', self = false) {
let newRawNodes = [];
if (selector.indexOf('/') >= 0) {
const selectors = selector.split('/');
let res = this;
for (const sel of selectors) {
res = res.select(sel, self);
self = false;
}
newRawNodes = res.rawNodes;
} else {
const selectorObj = this.wideSelector(selector);
if (self) {
this.rawSelect(this.rawNodes, selectorObj, (node) => {
newRawNodes.push(node);
})
} else {
for (const n of this.rawNodes) {
if (n && n[0] === NODE && Array.isArray(n[3])) {
this.rawSelect(n[3], selectorObj, (node) => {
newRawNodes.push(node);
})
}
}
}
}
return new XmlParser(newRawNodes);
}
selectSelf(selector) {
return this.select(selector, true);
}
selectFirst(selector, self) {
const result = this.select(selector, self);
const node = (result.count ? result.rawNodes[0] : null);
return new NodeObject(node);
}
selectFirstSelf(selector) {
return this.selectFirst(selector, true);
}
toJson(options = {}) {
const {format = false} = options;
if (format)
return JSON.stringify(this.rawNodes, null, 2);
else
return JSON.stringify(this.rawNodes);
}
fromJson(jsonString) {
const parsed = JSON.parse(jsonString);
if (!Array.isArray(parsed))
throw new Error('JSON parse error: root element must be array');
this.rawNodes = parsed;
return this;
}
toString(options = {}) {
const {
encoding = 'utf-8',
format = false,
noHeader = false,
expandEmpty = false
} = options;
let deepType = 0;
let out = '';
if (!noHeader)
out += `<?xml version="1.0" encoding="${encoding}"?>`;
const nodesToString = (nodes, depth = 0) => {
let result = '';
const indent = '\n' + ' '.repeat(depth);
let lastType = 0;
for (const n of nodes) {
const node = new NodeObject(n);
let open = '';
let body = '';
let close = '';
if (node.type === NODE) {
if (!node.name)
continue;
let attrs = '';
const nodeAttrs = node.attrs();
if (nodeAttrs) {
for (const [attrName, attrValue] of nodeAttrs) {
if (typeof(attrValue) === 'string')
attrs += ` ${attrName}="${attrValue}"`;
else
if (attrValue)
attrs += ` ${attrName}`;
}
}
if (node.value)
body = nodesToString(node.value, depth + 2);
if (!body && !expandEmpty) {
open = (format && lastType !== TEXT ? indent : '');
open += `<${node.name}${attrs}/>`;
} else {
open = (format && lastType !== TEXT ? indent : '');
open += `<${node.name}${attrs}>`;
close = (format && deepType && deepType !== TEXT ? indent : '');
close += `</${node.name}>`;
}
} else if (node.type === TEXT) {
body = node.value || '';
} else if (node.type === CDATA) {
body = (format && lastType !== TEXT ? indent : '');
body += `<![CDATA[${node.value || ''}]]>`;
} else if (node.type === COMMENT) {
body = (format && lastType !== TEXT ? indent : '');
body += `<!--${node.value || ''}-->`;
}
result += `${open}${body}${close}`;
lastType = node.type;
}
deepType = lastType;
return result;
}
out += nodesToString(this.rawNodes) + (format ? '\n' : '');
return out;
}
fromString(xmlString, options = {}) {
const {
lowerCase = false,
whiteSpace = false,
pickNode = false,
} = options;
const parsed = [];
const root = this.createNode('root', null, parsed);//fake node
let node = root;
let route = '';
let routeStack = [];
let ignoreNode = false;
const onStartNode = (tag, tail, singleTag, cutCounter, cutTag) => {// eslint-disable-line no-unused-vars
if (tag == '?xml')
return;
if (!ignoreNode && pickNode) {
route += `${route ? '/' : ''}${tag}`;
ignoreNode = !pickNode(route);
}
let newNode = node;
if (!ignoreNode)
newNode = this.createNode(tag);
routeStack.push({tag, route, ignoreNode, node: newNode});
if (ignoreNode)
return;
if (tail && tail.trim() !== '') {
const parsedAttrs = sax.getAttrsSync(tail, lowerCase);
const attrs = new Map();
for (const attr of parsedAttrs.values()) {
attrs.set(attr.fn, attr.value);
}
if (attrs.size)
newNode.attrs(attrs);
}
if (!node.value)
node.value = [];
node.value.push(newNode.raw);
node = newNode;
};
const onEndNode = (tag, tail, singleTag, cutCounter, cutTag) => {// eslint-disable-line no-unused-vars
if (routeStack.length && routeStack[routeStack.length - 1].tag === tag) {
routeStack.pop();
if (routeStack.length) {
const last = routeStack[routeStack.length - 1];
route = last.route;
ignoreNode = last.ignoreNode;
node = last.node;
} else {
route = '';
ignoreNode = false;
node = root;
}
}
}
const onTextNode = (text, cutCounter, cutTag) => {// eslint-disable-line no-unused-vars
if (ignoreNode || (pickNode && !pickNode(`${route}/*TEXT`)))
return;
if (!whiteSpace && text.trim() == '')
return;
if (!node.value)
node.value = [];
node.value.push(this.createText(text).raw);
};
const onCdata = (tagData, cutCounter, cutTag) => {// eslint-disable-line no-unused-vars
if (ignoreNode || (pickNode && !pickNode(`${route}/*CDATA`)))
return;
if (!node.value)
node.value = [];
node.value.push(this.createCdata(tagData).raw);
}
const onComment = (tagData, cutCounter, cutTag) => {// eslint-disable-line no-unused-vars
if (ignoreNode || (pickNode && !pickNode(`${route}/*COMMENT`)))
return;
if (!node.value)
node.value = [];
node.value.push(this.createComment(tagData).raw);
}
sax.parseSync(xmlString, {
onStartNode, onEndNode, onTextNode, onCdata, onComment, lowerCase
});
this.rawNodes = parsed;
return this;
}
toObject(options = {}) {
const {
compactText = false
} = options;
const nodesToObject = (nodes) => {
const result = {};
for (const n of nodes) {
const node = new NodeObject(n);
if (node.type === NODE) {
if (!node.name)
continue;
let newNode = {};
const nodeAttrs = node.attrs();
if (nodeAttrs)
newNode['*ATTRS'] = Object.fromEntries(nodeAttrs);
if (node.value) {
Object.assign(newNode, nodesToObject(node.value));
//схлопывание текстового узла до string
if (compactText
&& !Array.isArray(newNode)
&& Object.prototype.hasOwnProperty.call(newNode, '*TEXT')
&& Object.keys(newNode).length === 1) {
newNode = newNode['*TEXT'];
}
}
if (!Object.prototype.hasOwnProperty.call(result, node.name)) {
result[node.name] = newNode;
} else {
if (!Array.isArray(result[node.name])) {
result[node.name] = [result[node.name]];
}
result[node.name].push(newNode);
}
} else if (node.type === TEXT) {
if (!result['*TEXT'])
result['*TEXT'] = '';
result['*TEXT'] += node.value || '';
} else if (node.type === CDATA) {
if (!result['*CDATA'])
result['*CDATA'] = '';
result['*CDATA'] += node.value || '';
} else if (node.type === COMMENT) {
if (!result['*COMMENT'])
result['*COMMENT'] = '';
result['*COMMENT'] += node.value || '';
}
}
return result;
}
return nodesToObject(this.rawNodes);
}
fromObject(xmlObject) {
const objectToNodes = (obj) => {
const result = [];
for (const [tag, objNode] of Object.entries(obj)) {
if (tag === '*TEXT') {
result.push(this.createText(objNode).raw);
} else if (tag === '*CDATA') {
result.push(this.createCdata(objNode).raw);
} else if (tag === '*COMMENT') {
result.push(this.createComment(objNode).raw);
} else if (tag === '*ATTRS') {
//пропускаем
} else {
if (typeof(objNode) === 'string') {
result.push(this.createNode(tag, null, [this.createText(objNode).raw]).raw);
} else if (Array.isArray(objNode)) {
for (const n of objNode) {
if (typeof(n) === 'string') {
result.push(this.createNode(tag, null, [this.createText(n).raw]).raw);
} else if (typeof(n) === 'object') {
result.push(this.createNode(tag, (n['*ATTRS'] ? Object.entries(n['*ATTRS']) : null), objectToNodes(n)).raw);
}
}
} else if (typeof(objNode) === 'object') {
result.push(this.createNode(tag, (objNode['*ATTRS'] ? Object.entries(objNode['*ATTRS']) : null), objectToNodes(objNode)).raw);
}
}
}
return result;
};
this.rawNodes = objectToNodes(xmlObject);
return this;
}
// XML Inspector start
narrowSelector(selector) {
const result = [];
selector = selector.trim();
//последний индекс не учитывется, только если не задан явно
if (selector && selector[selector.length - 1] == ']')
selector += '/';
const levels = selector.split('/');
for (const level of levels) {
const [name, indexPart] = level.split('[');
let index = 0;
if (indexPart) {
const i = indexPart.indexOf(']');
index = parseInt(indexPart.substring(0, i), 10) || 0;
}
let type = NODE;
if (name[0] === '*') {
const typeName = name.substring(1);
type = name2type[typeName];
if (!type)
throw new Error(`Unknown selector type: ${typeName}`);
}
result.push({type, name, index});
}
if (result.length);
result[result.length - 1].last = true;
return result;
}
inspect(selector = '') {
selector = this.narrowSelector(selector);
let raw = this.rawNodes;
for (const s of selector) {
if (s.name) {
let found = [];
for (const n of raw) {
if (n[0] === s.type && (n[0] !== NODE || s.name === '*NODE' || n[1] === s.name)) {
found.push(n);
if (found.length > s.index && !s.last)
break;
}
}
raw = found;
}
if (raw.length && !s.last) {
if (s.index < raw.length) {
raw = raw[s.index];
if (raw[0] === NODE && raw[3])
raw = raw[3];
else {
raw = [];
break;
}
} else {
raw = [];
break;
}
}
}
return new XmlParser(raw);
}
$$(selector) {
return this.inspect(selector);
}
$$array(selector) {
const res = this.inspect(selector);
const result = [];
for (const n of res.rawNodes)
if (n[0] === NODE)
result.push(new XmlParser([n]));
return result;
}
$(selector) {
const res = this.inspect(selector);
const node = (res.count ? res.rawNodes[0] : null);
return new NodeObject(node);
}
v(selector = '') {
const res = this.$(selector);
return (res.type ? res.value : null);
}
text(selector = '') {
const res = this.$(`${selector}/*TEXT`);
return (res.type === TEXT ? res.value : null);
}
comment(selector = '') {
const res = this.$(`${selector}/*COMMENT`);
return (res.type === COMMENT ? res.value : null);
}
cdata(selector = '') {
const res = this.$(`${selector}/*CDATA`);
return (res.type === CDATA ? res.value : null);
}
concat(selector = '') {
const res = this.$$(selector);
const out = [];
for (const n of res.rawNodes) {
const node = new NodeObject(n);
if (node.type && node.type !== NODE)
out.push(node.value);
}
return (out.length ? out.join('') : null);
}
attrs(selector = '') {
const res = this.$(selector);
const attrs = res.attrs();
return (res.type === NODE && attrs ? Object.fromEntries(attrs) : null);
}
// XML Inspector finish
}
module.exports = XmlParser;

367
server/core/xml/sax.js Normal file
View File

@@ -0,0 +1,367 @@
function parseSync(xstr, options) {
const dummy = () => {};
let {onStartNode: _onStartNode = dummy,
onEndNode: _onEndNode = dummy,
onTextNode: _onTextNode = dummy,
onCdata: _onCdata = dummy,
onComment: _onComment = dummy,
onProgress: _onProgress = dummy,
innerCut = new Set(),
lowerCase = true,
} = options;
let i = 0;
const len = xstr.length;
const progStep = len/20;
let nextProg = 0;
let cutCounter = 0;
let cutTag = '';
let inCdata;
let inComment;
let leftData = 0;
while (i < len) {
inCdata = false;
inComment = false;
let singleTag = false;
let left = xstr.indexOf('<', i);
if (left < 0)
break;
leftData = left;
if (left < len - 2 && xstr[left + 1] == '!') {
if (xstr[left + 2] == '-') {
const leftComment = xstr.indexOf('<!--', left);
if (leftComment == left) {
inComment = true;
leftData = left + 3;
}
}
if (!inComment && xstr[left + 2] == '[') {
const leftCdata = xstr.indexOf('<![CDATA[', left);
if (leftCdata == left) {
inCdata = true;
leftData = left + 8;
}
}
}
if (left != i) {
const text = xstr.substr(i, left - i);
_onTextNode(text, cutCounter, cutTag);
}
let right = null;
let rightData = null;
if (inCdata) {
rightData = xstr.indexOf(']]>', leftData + 1);
if (rightData < 0)
break;
right = rightData + 2;
} else if (inComment) {
rightData = xstr.indexOf('-->', leftData + 1);
if (rightData < 0)
break;
right = rightData + 2;
} else {
rightData = xstr.indexOf('>', leftData + 1);
if (rightData < 0)
break;
right = rightData;
if (xstr[right - 1] === '/') {
singleTag = true;
rightData--;
}
}
let tagData = xstr.substr(leftData + 1, rightData - leftData - 1);
if (inCdata) {
_onCdata(tagData, cutCounter, cutTag);
} else if (inComment) {
_onComment(tagData, cutCounter, cutTag);
} else {
let tag = '';
let tail = '';
const firstSpace = tagData.indexOf(' ');
if (firstSpace >= 0) {
tail = tagData.substr(firstSpace);
tag = tagData.substr(0, firstSpace);
} else {
tag = tagData;
}
if (lowerCase)
tag = tag.toLowerCase();
if (innerCut.has(tag) && (!cutCounter || cutTag === tag)) {
if (!cutCounter)
cutTag = tag;
cutCounter++;
}
let endTag = (singleTag ? tag : '');
if (tag === '' || tag[0] !== '/') {
_onStartNode(tag, tail, singleTag, cutCounter, cutTag);
} else {
endTag = tag.substr(1);
}
if (endTag)
_onEndNode(endTag, tail, singleTag, cutCounter, cutTag);
if (cutTag === endTag) {
cutCounter = (cutCounter > 0 ? cutCounter - 1 : 0);
if (!cutCounter)
cutTag = '';
}
}
if (right >= nextProg) {
_onProgress(Math.round(right/(len + 1)*100));
nextProg += progStep;
}
i = right + 1;
}
if (i < len) {
if (inCdata) {
_onCdata(xstr.substr(leftData + 1, len - leftData - 1), cutCounter, cutTag);
} else if (inComment) {
_onComment(xstr.substr(leftData + 1, len - leftData - 1), cutCounter, cutTag);
} else {
_onTextNode(xstr.substr(i, len - i), cutCounter, cutTag);
}
}
_onProgress(100);
}
//асинхронная копия parseSync
//делается заменой "_on" => "await _on" после while
async function parse(xstr, options) {
const dummy = () => {};
let {onStartNode: _onStartNode = dummy,
onEndNode: _onEndNode = dummy,
onTextNode: _onTextNode = dummy,
onCdata: _onCdata = dummy,
onComment: _onComment = dummy,
onProgress: _onProgress = dummy,
innerCut = new Set(),
lowerCase = true,
} = options;
let i = 0;
const len = xstr.length;
const progStep = len/20;
let nextProg = 0;
let cutCounter = 0;
let cutTag = '';
let inCdata;
let inComment;
let leftData = 0;
while (i < len) {
inCdata = false;
inComment = false;
let singleTag = false;
let left = xstr.indexOf('<', i);
if (left < 0)
break;
leftData = left;
if (left < len - 2 && xstr[left + 1] == '!') {
if (xstr[left + 2] == '-') {
const leftComment = xstr.indexOf('<!--', left);
if (leftComment == left) {
inComment = true;
leftData = left + 3;
}
}
if (!inComment && xstr[left + 2] == '[') {
const leftCdata = xstr.indexOf('<![CDATA[', left);
if (leftCdata == left) {
inCdata = true;
leftData = left + 8;
}
}
}
if (left != i) {
const text = xstr.substr(i, left - i);
await _onTextNode(text, cutCounter, cutTag);
}
let right = null;
let rightData = null;
if (inCdata) {
rightData = xstr.indexOf(']]>', leftData + 1);
if (rightData < 0)
break;
right = rightData + 2;
} else if (inComment) {
rightData = xstr.indexOf('-->', leftData + 1);
if (rightData < 0)
break;
right = rightData + 2;
} else {
rightData = xstr.indexOf('>', leftData + 1);
if (rightData < 0)
break;
right = rightData;
if (xstr[right - 1] === '/') {
singleTag = true;
rightData--;
}
}
let tagData = xstr.substr(leftData + 1, rightData - leftData - 1);
if (inCdata) {
await _onCdata(tagData, cutCounter, cutTag);
} else if (inComment) {
await _onComment(tagData, cutCounter, cutTag);
} else {
let tag = '';
let tail = '';
const firstSpace = tagData.indexOf(' ');
if (firstSpace >= 0) {
tail = tagData.substr(firstSpace);
tag = tagData.substr(0, firstSpace);
} else {
tag = tagData;
}
if (lowerCase)
tag = tag.toLowerCase();
if (innerCut.has(tag) && (!cutCounter || cutTag === tag)) {
if (!cutCounter)
cutTag = tag;
cutCounter++;
}
let endTag = (singleTag ? tag : '');
if (tag === '' || tag[0] !== '/') {
await _onStartNode(tag, tail, singleTag, cutCounter, cutTag);
} else {
endTag = tag.substr(1);
}
if (endTag)
await _onEndNode(endTag, tail, singleTag, cutCounter, cutTag);
if (cutTag === endTag) {
cutCounter = (cutCounter > 0 ? cutCounter - 1 : 0);
if (!cutCounter)
cutTag = '';
}
}
if (right >= nextProg) {
await _onProgress(Math.round(right/(len + 1)*100));
nextProg += progStep;
}
i = right + 1;
}
if (i < len) {
if (inCdata) {
await _onCdata(xstr.substr(leftData + 1, len - leftData - 1), cutCounter, cutTag);
} else if (inComment) {
await _onComment(xstr.substr(leftData + 1, len - leftData - 1), cutCounter, cutTag);
} else {
await _onTextNode(xstr.substr(i, len - i), cutCounter, cutTag);
}
}
await _onProgress(100);
}
function getAttrsSync(tail, lowerCase = true) {
let result = new Map();
let name = '';
let value = '';
let vOpen = '';
let inName = false;
let inValue = false;
let waitValue = false;
let waitEq = true;
const pushResult = () => {
if (waitEq)
value = true;
if (lowerCase)
name = name.toLowerCase();
if (name != '') {
const fn = name;
let ns = '';
if (fn.indexOf(':') >= 0) {
[ns, name] = fn.split(':');
}
result.set(fn, {value, ns, name, fn});
}
name = '';
value = '';
vOpen = '';
inName = false;
inValue = false;
waitValue = false;
waitEq = true;
};
tail = tail.replace(/[\t\n\r]/g, ' ');
for (let i = 0; i < tail.length; i++) {
const c = tail.charAt(i);
if (c == ' ') {
if (inValue) {
if (vOpen == '"')
value += c;
else
pushResult();
} else if (inName) {
inName = false;
}
} else if (!inValue && c == '=') {
waitEq = false;
waitValue = true;
inName = false;
} else if (c == '"') {
if (inValue) {
pushResult();
} else if (waitValue) {
inValue = true;
vOpen = '"';
}
} else if (inValue) {
value += c;
} else if (inName) {
name += c;
} else if (waitEq) {
pushResult();
inName = true;
name = c;
} else if (waitValue) {
waitValue = false;
inValue = true;
vOpen = ' ';
value = c;
} else {
inName = true;
name = c;
}
}
if (name != '')
pushResult();
return result;
}
module.exports = {
parseSync,
getAttrsSync,
parse
}

View File

@@ -154,10 +154,16 @@ async function main() {
if (devModule)
devModule.logQueries(app);
const opds = require('./core/opds');
opds(app, config);
initStatic(app, config);
const WebAccess = require('./core/WebAccess');
const webAccess = new WebAccess(config);
await webAccess.init();
const { WebSocketController } = require('./controllers');
new WebSocketController(wss, config);
new WebSocketController(wss, webAccess, config);
if (devModule) {
devModule.logErrors(app);
@@ -189,32 +195,31 @@ function initStatic(app, config) {
return next();
}
if (path.extname(req.path) == '.json')
return next();
if (path.extname(req.path) == '') {
const bookFile = `${config.publicFilesDir}${req.path}`;
const bookFileDesc = `${bookFile}.d.json`;
const bookFile = `${config.publicFilesDir}${req.path}`;
const bookFileDesc = `${bookFile}.json`;
let downFileName = '';
//восстановим из json-файла описания
try {
if (await fs.pathExists(bookFile) && await fs.pathExists(bookFileDesc)) {
await utils.touchFile(bookFile);
await utils.touchFile(bookFileDesc);
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);
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);
}
} catch(e) {
log(LM_ERR, e.message);
}
if (downFileName)
res.downFileName = downFileName;
if (downFileName)
res.downFileName = downFileName;
}
return next();
});