@@ -5,7 +5,7 @@ module.exports = function (options) {
55 formidable = require ( 'formidable' ) ,
66 imageMagick = require ( 'imagemagick' ) ,
77 _ = require ( 'lodash' ) ,
8- // Since Node 0.8, .existsSync() moved from path to fs:
8+ // Since Node 0.8, .existsSync() moved from path to fs:
99 _existsSync = fs . existsSync || path . existsSync ,
1010 utf8encode = function ( str ) {
1111 return unescape ( encodeURIComponent ( str ) ) ;
@@ -73,13 +73,13 @@ module.exports = function (options) {
7373 var that = this ,
7474 baseUrl = ( options . ssl ? 'https:' : 'http:' ) +
7575 '//' + req . headers . host ;
76- this . delete_url = baseUrl + req . originalUrl + '/' + encodeURIComponent ( this . name ) ;
77- this . url = baseUrl + options . uploadUrl + '/' + encodeURIComponent ( this . name ) ;
76+ this . delete_url = baseUrl + req . originalUrl + '/' + encodeURIComponent ( this . name ) ;
77+ this . url = baseUrl + options . uploadUrl + '/' + encodeURIComponent ( this . name ) ;
7878 Object . keys ( options . imageVersions ) . forEach ( function ( version ) {
7979 if ( _existsSync (
8080 options . uploadDir + '/' + version + '/' + that . name
8181 ) ) {
82- that [ version + '_url' ] = baseUrl + options . uploadUrl + '/' + version + '/' + encodeURIComponent ( that . name ) ;
82+ that [ version + '_url' ] = baseUrl + options . uploadUrl + '/' + version + '/' + encodeURIComponent ( that . name ) ;
8383 }
8484 } ) ;
8585 }
@@ -165,7 +165,7 @@ module.exports = function (options) {
165165
166166 var generatePreviews = function ( ) {
167167 if ( options . imageTypes . test ( fileInfo . name ) && _ . keys ( options . imageVersions ) . length ) {
168- Object . keys ( options . imageVersions ) . forEach ( function ( version ) {
168+ _ . keys ( options . imageVersions ) . forEach ( function ( version ) {
169169 if ( ! _existsSync ( options . uploadDir + '/' + version + '/' ) )
170170 throw new Error ( options . uploadDir + '/' + version + '/' + ' not exists' ) ;
171171 counter ++ ;
@@ -180,22 +180,24 @@ module.exports = function (options) {
180180 }
181181 }
182182
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 -- ;
196- } ) ;
197- is . pipe ( os ) ;
198- }
183+ counter ++ ;
184+ fs . rename ( file . path , options . uploadDir + '/' + fileInfo . name , function ( err ) {
185+ if ( ! err ) {
186+ generatePreviews ( ) ;
187+ finish ( ) ;
188+ } else {
189+ var is = fs . createReadStream ( file . path ) ;
190+ var os = fs . createWriteStream ( options . uploadDir + '/' + fileInfo . name ) ;
191+ is . on ( 'end' , function ( err ) {
192+ if ( ! err ) {
193+ fs . unlinkSync ( file . path ) ;
194+ generatePreviews ( ) ;
195+ }
196+ finish ( ) ;
197+ } ) ;
198+ is . pipe ( os ) ;
199+ }
200+ } ) ;
199201 }
200202 } )
201203 . on ( 'aborted' , function ( ) {
0 commit comments