Поправки для поддержки reverse-proxy, рефакторинг
This commit is contained in:
@@ -50,13 +50,14 @@ async function init() {
|
||||
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}`;
|
||||
config.rootPathStatic = `/root`;
|
||||
config.bookPathStatic = `${config.rootPathStatic}/book`;
|
||||
config.bookDir = `${config.publicFilesDir}/book`;
|
||||
|
||||
configManager.config = config;
|
||||
|
||||
await fs.ensureDir(config.dataDir);
|
||||
await fs.ensureDir(config.filesDir);
|
||||
await fs.ensureDir(config.bookDir);
|
||||
await fs.ensureDir(config.tempDir);
|
||||
await fs.emptyDir(config.tempDir);
|
||||
|
||||
@@ -148,7 +149,7 @@ async function main() {
|
||||
if (branch == 'development') {
|
||||
const devFileName = './dev.js'; //require ignored by pkg -50Mb executable size
|
||||
devModule = require(devFileName);
|
||||
devModule.webpackDevMiddleware(app);
|
||||
//devModule.webpackDevMiddleware(app);
|
||||
}
|
||||
|
||||
if (devModule)
|
||||
@@ -156,6 +157,8 @@ async function main() {
|
||||
|
||||
const opds = require('./core/opds');
|
||||
opds(app, config);
|
||||
|
||||
const initStatic = require('./static');
|
||||
initStatic(app, config);
|
||||
|
||||
const webAccess = new (require('./core/WebAccess'))(config);
|
||||
@@ -179,73 +182,6 @@ async function main() {
|
||||
});
|
||||
}
|
||||
|
||||
function initStatic(app, config) {
|
||||
/*
|
||||
publicFilesDir = `${config.dataDir}/public-files`;
|
||||
filesPathStatic = `/book`;
|
||||
filesDir = `${config.publicFilesDir}${config.filesPathStatic}`;
|
||||
*/
|
||||
//загрузка или восстановление файлов в /files, при необходимости
|
||||
app.use(config.filesPathStatic, async(req, res, next) => {
|
||||
if (req.method !== 'GET' && req.method !== 'HEAD') {
|
||||
return next();
|
||||
}
|
||||
|
||||
if (path.extname(req.path) == '') {
|
||||
const bookFile = `${config.filesDir}${req.path}`;
|
||||
const bookFileDesc = `${bookFile}.d.json`;
|
||||
|
||||
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);
|
||||
}
|
||||
} catch(e) {
|
||||
log(LM_ERR, e.message);
|
||||
}
|
||||
|
||||
if (downFileName) {
|
||||
res.downFileName = downFileName;
|
||||
|
||||
if (!req.acceptsEncodings('gzip')) {
|
||||
//не принимает gzip, тогда распакуем
|
||||
const rawFile = `${bookFile}.raw`;
|
||||
if (!await fs.pathExists(rawFile))
|
||||
await utils.gunzipFile(bookFile, rawFile);
|
||||
|
||||
req.url += '.raw';
|
||||
res.rawFile = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return next();
|
||||
});
|
||||
|
||||
//заголовки при отдаче
|
||||
app.use(config.filesPathStatic, express.static(config.filesDir, {
|
||||
setHeaders: (res) => {
|
||||
if (res.downFileName) {
|
||||
if (!res.rawFile)
|
||||
res.set('Content-Encoding', 'gzip');
|
||||
|
||||
res.set('Content-Disposition', `inline; filename*=UTF-8''${encodeURIComponent(res.downFileName)}`);
|
||||
}
|
||||
},
|
||||
}));
|
||||
|
||||
app.use(express.static(config.publicDir));
|
||||
}
|
||||
|
||||
(async() => {
|
||||
try {
|
||||
await init();
|
||||
|
||||
Reference in New Issue
Block a user