Skip to content

Commit

Permalink
init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sagidM committed Sep 9, 2017
1 parent 001ab97 commit 29f221c
Show file tree
Hide file tree
Showing 1,177 changed files with 199,686 additions and 0 deletions.
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
Static website hosting
```xml
<RoutingRules>
<RoutingRule>
<Condition>
<KeyPrefixEquals/>
<HttpErrorCodeReturnedEquals>404</HttpErrorCodeReturnedEquals>
</Condition>
<Redirect>
<Protocol>https</Protocol>
<HostName>__DOMAIN__</HostName>
<ReplaceKeyPrefixWith>__PATH_TO_LAMBDA__?path=</ReplaceKeyPrefixWith>
<HttpRedirectCode>307</HttpRedirectCode>
</Redirect>
</RoutingRule>
</RoutingRules>
```


Bucket policy
```json
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AddPerm",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::__BUCKET_NAME__/*"
}
]
}
```


Role policy document
```json
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:*"
},
{
"Effect": "Allow",
"Action": "s3:PutObject",
"Resource": "arn:aws:s3:::__BUCKET_NAME__/*"
}
]
}
```
72 changes: 72 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
'use strict'


const AWS = require('aws-sdk')
const S3 = new AWS.S3({signatureVersion: 'v4'});
const Sharp = require('sharp');
const Pattern = new RegExp("(.*/)?(.*)/(.*)");

// parameters
const BUCKET = process.env.BUCKET;
const URL = process.env.URL;


exports.handler = function(event, context, callback) {
var path = event.queryStringParameters.path;
var parts = Pattern.exec(path);
var dir = parts[1] || '';
var options = parts[2].split('_');
var filename = parts[3];


var sizes = options[0].split("x");
var func = options.length > 1 ? options[1] : null;

var contentType;
S3.getObject({Bucket: BUCKET, Key: dir + filename})
.promise()
.then(data => {
contentType = data.ContentType;
var img = Sharp(data.Body)
.resize(
sizes[0] === 'AUTO' ? null : parseInt(sizes[0]),
sizes[1] === 'AUTO' ? null : parseInt(sizes[1]));

switch (func){
case 'max': img = img.max(); break;
case 'min': img = img.min(); break;
case null: break;
default:
callback(null, {
statusCode: 400,
body: `Unknown func parameter "${func}"\n` +
'For query ".../150x150_func", "_func" must be either empty or "_min" or "_max"',
headers: {"Content-Type": "text/plain"}
})
return new Promise(() => {});
}

return img.withoutEnlargement().toBuffer();
})
.then(result =>
S3.putObject({
Body: result,
Bucket: BUCKET,
ContentType: contentType,
Key: path
}).promise()
)
.then(() =>
callback(null, {
statusCode: 301,
headers: {"Location" : `${URL}/${path}`}
})
)
.catch(e => {
callback(null, {
statusCode: e.statusCode || 400,
body: 'Exception: ' + e.message,
headers: {"Content-Type": "text/plain"}
})
});
}
1 change: 1 addition & 0 deletions node_modules/.bin/mkdirp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/.bin/semver

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added node_modules/.zip
Binary file not shown.
37 changes: 37 additions & 0 deletions node_modules/caw/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions node_modules/caw/license

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

117 changes: 117 additions & 0 deletions node_modules/caw/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 51 additions & 0 deletions node_modules/caw/readme.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions node_modules/chownr/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions node_modules/chownr/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 29f221c

Please sign in to comment.