Заменил пакет download на got, мелкий рефакторинг

This commit is contained in:
Book Pauk
2019-01-13 15:05:24 +07:00
parent 9adbffc9e5
commit 47b4bd9838
7 changed files with 118 additions and 356 deletions

View File

@@ -1,11 +1,15 @@
const fs = require('fs-extra');
const FileDetector = require('./FileDetector');
class BookConverter {
constructor() {
this.detector = new FileDetector();
}
async convertToFb2(inputFile, outputFile, fileType, callback) {
if (fileType.ext == 'html' || fileType.ext == 'xml') {
async convertToFb2(inputFile, outputFile, url, callback) {
const fileType = await this.detector.detectFile(inputFile);
if (fileType && (fileType.ext == 'html' || fileType.ext == 'xml')) {
const data = await fs.readFile(inputFile, 'utf8');
if (data.indexOf('FictionBook') >= 0) {
@@ -17,7 +21,10 @@ class BookConverter {
await fs.writeFile(outputFile, data);
callback(100);
} else {
throw new Error(`unknown file format: ${fileType.mime}`);
if (fileType)
throw new Error(`unknown file format: ${fileType.mime}`);
else
throw new Error(`unsupported file format: ${url}`);
}
}
}