Улучшено скачивание файлов - добавлено читабельное имя файла

This commit is contained in:
Book Pauk
2022-09-26 16:04:26 +07:00
parent 92303cadc3
commit afef0ed04c
6 changed files with 87 additions and 21 deletions

View File

@@ -147,8 +147,10 @@ class WebSocketController {
async getBookLink(req, ws) {
if (!utils.hasProp(req, 'bookPath'))
throw new Error(`bookPath is empty`);
if (!utils.hasProp(req, 'downFileName'))
throw new Error(`downFileName is empty`);
const result = await this.webWorker.getBookLink(req.bookPath);
const result = await this.webWorker.getBookLink({bookPath: req.bookPath, downFileName: req.downFileName});
this.send(result, req, ws);
}

View File

@@ -307,7 +307,7 @@ class WebWorker {
});
}
async restoreBook(bookPath) {
async restoreBook(bookPath, downFileName) {
const db = this.db;
const extractedFile = await this.extractBook(bookPath);
@@ -333,16 +333,18 @@ class WebWorker {
replace: true,
rows: [
{id: bookPath, hash},
{id: hash, bookPath}
{id: hash, bookPath, downFileName}
]
});
return link;
}
async getBookLink(bookPath) {
async getBookLink(params) {
this.checkMyState();
const {bookPath, downFileName} = params;
try {
const db = this.db;
let link = '';
@@ -360,7 +362,7 @@ class WebWorker {
}
if (!link) {
link = await this.restoreBook(bookPath)
link = await this.restoreBook(bookPath, downFileName)
}
if (!link)
@@ -380,11 +382,13 @@ class WebWorker {
const db = this.db;
const hash = path.basename(publicPath);
//найдем bookPath
//найдем bookPath и downFileName
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);
if (rows.length) {//нашли по хешу
const rec = rows[0];
await this.restoreBook(rec.bookPath, rec.downFileName);
return rec.downFileName;
} else {//bookPath не найден
throw new Error('404 Файл не найден');
}
@@ -396,6 +400,19 @@ class WebWorker {
}
}
async getDownFileName(publicPath) {
const db = this.db;
const hash = path.basename(publicPath);
//найдем downFileName
const rows = await db.select({table: 'file_hash', where: `@@id(${db.esc(hash)})`});
if (rows.length) {//downFileName найден по хешу
return rows[0].downFileName;
} else {//bookPath не найден
throw new Error('404 Файл не найден');
}
}
async logServerStats() {
while (1) {// eslint-disable-line
try {

View File

@@ -163,15 +163,21 @@ function initStatic(app, config) {
const publicPath = `${config.publicDir}${req.path}`;
let downFileName = '';
//восстановим
try {
if (!await fs.pathExists(publicPath)) {
await webWorker.restoreBookFile(publicPath);
downFileName = await webWorker.restoreBookFile(publicPath);
} else {
downFileName = await webWorker.getDownFileName(publicPath);
}
} catch(e) {
//quiet
}
if (downFileName)
res.downFileName = downFileName;
return next();
});
@@ -183,6 +189,9 @@ function initStatic(app, config) {
setHeaders: (res, filePath) => {
if (path.dirname(filePath) == filesDir) {
res.set('Content-Encoding', 'gzip');
if (res.downFileName)
res.set('Content-Disposition', `inline; filename*=UTF-8''${encodeURIComponent(res.downFileName)}`);
}
},
}));