Skip to content

Commit

Permalink
feat(path): add support for custom random paths
Browse files Browse the repository at this point in the history
Add support for selecting a custom random path generator function such as
`broofa/node-uuid`.

PR-URL: #41
Implements: #20

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 ffb6329 commit 6c20395
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 16 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ var Upload = require('s3-uploader');
* **number** `awsImageExpires` - add `Expires` header to image version
* **number** `awsImageCacheControl` - add `Cache-Control` header to image version

* **function** `randomPath` - custom random path function

#### AWS note
> The `aws` object is passed directly to `aws-sdk`. You can add any of [these
> options](http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#constructor_details)
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@
"devDependencies": {
"coffee-script": "~1",
"coffeelint": "~1",
"mocha": "~2"
"mocha": "~2",
"uuid": "^2"
},
"dependencies": {
"@starefossen/rand-path": "^1.0.1",
"async": "~1.4",
"aws-sdk": "~2.1",
"im-resize": "~2.3",
Expand Down
16 changes: 2 additions & 14 deletions src/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,12 @@ Upload = module.exports = (bucketName, @opts = {}) ->
else if not @opts.url
@opts.url ?= "https://s3-#{@opts.aws.region}.amazonaws.com/#{bucketName}/"

@._getRandomPath = @opts.randomPath or require('@starefossen/rand-path')

@s3 = new S3 @opts.aws

@

##
# Generate a random path on the form /xx/yy/zz
##
Upload.prototype._getRandomPath = ->
input = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
res = []

for i in [1..3]
x = input[Math.floor((Math.random() * input.length))]
y = input[Math.floor((Math.random() * input.length))]
res.push x + y

return res.join '/'

##
# Generate a random avaiable path on the S3 bucket
##
Expand Down
9 changes: 8 additions & 1 deletion test/suite.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,14 @@ describe 'Upload', ->
describe '#_getRandomPath()', ->
it 'returns a new random path', ->
path = upload._getRandomPath()
assert(/^[A-Za-z0-9]{2}\/[A-Za-z0-9]{2}\/[A-Za-z0-9]{2}$/.test(path))
assert(/^\w{2}(\/\w{2}){2}$/.test(path))

it 'returns custom random path', ->
upload = new Upload process.env.AWS_BUCKET_NAME,
randomPath: require('uuid').v1

path = upload._getRandomPath()
assert(/^\w+(-\w+){4}$/.test(path))

describe '#_getDestPath()', ->
beforeEach ->
Expand Down

0 comments on commit 6c20395

Please sign in to comment.