Улучшение отдачи файлов книг и статики
This commit is contained in:
@@ -58,8 +58,8 @@ class WebWorker {
|
||||
|
||||
const dirConfig = [
|
||||
{
|
||||
dir: `${this.config.publicDir}/files`,
|
||||
maxSize: this.config.maxFilesDirSize,
|
||||
dir: config.filesDir,
|
||||
maxSize: config.maxFilesDirSize,
|
||||
},
|
||||
];
|
||||
|
||||
@@ -358,20 +358,25 @@ class WebWorker {
|
||||
hash = await this.remoteLib.downloadBook(bookPath, downFileName);
|
||||
}
|
||||
|
||||
const link = `/files/${hash}`;
|
||||
const publicPath = `${this.config.publicDir}${link}`;
|
||||
const link = `${this.config.filesPathStatic}/${hash}`;
|
||||
const bookFile = `${this.config.filesDir}/${hash}`;
|
||||
const bookFileDesc = `${bookFile}.json`;
|
||||
|
||||
if (!await fs.pathExists(publicPath)) {
|
||||
await fs.ensureDir(path.dirname(publicPath));
|
||||
if (!await fs.pathExists(bookFile) || !await fs.pathExists(bookFileDesc)) {
|
||||
await fs.ensureDir(path.dirname(bookFile));
|
||||
|
||||
const tmpFile = `${this.config.tempDir}/${utils.randomHexString(30)}`;
|
||||
await utils.gzipFile(extractedFile, tmpFile, 4);
|
||||
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 {
|
||||
if (extractedFile)
|
||||
await fs.remove(extractedFile);
|
||||
await utils.touchFile(publicPath);
|
||||
|
||||
await utils.touchFile(bookFile);
|
||||
await utils.touchFile(bookFileDesc);
|
||||
}
|
||||
|
||||
await db.insert({
|
||||
@@ -399,11 +404,10 @@ class WebWorker {
|
||||
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 = `${this.config.publicDir}${link}`;
|
||||
const bookFileDesc = `${this.config.filesDir}/${hash}.json`;
|
||||
|
||||
if (!await fs.pathExists(publicPath)) {
|
||||
link = '';
|
||||
if (await fs.pathExists(bookFileDesc)) {
|
||||
link = `${this.config.filesPathStatic}/${hash}`;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -423,6 +427,7 @@ class WebWorker {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
async restoreBookFile(publicPath) {
|
||||
this.checkMyState();
|
||||
|
||||
@@ -462,6 +467,7 @@ class WebWorker {
|
||||
throw new Error('404 Файл не найден');
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
async getInpxFile(params) {
|
||||
let data = null;
|
||||
|
||||
@@ -13,15 +13,6 @@ module.exports = async(config) => {
|
||||
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);
|
||||
|
||||
//извлекаем новый webApp
|
||||
@@ -35,10 +26,6 @@ module.exports = async(config) => {
|
||||
await zipReader.close();
|
||||
}
|
||||
|
||||
//восстановим files
|
||||
if (tmpFilesDir)
|
||||
await fs.move(tmpFilesDir, filesDir);
|
||||
|
||||
await fs.writeFile(verFile, config.version);
|
||||
await fs.remove(zipFile);
|
||||
};
|
||||
@@ -49,9 +49,14 @@ async function init() {
|
||||
config.tempDir = `${config.dataDir}/tmp`;
|
||||
config.logDir = `${config.dataDir}/log`;
|
||||
config.publicDir = `${config.dataDir}/public`;
|
||||
config.publicFilesDir = `${config.dataDir}/public-files`;
|
||||
config.filesPathStatic = `/book`;
|
||||
config.filesDir = `${config.publicFilesDir}${config.filesPathStatic}`;
|
||||
|
||||
configManager.config = config;
|
||||
|
||||
await fs.ensureDir(config.dataDir);
|
||||
await fs.ensureDir(config.filesDir);
|
||||
await fs.ensureDir(config.tempDir);
|
||||
await fs.emptyDir(config.tempDir);
|
||||
|
||||
@@ -170,29 +175,42 @@ async function main() {
|
||||
}
|
||||
|
||||
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, при необходимости
|
||||
app.use(async(req, res, next) => {
|
||||
if ((req.method !== 'GET' && req.method !== 'HEAD') ||
|
||||
!(req.path.indexOf('/files/') === 0)
|
||||
!(req.path.indexOf(filesPath) === 0)
|
||||
) {
|
||||
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 = '';
|
||||
//восстановим
|
||||
//восстановим из json-файла описания
|
||||
try {
|
||||
if (!await fs.pathExists(publicPath)) {
|
||||
downFileName = await webWorker.restoreBookFile(publicPath);
|
||||
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 {
|
||||
downFileName = await webWorker.getDownFileName(publicPath);
|
||||
await fs.remove(bookFile);
|
||||
await fs.remove(bookFileDesc);
|
||||
}
|
||||
} catch(e) {
|
||||
//quiet
|
||||
log(LM_ERR, e.message);
|
||||
}
|
||||
|
||||
if (downFileName)
|
||||
@@ -202,20 +220,16 @@ function initStatic(app, config) {
|
||||
});
|
||||
|
||||
//заголовки при отдаче
|
||||
const filesDir = utils.toUnixPath(`${config.publicDir}/files`);
|
||||
app.use(express.static(config.publicDir, {
|
||||
setHeaders: (res, filePath) => {
|
||||
//res.set('Cache-Control', 'no-cache');
|
||||
//res.set('Expires', '-1');
|
||||
|
||||
if (utils.toUnixPath(path.dirname(filePath)) == filesDir) {
|
||||
app.use(config.filesPathStatic, express.static(config.filesDir, {
|
||||
setHeaders: (res) => {
|
||||
if (res.downFileName) {
|
||||
res.set('Content-Encoding', 'gzip');
|
||||
|
||||
if (res.downFileName)
|
||||
res.set('Content-Disposition', `inline; filename*=UTF-8''${encodeURIComponent(res.downFileName)}`);
|
||||
res.set('Content-Disposition', `inline; filename*=UTF-8''${encodeURIComponent(res.downFileName)}`);
|
||||
}
|
||||
},
|
||||
}));
|
||||
|
||||
app.use(express.static(config.publicDir));
|
||||
}
|
||||
|
||||
(async() => {
|
||||
|
||||
Reference in New Issue
Block a user