Skip to content

Commit

Permalink
Merge pull request #15 from nicholasgubbins/uploader
Browse files Browse the repository at this point in the history
upload image
  • Loading branch information
marcusmolchany authored Aug 22, 2017
2 parents 0d79911 + cf4bddb commit e8d6829
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
30 changes: 30 additions & 0 deletions functions/uploadImage/index.js
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;
24 changes: 24 additions & 0 deletions functions/uploadImage/index.spec.js
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();
});
});
});
10 changes: 8 additions & 2 deletions serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,21 @@ functions:
- http: GET {proxy+}
package:
include:
- bin/APIInterface/**
- functions/getImage/**
resizeImage:
name: ${self:provider.environment.RESIZE_LAMBDA}
handler: functions/resizeImage/index.handler
package:
include:
- bin/APIInterface/**
- functions/resizeImage/**
uploadImage:
name: ${self:service}-${self:provider.stage}-uploadImage
handler: functions/uploadImage/index.handler
events:
- http: POST /upload
package:
include:
- functions/uploadImage/**



Expand Down

0 comments on commit e8d6829

Please sign in to comment.