Skip to content

Commit

Permalink
Add content type prefix to post policy (#1047)
Browse files Browse the repository at this point in the history
  • Loading branch information
joaofranciscosantos authored Aug 3, 2022
1 parent 728336b commit f1f16fa
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/main/minio.js
Original file line number Diff line number Diff line change
Expand Up @@ -3665,6 +3665,15 @@ export class PostPolicy {
this.formData['Content-Type'] = type
}

// set Content-Type prefix, i.e image/ allows any image
setContentTypeStartsWith(prefix) {
if (!prefix) {
throw new Error('content-type cannot be null')
}
this.policy.conditions.push(['starts-with', '$Content-Type', prefix])
this.formData['Content-Type'] = prefix
}

// set minimum/maximum length of what Content-Length can be.
setContentLengthRange(min, max) {
if (min > max) {
Expand Down
38 changes: 38 additions & 0 deletions src/test/functional/functional-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1063,6 +1063,44 @@ describe('functional tests', function () {
})
})

step('presignedPostPolicy(postPolicy, cb)_postPolicy:setContentType', done => {
var policy = client.newPostPolicy()
policy.setKey(_1byteObjectName)
policy.setBucket(bucketName)
policy.setContentType('text/plain')

client.presignedPostPolicy(policy, (e, data) => {
if (e) return done(e)
var req = superagent.post(data.postURL)
_.each(data.formData, (value, key) => req.field(key, value))
req.attach('file', Buffer.from([_1byte]), 'test')
req.end(function(e) {
if (e) return done(e)
done()
})
req.on('error', e => done(e))
})
})

step('presignedPostPolicy(postPolicy, cb)_postPolicy:setContentTypeStartsWith', done => {
var policy = client.newPostPolicy()
policy.setKey(_1byteObjectName)
policy.setBucket(bucketName)
policy.setContentTypeStartsWith('text/')

client.presignedPostPolicy(policy, (e, data) => {
if (e) return done(e)
var req = superagent.post(data.postURL)
_.each(data.formData, (value, key) => req.field(key, value))
req.attach('file', Buffer.from([_1byte]), 'test')
req.end(function(e) {
if (e) return done(e)
done()
})
req.on('error', e => done(e))
})
})

step('presignedPostPolicy(postPolicy)_postPolicy: null_', done => {
client.presignedPostPolicy(null)
.then(() => {
Expand Down

0 comments on commit f1f16fa

Please sign in to comment.