Наладил сжатие файлов книг в /tmp
This commit is contained in:
@@ -57,8 +57,9 @@ class Reader {
|
|||||||
async loadCachedBook(url, callback){
|
async loadCachedBook(url, callback){
|
||||||
const options = {
|
const options = {
|
||||||
onDownloadProgress: progress => {
|
onDownloadProgress: progress => {
|
||||||
|
const total = (progress.total ? progress.total : progress.loaded + 200000);
|
||||||
if (callback)
|
if (callback)
|
||||||
callback({state: 'loading', progress: Math.round((progress.loaded*100)/progress.total)});
|
callback({state: 'loading', progress: Math.round((progress.loaded*100)/total)});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//загрузка
|
//загрузка
|
||||||
|
|||||||
@@ -53,6 +53,7 @@ class ProgressPage extends Vue {
|
|||||||
this.step = (state.step ? state.step : this.step);
|
this.step = (state.step ? state.step : this.step);
|
||||||
this.totalSteps = (state.totalSteps > this.totalSteps ? state.totalSteps : this.totalSteps);
|
this.totalSteps = (state.totalSteps > this.totalSteps ? state.totalSteps : this.totalSteps);
|
||||||
this.progress = state.progress || 0;
|
this.progress = state.progress || 0;
|
||||||
|
console.log(state);
|
||||||
}
|
}
|
||||||
|
|
||||||
get percentage() {
|
get percentage() {
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
const fs = require('fs-extra');
|
||||||
|
const zlib = require('zlib');
|
||||||
|
const crypto = require('crypto');
|
||||||
const decompress = require('decompress');
|
const decompress = require('decompress');
|
||||||
const FileDetector = require('./FileDetector');
|
const FileDetector = require('./FileDetector');
|
||||||
|
|
||||||
@@ -28,6 +31,29 @@ class FileDecompressor {
|
|||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async gzipBuffer(buf) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
zlib.gzip(buf, {level: 1}, (err, result) => {
|
||||||
|
if (err) reject(err);
|
||||||
|
resolve(result);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async gzipFileIfNotExists(filename, outDir) {
|
||||||
|
const buf = await fs.readFile(filename);
|
||||||
|
|
||||||
|
const hash = crypto.createHash('sha256').update(buf).digest('hex');
|
||||||
|
|
||||||
|
const outFilename = `${outDir}/${hash}`;
|
||||||
|
|
||||||
|
if (!await fs.pathExists(outFilename)) {
|
||||||
|
await fs.writeFile(outFilename, await this.gzipBuffer(buf))
|
||||||
|
}
|
||||||
|
|
||||||
|
return outFilename;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = FileDecompressor;
|
module.exports = FileDecompressor;
|
||||||
@@ -26,7 +26,7 @@ class FileDownloader {
|
|||||||
if (estSize)
|
if (estSize)
|
||||||
prog = Math.round(progress.transferred/estSize*100);
|
prog = Math.round(progress.transferred/estSize*100);
|
||||||
else if (progress.transferred)
|
else if (progress.transferred)
|
||||||
prog = Math.round(progress.transferred/(progress.transferred + 100000)*100);
|
prog = Math.round(progress.transferred/(progress.transferred + 200000)*100);
|
||||||
|
|
||||||
if (prog != prevProg && callback)
|
if (prog != prevProg && callback)
|
||||||
callback(prog);
|
callback(prog);
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
const fs = require('fs-extra');
|
const fs = require('fs-extra');
|
||||||
|
const path = require('path');
|
||||||
|
|
||||||
const workerState = require('./workerState');
|
const workerState = require('./workerState');
|
||||||
const FileDownloader = require('./FileDownloader');
|
const FileDownloader = require('./FileDownloader');
|
||||||
@@ -25,6 +26,7 @@ class ReaderWorker {
|
|||||||
let errMes = '';
|
let errMes = '';
|
||||||
let decompDir = '';
|
let decompDir = '';
|
||||||
let downloadedFilename = '';
|
let downloadedFilename = '';
|
||||||
|
let convertFilename = '';
|
||||||
try {
|
try {
|
||||||
wState.set({state: 'download', step: 1, totalSteps: 3, url});
|
wState.set({state: 'download', step: 1, totalSteps: 3, url});
|
||||||
|
|
||||||
@@ -49,14 +51,19 @@ class ReaderWorker {
|
|||||||
|
|
||||||
//parse book
|
//parse book
|
||||||
wState.set({state: 'convert', step: 3, progress: 0});
|
wState.set({state: 'convert', step: 3, progress: 0});
|
||||||
let resultFilename = `${this.config.tempPublicDir}/${tempFilename2}`;
|
convertFilename = `${this.config.tempDownloadDir}/${tempFilename2}`;
|
||||||
await this.bookConverter.convertToFb2(decompFilename, resultFilename, url, progress => {
|
await this.bookConverter.convertToFb2(decompFilename, convertFilename, url, progress => {
|
||||||
wState.set({progress});
|
wState.set({progress});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
//compress file to tmp dir, if not exists with the same hashname
|
||||||
|
const compFilename = await this.decomp.gzipFileIfNotExists(convertFilename, `${this.config.tempPublicDir}`);
|
||||||
|
|
||||||
wState.set({progress: 100});
|
wState.set({progress: 100});
|
||||||
|
|
||||||
//finish
|
//finish
|
||||||
wState.finish({path: `/tmp/${tempFilename2}`});
|
const finishFilename = path.basename(compFilename);
|
||||||
|
wState.finish({path: `/tmp/${finishFilename}`});
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
wState.set({state: 'error', error: (errMes ? errMes : e.message)});
|
wState.set({state: 'error', error: (errMes ? errMes : e.message)});
|
||||||
@@ -67,6 +74,8 @@ class ReaderWorker {
|
|||||||
await fs.remove(decompDir);
|
await fs.remove(decompDir);
|
||||||
if (downloadedFilename)
|
if (downloadedFilename)
|
||||||
await fs.remove(downloadedFilename);
|
await fs.remove(downloadedFilename);
|
||||||
|
if (convertFilename)
|
||||||
|
await fs.remove(convertFilename);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ initLogger(config);
|
|||||||
const log = getLog();
|
const log = getLog();
|
||||||
|
|
||||||
const fs = require('fs-extra');
|
const fs = require('fs-extra');
|
||||||
|
const path = require('path');
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const compression = require('compression');
|
const compression = require('compression');
|
||||||
|
|
||||||
@@ -42,7 +43,16 @@ async function main() {
|
|||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
if (devModule)
|
if (devModule)
|
||||||
devModule.logQueries(app);
|
devModule.logQueries(app);
|
||||||
app.use(express.static(serverConfig.publicDir, { maxAge: '30d' }));
|
|
||||||
|
app.use(express.static(serverConfig.publicDir, {
|
||||||
|
maxAge: '30d',
|
||||||
|
setHeaders: (res, filePath) => {
|
||||||
|
if (path.basename(path.dirname(filePath)) == 'tmp') {
|
||||||
|
res.set('Content-Type', 'text/xml');
|
||||||
|
res.set('Content-Encoding', 'gzip');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
|
||||||
require('./routes').initRoutes(app, connPool, serverConfig);
|
require('./routes').initRoutes(app, connPool, serverConfig);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user