@@ -5,10 +5,13 @@ var EventEmitter = require('events').EventEmitter,
5
5
imageMagick = require ( 'imagemagick' ) ,
6
6
mkdirp = require ( 'mkdirp' ) ,
7
7
_ = require ( 'lodash' ) ,
8
- async = require ( 'async' ) ;
8
+ async = require ( 'async' ) ,
9
+ AWS = require ( 'aws-sdk' ) ;
9
10
10
11
module . exports = function ( options ) {
11
12
13
+ var s3 = new AWS . S3 ( ) ;
14
+
12
15
var FileInfo = require ( './fileinfo' ) (
13
16
_ . extend ( {
14
17
baseDir : options . uploadDir
@@ -74,6 +77,7 @@ module.exports = function (options) {
74
77
finish = _ . bind ( function ( ) {
75
78
if ( ! -- counter ) {
76
79
async . each ( files , _ . bind ( function ( fileInfo , cb ) {
80
+ this . moveToS3 ( fileInfo ) ;
77
81
this . initUrls ( fileInfo , _ . bind ( function ( err ) {
78
82
this . emit ( 'end' , fileInfo ) ;
79
83
cb ( err ) ;
@@ -197,18 +201,58 @@ module.exports = function (options) {
197
201
} ;
198
202
199
203
UploadHandler . prototype . initUrls = function ( fileInfo , cb ) {
200
- var baseUrl = ( options . ssl ? 'https:' : 'http:' ) + '//' + ( options . hostname || this . req . get ( 'Host' ) ) ;
201
- fileInfo . setUrl ( null , baseUrl + options . uploadUrl ( ) ) ;
202
- fileInfo . setUrl ( 'delete' , baseUrl + this . req . originalUrl ) ;
204
+ //http://<<bucket>>.s3-<<region>>.amazonaws.com
205
+ var baseUrl = ( options . ssl ? 'https:' : 'http:' ) + '//' + options . s3Bucket ( ) + '.s3-' + options . s3Region ( ) + '.amazonaws.com' ;
206
+ fileInfo . setUrl ( null , baseUrl + options . s3Url ( ) ) ;
207
+ fileInfo . setUrl ( 'delete' , baseUrl + this . req . s3Url ) ;
203
208
async . each ( Object . keys ( options . imageVersions ) , function ( version , cb ) {
204
209
fs . exists ( options . uploadDir ( ) + '/' + version + '/' + fileInfo . name , function ( exists ) {
205
- if ( exists ) fileInfo . setUrl ( version , baseUrl + options . uploadUrl ( ) + '/' + version ) ;
210
+ if ( exists ) fileInfo . setUrl ( version , baseUrl + options . s3Url ( ) + '/' + version ) ;
206
211
cb ( null ) ;
207
212
} )
208
213
} ,
209
214
cb ) ;
210
215
} ;
211
216
217
+ UploadHandler . prototype . moveToS3 = function ( fileInfo ) {
218
+ var fileName = options . uploadDir ( ) + '/' + fileInfo . name ;
219
+ fs . readFile ( fileName , _ . bind ( function ( err , data ) {
220
+ if ( ! err ) {
221
+ this . uploadToS3 ( options . s3Url ( ) + '/' + fileInfo . name , data ) ;
222
+
223
+ async . each ( Object . keys ( options . imageVersions ) , _ . bind ( function ( version ) {
224
+ fileName = options . uploadDir ( ) + '/' + version + '/' + fileInfo . name ;
225
+ fs . readFile ( fileName , _ . bind ( function ( err , data ) {
226
+ if ( ! err ) {
227
+ this . uploadToS3 ( options . s3Url ( ) + '/' + version + '/' + fileInfo . name , data ) ;
228
+ }
229
+ } , this ) ) ;
230
+ } , this ) ) ;
231
+ }
232
+ } , this ) ) ;
233
+ } ;
234
+
235
+ UploadHandler . prototype . uploadToS3 = function ( fileName , data ) {
236
+ fs . unlink ( fileName , _ . bind ( function ( err ) {
237
+ this . emit ( 'error' , err ) ;
238
+ } , this ) ) ;
239
+
240
+ var bucket = options . s3Bucket ( ) ;
241
+ s3 . createBucket ( { Bucket : bucket } , _ . bind ( function ( ) {
242
+ var params = {
243
+ Bucket : bucket ,
244
+ Key : fileName ,
245
+ Body : data , ACL :'public-read'
246
+ } ;
247
+ s3 . putObject ( params , _ . bind ( function ( err , data ) {
248
+ console . log ( err ) ;
249
+ if ( err ) {
250
+ this . emit ( 'error' , err ) ;
251
+ }
252
+ } , this ) ) ;
253
+ } , this ) ) ;
254
+ } ;
255
+
212
256
return UploadHandler ;
213
257
}
214
258
0 commit comments