Наладил сжатие файлов книг в /tmp
This commit is contained in:
@@ -57,8 +57,9 @@ class Reader {
|
||||
async loadCachedBook(url, callback){
|
||||
const options = {
|
||||
onDownloadProgress: progress => {
|
||||
const total = (progress.total ? progress.total : progress.loaded + 200000);
|
||||
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.totalSteps = (state.totalSteps > this.totalSteps ? state.totalSteps : this.totalSteps);
|
||||
this.progress = state.progress || 0;
|
||||
console.log(state);
|
||||
}
|
||||
|
||||
get percentage() {
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
const fs = require('fs-extra');
|
||||
const zlib = require('zlib');
|
||||
const crypto = require('crypto');
|
||||
const decompress = require('decompress');
|
||||
const FileDetector = require('./FileDetector');
|
||||
|
||||
@@ -28,6 +31,29 @@ class FileDecompressor {
|
||||
|
||||
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;
|
||||
@@ -26,7 +26,7 @@ class FileDownloader {
|
||||
if (estSize)
|
||||
prog = Math.round(progress.transferred/estSize*100);
|
||||
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)
|
||||
callback(prog);
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
const fs = require('fs-extra');
|
||||
const path = require('path');
|
||||
|
||||
const workerState = require('./workerState');
|
||||
const FileDownloader = require('./FileDownloader');
|
||||
@@ -25,6 +26,7 @@ class ReaderWorker {
|
||||
let errMes = '';
|
||||
let decompDir = '';
|
||||
let downloadedFilename = '';
|
||||
let convertFilename = '';
|
||||
try {
|
||||
wState.set({state: 'download', step: 1, totalSteps: 3, url});
|
||||
|
||||
@@ -49,14 +51,19 @@ class ReaderWorker {
|
||||
|
||||
//parse book
|
||||
wState.set({state: 'convert', step: 3, progress: 0});
|
||||
let resultFilename = `${this.config.tempPublicDir}/${tempFilename2}`;
|
||||
await this.bookConverter.convertToFb2(decompFilename, resultFilename, url, progress => {
|
||||
convertFilename = `${this.config.tempDownloadDir}/${tempFilename2}`;
|
||||
await this.bookConverter.convertToFb2(decompFilename, convertFilename, url, 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});
|
||||
|
||||
//finish
|
||||
wState.finish({path: `/tmp/${tempFilename2}`});
|
||||
const finishFilename = path.basename(compFilename);
|
||||
wState.finish({path: `/tmp/${finishFilename}`});
|
||||
|
||||
} catch (e) {
|
||||
wState.set({state: 'error', error: (errMes ? errMes : e.message)});
|
||||
@@ -67,6 +74,8 @@ class ReaderWorker {
|
||||
await fs.remove(decompDir);
|
||||
if (downloadedFilename)
|
||||
await fs.remove(downloadedFilename);
|
||||
if (convertFilename)
|
||||
await fs.remove(convertFilename);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ initLogger(config);
|
||||
const log = getLog();
|
||||
|
||||
const fs = require('fs-extra');
|
||||
const path = require('path');
|
||||
const express = require('express');
|
||||
const compression = require('compression');
|
||||
|
||||
@@ -42,7 +43,16 @@ async function main() {
|
||||
app.use(express.json());
|
||||
if (devModule)
|
||||
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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user