Работа над проектом
This commit is contained in:
@@ -442,6 +442,9 @@ class DbCreator {
|
||||
await db.create({table: 'query_cache'});
|
||||
await db.create({table: 'query_time'});
|
||||
|
||||
//кэш-таблица имен файлов и их хешей
|
||||
await db.create({table: 'file_hash'});
|
||||
|
||||
callback({job: 'done', jobMessage: ''});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
const os = require('os');
|
||||
const path = require('path');
|
||||
const fs = require('fs-extra');
|
||||
const _ = require('lodash');
|
||||
|
||||
const ZipReader = require('./ZipReader');
|
||||
const WorkerState = require('./WorkerState');
|
||||
const { JembaDbThread } = require('jembadb');
|
||||
const DbCreator = require('./DbCreator');
|
||||
@@ -263,6 +265,95 @@ class WebWorker {
|
||||
return result;
|
||||
}
|
||||
|
||||
async extractBook(bookPath) {
|
||||
const tempDir = this.config.tempDir;
|
||||
const outFile = `${tempDir}/${utils.randomHexString(30)}`;
|
||||
|
||||
const folder = path.dirname(bookPath);
|
||||
const file = path.basename(bookPath);
|
||||
|
||||
const zipReader = new ZipReader();
|
||||
await zipReader.open(folder);
|
||||
|
||||
try {
|
||||
await zipReader.extractToFile(file, outFile);
|
||||
return outFile;
|
||||
} finally {
|
||||
zipReader.close();
|
||||
}
|
||||
}
|
||||
|
||||
async restoreBook(bookPath) {
|
||||
const db = this.db;
|
||||
const publicDir = this.config.publicDir;
|
||||
|
||||
const extractedFile = await this.extractBook(bookPath);
|
||||
|
||||
const hash = await utils.getFileHash(extractedFile, 'sha256', 'hex');
|
||||
const link = `/files/${hash}`;
|
||||
const publicPath = `${publicDir}${link}`;
|
||||
|
||||
if (!await fs.pathExists(publicPath)) {
|
||||
await fs.move(extractedFile, publicPath);
|
||||
} else {
|
||||
await fs.remove(extractedFile);
|
||||
}
|
||||
|
||||
await db.insert({
|
||||
table: 'file_hash',
|
||||
replace: true,
|
||||
rows: [
|
||||
{id: bookPath, hash},
|
||||
{id: hash, bookPath}
|
||||
]
|
||||
});
|
||||
|
||||
return link;
|
||||
}
|
||||
|
||||
async getBookLink(bookPath) {
|
||||
this.checkMyState();
|
||||
|
||||
const db = this.db;
|
||||
const publicDir = this.config.publicDir;
|
||||
let link = '';
|
||||
|
||||
//найдем хеш
|
||||
const rows = await db.select({table: 'file_hash', where: `@@id(${db.esc(bookPath)})`});
|
||||
if (rows.length) {//хеш найден по bookPath
|
||||
const hash = rows[0].hash;
|
||||
link = `/files/${hash}`;
|
||||
const publicPath = `${publicDir}${link}`;
|
||||
|
||||
if (!await fs.pathExists(publicPath)) {
|
||||
link = '';
|
||||
}
|
||||
}
|
||||
|
||||
if (!link) {
|
||||
link = await this.restoreBook(bookPath)
|
||||
}
|
||||
|
||||
if (!link)
|
||||
throw new Error('404 Файл не найден');
|
||||
|
||||
return {link};
|
||||
}
|
||||
|
||||
async restoreBookFile(publicPath) {
|
||||
const db = this.db;
|
||||
const hash = path.basename(publicPath);
|
||||
|
||||
//найдем bookPath
|
||||
const rows = await db.select({table: 'file_hash', where: `@@id(${db.esc(hash)})`});
|
||||
if (rows.length) {//bookPath найден по хешу
|
||||
const bookPath = rows[0].bookPath;
|
||||
await this.restoreBook(bookPath);
|
||||
} else {//bookPath не найден
|
||||
throw new Error('404 Файл не найден');
|
||||
}
|
||||
}
|
||||
|
||||
async logServerStats() {
|
||||
while (1) {// eslint-disable-line
|
||||
try {
|
||||
@@ -274,7 +365,7 @@ class WebWorker {
|
||||
} catch (e) {
|
||||
log(LM_ERR, e.message);
|
||||
}
|
||||
await utils.sleep(5000);
|
||||
await utils.sleep(5*1000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,6 +89,10 @@ function intersectSet(arrSet) {
|
||||
return result;
|
||||
}
|
||||
|
||||
function randomHexString(len) {
|
||||
return crypto.randomBytes(len).toString('hex')
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
sleep,
|
||||
versionText,
|
||||
@@ -99,4 +103,5 @@ module.exports = {
|
||||
getFileHash,
|
||||
getBufHash,
|
||||
intersectSet,
|
||||
randomHexString,
|
||||
};
|
||||
Reference in New Issue
Block a user