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

Set absolute path when uploadeding image #47

Closed
wants to merge 1 commit into from
Closed
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
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