Skip to content

Commit

Permalink
initial mock of product handler
Browse files Browse the repository at this point in the history
  • Loading branch information
noogen committed Apr 9, 2019
1 parent d711814 commit 07d329b
Show file tree
Hide file tree
Showing 10 changed files with 12,314 additions and 26 deletions.
1 change: 0 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

MIT License

Copyright (c) friends@niiknow.org
Expand Down
6 changes: 4 additions & 2 deletions handler.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import store from './lib/storeHandler'
import store from './src/storeHandler'
import research from './src/researchHandler'

export const storeHandler = post
export const storeHandler = store
export const researchHandler = research
12,167 changes: 12,167 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 2 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,9 @@
},
"dependencies": {
"busboy": "^0.2.14",
"consolidate": "^0.15.1",
"debug": "^4.1.1",
"mjml": "^4.3.1",
"nodemailer": "^4.7.0",
"nunjucks": "^3.2.0",
"recaptcha2": "^1.3.3",
"source-map-support": "^0.5.12",
"temp": "^0.8.3",
"uuid": "^3.3.2",
"zlib": "^1.0.5"
"uuid": "^3.3.2"
},
"devDependencies": {
"@babel/cli": "^7.4.3",
Expand All @@ -50,6 +43,7 @@
"eslint-config-prettier": "^3.6.0",
"eslint-plugin-import": "^2.16.0",
"eslint-plugin-jest": "^21.27.2",
"got": "^9.6.0",
"jest": "^24.7.1",
"serverless": "^1.40.0",
"serverless-apigw-binary": "^0.4.4",
Expand Down
24 changes: 22 additions & 2 deletions serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,29 @@ functions:
events:
- http:
method: post
path: store/{gtin}
path: store/{gtin}/{vendor}
cors: true
request:
parameters:
paths:
id: true
gtin: true
- http:
method: get
path: store/{gtin}/{vendor}
cors: true
request:
parameters:
paths:
gtin: true

researchHandler:
handler: handler.researchHandler
events:
- http:
method: get
path: research/{gtin}/{vendor}
cors: true
request:
parameters:
paths:
gtin: true
29 changes: 29 additions & 0 deletions src/fetchToS3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import fs from 'fs'
import path from 'path'
import got from 'got'
import AWS from 'aws-sdk'

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

/**
* Convert date to a number that enable most-recent first sort/ordering
* on storage system such as AWS S3
*
* @param {string} url the data url
* @return {string} the response
*/
export default (url, bucket, path) => {
const fstream = got.stream(url)

s3.upload({
Bucket: bucket,
Key: path,
Body: fstream,
},
function (err, data) {
console.log(err, data);
});
}
18 changes: 13 additions & 5 deletions src/gtinPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@
*
* Example: 00123456789012 becomes 123/456/789/00123456789012
*
* @param {string} gtin the 14 digits Global Trade Item Number
* @param {string} gtin the 14 digits Global Trade Item Number
* @param {string} vendor the vendor
* @return {string} the folder path
*/
export default (gtin) => {
const upc = gtin2.slice(-12);
const parts = [upc.substr(0, 3), upc.substr(3, 3), upc.substr(6, 3)];
return parts.join('/') + '/' + gtin;
export default (gtin, vendor = '') => {
const upc = gtin2.slice(-12)
const parts = [upc.substr(0, 3), upc.substr(3, 3), upc.substr(6, 3)]
let myPath = parts.join('/') + '/' + gtin + '/'
vendor = (vendor || '').trim()

if (vendor.length > 0) {
myPath = `${vendor}/${myPath}`
}

return myPath
}
16 changes: 16 additions & 0 deletions src/researchHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import fs from 'fs'
import res from './response'
import saveToS3 from './saveToS3'

const debug = require('debug')('gtin-cloud')


/**
* Proxy request to external vendor for GTIN data
*
* @param object event the event
* @param object context the context
* @param Function callback the callback
*/
export default async (event, context, callback) => {
}
28 changes: 28 additions & 0 deletions src/saveToS3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import AWS from 'aws-sdk'

const s3 = new AWS.S3()
const debug = require('debug')('gtin-cloud')

export default (path, data, contentType = 'image/jpeg') => {
return new Promise((resolve, reject) => {
const params = {
Bucket: process.env.AWS_BUCKET,
Body: data,
Key: path
}

if (contentType) {
params.ContentType = contentType
}

s3.putObject(params, (err2, data) => {
if (err2) {
debug(`'${path}' upload error`, err2)
return reject(err2)
}

return resolve(data)
}
)
})
}
41 changes: 33 additions & 8 deletions src/storeHandler.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import fs from 'fs'
import res from './response'
import fs from 'fs'
import res from './response'
import saveToS3 from './saveToS3'

const debug = require('debug')('gtin-cloud')
const debug = require('debug')('gtin-cloud')

/**
* Handle form post of type:
* application/json
* application/x-www-form-urlencoded
* multipart/form-data
* Handle storing of gtin featured image, data, and media
*
* @param object event the event
* @param object context the context
Expand All @@ -19,6 +17,33 @@ export default async (event, context, callback) => {
// @param string image_url the image url
// @param string data_url the data url
// @param string vendor the vendor (optional - default empty)
const gtin = event.pathParameters.gtin
const gtin = event.pathParameters.gtin
const qs = event.queryStringParameters || {}
const url = qs.url
const type = qs.type || ''
const rspHandler = res(context, callback)
const rawUrl = url.split(/\#|\?/)[0];
const fileName = rawUrl.split('/').pop().trim().toLowerCase();
const ext = fileName.split('.').pop().trim();
const basePath = gtinPath(gtin, event.pathParameters.vendor);
let destPath = basePath + 'index.jpg'

// possible action values: image or media
// image is featured image
if (type.indexOf('image') > -1) {
// download and store image as index.jpg
const fstream = got.stream(url)
await saveToS3(destPath, fstream, 'image/jpeg');
} else if (type.indexOf('media') > -1) {
// handle media storage
fstream = got.stream(url)
destPath = basePath + 'media/' + fileName
await saveToS3(destPath);
}

if (event.body) {
await saveToS3(basePath + 'index.json', event.body, 'application/json');
}

return rspHandler(`Uploaed as: ${destPath}`, 422)
}

0 comments on commit 07d329b

Please sign in to comment.