@@ -60,7 +60,7 @@ module.exports = function (options) {
60
60
else cb ( err ) ;
61
61
} , this ) ) ;
62
62
} , this ) ,
63
- _ . bind ( function ( err ) {
63
+ _ . bind ( function ( err ) {
64
64
this . callback ( { files : files } ) ;
65
65
} , this ) ) ;
66
66
} , this ) ) ;
@@ -83,7 +83,7 @@ module.exports = function (options) {
83
83
cb ( err ) ;
84
84
} , this ) ) ;
85
85
} , this ) ,
86
- _ . bind ( function ( err ) {
86
+ _ . bind ( function ( err ) {
87
87
this . callback ( { files : files } , redirect ) ;
88
88
} , this ) ) ;
89
89
}
@@ -93,92 +93,92 @@ module.exports = function (options) {
93
93
94
94
form . uploadDir = options . tmpDir ;
95
95
form
96
- . on ( 'fileBegin' , function ( name , file ) {
97
- tmpFiles . push ( file . path ) ;
98
- var fileInfo = new FileInfo ( file ) ;
99
- fileInfo . safeName ( ) ;
100
- map [ path . basename ( file . path ) ] = fileInfo ;
101
- files . push ( fileInfo ) ;
102
- self . emit ( 'begin' , fileInfo ) ;
103
- } )
104
- . on ( 'field' , function ( name , value ) {
105
- if ( name === 'redirect' ) {
106
- redirect = value ;
107
- }
108
- if ( ! self . req . fields )
109
- self . req . fields = { } ;
110
- self . req . fields [ name ] = value ;
111
- } )
112
- . on ( 'file' , function ( name , file ) {
113
- counter ++ ;
114
- var fileInfo = map [ path . basename ( file . path ) ] ;
115
- fs . exists ( file . path , function ( exists ) {
116
- if ( exists ) {
117
- fileInfo . size = file . size ;
118
- if ( ! fileInfo . validate ( ) ) {
119
- fs . unlink ( file . path ) ;
120
- finish ( ) ;
121
- return ;
122
- }
96
+ . on ( 'fileBegin' , function ( name , file ) {
97
+ tmpFiles . push ( file . path ) ;
98
+ var fileInfo = new FileInfo ( file ) ;
99
+ fileInfo . safeName ( ) ;
100
+ map [ path . basename ( file . path ) ] = fileInfo ;
101
+ files . push ( fileInfo ) ;
102
+ self . emit ( 'begin' , fileInfo ) ;
103
+ } )
104
+ . on ( 'field' , function ( name , value ) {
105
+ if ( name === 'redirect' ) {
106
+ redirect = value ;
107
+ }
108
+ if ( ! self . req . fields )
109
+ self . req . fields = { } ;
110
+ self . req . fields [ name ] = value ;
111
+ } )
112
+ . on ( 'file' , function ( name , file ) {
113
+ counter ++ ;
114
+ var fileInfo = map [ path . basename ( file . path ) ] ;
115
+ fs . exists ( file . path , function ( exists ) {
116
+ if ( exists ) {
117
+ fileInfo . size = file . size ;
118
+ if ( ! fileInfo . validate ( ) ) {
119
+ fs . unlink ( file . path ) ;
120
+ finish ( ) ;
121
+ return ;
122
+ }
123
123
124
- var generatePreviews = function ( ) {
125
- if ( options . imageTypes . test ( fileInfo . name ) ) {
126
- _ . each ( options . imageVersions , function ( value , version ) {
127
- counter ++ ;
128
- // creating directory recursive
129
- mkdirp ( options . uploadDir ( ) + '/' + version + '/' , function ( err , made ) {
130
- var opts = options . imageVersions [ version ] ;
131
- imageMagick . resize ( {
132
- width : opts . width ,
133
- height : opts . height ,
134
- srcPath : options . uploadDir ( ) + '/' + fileInfo . name ,
135
- dstPath : options . uploadDir ( ) + '/' + version + '/' + fileInfo . name ,
136
- customArgs : opts . imageArgs || [ '-auto-orient' ]
137
- } , finish ) ;
138
- } ) ;
124
+ var generatePreviews = function ( ) {
125
+ if ( options . imageTypes . test ( fileInfo . name ) ) {
126
+ _ . each ( options . imageVersions , function ( value , version ) {
127
+ counter ++ ;
128
+ // creating directory recursive
129
+ mkdirp ( options . uploadDir ( ) + '/' + version + '/' , function ( err , made ) {
130
+ var opts = options . imageVersions [ version ] ;
131
+ imageMagick . resize ( {
132
+ width : opts . width ,
133
+ height : opts . height ,
134
+ srcPath : options . uploadDir ( ) + '/' + fileInfo . name ,
135
+ dstPath : options . uploadDir ( ) + '/' + version + '/' + fileInfo . name ,
136
+ customArgs : opts . imageArgs || [ '-auto-orient' ]
137
+ } , finish ) ;
139
138
} ) ;
140
- }
139
+ } ) ;
141
140
}
141
+ }
142
142
143
- mkdirp ( options . uploadDir ( ) + '/' , function ( err , made ) {
144
- fs . rename ( file . path , options . uploadDir ( ) + '/' + fileInfo . name , function ( err ) {
145
- if ( ! err ) {
146
- generatePreviews ( ) ;
143
+ mkdirp ( options . uploadDir ( ) + '/' , function ( err , made ) {
144
+ fs . rename ( file . path , options . uploadDir ( ) + '/' + fileInfo . name , function ( err ) {
145
+ if ( ! err ) {
146
+ generatePreviews ( ) ;
147
+ finish ( ) ;
148
+ } else {
149
+ var is = fs . createReadStream ( file . path ) ;
150
+ var os = fs . createWriteStream ( options . uploadDir ( ) + '/' + fileInfo . name ) ;
151
+ is . on ( 'end' , function ( err ) {
152
+ if ( ! err ) {
153
+ fs . unlink ( file . path ) ;
154
+ generatePreviews ( ) ;
155
+ }
147
156
finish ( ) ;
148
- } else {
149
- var is = fs . createReadStream ( file . path ) ;
150
- var os = fs . createWriteStream ( options . uploadDir ( ) + '/' + fileInfo . name ) ;
151
- is . on ( 'end' , function ( err ) {
152
- if ( ! err ) {
153
- fs . unlink ( file . path ) ;
154
- generatePreviews ( ) ;
155
- }
156
- finish ( ) ;
157
- } ) ;
158
- is . pipe ( os ) ;
159
- }
160
- } ) ;
157
+ } ) ;
158
+ is . pipe ( os ) ;
159
+ }
161
160
} ) ;
162
- }
163
- else finish ( ) ;
164
- } ) ;
165
- } )
166
- . on ( 'aborted' , function ( ) {
167
- _ . each ( tmpFiles , function ( file ) {
168
- var fileInfo = map [ path . basename ( file ) ] ;
169
- self . emit ( 'abort' , fileInfo ) ;
170
- fs . unlink ( file ) ;
171
- } ) ;
172
- } )
173
- . on ( 'error' , function ( e ) {
174
- self . emit ( 'error' , e ) ;
175
- } )
176
- . on ( 'progress' , function ( bytesReceived , bytesExpected ) {
177
- if ( bytesReceived > options . maxPostSize )
178
- self . req . connection . destroy ( ) ;
179
- } )
180
- . on ( 'end' , finish )
181
- . parse ( self . req ) ;
161
+ } ) ;
162
+ }
163
+ else finish ( ) ;
164
+ } ) ;
165
+ } )
166
+ . on ( 'aborted' , function ( ) {
167
+ _ . each ( tmpFiles , function ( file ) {
168
+ var fileInfo = map [ path . basename ( file ) ] ;
169
+ self . emit ( 'abort' , fileInfo ) ;
170
+ fs . unlink ( file ) ;
171
+ } ) ;
172
+ } )
173
+ . on ( 'error' , function ( e ) {
174
+ self . emit ( 'error' , e ) ;
175
+ } )
176
+ . on ( 'progress' , function ( bytesReceived , bytesExpected ) {
177
+ if ( bytesReceived > options . maxPostSize )
178
+ self . req . connection . destroy ( ) ;
179
+ } )
180
+ . on ( 'end' , finish )
181
+ . parse ( self . req ) ;
182
182
} ;
183
183
184
184
UploadHandler . prototype . destroy = function ( ) {
@@ -202,36 +202,44 @@ module.exports = function (options) {
202
202
203
203
UploadHandler . prototype . initUrls = function ( fileInfo , cb ) {
204
204
//http://<<bucket>>.s3-<<region>>.amazonaws.com
205
- var baseUrl = ( options . ssl ? 'https:' : 'http:' ) + '//' + options . s3Bucket ( ) + '.s3-' + options . s3Region ( ) + '.amazonaws.com' ;
205
+ var baseUrl = ( options . ssl ? 'https:' : 'http:' ) + '//' + options . s3Bucket ( ) + '.s3-' + options . s3Region ( ) + '.amazonaws.com/ ' ;
206
206
fileInfo . setUrl ( null , baseUrl + options . s3Url ( ) ) ;
207
- fileInfo . setUrl ( 'delete' , baseUrl + this . req . s3Url ) ;
208
- async . each ( Object . keys ( options . imageVersions ) , function ( version , cb ) {
209
- fs . exists ( options . uploadDir ( ) + '/' + version + '/' + fileInfo . name , function ( exists ) {
210
- if ( exists ) fileInfo . setUrl ( version , baseUrl + options . s3Url ( ) + '/' + version ) ;
211
- cb ( null ) ;
212
- } )
213
- } ,
214
- cb ) ;
207
+ fileInfo . setUrl ( 'delete' , baseUrl + options . s3Url ( ) ) ;
208
+ async . each ( Object . keys ( options . imageVersions ) , _ . bind ( function ( version , cb ) {
209
+ //fs.exists(options.uploadDir() + '/' + version + '/' + fileInfo.name, function(exists) {
210
+ /*if (exists)*/ fileInfo . setUrl ( version , baseUrl + this . removeExtraSlashes ( options . s3Url ( ) + '/' + version ) ) ;
211
+ cb ( null ) ;
212
+ //})
213
+ } , this ) , cb ) ;
215
214
} ;
216
215
217
216
UploadHandler . prototype . moveToS3 = function ( fileInfo ) {
218
217
var fileName = options . uploadDir ( ) + '/' + fileInfo . name ;
219
218
fs . readFile ( fileName , _ . bind ( function ( err , data ) {
220
219
if ( ! err ) {
221
- this . uploadToS3 ( options . s3Url ( ) + '/' + fileInfo . name , data ) ;
220
+ this . uploadToS3 ( this . removeExtraSlashes ( options . s3Url ( ) + '/' + fileInfo . name , data ) ) ;
222
221
223
222
async . each ( Object . keys ( options . imageVersions ) , _ . bind ( function ( version ) {
224
223
fileName = options . uploadDir ( ) + '/' + version + '/' + fileInfo . name ;
225
224
fs . readFile ( fileName , _ . bind ( function ( err , data ) {
226
225
if ( ! err ) {
227
- this . uploadToS3 ( options . s3Url ( ) + '/' + version + '/' + fileInfo . name , data ) ;
226
+ this . uploadToS3 ( this . removeExtraSlashes ( options . s3Url ( ) + '/' + version + '/' + fileInfo . name , data ) ) ;
228
227
}
229
228
} , this ) ) ;
230
229
} , this ) ) ;
231
230
}
232
231
} , this ) ) ;
233
232
} ;
234
-
233
+
234
+ UploadHandler . prototype . removeExtraSlashes = function ( filePath ) {
235
+ if ( filePath . indexOf ( '//' ) !== - 1 ) {
236
+ filePath = filePath . replace ( '//' , '/' ) ;
237
+ filePath = this . removeExtraSlashes ( filePath ) ;
238
+ }
239
+
240
+ return filePath ;
241
+ } ;
242
+
235
243
UploadHandler . prototype . uploadToS3 = function ( fileName , data ) {
236
244
fs . unlink ( fileName , _ . bind ( function ( err ) {
237
245
this . emit ( 'error' , err ) ;
0 commit comments