-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathtika.js
106 lines (85 loc) · 2.04 KB
/
tika.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
/**
* @overview
* @author Matthew Caruana Galizia <[email protected]>
* @license MIT
* @copyright Copyright (c) 2013 The Center for Public Integrity®
*/
/*jshint node:true*/
'use strict';
var java = require('java');
java.classpath.push(__dirname + '/jar/node-tika-1.13.jar');
java.options.push('-Djava.awt.headless=true');
java.options.push('-Xrs');
var NodeTika = java.import('org.icij.nodetika.NodeTika');
exports.extract = function(uri, options, cb) {
if (arguments.length < 3) {
cb = options;
options = null;
}
exports.text(uri, options, function(err, text) {
if (err) {
return cb(err);
}
exports.meta(uri, options, function(err, meta) {
cb(err, text, meta);
});
});
};
exports.text = function(uri, options, cb) {
if (arguments.length < 3) {
cb = options;
options = null;
}
NodeTika.extractText(uri, JSON.stringify(options), cb);
};
exports.xhtml = function(uri, options, cb) {
if (arguments.length < 3) {
cb = options;
options = null;
}
NodeTika.extractXml(uri, 'html', JSON.stringify(options), cb);
};
exports.meta = function(uri, options, cb) {
var handler = function(err, meta) {
if (err) {
return cb(err);
}
cb(null, JSON.parse(meta));
};
if (arguments.length < 3) {
cb = options;
options = null;
}
if (options) {
NodeTika.extractMeta(uri, options.contentType, handler);
} else {
NodeTika.extractMeta(uri, handler);
}
};
exports.type = exports.contentType = function(uri, cb) {
NodeTika.detectContentType(uri, cb);
};
exports.charset = function(uri, options, cb) {
if (arguments.length < 3) {
cb = options;
options = null;
}
if (options) {
NodeTika.detectCharset(uri, options.contentType, cb);
} else {
NodeTika.detectCharset(uri, cb);
}
};
exports.typeAndCharset = function(uri, cb) {
NodeTika.detectContentTypeAndCharset(uri, cb);
};
exports.language = function(text, cb) {
NodeTika.detectLanguage(text, function(err, language) {
if (err) {
cb(err);
} else {
language = JSON.parse(language);
cb(null, language.language, language.reasonablyCertain);
}
});
};