Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
KensoDev committed May 10, 2017
0 parents commit c8d2830
Show file tree
Hide file tree
Showing 7 changed files with 431 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
1 change: 1 addition & 0 deletions .node-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v6.10
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6.10
5 changes: 5 additions & 0 deletions fixtures/test-event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"queryStringParameters": {
"key": "originals/87624234234234/300x300/girl-people-landscape-sun-38554.jpeg"
}
}
55 changes: 55 additions & 0 deletions lambda/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const AWS = require("aws-sdk");
const Sharp = require("sharp");

exports.handler = (event, context, callback) => {
const local = true;

const s3 = new AWS.S3({
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
});

const bucketName = "creative-network-uploads";
let key = event.queryStringParameters.key;
const m = key.match(/(.*)\/(.*)\/(\d+)x(\d+)\/(.*)\.([a-zA-Z]{3,4})$/)

const directory = m[1];
const imageId = m[2];
const width = parseInt(m[3], 10);
const height = parseInt(m[4], 10);
const imageName= m[5];
const extension = m[6];

console.log(key);

key = key.replace(extension, "png");

const originalKey = `${directory}/${imageId}/${imageName}.${extension}`

s3.getObject({ Bucket: bucketName, Key: key }).promise().then((data) => {
console.log("Image is already there");
}).catch((error) => {
s3.getObject({Bucket: bucketName, Key: originalKey }).promise().then((data) => {
console.log(data);
Sharp(data.Body)
.resize(width, height)
.toFormat("png")
.toBuffer()
.then((buffer) => {
console.log(`Buffer: ${buffer}`)
s3.putObject({
ContentType: "image/png",
Body: buffer,
Bucket: bucketName,
Key: key
}).promise().catch(error => console.log(error))
})
.catch((error) => {
console.log(error);
})
}).catch((error) => {
console.log(error);
});
});

};
17 changes: 17 additions & 0 deletions lambda/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "creative.network.lambda",
"version": "0.0.1",
"description": "Processing uploaded images for the crative network",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Avi Zurel <avi@avi.io>",
"license": "MIT",
"dependencies": {
"sharp": "^0.17.3"
},
"devDependencies": {
"aws-sdk": "^2.49.0"
}
}

0 comments on commit c8d2830

Please sign in to comment.