-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.js
More file actions
318 lines (270 loc) · 10.6 KB
/
example.js
File metadata and controls
318 lines (270 loc) · 10.6 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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
//Target type constant value for URL type.
var SOURCE_TYPE_URL = "URL";
// Target type constant value for FILE_BASE64 type.
var SOURCE_TYPE_FILE_BASE64 = 'FILE_BASE64';
// Target type constant value for FILE_PATH type.
var SOURCE_TYPE_FILE_PATH = 'FILE_PATH';
var EVENT_ON_DATA_GET = "dataget";
var EVENT_ON_DATA_FAIL = "datafail";
var OSC_EVENTS = {
TOKEN: {GET: "token_created", FAIL: "token_fail"},
SERVER: {GET: "server_get", FAIL: "server_fail"},
FILE_DELETE: {GET: "file_get_deleted", FAIL: "file_get_fail"},
PROGRESS: {GET: "progress_get", FAIL: "progress_fail"},
API_CALL: {GET: "api_call_get", FAIL: "api_call_fail"},
CONVERT: {GET: "convert_converted", FAIL: "convert_fail"}
};
function OnlineConvert() {
var self = this;
this.EVENT_ON_DATA_GET = EVENT_ON_DATA_GET;
this.EVENT_ON_DATA_FAIL = EVENT_ON_DATA_FAIL;
this.EVENTS = OSC_EVENTS;
//Online Converter API URL
this.URL = "http://api.online-convert.com";
//Target type constant value for URL type.
this.SOURCE_TYPE_URL = SOURCE_TYPE_URL;
// Target type constant value for FILE_BASE64 type.
this.SOURCE_TYPE_FILE_BASE64 = SOURCE_TYPE_FILE_BASE64;
// Target type constant value for FILE_PATH type.
this.SOURCE_TYPE_FILE_PATH = SOURCE_TYPE_FILE_PATH;
/**@var string Online Converter API Key*/
var _apiKey;
/**@var boolean*/
var _testMode;
/**@var string*/
var _url = this.URL;
/**@var string*/
var _targetType;
/**@var string */
var _sourceType;
/**@var string*/
this.hash = "";
/**@var string*/
this.downloadUrl = "";
/**
* @var array
*/
var _targetTypeOptions = {
'convert-to-7z': 'archive', 'convert-to-bz2': 'archive', 'convert-to-gz': 'archive', 'convert-to-zip': 'archive', 'convert-to-aac': 'audio', 'convert-to-aiff': 'audio', 'convert-to-flac': 'audio', 'convert-to-m4a': 'audio',
'convert-to-mp3': 'audio', 'convert-to-ogg': 'audio', 'convert-to-opus': 'audio', 'convert-to-wav': 'audio', 'convert-to-wma': 'audio', 'convert-to-doc': 'document', 'convert-to-docx': 'document', 'convert-to-flash': 'document',
'convert-to-html': 'document', 'convert-to-odt': 'document', 'convert-to-pdf': 'document', 'convert-to-ppt': 'document', 'convert-to-rtf': 'document', 'convert-to-txt': 'document', 'convert-to-azw3': 'ebook',
'convert-to-epub': 'ebook', 'convert-to-fb2': 'ebook', 'convert-to-lit': 'ebook', 'convert-to-lrf': 'ebook', 'convert-to-mobi': 'ebook', 'convert-to-pdb': 'ebook', /* 'convert-to-pdf' : 'ebook',*/
'convert-to-tcr': 'ebook', 'convert-to-bmp': 'image', 'convert-to-jpg': 'image', 'convert-to-png': 'image', 'convert-to-mp4': 'video', 'convert-to-wmv': 'video'
};
/**
* @var array
*/
var _sourceTypeOptions = {
'URL': 'URL',
'FILE_BASE64': 'FILE_BASE64',
'FILE_PATH': 'FILE_PATH'
};
/*
*
* @param string $apiKey
* @param boolean $testMode
* @returns OnlineConvert
*/
this.create = function ($apiKey, $testMode) {
var oc = new OnlineConvert();
_apiKey = $apiKey;
_testMode = $testMode;
return oc;
};
/**
*
* @param {type} $targetType
* @returns {OnlineConvert@call;apiCall}
*/
this.getServer = function ($targetType) {
if(!$targetType){
throw new Error("Target type should be not null.");
}
var xmlData = {queue: {'apiKey': _apiKey, 'targetType': $targetType}};
return this.apiCall('get-queue', xmlData, self.EVENTS.SERVER);
};
/**
* createToken
* Create one time token to use API.
*
* @return string xml response string from server.
*/
this.createToken = function () {
var xmlData = {queue: {'apiKey': _apiKey}};
return this.apiCall('request-token', xmlData, self.EVENTS.TOKEN);
};
/**
* deleteFile
* Delete file from server after process or you can manually delete for specific hash.
*
* @param string $hash Hash value of process which you want to delete.
* @return string xml response string from server.
*/
this.deleteFile = function ($hash) {
if (self.hash.length < 1 && $hash === undefined) {
throw new Error("Delete File: Hash not found.");
}
var xmlData = {queue: {'apiKey': _apiKey, 'hash': ($hash !== undefined) ? $hash : self.hash, 'method': 'deleteFile'}};
return this.apiCall('queue-manager', xmlData, self.EVENTS.FILE_DELETE);
}
/**
* getProgress
* Provide process status for created instance or you can manually check status for specific hash.
*
* @param string $hash Hash value of process which you want to check status.
* @return string xml response string from server.
*/
this.getProgress = function ($hash)
{
if (self.hash.length < 1 && $hash === undefined) {
throw new Error('Get Progress: Hash not found.');
}
var xmlData = {queue: {'apiKey': _apiKey, 'hash': ($hash !== undefined) ? $hash : self.hash}};
return this.apiCall('queue-status', xmlData, self.EVENTS.PROGRESS);
}
/**
*
* @param string $xmlString
* @returns object
*/
this.getXML2Object = function ($xmlString) {
var parseString = require('xml2js').parseString;
var xmlObject;
parseString($xmlString, function (err, result) {
xmlObject = result;
});
return xmlObject;
};
/**
*
* @param object $object
* @returns string xmlString
*/
this.getObject2XML = function ($object) {
var fs = require('fs'),
xml2js = require('xml2js');
var builder = new xml2js.Builder();
var xml = builder.buildObject($object);
return xml;
};
/**
*
* @param {type} $action
* @param {type} $xmlData
* @returns {Number}
*/
this.apiCall = function ($action, $xmlData, $fireEvent) {
var http = require("http");
var querystring = require('querystring');
if ($action !== "get-queue" && _targetType) {
this.getServer(_targetTypeOptions[_targetType], self.EVENTS.SERVER);
}
var queryData = {queue: this.getObject2XML($xmlData)};
var postData = querystring.stringify(queryData);
var options = {
host: "api.online-convert.com",
port: 80,
path: '/' + $action,
method: 'POST',
headers: {
'Content-type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(postData)
}
};
var req = http.request(options, function (res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
self.emit($fireEvent.GET, chunk);
});
});
req.on('error', function (e) {
self.emit($fireEvent.FAIL);
});
req.write(postData);
req.end();
};
/**
* convert
* Make a API call to convert file/url/hash based on parameters and return xml response.
*
* @param string $targetType To which file you want to convert (like convert-to-jpg, convert-to-mp3)
* @param string $sourceType 3 source types you can set (URL, FILE_PATH and FILE_BASE64)
* @param string $source Source can be provide based on sourceType if sourceType = URL you have to provide url string to this param.
* @param string $sourceName Provide file Name. This param used only with sourceType = FILE_PATH or FILE_BASE64
* @param string $sourceOptions Provide file conversion required extra parameters as array using this param.
* @param string $notificationUrl For set notification url for api actions.
*/
this.convert = function ($targetType, $sourceType, $source, $sourceName, $sourceOptions, $notificationUrl)
{
if (!_targetTypeOptions[$targetType]) {
throw new Error('Invalid Target Type.');
}
_targetType = $targetType;
if (!_sourceTypeOptions[$sourceType]) {
throw new Error('Invalid Source Type.');
}
_sourceType = $sourceType;
if (_sourceType === self.SOURCE_TYPE_FILE_BASE64 || _sourceType === self.SOURCE_TYPE_FILE_PATH) {
if ($sourceName === undefined || $sourceName.length < 1) {
throw new Error('Invalid Source Name.');
}
}
var base64data;
if (_sourceType === self.SOURCE_TYPE_FILE_PATH) {
var fs = require('fs');
if (!fs.existsSync($source)) {
throw new Error('File not found: '.$source);
}
base64Image($source);
function base64Image(src) {
var data = fs.readFileSync(src);
if (!data) {
throw new Error('File not found: '.$source);
}
data = data.toString("base64");
base64data =data;
}
}
var xmlData = {'queue': {
'apiKey': _apiKey,
'targetType': _targetTypeOptions[$targetType],
'targetMethod': $targetType,
'testMode': _testMode,
'notificationUrl': $notificationUrl === undefined ? "" : $notificationUrl
}
}
if (_sourceType === self.SOURCE_TYPE_URL) {
xmlData.queue.sourceUrl = $source;
} else {
xmlData.queue.file = {
fileName: $sourceName,
fileData: base64data
};
}
if ($sourceOptions !== undefined) {
xmlData.queue.format = $sourceOptions;
}
self.apiCall('queue-insert', xmlData, self.EVENTS.CONVERT);
self.on(self.EVENTS.CONVERT.GET, function (data) {
$responseArray = self.getXML2Object(data);
if ($responseArray['queue-answer'].status[0].code[0] && $responseArray['queue-answer'].status[0].code[0] == 0) {
self.hash = $responseArray['queue-answer'].params[0].hash[0];
self.downloadUrl = $responseArray['queue-answer'].params[0].downloadUrl[0];
}
self.url = self.URL;
});
self.on(self.EVENTS.CONVERT.FAIL, function () {
throw new Error("Error occured");
});
}
return this;
}
;
var util = require('util'),
EventEmitter = require('events').EventEmitter;
util.inherits(OnlineConvert, EventEmitter);
exports = module.exports = new OnlineConvert();
exports.SOURCE_TYPE_URL = SOURCE_TYPE_URL;
exports.SOURCE_TYPE_FILE_BASE64 = SOURCE_TYPE_FILE_BASE64;
exports.SOURCE_TYPE_FILE_PATH = SOURCE_TYPE_FILE_PATH;
exports.EVENTS = OSC_EVENTS;