Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

add error handling on stream #44

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const fs = require('fs');
const StreamZip = require('node-stream-zip');
const XLSX = require('xlsx');
const pdf = require('pdf-parse');
let WordExtractor = require('word-extractor');
var WordExtractor = require('word-extractor');

// extract text from office books as doc and docx
extract = (filePath) => {
Expand All @@ -19,6 +19,8 @@ extract = (filePath) => {
body += content;
}
resolve(body);
}).catch((err) => {
reject(err);
});
});
};
Expand Down Expand Up @@ -47,6 +49,9 @@ open = (filePath) => {
});
});
});
zip.on('error', (err) => {
reject(err);
});
});
};

Expand All @@ -69,11 +74,15 @@ exports.getText = async (filePath) => {
switch (fileExtension) {
// read pdf
case '.pdf':
fileContent = (await pdf(data)).text;
fileContent = await (await pdf(data)).text;
break;

// read docs

case '.docx':
fileContent = await extract(filePath);
break;

case '.doc':
var extractor = new WordExtractor();
var extracted = await extractor.extract(filePath);
Expand All @@ -97,16 +106,17 @@ exports.getText = async (filePath) => {
fileContent = JSON.stringify(result);
break;

// read text, csv and json
// read text and csv
case '.txt':
case '.csv':
case '.json':
fileContent = data.toString();
break;

// default case
default:
throw new Error('unknown extension found!');
}
// console.log(`This is file content ==> ${fileContent}`);
return fileContent;
};