-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from nicholasgubbins/uploader
upload image
- Loading branch information
Showing
3 changed files
with
62 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
|
||
|
||
const AWS = require('aws-sdk'); | ||
|
||
const s3 = new AWS.S3(); | ||
|
||
const v4 = require('uuid').v4; | ||
|
||
function generateUploadParams(content_type, extension, key){ | ||
return { | ||
Bucket: process.env.S3_BUCKET, | ||
Key: `${process.env.NODE_ENV}/${(key) ? key : v4()}${extension}`, | ||
ContentType: content_type, | ||
ACL: 'public-read' | ||
} | ||
} | ||
|
||
module.exports.handler = (event, context, callback) => { | ||
let query = (event.queryStringParameters) ? event.queryStringParameters : event; | ||
let extension = /^\.\w*$/.test(query.extension) ? query.extension : (typeof query.extension != 'undefined') ? `.${query.extension}` : ".undefined"; | ||
let key = (query.key) ? query.key : null; | ||
let content_type = query.content_type; | ||
if (!content_type) return callback(null, {statusCode:400, body: JSON.stringify({message:'please provide a content_type and extension'})}); | ||
s3.getSignedUrl('putObject', generateUploadParams(content_type, extension, key), function (error, url) { | ||
if(error) return callback(null, {statusCode:error.statusCode || 500, body:JSON.stringify({message:error.message})}); | ||
else callback(null, {statusCode:200, body:JSON.stringify({url:url})}); | ||
}); | ||
} | ||
|
||
module.exports.generateUploadParams = generateUploadParams; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const { | ||
generateUploadParams, | ||
} = require('./index.js'); | ||
|
||
|
||
describe('uploadImage', () => { | ||
describe('generateUploadParams', () => { | ||
// todo: if event is `{}` promise goes unresolved | ||
// todo: JSON stringify responses | ||
// todo: 'crop' twice in GM_KEYS | ||
|
||
test('should generate correct upload parameters', (done) => { | ||
const expected = { | ||
Bucket: process.env.S3_BUCKET, | ||
Key: `${process.env.NODE_ENV}/test.jpg`, | ||
ContentType: 'image/jpeg', | ||
ACL: 'public-read' | ||
}; | ||
const actual = generateUploadParams('image/jpeg', '.jpg', 'test'); | ||
expect(actual).toEqual(expected); | ||
done(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters