@@ -12,40 +12,40 @@ module.exports = function FileUpload(config) {
12
12
13
13
var MAX_FILE_COUNT = 10 ;
14
14
var MAX_FILE_SIZE = 100000000 ;
15
- var stat = 0 ;
16
15
17
- var logger = NodeSDK . logger ;
16
+ var _stat = 0 ;
17
+
18
+ var _logger = NodeSDK . logger ;
18
19
var _config = config || { } ;
19
20
20
21
function isFileOK ( file ) {
21
- //TODO optimize for async flow
22
22
var error = null ;
23
23
24
24
if ( file . path ) {
25
25
if ( ! fs . existsSync ( file . path ) ) {
26
26
error = new Error ( '[FileUpload] file not found' ) ;
27
27
}
28
- stat = fs . statSync ( file . path ) ;
29
- if ( stat . size > MAX_FILE_SIZE ) {
28
+ _stat = fs . statSync ( file . path ) ;
29
+ if ( _stat . size > MAX_FILE_SIZE ) {
30
30
error = new Error ( '[FileUpload] max file size exceeded' ) ;
31
31
}
32
32
} else if ( file . buffer ) {
33
- stat = { size : file . buffer . length } ;
34
- if ( stat . size > MAX_FILE_SIZE ) {
33
+ _stat = { size : file . buffer . length } ;
34
+ if ( _stat . size > MAX_FILE_SIZE ) {
35
35
error = new Error ( '[FileUpload] max file size exceeded' ) ;
36
36
}
37
37
} else {
38
38
error = new Error ( '[FileUpload] no file provided' ) ;
39
39
}
40
40
if ( file . type === 'application/json' ) {
41
- logger . debug ( '[FileUpload]: replace file type ' , file . type ) ;
41
+ _logger . debug ( '[FileUpload]: replace file type ' , file . type ) ;
42
42
file . type = 'text/plain' ; //upload json files as text files
43
43
}
44
44
return error ;
45
45
}
46
46
47
47
function urlToOptions ( url , file , stat , opts ) {
48
- logger . debug ( '[FileUpload] urlToOptions' , url , file , stat ) ;
48
+ _logger . debug ( '[FileUpload] urlToOptions' , url , file , stat ) ;
49
49
var parts = nodeUrl . parse ( url ) ;
50
50
var options = {
51
51
rejectUnauthorized : NodeSDK . rejectUnauthorized ,
@@ -68,7 +68,7 @@ module.exports = function FileUpload(config) {
68
68
opts . rtcSessionId && ( options . headers [ 'x-rtcsession' ] = opts . rtcSessionId ) ;
69
69
}
70
70
71
- logger . debug ( '[FileUpload] options' , options ) ;
71
+ _logger . debug ( '[FileUpload] options' , options ) ;
72
72
return options ;
73
73
}
74
74
@@ -79,47 +79,47 @@ module.exports = function FileUpload(config) {
79
79
reject ( error ) ;
80
80
return ;
81
81
}
82
- var options = urlToOptions ( url , file , stat , opts ) ;
82
+ var options = urlToOptions ( url , file , _stat , opts ) ;
83
83
var req = https . request ( options , function ( res ) {
84
84
if ( ! res ) {
85
- logger . error ( '[FileUpload]: no res in https callback' ) ;
85
+ _logger . error ( '[FileUpload]: no res in https callback' ) ;
86
86
error = new Error ( '[FileUpload]: no res in https callback' ) ;
87
87
reject ( error ) ;
88
88
}
89
- logger . debug ( '[FileUpload]: https callback' , res . statusCode , res . statusMessage , res . headers ) ;
89
+ _logger . debug ( '[FileUpload]: https callback' , res . statusCode , res . statusMessage , res . headers ) ;
90
90
if ( res . statusCode !== 200 ) {
91
- logger . error ( '[FileUpload]: status code: ' + res . statusCode , res . statusMessage ) ;
91
+ _logger . error ( '[FileUpload]: status code: ' + res . statusCode , res . statusMessage ) ;
92
92
error = new Error ( '[FileUpload]: status code ' + res . statusCode , res . statusMessage ) ;
93
93
reject ( error ) ;
94
94
}
95
95
var body = '' ;
96
96
res . on ( 'data' , function ( d ) {
97
- logger . debug ( '[FileUpload]: result on data' , d ) ;
97
+ _logger . debug ( '[FileUpload]: result on data' , d ) ;
98
98
body += d ;
99
99
} ) ;
100
100
res . on ( 'end' , function ( ) {
101
- logger . debug ( '[FileUpload]: result on end' , body . toString ( 'utf8' ) ) ;
101
+ _logger . debug ( '[FileUpload]: result on end' , body . toString ( 'utf8' ) ) ;
102
102
resolve ( body ) ;
103
103
} ) ;
104
104
res . on ( 'error' , function ( e ) {
105
- logger . debug ( '[FileUpload]: result on error' , e ) ;
105
+ _logger . debug ( '[FileUpload]: result on error' , e ) ;
106
106
reject ( e ) ;
107
107
} ) ;
108
108
} ) ;
109
109
110
110
if ( file . path ) {
111
111
var readStream = fs . createReadStream ( file . path ) ;
112
112
readStream . on ( 'data' , function ( chunk ) {
113
- logger . debug ( '[FileUpload]: readStream on data' , file . path , chunk ) ;
113
+ _logger . debug ( '[FileUpload]: readStream on data' , file . path , chunk ) ;
114
114
req . write ( chunk ) ;
115
115
} ) ;
116
116
readStream . on ( 'end' , function ( ) {
117
- logger . debug ( '[FileUpload]: readStream on end ' , file . path ) ;
117
+ _logger . debug ( '[FileUpload]: readStream on end ' , file . path ) ;
118
118
req . end ( ) ;
119
119
} ) ;
120
120
121
121
req . on ( 'error' , function ( e ) {
122
- logger . debug ( '[FileUpload]: request on error' , file . path , e ) ;
122
+ _logger . debug ( '[FileUpload]: request on error' , file . path , e ) ;
123
123
reject ( e ) ;
124
124
} ) ;
125
125
} else if ( file . buffer ) {
@@ -130,58 +130,40 @@ module.exports = function FileUpload(config) {
130
130
131
131
this . uploadFiles = function ( files , domain ) {
132
132
domain = domain || _config . domain ;
133
- logger . debug ( '[FileUpload] node SDK uploadFiles: ' , files , domain ) ;
133
+ _logger . debug ( '[FileUpload] node SDK uploadFiles: ' , files , domain ) ;
134
134
135
135
if ( files . length > MAX_FILE_COUNT ) {
136
136
return Promise . reject ( '[FileUpload]: Exceeded maximum of %d% files per message' , MAX_FILE_COUNT ) ;
137
137
}
138
138
139
139
var result = [ ] ;
140
140
141
- // Find large images that need to be resized
142
- var resizePromises = [ ] ;
143
- /*
144
- files.forEach(function (file) {
145
- // Even create a thumbnail for small images
146
- if (circuit.Utils.isSupportedImage && circuit.Utils.isSupportedImage(file.type)) {
147
- resizePromises.push(createThumbnail(file));
148
- logger.warning('[FileUpload]: node SDK does not yet support creation of thumbnails');
149
- }
150
- });
151
- */
152
-
153
- return Promise . all ( resizePromises )
154
- . then ( function ( blobs ) {
155
- // Add the blob's (thumbnails) to the list of files to be upoaded
156
- files = files . concat ( blobs ) ;
157
-
158
- // Sequentially (but async) upload the files. Once all files are
159
- // uploaded, resolve the Promise passing the upload results.
160
- var itemId = null ;
161
- return files . reduce ( function ( sequence , file ) {
162
- return sequence . then ( function ( ) {
163
- var url = 'https://' + domain + '/fileapi?itemid=' + ( itemId || 'NULL' ) ;
164
- return uploadFile ( file , url )
165
- . then ( function ( res ) {
166
- var resp = JSON . parse ( res ) [ 0 ] ;
167
- itemId = itemId || resp . id ;
168
- var attachmentMetaData = {
169
- fileId : resp . fileid . slice ( - 1 ) [ 0 ] ,
170
- fileName : file . name ,
171
- itemId : itemId ,
172
- mimeType : resp . mimeType || file . type ,
173
- size : file . size
174
- } ;
175
- result . push ( attachmentMetaData ) ;
176
- return result ;
177
- } ) ;
141
+ // Sequentially (but async) upload the files. Once all files are
142
+ // uploaded, resolve the Promise passing the upload results.
143
+ var itemId = null ;
144
+ return files . reduce ( function ( sequence , file ) {
145
+ return sequence . then ( function ( ) {
146
+ var url = 'https://' + domain + '/fileapi?itemid=' + ( itemId || 'NULL' ) ;
147
+ return uploadFile ( file , url )
148
+ . then ( function ( res ) {
149
+ var resp = JSON . parse ( res ) [ 0 ] ;
150
+ itemId = itemId || resp . id ;
151
+ var attachmentMetaData = {
152
+ fileId : resp . fileid . slice ( - 1 ) [ 0 ] ,
153
+ fileName : file . name ,
154
+ itemId : itemId ,
155
+ mimeType : resp . mimeType || file . type ,
156
+ size : file . size
157
+ } ;
158
+ result . push ( attachmentMetaData ) ;
159
+ return result ;
178
160
} ) ;
179
- } , Promise . resolve ( ) ) ;
180
- } ) ;
161
+ } ) ;
162
+ } , Promise . resolve ( ) ) ;
181
163
} ;
182
164
183
165
this . uploadWhiteboardFile = function ( file , rtcSessionId ) {
184
- logger . debug ( '[FileUpload] node SDK uploadWhiteboardFile: ' , file ) ;
166
+ _logger . debug ( '[FileUpload] node SDK uploadWhiteboardFile: ' , file ) ;
185
167
186
168
var url = 'https://' + _config . domain + '/fileapi' ;
187
169
var opts = {
0 commit comments