Skip to content

Commit

Permalink
feat(aws): support image cache control headers
Browse files Browse the repository at this point in the history
Add support for `Expires` and `Caceh-Control` headers for
images uploaded to AWS S3. It is good practice to cache as
much as possible and adding the headers retrospectively to
AWS is kind of a pain.

PR-URL: #45

Signed-off-by: Hans Kristian Flaatten <hans.kristian.flaatten@turistforeningen.no>
  • Loading branch information
Hans Kristian Flaatten authored and anthonyringoet committed Oct 28, 2015
1 parent 3eebc0b commit ffb6329
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 13 additions & 6 deletions src/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
35 changes: 32 additions & 3 deletions test/suite.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ beforeEach ->
original: false
original:
awsImageAcl: 'private'
awsImageMaxAge: 31536000
versions: [{
maxHeight: 1040
maxWidth: 1040
Expand All @@ -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
Expand Down Expand Up @@ -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) ->
Expand Down Expand Up @@ -333,20 +354,28 @@ 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
path: image.src

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
Expand Down

0 comments on commit ffb6329

Please sign in to comment.