-
Notifications
You must be signed in to change notification settings - Fork 192
/
Copy pathindex.js
163 lines (145 loc) · 4.67 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
var fs = require( 'fs' )
, path = require( 'path' )
, mime = require( 'mime' )
, extract = require( './extract' )
, os = require( 'os' )
, got = require( 'got' )
, tmpDir = os.tmpdir()
;
function _genRandom() {
return Math.floor( ( Math.random() * 100000000000 ) + 1 );
}
function _extractWithType( type, filePath, options, cb ) {
fs.exists( filePath, function( exists ) {
if ( exists ) {
extract( type, filePath, options, cb );
} else {
cb( new Error( 'File at path [[ ' + filePath + ' ]] does not exist.' ), null );
}
});
}
function _returnArgsError( _args ) {
var args = Array.prototype.slice.call( _args )
, callback
;
args.forEach( function( parm ) {
if ( parm && typeof parm === 'function' ) {
callback = parm;
}
});
if ( callback ) {
callback( new Error( 'Incorrect parameters passed to textract.' ), null );
} else {
// eslint-disable-next-line no-console
console.error( 'textract could not find a callback function to execute.' );
}
}
function _writeBufferToDisk( buff, cb ) {
var fullPath = path.join( tmpDir, 'textract_file_' + _genRandom() );
fs.open( fullPath, 'w', function( err, fd ) {
if ( err ) {
throw new Error( 'error opening temp file: ' + err );
} else {
fs.write( fd, buff, 0, buff.length, null, function( err2 ) {
if ( err2 ) {
throw new Error( 'error writing temp file: ' + err2 );
} else {
fs.close( fd, function() {
cb( fullPath );
});
}
});
}
});
}
function fromFileWithMimeAndPath( type, filePath, options, cb ) {
var called = false;
if ( typeof type === 'string' && typeof filePath === 'string' ) {
if ( typeof cb === 'function' && typeof options === 'object' ) {
// (mimeType, filePath, options, callback)
_extractWithType( type, filePath, options, cb );
called = true;
} else if ( typeof options === 'function' && cb === undefined ) {
// (mimeType, filePath, callback)
_extractWithType( type, filePath, {}, options );
called = true;
}
}
if ( !called ) {
_returnArgsError( arguments );
}
}
function fromFileWithPath( filePath, options, cb ) {
var type;
if ( typeof filePath === 'string' &&
( typeof options === 'function' || typeof cb === 'function' ) ) {
type = ( options && options.typeOverride ) || mime.getType( filePath );
fromFileWithMimeAndPath( type, filePath, options, cb );
} else {
_returnArgsError( arguments );
}
}
// eslint-disable-next-line no-unused-vars
function fromBufferWithMime( type, bufferContent, options, cb, withPath ) {
if ( typeof type === 'string' &&
bufferContent &&
bufferContent instanceof Buffer &&
( typeof options === 'function' || typeof cb === 'function' ) ) {
_writeBufferToDisk( bufferContent, function( newPath ) {
fromFileWithMimeAndPath( type, newPath, options, cb );
});
} else {
_returnArgsError( arguments );
}
}
function fromBufferWithName( filePath, bufferContent, options, cb ) {
var type;
if ( typeof filePath === 'string' ) {
type = mime.getType( filePath );
fromBufferWithMime( type, bufferContent, options, cb, true );
} else {
_returnArgsError( arguments );
}
}
function fromUrl( url, options, cb ) {
var urlNoQueryParams, extname, filePath, fullFilePath, file, href, callbackCalled;
// allow url to be either a string or to be a
// Node URL Object: https://nodejs.org/api/url.html
href = ( typeof url === 'string' ) ? url : url.href;
if ( href ) {
options = options || {};
urlNoQueryParams = href.split( '?' )[0];
extname = path.extname( urlNoQueryParams );
filePath = _genRandom() + extname;
fullFilePath = path.join( tmpDir, filePath );
file = fs.createWriteStream( fullFilePath );
file.on( 'finish', function() {
if ( !callbackCalled ) {
fromFileWithPath( fullFilePath, options, cb );
}
});
got.stream( url )
.on( 'response', function( response ) {
// allows for overriding by the developer or automatically
// populating based on server response.
if ( !options.typeOverride ) {
options.typeOverride = response.headers['content-type'].split( /;/ )[0];
}
})
.on( 'error', function( error ) {
var _cb = ( typeof options === 'function' ) ? options : cb;
callbackCalled = true;
_cb( error );
})
.pipe( file );
} else {
_returnArgsError( arguments );
}
}
module.exports = {
fromFileWithPath: fromFileWithPath,
fromFileWithMimeAndPath: fromFileWithMimeAndPath,
fromBufferWithName: fromBufferWithName,
fromBufferWithMime: fromBufferWithMime,
fromUrl: fromUrl
};