From f100cc595d50c24efda936c01c02f95565bf40ef Mon Sep 17 00:00:00 2001 From: Danielwhyte Date: Wed, 1 Aug 2018 10:21:24 +0100 Subject: [PATCH 1/7] swaps id/name headers, #13 --- lambda/templates/html-template.hbs | 2 +- lambda/uploadForm/index.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lambda/templates/html-template.hbs b/lambda/templates/html-template.hbs index 9406e36..df29ea3 100644 --- a/lambda/templates/html-template.hbs +++ b/lambda/templates/html-template.hbs @@ -53,8 +53,8 @@ - + diff --git a/lambda/uploadForm/index.js b/lambda/uploadForm/index.js index 9e87757..b41b52a 100644 --- a/lambda/uploadForm/index.js +++ b/lambda/uploadForm/index.js @@ -14,8 +14,8 @@ module.exports.handler = (event, context, callback) => { success_action_redirect: `https://${event.headers.Host}/${process.env.STAGE}/list-objects` }, Conditions: [ - ['starts-with', '$key', ''] - ] + ['starts-with', '$key', ''] + ] }; s3.createPresignedPost(params, (error, data) => { From 5f4a368743ece433b33a96945ce3007ad61f59af Mon Sep 17 00:00:00 2001 From: Danielwhyte Date: Wed, 1 Aug 2018 11:36:22 +0100 Subject: [PATCH 2/7] adds modified date to objects list, #13 --- lambda/processFile/utils.js | 2 +- lambda/templates/html-template.hbs | 2 ++ lambda/templates/json-template.hbs | 2 +- lambda/toggleStatus/index.js | 2 +- 4 files changed, 5 insertions(+), 3 deletions(-) diff --git a/lambda/processFile/utils.js b/lambda/processFile/utils.js index 9376096..42aeb05 100644 --- a/lambda/processFile/utils.js +++ b/lambda/processFile/utils.js @@ -173,7 +173,7 @@ function saveToDB (item, callback) { 'active': { BOOL: (item.objectId && item.title) ? true : false }, - 'modification_date': { + 'modificationDate': { S: (new Date(Date.now())).toISOString() } }; diff --git a/lambda/templates/html-template.hbs b/lambda/templates/html-template.hbs index df29ea3..7ee5b61 100644 --- a/lambda/templates/html-template.hbs +++ b/lambda/templates/html-template.hbs @@ -55,6 +55,7 @@ + @@ -63,6 +64,7 @@ +
Name IDName Status
ID NameLast Modified Status
{{#if objectId}}{{objectId.S}}{{else}}missing{{/if}} {{#if title}}{{title.S}}{{else}}missing{{/if}}{{#if modificationDate}}{{modificationDate.S}}{{/if}} {{#if active.BOOL}} Active diff --git a/lambda/templates/json-template.hbs b/lambda/templates/json-template.hbs index 2d89b56..eaa1918 100644 --- a/lambda/templates/json-template.hbs +++ b/lambda/templates/json-template.hbs @@ -5,7 +5,7 @@ "id": "{{objectId.S}}", "path": "{{../bucket}}/{{id.S}}", "active": {{active.BOOL}}, - "modification_date": "{{modification_date.S}}" + "modification_date": "{{modificationDate.S}}" }{{#if @last}}{{else}},{{/if}} {{/each}} ] diff --git a/lambda/toggleStatus/index.js b/lambda/toggleStatus/index.js index 3ca1cdb..577594d 100644 --- a/lambda/toggleStatus/index.js +++ b/lambda/toggleStatus/index.js @@ -8,7 +8,7 @@ module.exports.handler = function (event, context, callback) { return dynamoDB.updateItem({ TableName: process.env.TABLE_NAME, - UpdateExpression: 'SET active = :a, modification_date = :m', + UpdateExpression: 'SET active = :a, modificationDate = :m', ExpressionAttributeValues: { ':a': { BOOL: params.active === 'true' From 629e1b689646c98ac6c14cb5f01ca946f5ae7ca0 Mon Sep 17 00:00:00 2001 From: Danielwhyte Date: Wed, 1 Aug 2018 11:58:21 +0100 Subject: [PATCH 3/7] adds refresh button to object list, #13 --- lambda/templates/html-template.hbs | 10 ++++++++++ lambda/uploadForm/index.js | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lambda/templates/html-template.hbs b/lambda/templates/html-template.hbs index 7ee5b61..c46e07f 100644 --- a/lambda/templates/html-template.hbs +++ b/lambda/templates/html-template.hbs @@ -29,6 +29,11 @@ border-spacing: 0; border-radius: 2px; } + .refresh-container { + width: 90%; + margin: auto; + text-align: right; + } td { text-align: center; } @@ -51,6 +56,11 @@ {{/if}} +
+
+ +
+
diff --git a/lambda/uploadForm/index.js b/lambda/uploadForm/index.js index b41b52a..039262d 100644 --- a/lambda/uploadForm/index.js +++ b/lambda/uploadForm/index.js @@ -11,7 +11,7 @@ module.exports.handler = (event, context, callback) => { Bucket: process.env.S3_BUCKET, Fields: { acl: 'public-read', - success_action_redirect: `https://${event.headers.Host}/${process.env.STAGE}/list-objects` + success_action_redirect: `https://${event.headers.Host}/${process.env.STAGE}/list-objects?uploaded=true` }, Conditions: [ ['starts-with', '$key', ''] From 87f622e96e33af3ca2ca7f25e05a43e4d6d4f69d Mon Sep 17 00:00:00 2001 From: Danielwhyte Date: Wed, 1 Aug 2018 14:36:46 +0100 Subject: [PATCH 4/7] makes s3 bucket publicly accessible, #13 --- serverless.yml | 49 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 6 deletions(-) diff --git a/serverless.yml b/serverless.yml index a356cef..afebd6d 100644 --- a/serverless.yml +++ b/serverless.yml @@ -56,12 +56,6 @@ functions: processFile: handler: lambda/processFile/index.handler timeout: 90 - events: - - s3: - bucket: ${opt:bucket, 'smgco-360'} - event: s3:ObjectCreated:* - rules: - - suffix: .ggpkg toggleStatus: handler: lambda/toggleStatus/index.handler @@ -97,6 +91,49 @@ resources: ProvisionedThroughput: ReadCapacityUnits: 1 WriteCapacityUnits: 1 + S3Bucket360: + Type: 'AWS::S3::Bucket' + Properties: + BucketName: ${opt:bucket, 'smgco-360'} + CorsConfiguration: + CorsRules: + - AllowedHeaders: + - "*" + AllowedMethods: + - GET + AllowedOrigins: + - "*" + NotificationConfiguration: + LambdaConfigurations: + - Event: "s3:ObjectCreated:*" + Function: + "Fn::GetAtt": [ ProcessFileLambdaFunction, Arn ] + ProcessFileLambdaPermissionS3Bucket360: + DependsOn: + - ProcessFileLambdaFunction + Type: AWS::Lambda::Permission + Properties: + FunctionName: + "Fn::GetAtt": [ ProcessFileLambdaFunction, Arn ] + Action: "lambda:InvokeFunction" + Principal: "s3.amazonaws.com" + SourceArn: "arn:aws:s3:::${opt:bucket, 'smgco-360'}" + 360BucketPolicy: + Type: AWS::S3::BucketPolicy + Properties: + Bucket: + Ref: S3Bucket360 + PolicyDocument: + Statement: + Action: "s3:GetObject" + Effect: "Allow" + Resource: + Fn::Join: + - "" + - - "arn:aws:s3:::" + - Ref: S3Bucket360 + - "/*" + Principal: "*" plugins: - serverless-offline From 60c1313d5d3dc58f5dcf8f291c378b7717ac8c51 Mon Sep 17 00:00:00 2001 From: Danielwhyte Date: Wed, 1 Aug 2018 14:53:15 +0100 Subject: [PATCH 5/7] adds preview thumbnail to image list, #13 --- lambda/lib/listObjects.js | 1 + lambda/templates/html-template.hbs | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/lambda/lib/listObjects.js b/lambda/lib/listObjects.js index 8d9770b..092e382 100644 --- a/lambda/lib/listObjects.js +++ b/lambda/lib/listObjects.js @@ -15,6 +15,7 @@ module.exports = (event, context, callback) => { var templateData = Object.assign({}, data, { bucket: process.env.S3_BUCKET, stage: process.env.STAGE, + region: process.env.REGION, uploaded: uploaded }); diff --git a/lambda/templates/html-template.hbs b/lambda/templates/html-template.hbs index c46e07f..c685ef0 100644 --- a/lambda/templates/html-template.hbs +++ b/lambda/templates/html-template.hbs @@ -34,6 +34,15 @@ margin: auto; text-align: right; } + .thumbnail { + object-fit: cover; + width: 100%; + height: 100%; + } + .thumbnail-container { + width: 5em; + height: 5em; + } td { text-align: center; } @@ -65,6 +74,7 @@ + @@ -74,6 +84,11 @@ +
ID
ID Name Last Modified Status
{{#if objectId}}{{objectId.S}}{{else}}missing{{/if}} {{#if title}}{{title.S}}{{else}}missing{{/if}} +
+ object image preview +
+
{{#if modificationDate}}{{modificationDate.S}}{{/if}} {{#if active.BOOL}} From c2163ece3d3d668df720c083756aca8bb62c86b1 Mon Sep 17 00:00:00 2001 From: Danielwhyte Date: Wed, 1 Aug 2018 15:20:56 +0100 Subject: [PATCH 6/7] adds links between pages, #13 --- lambda/templates/html-template.hbs | 24 +++++++++++++++++++----- lambda/templates/upload-form.hbs | 1 + lambda/uploadForm/index.js | 3 ++- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/lambda/templates/html-template.hbs b/lambda/templates/html-template.hbs index c685ef0..0d7f392 100644 --- a/lambda/templates/html-template.hbs +++ b/lambda/templates/html-template.hbs @@ -29,10 +29,19 @@ border-spacing: 0; border-radius: 2px; } - .refresh-container { + .link-container { width: 90%; margin: auto; + } + .refresh-container { + width: 49%; text-align: right; + display: inline-block; + } + .upload-container { + width: 50%; + text-align: left; + display: inline-block; } .thumbnail { object-fit: cover; @@ -65,10 +74,15 @@ {{/if}} -
-
- -
+ diff --git a/lambda/templates/upload-form.hbs b/lambda/templates/upload-form.hbs index 102e4e4..b2d7c15 100644 --- a/lambda/templates/upload-form.hbs +++ b/lambda/templates/upload-form.hbs @@ -25,6 +25,7 @@ + List uploaded objects diff --git a/lambda/uploadForm/index.js b/lambda/uploadForm/index.js index 039262d..d8dfd99 100644 --- a/lambda/uploadForm/index.js +++ b/lambda/uploadForm/index.js @@ -23,7 +23,8 @@ module.exports.handler = (event, context, callback) => { callback(error); } else { data.fields.key = '${filename}'; - body = handlebars.compile(fs.readFileSync(__dirname + '/../templates/upload-form.hbs', 'utf8'))(data); + const templateData = Object.assign({}, data, {stage: process.env.STAGE}); + body = handlebars.compile(fs.readFileSync(__dirname + '/../templates/upload-form.hbs', 'utf8'))(templateData); const response = { statusCode: 200, headers: { From 3c5295f7b813266313b1d4623c880f2984d132bb Mon Sep 17 00:00:00 2001 From: Danielwhyte Date: Wed, 1 Aug 2018 16:41:20 +0100 Subject: [PATCH 7/7] adds delete functionality, #13 --- lambda/deleteObject/index.js | 29 +++++++++++++++++++++++++++++ lambda/templates/html-template.hbs | 11 ++++++++++- serverless.yml | 10 ++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 lambda/deleteObject/index.js diff --git a/lambda/deleteObject/index.js b/lambda/deleteObject/index.js new file mode 100644 index 0000000..feda8ee --- /dev/null +++ b/lambda/deleteObject/index.js @@ -0,0 +1,29 @@ +const AWS = require('aws-sdk'); +const dynamoDB = new AWS.DynamoDB({region: process.env.REGION}); +const querystring = require('querystring'); +const listObjects = require('../lib/listObjects'); +const s3 = new AWS.S3({region: process.env.REGION}); + +module.exports.handler = function (event, context, callback) { + var params = querystring.parse(event.body); + + return dynamoDB.deleteItem({ + TableName: process.env.TABLE_NAME, + Key: { + 'id': { + S: params.id + } + } + }, function (err, data) { + if (err) return callback(err); + + return s3.deleteObject({ + Bucket: process.env.S3_BUCKET, + Key: params.id + }, function(err, data) { + if (err) return callback(err); + + return listObjects({}, {}, callback); + }); + }); +}; diff --git a/lambda/templates/html-template.hbs b/lambda/templates/html-template.hbs index 0d7f392..64301d9 100644 --- a/lambda/templates/html-template.hbs +++ b/lambda/templates/html-template.hbs @@ -79,7 +79,7 @@ Upload File
-
+
@@ -93,6 +93,7 @@
+ {{#each Items}} @@ -123,6 +124,14 @@ + {{/each}}
Status
Go to files +
+ + +
+
diff --git a/serverless.yml b/serverless.yml index afebd6d..ccda93c 100644 --- a/serverless.yml +++ b/serverless.yml @@ -22,6 +22,7 @@ provider: - dynamodb:PutItem - dynamodb:UpdateItem - dynamodb:Scan + - dynamodb:DeleteItem Resource: - { "Fn::GetAtt": ["360ImagesDynamoDBTable", "Arn" ] } - Effect: Allow @@ -75,6 +76,15 @@ functions: method: get cors: true + deleteObject: + handler: lambda/deleteObject/index.handler + timeout: 90 + events: + - http: + path: /delete-object + method: post + cors: true + resources: Resources: 360ImagesDynamoDBTable: