-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
165 lines (108 loc) · 3.41 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//
// office-document-properties
// https://github.com/swestmoreland/office-document-properties
//
"use strict";
var _ = require('lodash'),
parse = require('xml2js').parseString,
yauzl = require('yauzl');
var APP_XML = require('./app.xml.json'),
CORE_XML = require('./core.xml.json');
function fromBuffer(buffer, cb) {
if (buffer && buffer instanceof Buffer && typeof cb === 'function') {
yauzl.fromBuffer(buffer, {lazyEntries: true}, function(err, zipfile) {
if (err) return cb(err, null);
readEntries(zipfile, cb);
});
} else {
if( typeof cb === 'function' ) {
cb( new Error('Incorrect parameters.'), null);
} else {
console.error('Incorrect parameters.');
}
}
}
function fromFilePath(filePath, cb) {
if (typeof filePath === 'string' && typeof cb === 'function') {
yauzl.open(filePath, {lazyEntries: true}, function(err, zipfile) {
if (err) return cb(err, null);
readEntries(zipfile, cb);
});
} else {
if( typeof cb === 'function' ) {
cb( new Error('Incorrect parameters.'), null);
} else {
console.error('Incorrect parameters.');
}
}
}
function readEntries(zipfile, cb) {
var data = {};
zipfile.readEntry();
zipfile.on('end', function() {
cb(null, sortByKeys(data));
});
zipfile.on('entry', function(entry) {
switch (entry.fileName) {
case "docProps/app.xml":
readEntryStreamXML(zipfile, entry, function(err, result) {
_.assign(data, getDocumentProperties(result, APP_XML));
zipfile.readEntry();
});
break;
case "docProps/core.xml":
readEntryStreamXML(zipfile, entry, function(err, result) {
_.assign(data, getDocumentProperties(result, CORE_XML));
zipfile.readEntry();
});
break;
default:
zipfile.readEntry();
}
});
}
function readEntryStreamXML(zipfile, entry, cb) {
zipfile.openReadStream(entry, function(err, readStream) {
var data = '';
if (err) return cb(err, null);
readStream.on('data', function(chunk) {
data += chunk;
});
readStream.on('end', function() {
parse(data, function(err, result) {
if (err) return cb(err, null);
cb(null, result);
});
});
});
}
function getDocumentProperties(obj, props) {
var data = {};
_.forEach(props, function(prop) {
var val;
if (_.has(obj, prop.path)) {
switch (prop.type) {
case "number":
val = _.toNumber(_.get(obj, prop.path));
break;
case "string":
default:
val = _.toString(_.get(obj, prop.path));
}
if (prop.type == "string" && _.isEmpty(val)) return;
_.set(data, prop.name, val);
}
});
return data;
}
var sortByKeys = object => {
const keys = Object.keys(object)
const sortedKeys = _.sortBy(keys)
return _.fromPairs(
_.map(sortedKeys, key => [key, object[key]])
)
}
module.exports = {
fromBuffer: fromBuffer,
fromFilePath: fromFilePath
};