-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit c8d2830
Showing
7 changed files
with
431 additions
and
0 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 @@ | ||
node_modules |
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 @@ | ||
v6.10 |
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 @@ | ||
6.10 |
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,5 @@ | ||
{ | ||
"queryStringParameters": { | ||
"key": "originals/87624234234234/300x300/girl-people-landscape-sun-38554.jpeg" | ||
} | ||
} |
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,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); | ||
}); | ||
}); | ||
|
||
}; |
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,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" | ||
} | ||
} |
Oops, something went wrong.