Skip to content
This repository has been archived by the owner on May 12, 2020. It is now read-only.

Commit

Permalink
Fixed stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCJGCJG committed Nov 15, 2017
1 parent d5d25cc commit 924ab34
Show file tree
Hide file tree
Showing 6 changed files with 1,248 additions and 76 deletions.
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@
"scripts": {
"start": "npm run local",
"local": "serverless offline",
"deploy": "serverless deploy -v"
"deploy": "serverless deploy -v",
"test": "standard --fix --verbose | snazzy"
},
"devDependencies": {
"serverless": "^1.24.1",
"serverless-offline": "^3.16.0"
"serverless-offline": "^3.16.0",
"standard": "^10.0.3"
},
"dependencies": {
"aws-sdk": "^2.149.0",
"bcrypt": "^1.0.3",
"file-type": "^7.2.0",
"lodash": "^4.17.4",
"snazzy": "^7.0.0",
"uuid": "^3.1.0"
}
}
41 changes: 33 additions & 8 deletions serverless.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
service: screenshottr-service

plugins:
- serverless-offline

provider:
name: aws
Expand All @@ -8,10 +10,8 @@ provider:
stage: dev
region: eu-west-1


plugins:
- serverless-offline

environment:
SS_STAGE: ${self:provider.stage}

iamRoleStatements:
- Effect: Allow
Expand All @@ -21,36 +21,48 @@ provider:
- Effect: Allow
Action:
- s3:*
Resource: arn:aws:s3:::screenshottr-service-images-saved/*
Resource: arn:aws:s3:::screenshottr-service-images-saved-dev/*
- Effect: Allow
Action:
- dynamodb:*
Resource: "arn:aws:dynamodb:eu-west-1:660773155982:table/screenshottr-service-uploads-dev"
- Effect: Allow
Action:
- dynamodb:*
Resource: "arn:aws:dynamodb:eu-west-1:660773155982:table/screenshottr-service-uploads"
Resource: "arn:aws:dynamodb:eu-west-1:660773155982:table/screenshottr-service-user-dev"

functions:
getSignedS3URL:
handler: src/getSignedS3URL.getSignedS3URL
events:
- http: GET api/getSignedS3URL
memorySize: 128
processUpload:
handler: src/processUpload.processUpload
events:
- s3: screenshottr-service-images-unprocessed-dev
memorySize: 256
getMetadata:
handler: src/getMetadata.getMetadata
events:
- http: GET api/getImageMetadata
memorySize: 128
createUser:
handler: src/createUser.createUser
events:
- http: GET api/user/createUser
memorySize: 128

resources:
Resources:
storageBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: screenshottr-service-images-saved
BucketName: screenshottr-service-images-saved-dev
entriesDynamo:
Type: AWS::DynamoDB::Table
Properties:
TableName: screenshottr-service-uploads
TableName: screenshottr-service-uploads-dev
AttributeDefinitions:
- AttributeName: image_id
AttributeType: S
Expand All @@ -60,3 +72,16 @@ resources:
ProvisionedThroughput:
ReadCapacityUnits: 5
WriteCapacityUnits: 5
usersDynamo:
Type: AWS::DynamoDB::Table
Properties:
TableName: screenshottr-service-user-dev
AttributeDefinitions:
- AttributeName: user_name
AttributeType: S
KeySchema:
- AttributeName: user_name
KeyType: HASH
ProvisionedThroughput:
ReadCapacityUnits: 5
WriteCapacityUnits: 5
9 changes: 4 additions & 5 deletions src/getMetadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ const aws = require('aws-sdk')
const dynamo = new aws.DynamoDB.DocumentClient()

module.exports.getMetadata = (event, context, callback) => {
const image_id = event.queryStringParameters.image_id
const imageId = event.queryStringParameters.image_id
let response = {}

const dynamoParams = {
TableName: 'screenshottr-service-uploads',
TableName: `screenshottr-service-uploads-${process.env.SS_STAGE}`,
Key: {
'image_id': image_id
'image_id': imageId
}
}

dynamo.get(dynamoParams, function(error, data) {
dynamo.get(dynamoParams, function (error, data) {
if (error) {
response = {
statusCode: 500,
Expand All @@ -35,5 +35,4 @@ module.exports.getMetadata = (event, context, callback) => {
}
callback(null, response)
})

}
37 changes: 30 additions & 7 deletions src/getSignedS3URL.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,51 @@ const s3 = new aws.S3()
const dynamo = new aws.DynamoDB.DocumentClient()

module.exports.getSignedS3URL = (event, context, callback) => {
const date = new Date
let imageOwner
if (event.queryStringParameters && event.queryStringParameters.user_name && event.queryStringParameters.token) {
const dynamoParams = {
TableName: `screenshottr-service-user-${process.env.SS_STAGE}`,
Key: {
'user_name': event.queryStringParameters.user_name
}
}

dynamo.get(dynamoParams, function (error, data) {
if (error) {
console.log(error)
} else {
if (data.Item.token && data.Item.token === event.queryStringParameters.token) {
imageOwner = event.queryStringParameters.user_name
} else {
imageOwner = '0'
}
}
})
} else {
imageOwner = '0'
}
const key = uuidv4()
const S3params = {
Bucket: 'screenshottr-service-images-unprocessed-dev',
Bucket: `screenshottr-service-images-unprocessed-${process.env.SS_STAGE}`,
Key: key,
ACL: 'public-read',
Expires: 250,
ContentType: 'binary/octet-stream'
}
const dynamoParams = {
TableName: 'screenshottr-service-uploads',
TableName: `screenshottr-service-uploads-${process.env.SS_STAGE}`,
Item: {
image_id: key,
image_status: 'not_yet_uploaded',
dates: {
created: Date.now()
},
deleted: false
deleted: false,
image_owner: imageOwner
}
}

dynamo.put(dynamoParams, function(error, data) {
dynamo.put(dynamoParams, function (error, data) {
if (error) {
console.log(error)
}
Expand All @@ -42,9 +65,9 @@ module.exports.getSignedS3URL = (event, context, callback) => {
success: false
})
}
callback(null, response)
callback(null, response)
} else {
console.log("URL", url)
console.log('URL', url)
const response = {
statusCode: 200,
body: JSON.stringify({
Expand Down
53 changes: 25 additions & 28 deletions src/processUpload.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
'use strict';
'use strict'
const _ = require('lodash')
const fileType = require('file-type')
const aws = require('aws-sdk')
const s3 = new aws.S3()
const dynamo = new aws.DynamoDB.DocumentClient()

module.exports.processUpload = (event, context, callback) => {

_.map(event.Records, (event) => {
let filename = ''
let mimeType = ''
Expand All @@ -23,19 +22,19 @@ module.exports.processUpload = (event, context, callback) => {
callback(null, {})
} else {
const dynamoParams = {
TableName: 'screenshottr-service-uploads',
TableName: `screenshottr-service-uploads-${process.env.SS_STAGE}`,
Key: {
image_id: event.s3.object.key
},
UpdateExpression: "set dates.deletedAt = :deletedTime, image_status = :image_status, delete_reason = :delete_reason, deleted = :deleted",
UpdateExpression: 'set dates.deletedAt = :deletedTime, image_status = :image_status, delete_reason = :delete_reason, deleted = :deleted',
ExpressionAttributeValues: {
":deletedTime": Date.now(),
":image_status": 'image_deleted',
":deleted": true,
":delete_reason": 'image_too_large'
':deletedTime': Date.now(),
':image_status': 'image_deleted',
':deleted': true,
':delete_reason': 'image_too_large'
}
}
dynamo.update(dynamoParams, function(error, data) {
dynamo.update(dynamoParams, function (error, data) {
if (error) {
console.log(error)
}
Expand All @@ -52,13 +51,13 @@ module.exports.processUpload = (event, context, callback) => {
} else {
const image = data.Body
const fileMeta = fileType(image)
const fileExtensions = ["png", "jpg", "jpeg", "svg", "gif", "tiff"]
const fileExtensions = ['png', 'jpg', 'jpeg', 'svg', 'gif', 'tiff']
if (fileMeta !== null && typeof fileMeta.ext !== 'undefined' && fileExtensions.indexOf(fileMeta.ext) > -1) {
filename = `${event.s3.object.key}.${fileMeta.ext}`
mimeType = fileMeta.mime
const putParams = {
Body: image,
Bucket: 'screenshottr-service-images-saved',
Bucket: `screenshottr-service-images-saved-${process.env.SS_STAGE}`,
Key: filename,
ContentType: mimeType,
ACL: 'public-read'
Expand All @@ -70,44 +69,42 @@ module.exports.processUpload = (event, context, callback) => {
callback(null, {})
} else {
const dynamoParams = {
TableName: 'screenshottr-service-uploads',
TableName: `screenshottr-service-uploads-${process.env.SS_STAGE}`,
Key: {
image_id: event.s3.object.key
},
UpdateExpression: "set dates.uploaded_at = :modifiedTime, image_url = :image_url, fileMeta = :fileMeta, filename = :filename, image_status = :image_status, image_owner = :image_owner",
UpdateExpression: 'set dates.uploaded_at = :modifiedTime, image_url = :image_url, fileMeta = :fileMeta, filename = :filename, image_status = :image_status',
ExpressionAttributeValues: {
":modifiedTime": Date.now(),
":image_url": `https://i.screenshottr.us/${filename}`,
":fileMeta": fileMeta,
":filename": filename,
":image_status": 'image_uploaded',
":image_owner": 0
':modifiedTime': Date.now(),
':image_url': `https://i.screenshottr.us/${filename}`,
':fileMeta': fileMeta,
':filename': filename,
':image_status': 'image_uploaded'
}
}
dynamo.update(dynamoParams, function(error, data) {
dynamo.update(dynamoParams, function (error, data) {
if (error) {
console.log(error)
}
})
console.log(`PUT ${event.s3.object.key}`)
}
})

} else {
const dynamoParams = {
TableName: 'screenshottr-service-uploads',
TableName: `screenshottr-service-uploads-${process.env.SS_STAGE}`,
Key: {
image_id: event.s3.object.key
},
UpdateExpression: "set dates.deletedAt = :deletedTime, image_status = :image_status, delete_reason = :delete_reason, deleted = :deleted",
UpdateExpression: 'set dates.deletedAt = :deletedTime, image_status = :image_status, delete_reason = :delete_reason, deleted = :deleted',
ExpressionAttributeValues: {
":deletedTime": Date.now(),
":image_status": 'image_deleted',
":deleted": true,
":delete_reason": 'file_type_not_allowed'
':deletedTime': Date.now(),
':image_status': 'image_deleted',
':deleted': true,
':delete_reason': 'file_type_not_allowed'
}
}
dynamo.update(dynamoParams, function(error, data) {
dynamo.update(dynamoParams, function (error, data) {
if (error) {
console.log(error)
}
Expand Down
Loading

0 comments on commit 924ab34

Please sign in to comment.