@@ -5,7 +5,7 @@ module.exports = function (options) {
5
5
formidable = require ( 'formidable' ) ,
6
6
imageMagick = require ( 'imagemagick' ) ,
7
7
_ = require ( 'lodash' ) ,
8
- // Since Node 0.8, .existsSync() moved from path to fs:
8
+ // Since Node 0.8, .existsSync() moved from path to fs:
9
9
_existsSync = fs . existsSync || path . existsSync ,
10
10
utf8encode = function ( str ) {
11
11
return unescape ( encodeURIComponent ( str ) ) ;
@@ -73,13 +73,13 @@ module.exports = function (options) {
73
73
var that = this ,
74
74
baseUrl = ( options . ssl ? 'https:' : 'http:' ) +
75
75
'//' + 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 ) ;
78
78
Object . keys ( options . imageVersions ) . forEach ( function ( version ) {
79
79
if ( _existsSync (
80
80
options . uploadDir + '/' + version + '/' + that . name
81
81
) ) {
82
- that [ version + '_url' ] = baseUrl + options . uploadUrl + '/' + version + '/' + encodeURIComponent ( that . name ) ;
82
+ that [ version + '_url' ] = baseUrl + options . uploadUrl + '/' + version + '/' + encodeURIComponent ( that . name ) ;
83
83
}
84
84
} ) ;
85
85
}
@@ -165,7 +165,7 @@ module.exports = function (options) {
165
165
166
166
var generatePreviews = function ( ) {
167
167
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 ) {
169
169
if ( ! _existsSync ( options . uploadDir + '/' + version + '/' ) )
170
170
throw new Error ( options . uploadDir + '/' + version + '/' + ' not exists' ) ;
171
171
counter ++ ;
@@ -180,22 +180,24 @@ module.exports = function (options) {
180
180
}
181
181
}
182
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 -- ;
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
+ } ) ;
199
201
}
200
202
} )
201
203
. on ( 'aborted' , function ( ) {
0 commit comments