@@ -108,62 +108,60 @@ module.exports = function (options) {
108
108
. on ( 'file' , function ( name , file ) {
109
109
counter ++ ;
110
110
var fileInfo = map [ path . basename ( file . path ) ] ;
111
- fs . exists ( file . path , function ( exists ) {
112
- if ( exists ) {
113
- fileInfo . size = file . size ;
114
- if ( ! fileInfo . validate ( ) ) {
115
- fs . unlink ( file . path ) ;
116
- finish ( ) ;
117
- return ;
118
- }
111
+ if ( fs . existsSync ( file . path ) ) {
112
+ fileInfo . size = file . size ;
113
+ if ( ! fileInfo . validate ( ) ) {
114
+ fs . unlinkSync ( file . path ) ;
115
+ finish ( ) ;
116
+ return ;
117
+ }
119
118
120
- var generatePreviews = function ( ) {
121
- if ( options . imageTypes . test ( fileInfo . name ) ) {
122
- _ . each ( options . imageVersions , function ( value , version ) {
123
- counter ++ ;
124
- // creating directory recursive
125
- mkdirp ( options . uploadDir ( ) + '/' + version + '/' , function ( err , made ) {
126
- var opts = options . imageVersions [ version ] ;
127
- imageMagick . resize ( {
128
- width : opts . width ,
129
- height : opts . height ,
130
- srcPath : options . uploadDir ( ) + '/' + fileInfo . name ,
131
- dstPath : options . uploadDir ( ) + '/' + version + '/' + fileInfo . name ,
132
- customArgs : opts . imageArgs || [ '-auto-orient' ]
133
- } , finish ) ;
134
- } ) ;
119
+ var generatePreviews = function ( ) {
120
+ if ( options . imageTypes . test ( fileInfo . name ) ) {
121
+ _ . each ( options . imageVersions , function ( value , version ) {
122
+ counter ++ ;
123
+ // creating directory recursive
124
+ mkdirp ( options . uploadDir ( ) + '/' + version + '/' , function ( err , made ) {
125
+ var opts = options . imageVersions [ version ] ;
126
+ imageMagick . resize ( {
127
+ width : opts . width ,
128
+ height : opts . height ,
129
+ srcPath : options . uploadDir ( ) + '/' + fileInfo . name ,
130
+ dstPath : options . uploadDir ( ) + '/' + version + '/' + fileInfo . name ,
131
+ customArgs : opts . imageArgs || [ '-auto-orient' ]
132
+ } , finish ) ;
135
133
} ) ;
136
- }
134
+ } ) ;
137
135
}
136
+ }
138
137
139
- mkdirp ( options . uploadDir ( ) + '/' , function ( err , made ) {
140
- fs . rename ( file . path , options . uploadDir ( ) + '/' + fileInfo . name , function ( err ) {
141
- if ( ! err ) {
142
- generatePreviews ( ) ;
138
+ mkdirp ( options . uploadDir ( ) + '/' , function ( err , made ) {
139
+ fs . rename ( file . path , options . uploadDir ( ) + '/' + fileInfo . name , function ( err ) {
140
+ if ( ! err ) {
141
+ generatePreviews ( ) ;
142
+ finish ( ) ;
143
+ } else {
144
+ var is = fs . createReadStream ( file . path ) ;
145
+ var os = fs . createWriteStream ( options . uploadDir ( ) + '/' + fileInfo . name ) ;
146
+ is . on ( 'end' , function ( err ) {
147
+ if ( ! err ) {
148
+ fs . unlinkSync ( file . path ) ;
149
+ generatePreviews ( ) ;
150
+ }
143
151
finish ( ) ;
144
- } else {
145
- var is = fs . createReadStream ( file . path ) ;
146
- var os = fs . createWriteStream ( options . uploadDir ( ) + '/' + fileInfo . name ) ;
147
- is . on ( 'end' , function ( err ) {
148
- if ( ! err ) {
149
- fs . unlink ( file . path ) ;
150
- generatePreviews ( ) ;
151
- }
152
- finish ( ) ;
153
- } ) ;
154
- is . pipe ( os ) ;
155
- }
156
- } ) ;
152
+ } ) ;
153
+ is . pipe ( os ) ;
154
+ }
157
155
} ) ;
158
- }
159
- else finish ( ) ;
160
- } ) ;
156
+ } ) ;
157
+ }
158
+ else finish ( ) ;
161
159
} )
162
160
. on ( 'aborted' , function ( ) {
163
161
_ . each ( tmpFiles , function ( file ) {
164
162
var fileInfo = map [ path . basename ( file ) ] ;
165
163
self . emit ( 'abort' , fileInfo ) ;
166
- fs . unlink ( file ) ;
164
+ fs . unlinkSync ( file ) ;
167
165
} ) ;
168
166
} )
169
167
. on ( 'error' , function ( e ) {
@@ -189,7 +187,7 @@ module.exports = function (options) {
189
187
}
190
188
fs . unlink ( filepath , function ( ex ) {
191
189
_ . each ( options . imageVersions , function ( value , version ) {
192
- fs . unlink ( path . join ( options . uploadDir ( ) , version , fileName ) ) ;
190
+ fs . unlinkSync ( path . join ( options . uploadDir ( ) , version , fileName ) ) ;
193
191
} ) ;
194
192
self . emit ( 'delete' , fileName ) ;
195
193
self . callback ( { success : ! ex } ) ;
@@ -201,10 +199,10 @@ module.exports = function (options) {
201
199
fileInfo . setUrl ( null , baseUrl + options . uploadUrl ( ) ) ;
202
200
fileInfo . setUrl ( 'delete' , baseUrl + this . req . originalUrl ) ;
203
201
async . each ( Object . keys ( options . imageVersions ) , function ( version , cb ) {
204
- fs . exists ( options . uploadDir ( ) + '/' + version + '/' + fileInfo . name , function ( exists ) {
205
- if ( exists ) fileInfo . setUrl ( version , baseUrl + options . uploadUrl ( ) + '/' + version ) ;
206
- cb ( null ) ;
207
- } )
202
+ if ( fs . existsSync ( options . uploadDir ( ) + '/' + version + '/' + fileInfo . name ) ) {
203
+ fileInfo . setUrl ( version , baseUrl + options . uploadUrl ( ) + '/' + version ) ;
204
+ }
205
+ cb ( null ) ;
208
206
} ,
209
207
cb ) ;
210
208
} ;
0 commit comments