@@ -207,81 +207,87 @@ module.exports = function (conf) {
207207 } ;
208208
209209 const putFileContent = ( fileContent , fileName , isPrivate , callback ) => {
210- const fileInfo = getFileInfo ( fileName ) ;
211- const contentSettings = {
212- cacheControl : 'public, max-age=31556926'
213- } ;
214- if ( fileInfo . mimeType ) {
215- contentSettings . contentType = fileInfo . mimeType ;
216- }
210+ try {
211+ const fileInfo = getFileInfo ( fileName ) ;
212+ const contentSettings = {
213+ cacheControl : 'public, max-age=31556926'
214+ } ;
215+ if ( fileInfo . mimeType ) {
216+ contentSettings . contentType = fileInfo . mimeType ;
217+ }
217218
218- if ( fileInfo . gzip ) {
219- contentSettings . contentEncoding = 'gzip' ;
220- }
219+ if ( fileInfo . gzip ) {
220+ contentSettings . contentEncoding = 'gzip' ;
221+ }
222+
223+ const uploadToAzureContainer = ( rereadable , containerName , cb ) => {
224+ try {
225+ if ( fileContent instanceof stream . Stream ) {
226+ return fileContent . pipe (
227+ getClient ( ) . createWriteStreamToBlockBlob (
228+ containerName ,
229+ fileName ,
230+ { contentSettings : contentSettings } ,
231+ ( err , res ) => {
232+ if ( rereadable ) {
233+ // I need a fresh read stream and this is the only thing I came up with
234+ // very ugly and has poor performance, but works
235+ fileContent = getClient ( ) . createReadStream (
236+ containerName ,
237+ fileName
238+ ) ;
239+ }
240+
241+ cb ( err , res ) ;
242+ }
243+ )
244+ ) ;
245+ }
221246
222- const uploadToAzureContainer = ( rereadable , containerName , cb ) => {
223- if ( fileContent instanceof stream . Stream ) {
224- return fileContent . pipe (
225- getClient ( ) . createWriteStreamToBlockBlob (
247+ getClient ( ) . createBlockBlobFromText (
226248 containerName ,
227249 fileName ,
250+ fileContent ,
228251 { contentSettings : contentSettings } ,
229- ( err , res ) => {
230- if ( rereadable ) {
231- // I need a fresh read stream and this is the only thing I came up with
232- // very ugly and has poor performance, but works
233- fileContent = getClient ( ) . createReadStream (
234- containerName ,
235- fileName
236- ) ;
237- }
238-
239- cb ( err , res ) ;
240- }
241- )
242- ) ;
243- }
252+ cb
253+ ) ;
254+ } catch ( err ) {
255+ return cb ( err ) ;
256+ }
257+ } ;
244258
245- getClient ( ) . createBlockBlobFromText (
246- containerName ,
247- fileName ,
248- fileContent ,
249- { contentSettings : contentSettings } ,
250- cb
251- ) ;
252- } ;
259+ const makeReReadable = ! isPrivate ;
260+ uploadToAzureContainer (
261+ makeReReadable ,
262+ conf . privateContainerName ,
263+ ( err , result ) => {
264+ if ( err ) {
265+ return callback ( err ) ;
266+ }
253267
254- const makeReReadable = ! isPrivate ;
255- uploadToAzureContainer (
256- makeReReadable ,
257- conf . privateContainerName ,
258- ( err , result ) => {
259- if ( err ) {
260- return callback ( err ) ;
261- }
268+ if ( ! isPrivate ) {
269+ return uploadToAzureContainer (
270+ false ,
271+ conf . publicContainerName ,
272+ callback
273+ ) ;
274+ }
262275
263- if ( ! isPrivate ) {
264- return uploadToAzureContainer (
265- false ,
266- conf . publicContainerName ,
267- callback
268- ) ;
276+ return callback ( null , result ) ;
269277 }
270-
271- return callback ( null , result ) ;
272- }
273- ) ;
278+ ) ;
279+ } catch ( err ) {
280+ return callback ( err ) ;
281+ }
274282 } ;
275283
276284 const putFile = ( filePath , fileName , isPrivate , callback ) => {
277- let stream = null ;
278285 try {
279- stream = fs . createReadStream ( filePath ) ;
286+ const stream = fs . createReadStream ( filePath ) ;
287+ putFileContent ( stream , fileName , isPrivate , callback ) ;
280288 } catch ( e ) {
281- console . log ( 'putfile error!' , e ) ;
282289 return callback ( e ) ;
283290 }
284- return putFileContent ( stream , fileName , isPrivate , callback ) ;
285291 } ;
286292
287293 return {
0 commit comments