Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for AWS S3 cache control headers #45

Merged
merged 1 commit into from
Oct 13, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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