@@ -162,20 +162,39 @@ module.exports = function (options) {
162162 fs . unlink ( file . path ) ;
163163 return ;
164164 }
165- fs . renameSync ( file . path , options . uploadDir + '/' + fileInfo . name ) ;
166- if ( options . imageTypes . test ( fileInfo . name ) && _ . keys ( options . imageVersions ) . length ) {
167- Object . keys ( options . imageVersions ) . forEach ( function ( version ) {
168- if ( ! _existsSync ( options . uploadDir + '/' + version + '/' ) )
169- throw new Error ( options . uploadDir + '/' + version + '/' + ' not exists' ) ;
170- counter ++ ;
171- var opts = options . imageVersions [ version ] ;
172- imageMagick . resize ( {
173- width : opts . width ,
174- height : opts . height ,
175- srcPath : options . uploadDir + '/' + fileInfo . name ,
176- dstPath : options . uploadDir + '/' + version + '/' + fileInfo . name
177- } , finish ) ;
165+
166+ var generatePreviews = function ( ) {
167+ if ( options . imageTypes . test ( fileInfo . name ) && _ . keys ( options . imageVersions ) . length ) {
168+ Object . keys ( options . imageVersions ) . forEach ( function ( version ) {
169+ if ( ! _existsSync ( options . uploadDir + '/' + version + '/' ) )
170+ throw new Error ( options . uploadDir + '/' + version + '/' + ' not exists' ) ;
171+ counter ++ ;
172+ var opts = options . imageVersions [ version ] ;
173+ imageMagick . resize ( {
174+ width : opts . width ,
175+ height : opts . height ,
176+ srcPath : options . uploadDir + '/' + fileInfo . name ,
177+ dstPath : options . uploadDir + '/' + version + '/' + fileInfo . name
178+ } , finish ) ;
179+ } ) ;
180+ }
181+ }
182+
183+ try {
184+ fs . renameSync ( file . path , options . uploadDir + '/' + fileInfo . name ) ;
185+ generatePreviews ( ) ;
186+ } catch ( e ) {
187+ counter ++ ;
188+ var is = fs . createReadStream ( file . path ) ;
189+ var os = fs . createWriteStream ( options . uploadDir + '/' + fileInfo . name ) ;
190+ is . on ( 'end' , function ( err ) {
191+ if ( ! err ) {
192+ fs . unlinkSync ( file . path ) ;
193+ generatePreviews ( ) ;
194+ }
195+ counter -- ;
178196 } ) ;
197+ is . pipe ( os ) ;
179198 }
180199 }
181200 } )
0 commit comments