forked from dkiyatkin/node-office
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
116 lines (113 loc) · 3.25 KB
/
Copy pathindex.js
File metadata and controls
116 lines (113 loc) · 3.25 KB
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
var fs = require('fs');
var path = require('path');
var xmlParser = require('xml2json');
var temp = require('temp');
var spawn = require('child_process').spawn;
var exec = function(cmd, args, cb) {
var stdout = '';
var stderr = '';
cmd = spawn(cmd, args);
cmd.stdout.on('data', function (data) {
stdout = stdout + data;
});
cmd.stderr.on('data', function (data) {
stderr = stderr + data;
});
cmd.on('close', function (code) {
cb(code, stdout, stderr);
});
};
var office = {
spreadsheets: ['xls', 'xlsx', 'ods'],
documents: ['doc', 'docx', 'odt'],
xlsParse: function(file, callback) {
exec('xlhtml', ['-xml', file], function(code, stdout, stderr) {
if (stderr) { console.error(stderr); }
if (code) {
callback(code);
} else {
callback(null, xmlParser.toJson(stdout, {object: true}).excel_workbook);
}
});
},
spreadsheetParse: function(filename, options, cb) {
var self = this;
if (options.ext !== 'xls') {
var tempname = temp.path({prefix: 'node-office-'});
exec('unoconv', ['--output='+tempname, '--format=xls', filename], function(error, stdout, stderr) {
if (stderr) { console.error(stderr); }
if (!error) {
self.xlsParse(tempname, function(err, data) {
fs.unlink(tempname, function(e) {
if (e) { console.error(e); }
cb(err, data);
});
});
} else { cb(error); }
});
} else { self.xlsParse(filename, cb); }
},
documentParse: function(filename, options, cb) {
var self = this;
if (options.img) {
if (!options.path) {
options.path = temp.path({prefix: 'node-office-'});
}
}
if (options.path) {
exec('unoconv', ['--outputpath='+options.path, '--format=html', filename], function(error, stdout, stderr) {
if (stderr) { console.error(stderr); }
if (!error) {
cb(null, path.join(options.path, path.basename(filename, path.extname(filename))+'.html'));
} else { cb(error); }
});
} else {
exec('unoconv', ['--stdout', '--format=html', filename], function(error, stdout, stderr) {
if (stderr) { console.error(stderr); }
if (!error) {
cb(null, stdout);
} else { cb(error); }
});
}
},
getType: function(filename, cb) {
// проверка по расширению
var self = this;
fs.exists(filename, function(exists) {
if (!exists) {
cb(new Error('file does not exist'));
} else {
var ext = path.extname(filename).toLowerCase().slice(1);
if (self.spreadsheets.indexOf(ext) !== -1) {
cb(null, 'spreadsheet', ext);
} else if (self.documents.indexOf(ext) !== -1) {
cb(null, 'document');
} else {
// TODO проверка по mime-type
cb(new Error('wrong file format'));
}
}
});
},
parse: function(filename, options, cb) {
var self = this;
if (!cb && typeof(options) === 'function') { cb = options; options = { }; }
self.getType(filename, function(err, type, ext) {
options.ext = ext;
if (!err) {
if (type === 'spreadsheet') {
self.spreadsheetParse(filename, options, function(err, data) {
cb(err, data);
});
} else if (type === 'document') {
self.documentParse(filename, options, function(err, data) {
cb(err, data);
});
} else {
cb(new Error('wrong file format'));
}
} else { cb(err); }
});
}
};
module.exports = office;