Улучшение отдачи файлов книг и статики

This commit is contained in:
Book Pauk
2022-10-20 21:46:05 +07:00
parent f7297ac573
commit 8c1ec1dc93
3 changed files with 52 additions and 45 deletions

View File

@@ -58,8 +58,8 @@ class WebWorker {
const dirConfig = [ const dirConfig = [
{ {
dir: `${this.config.publicDir}/files`, dir: config.filesDir,
maxSize: this.config.maxFilesDirSize, maxSize: config.maxFilesDirSize,
}, },
]; ];
@@ -358,20 +358,25 @@ class WebWorker {
hash = await this.remoteLib.downloadBook(bookPath, downFileName); hash = await this.remoteLib.downloadBook(bookPath, downFileName);
} }
const link = `/files/${hash}`; const link = `${this.config.filesPathStatic}/${hash}`;
const publicPath = `${this.config.publicDir}${link}`; const bookFile = `${this.config.filesDir}/${hash}`;
const bookFileDesc = `${bookFile}.json`;
if (!await fs.pathExists(publicPath)) { if (!await fs.pathExists(bookFile) || !await fs.pathExists(bookFileDesc)) {
await fs.ensureDir(path.dirname(publicPath)); await fs.ensureDir(path.dirname(bookFile));
const tmpFile = `${this.config.tempDir}/${utils.randomHexString(30)}`; const tmpFile = `${this.config.tempDir}/${utils.randomHexString(30)}`;
await utils.gzipFile(extractedFile, tmpFile, 4); await utils.gzipFile(extractedFile, tmpFile, 4);
await fs.remove(extractedFile); await fs.remove(extractedFile);
await fs.move(tmpFile, publicPath, {overwrite: true}); await fs.move(tmpFile, bookFile, {overwrite: true});
await fs.writeFile(bookFileDesc, JSON.stringify({bookPath, downFileName}));
} else { } else {
if (extractedFile) if (extractedFile)
await fs.remove(extractedFile); await fs.remove(extractedFile);
await utils.touchFile(publicPath);
await utils.touchFile(bookFile);
await utils.touchFile(bookFileDesc);
} }
await db.insert({ await db.insert({
@@ -399,11 +404,10 @@ class WebWorker {
const rows = await db.select({table: 'file_hash', where: `@@id(${db.esc(bookPath)})`}); const rows = await db.select({table: 'file_hash', where: `@@id(${db.esc(bookPath)})`});
if (rows.length) {//хеш найден по bookPath if (rows.length) {//хеш найден по bookPath
const hash = rows[0].hash; const hash = rows[0].hash;
link = `/files/${hash}`; const bookFileDesc = `${this.config.filesDir}/${hash}.json`;
const publicPath = `${this.config.publicDir}${link}`;
if (!await fs.pathExists(publicPath)) { if (await fs.pathExists(bookFileDesc)) {
link = ''; link = `${this.config.filesPathStatic}/${hash}`;
} }
} }
@@ -423,6 +427,7 @@ class WebWorker {
} }
} }
/*
async restoreBookFile(publicPath) { async restoreBookFile(publicPath) {
this.checkMyState(); this.checkMyState();
@@ -462,6 +467,7 @@ class WebWorker {
throw new Error('404 Файл не найден'); throw new Error('404 Файл не найден');
} }
} }
*/
async getInpxFile(params) { async getInpxFile(params) {
let data = null; let data = null;

View File

@@ -13,15 +13,6 @@ module.exports = async(config) => {
return; return;
} }
//сохраним files
const filesDir = `${config.publicDir}/files`;
let tmpFilesDir = '';
if (await fs.pathExists(filesDir)) {
tmpFilesDir = `${config.dataDir}/files`;
if (!await fs.pathExists(tmpFilesDir))
await fs.move(filesDir, tmpFilesDir);
}
await fs.remove(config.publicDir); await fs.remove(config.publicDir);
//извлекаем новый webApp //извлекаем новый webApp
@@ -35,10 +26,6 @@ module.exports = async(config) => {
await zipReader.close(); await zipReader.close();
} }
//восстановим files
if (tmpFilesDir)
await fs.move(tmpFilesDir, filesDir);
await fs.writeFile(verFile, config.version); await fs.writeFile(verFile, config.version);
await fs.remove(zipFile); await fs.remove(zipFile);
}; };

View File

@@ -49,9 +49,14 @@ async function init() {
config.tempDir = `${config.dataDir}/tmp`; config.tempDir = `${config.dataDir}/tmp`;
config.logDir = `${config.dataDir}/log`; config.logDir = `${config.dataDir}/log`;
config.publicDir = `${config.dataDir}/public`; config.publicDir = `${config.dataDir}/public`;
config.publicFilesDir = `${config.dataDir}/public-files`;
config.filesPathStatic = `/book`;
config.filesDir = `${config.publicFilesDir}${config.filesPathStatic}`;
configManager.config = config; configManager.config = config;
await fs.ensureDir(config.dataDir); await fs.ensureDir(config.dataDir);
await fs.ensureDir(config.filesDir);
await fs.ensureDir(config.tempDir); await fs.ensureDir(config.tempDir);
await fs.emptyDir(config.tempDir); await fs.emptyDir(config.tempDir);
@@ -170,29 +175,42 @@ async function main() {
} }
function initStatic(app, config) { function initStatic(app, config) {
const WebWorker = require('./core/WebWorker');//singleton /*
const webWorker = new WebWorker(config); publicFilesDir = `${config.dataDir}/public-files`;
filesPathStatic = `/book`;
filesDir = `${config.publicFilesDir}${config.filesPathStatic}`;
*/
const filesPath = `${config.filesPathStatic}/`;
//загрузка или восстановление файлов в /files, при необходимости //загрузка или восстановление файлов в /files, при необходимости
app.use(async(req, res, next) => { app.use(async(req, res, next) => {
if ((req.method !== 'GET' && req.method !== 'HEAD') || if ((req.method !== 'GET' && req.method !== 'HEAD') ||
!(req.path.indexOf('/files/') === 0) !(req.path.indexOf(filesPath) === 0)
) { ) {
return next(); return next();
} }
const publicPath = `${config.publicDir}${req.path}`; if (path.extname(req.path) == '.json')
return next();
const bookFile = `${config.publicFilesDir}${req.path}`;
const bookFileDesc = `${bookFile}.json`;
let downFileName = ''; let downFileName = '';
//восстановим //восстановим из json-файла описания
try { try {
if (!await fs.pathExists(publicPath)) { if (await fs.pathExists(bookFile) && await fs.pathExists(bookFileDesc)) {
downFileName = await webWorker.restoreBookFile(publicPath); await utils.touchFile(bookFile);
await utils.touchFile(bookFileDesc);
let desc = await fs.readFile(bookFileDesc, 'utf8');
desc = JSON.parse(desc);
downFileName = desc.downFileName;
} else { } else {
downFileName = await webWorker.getDownFileName(publicPath); await fs.remove(bookFile);
await fs.remove(bookFileDesc);
} }
} catch(e) { } catch(e) {
//quiet log(LM_ERR, e.message);
} }
if (downFileName) if (downFileName)
@@ -202,20 +220,16 @@ function initStatic(app, config) {
}); });
//заголовки при отдаче //заголовки при отдаче
const filesDir = utils.toUnixPath(`${config.publicDir}/files`); app.use(config.filesPathStatic, express.static(config.filesDir, {
app.use(express.static(config.publicDir, { setHeaders: (res) => {
setHeaders: (res, filePath) => { if (res.downFileName) {
//res.set('Cache-Control', 'no-cache');
//res.set('Expires', '-1');
if (utils.toUnixPath(path.dirname(filePath)) == filesDir) {
res.set('Content-Encoding', 'gzip'); res.set('Content-Encoding', 'gzip');
res.set('Content-Disposition', `inline; filename*=UTF-8''${encodeURIComponent(res.downFileName)}`);
if (res.downFileName)
res.set('Content-Disposition', `inline; filename*=UTF-8''${encodeURIComponent(res.downFileName)}`);
} }
}, },
})); }));
app.use(express.static(config.publicDir));
} }
(async() => { (async() => {