@@ -5,7 +5,7 @@ var EventEmitter = require('events').EventEmitter,
55 imageMagick = require ( 'imagemagick' ) ,
66 mkdirp = require ( 'mkdirp' ) ,
77 _ = require ( 'lodash' ) ,
8- async = require ( 'async' ) ;
8+ async = require ( 'async' ) ;
99
1010module . exports = function ( options ) {
1111
@@ -35,28 +35,28 @@ module.exports = function (options) {
3535 this . noCache ( ) ;
3636 var files = [ ] ;
3737 fs . readdir ( options . uploadDir ( ) , _ . bind ( function ( err , list ) {
38- async . each ( list , _ . bind ( function ( name , cb ) {
39- fs . stat ( options . uploadDir ( ) + '/' + name , _ . bind ( function ( err , stats ) {
40- if ( ! err ) {
41- if ( stats . isFile ( ) ) {
42- fileInfo = new FileInfo ( {
43- name : name ,
44- size : stats . size
45- } ) ;
46- this . initUrls ( fileInfo , function ( err ) {
47- files . push ( fileInfo ) ;
48- cb ( err ) ;
49- return ;
50- } ) ;
51- }
52- }
53- cb ( err ) ;
54- } , this ) ) ;
55- } , this ) ,
56- function ( err ) {
57- if ( err ) console . log ( err ) ;
58- this . callback ( { files : files } ) ;
59- } ) ;
38+ async . each ( list , _ . bind ( function ( name , cb ) {
39+ fs . stat ( options . uploadDir ( ) + '/' + name , _ . bind ( function ( err , stats ) {
40+ if ( ! err ) {
41+ if ( stats . isFile ( ) ) {
42+ fileInfo = new FileInfo ( {
43+ name : name ,
44+ size : stats . size
45+ } ) ;
46+ this . initUrls ( fileInfo , function ( err ) {
47+ files . push ( fileInfo ) ;
48+ cb ( err ) ;
49+ return ;
50+ } ) ;
51+ }
52+ }
53+ cb ( err ) ;
54+ } , this ) ) ;
55+ } , this ) ,
56+ function ( err ) {
57+ if ( err ) console . log ( err ) ;
58+ this . callback ( { files : files } ) ;
59+ } ) ;
6060 } , this ) ) ;
6161 } ;
6262
@@ -70,15 +70,15 @@ module.exports = function (options) {
7070 redirect ,
7171 finish = _ . bind ( function ( ) {
7272 if ( ! -- counter ) {
73- async . each ( files , _ . bind ( function ( fileInfo , cb ) {
73+ async . each ( files , _ . bind ( function ( fileInfo , cb ) {
7474 this . initUrls ( fileInfo , _ . bind ( function ( err ) {
75- this . emit ( 'end' , fileInfo ) ;
76- cb ( err ) ;
77- } , this ) ) ;
78- } , this ) ,
79- _ . bind ( function ( err ) {
80- this . callback ( { files : files } , redirect ) ;
81- } , this ) ) ;
75+ this . emit ( 'end' , fileInfo ) ;
76+ cb ( err ) ;
77+ } , this ) ) ;
78+ } , this ) ,
79+ _ . bind ( function ( err ) {
80+ this . callback ( { files : files } , redirect ) ;
81+ } , this ) ) ;
8282 }
8383 } , this ) ;
8484
@@ -100,58 +100,58 @@ module.exports = function (options) {
100100 }
101101 } )
102102 . on ( 'file' , function ( name , file ) {
103- counter ++ ;
103+ counter ++ ;
104104 var fileInfo = map [ path . basename ( file . path ) ] ;
105- fs . exists ( file . path , function ( exists ) {
106- if ( exists ) {
107- fileInfo . size = file . size ;
108- if ( ! fileInfo . validate ( ) ) {
109- fs . unlink ( file . path ) ;
110- counter -- ;
111- return ;
112- }
113-
114- var generatePreviews = function ( ) {
115- if ( options . imageTypes . test ( fileInfo . name ) ) {
116- _ . each ( options . imageVersions , function ( value , version ) {
117- counter ++ ;
118- // creating directory recursive
119- mkdirp ( options . uploadDir ( ) + '/' + version + '/' , function ( err , made ) {
120- var opts = options . imageVersions [ version ] ;
121- imageMagick . resize ( {
122- width : opts . width ,
123- height : opts . height ,
124- srcPath : options . uploadDir ( ) + '/' + fileInfo . name ,
125- dstPath : options . uploadDir ( ) + '/' + version + '/' + fileInfo . name ,
126- customArgs : opts . imageArgs || [ '-auto-orient' ]
127- } , finish ) ;
128- } ) ;
129- } ) ;
105+ fs . exists ( file . path , function ( exists ) {
106+ if ( exists ) {
107+ fileInfo . size = file . size ;
108+ if ( ! fileInfo . validate ( ) ) {
109+ fs . unlink ( file . path ) ;
110+ counter -- ;
111+ return ;
130112 }
131- }
132-
133- mkdirp ( options . uploadDir ( ) + '/' , function ( err , made ) {
134- fs . rename ( file . path , options . uploadDir ( ) + '/' + fileInfo . name , function ( err ) {
135- if ( ! err ) {
136- generatePreviews ( ) ;
137- finish ( ) ;
138- } else {
139- var is = fs . createReadStream ( file . path ) ;
140- var os = fs . createWriteStream ( options . uploadDir ( ) + '/' + fileInfo . name ) ;
141- is . on ( 'end' , function ( err ) {
113+
114+ var generatePreviews = function ( ) {
115+ if ( options . imageTypes . test ( fileInfo . name ) ) {
116+ _ . each ( options . imageVersions , function ( value , version ) {
117+ counter ++ ;
118+ // creating directory recursive
119+ mkdirp ( options . uploadDir ( ) + '/' + version + '/' , function ( err , made ) {
120+ var opts = options . imageVersions [ version ] ;
121+ imageMagick . resize ( {
122+ width : opts . width ,
123+ height : opts . height ,
124+ srcPath : options . uploadDir ( ) + '/' + fileInfo . name ,
125+ dstPath : options . uploadDir ( ) + '/' + version + '/' + fileInfo . name ,
126+ customArgs : opts . imageArgs || [ '-auto-orient' ]
127+ } , finish ) ;
128+ } ) ;
129+ } ) ;
130+ }
131+ }
132+
133+ mkdirp ( options . uploadDir ( ) + '/' , function ( err , made ) {
134+ fs . rename ( file . path , options . uploadDir ( ) + '/' + fileInfo . name , function ( err ) {
142135 if ( ! err ) {
143- fs . unlink ( file . path ) ;
144136 generatePreviews ( ) ;
137+ finish ( ) ;
138+ } else {
139+ var is = fs . createReadStream ( file . path ) ;
140+ var os = fs . createWriteStream ( options . uploadDir ( ) + '/' + fileInfo . name ) ;
141+ is . on ( 'end' , function ( err ) {
142+ if ( ! err ) {
143+ fs . unlink ( file . path ) ;
144+ generatePreviews ( ) ;
145+ }
146+ finish ( ) ;
147+ } ) ;
148+ is . pipe ( os ) ;
145149 }
146- finish ( ) ;
147150 } ) ;
148- is . pipe ( os ) ;
149- }
150- } ) ;
151- } ) ;
152- }
153- else counter -- ;
154- } ) ;
151+ } ) ;
152+ }
153+ else counter -- ;
154+ } ) ;
155155 } )
156156 . on ( 'aborted' , function ( ) {
157157 _ . each ( tmpFiles , function ( file ) {
@@ -188,13 +188,13 @@ module.exports = function (options) {
188188 var baseUrl = ( options . ssl ? 'https:' : 'http:' ) + '//' + ( options . hostname || this . req . get ( 'Host' ) ) ;
189189 fileInfo . setUrl ( null , baseUrl + options . uploadUrl ( ) ) ;
190190 fileInfo . setUrl ( 'delete' , baseUrl + this . req . originalUrl ) ;
191- async . each ( Object . keys ( options . imageVersions ) , function ( version , cb ) {
192- fs . exists ( options . uploadDir ( ) + '/' + version + '/' + fileInfo . name , function ( exists ) {
193- if ( exists ) fileInfo . setUrl ( version , baseUrl + options . uploadUrl ( ) + '/' + version ) ;
194- cb ( null ) ;
195- } )
196- } ,
197- cb ) ;
191+ async . each ( Object . keys ( options . imageVersions ) , function ( version , cb ) {
192+ fs . exists ( options . uploadDir ( ) + '/' + version + '/' + fileInfo . name , function ( exists ) {
193+ if ( exists ) fileInfo . setUrl ( version , baseUrl + options . uploadUrl ( ) + '/' + version ) ;
194+ cb ( null ) ;
195+ } )
196+ } ,
197+ cb ) ;
198198 } ;
199199
200200 return UploadHandler ;
0 commit comments