Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Buffer in Render method #163

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
51 changes: 49 additions & 2 deletions lib/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ var file = {
});
});
});
},
/**
* isZippedBuffer return callback(true) if the buffer is zipped
* @param {Buffer} buffer file path
* @param {Function} callback(err, isZipped)
*/
isZippedBuffer : function (buffer, callback) {
var _buf = buffer;
callback(null, (buffer.slice(0, 2).toString() === 'PK'));
},

/**
Expand All @@ -47,7 +56,7 @@ var file = {

/**
* Unzip a file
* @param {String} filePath file to unzip
* @param {String} filePath file or buffer to unzip
* @param {Function} callback(err, files) files is an array of files ['name':'filename', 'buffer':Buffer]
*/
unzip : function (filePath, callback) {
Expand Down Expand Up @@ -130,9 +139,44 @@ var file = {
_zip.end();
},

/**
*
* @param {Buffer} buffer document as a buffer
* @param {*} callback
* @returns
*/
openTemplateBuffer : function (buffer, callback) {
file.isZippedBuffer(buffer, function(err, isZipped) {
if (err) return callback(err);
var _template = {
isZipped,
filename : 'template-from-buffer',
embeddings : [],
files : []
};
if (isZipped === true) {
var _filesToUnzip = [{
name : '',
data : buffer
}];
return unzipFiles(_template, _filesToUnzip, callback);
}
else {
var _file = {
name : 'file-from-buffer',
data : buffer,
isMarked : true,
parent : ''
};
_template.files.push(_file);
return callback(err, _template);
}
})
},

/**
* Open a template (zipped or not). It will find the template and convert the buffer into strings if it contains xml
* @param {String} templateId template name (with or without the path)
* @param {String} templateId template name (with or without the path) or buffer. When buffer is used, extension option must be provided
* @param {Function} callback(err, template)
*/
openTemplate : function (templateId, callback) {
Expand All @@ -142,6 +186,9 @@ var file = {
embeddings : [],
files : []
};
if(Buffer.isBuffer(templateId)){
return file.openTemplateBuffer(templateId, callback)
}
if (templateId instanceof Object) {
// accept already dezipped files (for test purpose)
return callback(null, templateId);
Expand Down
3 changes: 1 addition & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,8 @@ var carbone = {
return callback(err, null);
}
// Determine the template extension.
var _extension = file.detectType(template);
// It takes the user defined one, or use the file type.
options.extension = optionsRaw.extension || _extension;
options.extension = optionsRaw.extension || file.detectType(template);
if (options.extension === null) {
return callback('Unknown input file type. It should be a docx, xlsx, pptx, odt, ods, odp, xhtml, html or an xml file');
}
Expand Down
43 changes: 43 additions & 0 deletions test/test.file.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,24 @@ describe('file', function () {
});
});
});

describe('isZippedBuffer', function () {
it('should return true if the file is zipped', function (done) {
var buffer = fs.readFileSync(path.resolve('./test/datasets/test_word_render_A.docx'));
file.isZippedBuffer(buffer, function (err, isZipped) {
helper.assert(isZipped, true);
done();
});
});
it('should return false if the file is not zipped', function (done) {
var buffer = fs.readFileSync(path.resolve('./test/datasets/test_word_render_2003_XML.xml'));
file.isZippedBuffer(buffer, function (err, isZipped) {
helper.assert(isZipped, false);
done();
});
});
});


describe('unzip', function () {
it('should unzip a file and return a array which contain its content', function (done) {
Expand Down Expand Up @@ -493,6 +511,31 @@ describe('file', function () {
done();
});
});

it('should open embedded xlsx buffer', function (done) {
const buffer = fs.readFileSync(path.resolve(__dirname, 'datasets', 'test_word_with_embedded_excel.docx'))
file.openTemplate(buffer, function (err, template) {
helper.assert(err, null);
helper.assert(template.isZipped, true);
helper.assert(template.files.length, 29);
helper.assert(template.embeddings.length, 1);
helper.assert(template.embeddings, ['word/embeddings/Feuille_de_calcul_Microsoft_Excel1.xlsx']);
var _filesFromDocx = template.files.filter((file) => {
return file.parent === '';
});
helper.assert(_filesFromDocx.length, 16);
var _filesFromXslx = template.files.filter((file) => {
return file.parent === 'word/embeddings/Feuille_de_calcul_Microsoft_Excel1.xlsx';
});
helper.assert(_filesFromXslx.length, 13);
var _oneFileOfXlsx = _filesFromXslx.filter((file) => {
return file.name === 'xl/tables/table1.xml';
});
helper.assert(_oneFileOfXlsx.length, 1);
helper.assert(/Tableau1/.test(_oneFileOfXlsx[0].data), true);
done();
});
});
/* it.skip('should detect files which contains markers (not all xml)');*/
});

Expand Down