diff --git a/README.md b/README.md index e480cc5..e11f087 100644 --- a/README.md +++ b/README.md @@ -78,9 +78,13 @@ var Upload = require('s3-uploader'); * **string** `background` - set background for transparent images (**example:** `red`) * **boolean** `flatten` - flatten backgrund for transparent images * **string** `awsImageAcl` - access control for AWS S3 upload (**example:** `private`) + * **number** `awsImageExpires` - add `Expires` header to image version + * **number** `awsImageCacheControl` - add `Cache-Control` header to image version * **object** `original` * **string** `awsImageAcl` - access control for AWS S3 upload (**example:** `private`) + * **number** `awsImageExpires` - add `Expires` header to image version + * **number** `awsImageCacheControl` - add `Cache-Control` header to image version #### AWS note > The `aws` object is passed directly to `aws-sdk`. You can add any of [these diff --git a/src/index.coffee b/src/index.coffee index 33055f8..cccae3a 100644 --- a/src/index.coffee +++ b/src/index.coffee @@ -123,12 +123,13 @@ Image.prototype.resizeVersions = (cb, results) -> ## Image.prototype.uploadVersions = (cb, results) -> if @upload.opts.original - results.versions.push - awsImageAcl: @upload.opts.original.awsImageAcl - original: true - width: results.metadata.width - height: results.metadata.height - path: @src + org = JSON.parse(JSON.stringify(@upload.opts.original)) + org.original = true + org.width = results.metadata.width + org.height = results.metadata.height + org.path = @src + + results.versions.push org map results.versions, @_upload.bind(@, results.dest), cb @@ -158,6 +159,12 @@ Image.prototype._upload = (dest, version, cb) -> Body: fs.createReadStream version.path ContentType: "image/#{if format is 'jpg' then 'jpeg' else format}" + if version.awsImageExpires + options.Expires = new Date(Date.now() + version.awsImageExpires) + + if version.awsImageMaxAge + options.CacheControl = "public, max-age=#{version.awsImageMaxAge}" + @upload.s3.putObject options, (err, data) => return cb err if err diff --git a/test/suite.coffee b/test/suite.coffee index 04cab4a..15b2b78 100644 --- a/test/suite.coffee +++ b/test/suite.coffee @@ -15,6 +15,7 @@ beforeEach -> original: false original: awsImageAcl: 'private' + awsImageMaxAge: 31536000 versions: [{ maxHeight: 1040 maxWidth: 1040 @@ -34,11 +35,15 @@ beforeEach -> aspect: '1:1' format: 'png' suffix: '-thumb1' + awsImageExpires: 31536000 + cacheControl: 31536000 },{ maxHeight: 250 maxWidth: 250 aspect: '1:1' suffix: '-thumb2' + awsImageExpires: 31536000 + cacheControl: 31536000 }] # Mock S3 API calls @@ -242,6 +247,22 @@ describe 'Image', -> image._upload 'aa/bb/cc', version + it 'sets upload expire header for version', (done) -> + version = path: '/some/image.jpg', awsImageExpires: 1234 + image.upload.s3.putObject = (opts, cb) -> + assert opts.Expires - Date.now() <= 1234 + done() + + image._upload 'aa/bb/cc', version + + it 'sets upload cache-control header for version', (done) -> + version = path: '/some/image.jpg', awsImageMaxAge: 1234 + image.upload.s3.putObject = (opts, cb) -> + assert.equal opts.CacheControl, 'public, max-age=1234' + done() + + image._upload 'aa/bb/cc', version + it 'returns etag for uploaded version', (done) -> version = path: '/some/image.jpg' image._upload 'aa/bb/cc', version, (err, version) -> @@ -333,7 +354,9 @@ describe 'Image', -> it 'uploads original image', (done) -> image._upload = (dest, version, cb) -> assert.deepEqual version, - awsImageAcl: 'private' + awsImageAcl: 'public' + awsImageExpires: 31536000 + awsImageMaxAge: 31536000 original: true width: 111 height: 222 @@ -341,12 +364,18 @@ describe 'Image', -> cb null, version - image.upload.opts.original = awsImageAcl: 'private' + image.upload.opts.original = + awsImageAcl: 'public' + awsImageExpires: 31536000 + awsImageMaxAge: 31536000 + image.uploadVersions (err, versions) -> assert.ifError err assert.deepEqual versions, [ - awsImageAcl: 'private' + awsImageAcl: 'public' + awsImageExpires: 31536000 + awsImageMaxAge: 31536000 original: true width: 111 height: 222