Skip to content

Commit

Permalink
Merge branch 'feature/upload-absolute-path'
Browse files Browse the repository at this point in the history
  • Loading branch information
Hans Kristian Flaatten committed Oct 14, 2015
2 parents 2f648bc + 12e2b89 commit 1289672
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ var client = new Upload('my_s3_bucket', {

* **object** `opts`
* **string** `awsPath` - override the path on AWS set through `opts.aws.path`
* **string** `path` - set absolute path for uploaded image (disables random path)

* **function** `cb` - callback function (**Error** `err`, **object[]** `versions`, **object** `meta`)
* **Error** `err` - `null` if everything went fine
Expand Down
5 changes: 5 additions & 0 deletions src/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ Image.prototype.getMetadata = (src, cb) ->
##
Image.prototype.getDest = (cb) ->
prefix = @opts?.awsPath or @upload.opts.aws.path

if @opts.path
return process.nextTick =>
cb null, prefix + @opts.path

@upload._getDestPath prefix, cb

##
Expand Down
43 changes: 43 additions & 0 deletions test/suite.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,21 @@ describe 'Image', ->
assert.equal path, 'custom/path/aa/bb/cc'
done()

it 'returns fixed upload path', (done) ->
image.opts.path = 'my/image'
image.getDest (err, path) ->
assert.ifError err
assert.equal path, 'images_test/my/image'
done()

it 'returns fixed upload path with custom prefix', (done) ->
image.opts.awsPath = 'custom/path/'
image.opts.path = 'my/image'
image.getDest (err, path) ->
assert.ifError err
assert.equal path, 'custom/path/my/image'
done()

describe '#resizeVersions()', ->
it 'resizes image versions', (done) ->
image.getMetadata image.src, (err, metadata) ->
Expand Down Expand Up @@ -459,6 +474,34 @@ describe 'Integration Tests', ->
assert.equal typeof image.etag, 'string'
assert.equal typeof image.path, 'string'
assert.equal typeof image.key, 'string'
/^images_test(\/[\w]{2}){3}/.test image.key
assert.equal typeof image.url, 'string'

if image.original
assert.equal image.original, true
else
assert.equal typeof image.suffix, 'string'
assert.equal typeof image.height, 'number'
assert.equal typeof image.width, 'number'

done()

it 'uploads image to fixed path', (done) ->
@timeout 10000

file = __dirname + '/assets/portrait.jpg'
opts = path: 'path/to/image'

upload.upload file, opts, (err, images, meta) ->
assert.ifError err

for image in images
cleanup.push Key: image.key if image.key # clean up in AWS

assert.equal typeof image.etag, 'string'
assert.equal typeof image.path, 'string'
assert.equal typeof image.key, 'string'
/^images_test\/path\/to\/image/.test image.key
assert.equal typeof image.url, 'string'

if image.original
Expand Down

0 comments on commit 1289672

Please sign in to comment.