Skip to content

Commit

Permalink
Merge branch 'release/1.5.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
bookpauk committed Apr 25, 2023
2 parents c3810d1 + 74ece78 commit 84843fb
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 20 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
1.5.5 / 2023-04-25

- Улучшение работы с inpx: теперь понимает zip-архивы, вложенные в каталоги (библиотека Траума)
- Улучшение работы с zip-файлами: теперь понимает кодировку cp866 в именах файлов

1.5.4 / 2023-04-12

- Добавлена возможность поиска по типу файла
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "inpx-web",
"version": "1.5.4",
"version": "1.5.5",
"author": "Book Pauk <bookpauk@gmail.com>",
"license": "CC0-1.0",
"repository": "bookpauk/inpx-web",
Expand Down
39 changes: 22 additions & 17 deletions server/core/WebWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const os = require('os');
const path = require('path');
const fs = require('fs-extra');
const _ = require('lodash');
const iconv = require('iconv-lite');

const ZipReader = require('./ZipReader');
const WorkerState = require('./WorkerState');//singleton
Expand Down Expand Up @@ -369,15 +370,14 @@ class WebWorker {
return result;
}

async extractBook(bookPath) {
async extractBook(libFolder, libFile) {
const outFile = `${this.config.tempDir}/${utils.randomHexString(30)}`;

bookPath = bookPath.replace(/\\/g, '/').replace(/\/\//g, '/');

const i = bookPath.indexOf('/');
const folder = `${this.config.libDir}/${(i >= 0 ? bookPath.substring(0, i) : bookPath )}`;
const file = (i >= 0 ? bookPath.substring(i + 1) : '' );
libFolder = libFolder.replace(/\\/g, '/').replace(/\/\//g, '/');

const folder = `${this.config.libDir}/${libFolder}`;
const file = libFile;

const fullPath = `${folder}/${file}`;

if (!file || await fs.pathExists(fullPath)) {// файл есть на диске
Expand All @@ -390,21 +390,26 @@ class WebWorker {

try {
await zipReader.extractToFile(file, outFile);

if (!await fs.pathExists(outFile)) {//не удалось найти в архиве, попробуем имя файла в кодировке cp866
await zipReader.extractToFile(iconv.encode(file, 'cp866').toString(), outFile);
}

return outFile;
} finally {
await zipReader.close();
}
}
}

async restoreBook(bookUid, bookPath, downFileName) {
async restoreBook(bookUid, libFolder, libFile, downFileName) {
const db = this.db;

let extractedFile = '';
let hash = '';

if (!this.remoteLib) {
extractedFile = await this.extractBook(bookPath);
extractedFile = await this.extractBook(libFolder, libFile);
hash = await utils.getFileHash(extractedFile, 'sha256', 'hex');
} else {
hash = await this.remoteLib.downloadBook(bookUid);
Expand All @@ -424,7 +429,7 @@ class WebWorker {
await utils.touchFile(bookFile);
}

await fs.writeFile(bookFileDesc, JSON.stringify({bookPath, downFileName}));
await fs.writeFile(bookFileDesc, JSON.stringify({libFolder, libFile, downFileName}));
} else {
if (extractedFile)
await fs.remove(extractedFile);
Expand All @@ -437,8 +442,7 @@ class WebWorker {
table: 'file_hash',
replace: true,
rows: [
{id: bookPath, hash},
{id: hash, bookPath, downFileName}
{id: bookUid, hash},
]
});

Expand All @@ -452,7 +456,7 @@ class WebWorker {
const db = this.db;
let link = '';

//найдем downFileName и bookPath
//найдем downFileName, libFolder, libFile
let rows = await db.select({table: 'book', where: `@@hash('_uid', ${db.esc(bookUid)})`});
if (!rows.length)
throw new Error('404 Файл не найден');
Expand All @@ -479,11 +483,12 @@ class WebWorker {
if (downFileName.substring(downFileName.length - ext.length) != ext)
downFileName += ext;

const bookPath = `${book.folder}/${book.file}${ext}`;
const libFolder = book.folder;
const libFile = `${book.file}${ext}`;

//найдем хеш
rows = await db.select({table: 'file_hash', where: `@@id(${db.esc(bookPath)})`});
if (rows.length) {//хеш найден по bookPath
rows = await db.select({table: 'file_hash', where: `@@id(${db.esc(bookUid)})`});
if (rows.length) {//хеш найден по bookUid
const hash = rows[0].hash;
const bookFile = `${this.config.bookDir}/${hash}`;
const bookFileDesc = `${bookFile}.d.json`;
Expand All @@ -494,13 +499,13 @@ class WebWorker {
}

if (!link) {
link = await this.restoreBook(bookUid, bookPath, downFileName)
link = await this.restoreBook(bookUid, libFolder, libFile, downFileName);
}

if (!link)
throw new Error('404 Файл не найден');

return {link, bookPath, downFileName};
return {link, libFolder, libFile, downFileName};
} catch(e) {
log(LM_ERR, `getBookLink error: ${e.message}`);
if (e.message.indexOf('ENOENT') >= 0)
Expand Down

0 comments on commit 84843fb

Please sign in to comment.