-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathindex.js
182 lines (148 loc) · 4.45 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
'use strict';
var _ = require('underscore'),
childProcess = require('child_process'),
mime = require('mime');
var unoconv = exports = module.exports = {};
/**
* Convert a document.
*
* @param {String} file
* @param {String} outputFormat
* @param {Object|Function} options
* @param {Function} callback
* @api public
*/
unoconv.convert = function(file, outputFormat, options, callback) {
var self = this,
args,
bin = 'unoconv',
child,
stdout = [],
stderr = [];
if (_.isFunction(options)) {
callback = options;
options = null;
}
args = [
'-f' + outputFormat,
'--stdout'
];
if (options && options.port) {
args.push('-p' + options.port)
}
args.push(file);
if (options && options.bin) {
bin = options.bin;
}
child = childProcess.spawn(bin, args, function (err, stdout, stderr) {
if (err) {
return callback(err);
}
if (stderr) {
return callback(new Error(stderr.toString()));
}
callback(null, stdout);
});
child.stdout.on('data', function (data) {
stdout.push(data);
});
child.stderr.on('data', function (data) {
stderr.push(data);
});
child.on('exit', function () {
if (stderr.length) {
return callback(new Error(Buffer.concat(stderr).toString()));
}
callback(null, Buffer.concat(stdout));
});
};
/**
* Start a listener.
*
* @param {Object} options
* @return {ChildProcess}
* @api public
*/
unoconv.listen = function (options) {
var self = this,
args,
bin = 'unoconv';
args = [ '--listener' ];
if (options && options.port) {
args.push('-p' + options.port);
}
if (options && options.bin) {
bin = options.bin;
}
return childProcess.spawn(bin, args);
};
/**
* Detect supported conversion formats.
*
* @param {Object|Function} options
* @param {Function} callback
*/
unoconv.detectSupportedFormats = function (options, callback) {
var self = this,
docType,
detectedFormats = {
document: [],
graphics: [],
presentation: [],
spreadsheet: []
},
bin = 'unoconv';
if (_.isFunction(options)) {
callback = options;
options = null;
}
if (options && options.bin) {
bin = options.bin;
}
childProcess.execFile(bin, [ '--show' ], function (err, stdout, stderr) {
if (err) {
return callback(err);
}
// For some reason --show outputs to stderr instead of stdout
var lines = stderr.split('\n');
lines.forEach(function (line) {
if (line === 'The following list of document formats are currently available:') {
docType = 'document';
} else if (line === 'The following list of graphics formats are currently available:') {
docType = 'graphics';
} else if (line === 'The following list of presentation formats are currently available:') {
docType = 'presentation';
} else if (line === 'The following list of spreadsheet formats are currently available:') {
docType = 'spreadsheet';
} else {
var format = line.match(/^(.*)-/);
if (format) {
format = format[1].trim();
}
var extension = line.match(/\[(.*)\]/);
if (extension) {
extension = extension[1].trim().replace('.', '');
}
var description = line.match(/-(.*)\[/);
if (description) {
description = description[1].trim();
}
if (format && extension && description) {
detectedFormats[docType].push({
'format': format,
'extension': extension,
'description': description,
'mime': mime.lookup(extension)
});
}
}
});
if (detectedFormats.document.length < 1 &&
detectedFormats.graphics.length < 1 &&
detectedFormats.presentation.length < 1 &&
detectedFormats.spreadsheet.length < 1) {
return callback(new Error('Unable to detect supported formats'));
}
callback(null, detectedFormats);
});
};