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

upload image #15

Merged
merged 1 commit into from
Aug 22, 2017
Merged
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
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