Skip to content

Commit

Permalink
add auto research queue
Browse files Browse the repository at this point in the history
  • Loading branch information
noogen committed May 15, 2019
1 parent 7e75104 commit 20ee76b
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 6 deletions.
2 changes: 2 additions & 0 deletions env.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ dev:
DIGITEYES_APPKEY:
DIGITEYES_AUTHKEY:
CDN_BASE:
QUEUE_PATH:

prod:
AWS_BUCKET: my-bucket
Expand All @@ -28,3 +29,4 @@ prod:
DIGITEYES_APPKEY:
DIGITEYES_AUTHKEY:
CDN_BASE:
QUEUE_PATH:
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gtin-cloud",
"version": "1.0.6",
"version": "1.0.7",
"description": "GTIN cloud storage strategy",
"main": "handler.js",
"scripts": {
Expand Down
39 changes: 35 additions & 4 deletions src/imageHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@ import res from './response'
import realGtin from './realGtin'
import gtinPath from './gtinPath'
import got from 'got'
import isNational from './isNational'
import queueS3 from './queueS3'

const debug = require('debug')('gtin-cloud')
const baseUrl = process.env.CDN_BASE
const debug = require('debug')('gtin-cloud')
const baseUrl = process.env.CDN_BASE
const queuePath = process.env.QUEUE_PATH

/**
* Handle returning of image url by gtin
*
* Also automatically queue national gtin to s3
* for auto-research
*
* @param object event the event
* @param object context the context
* @param Function callback the callback
Expand All @@ -28,24 +34,36 @@ export default async (event, context, callback) => {
const headers = { method: 'head' }
const rgtin = realGtin(gtin)
let imageUrl = null
let count = 0
let hasQueue = false

// always search local first
if (client.length > 0) {
count++;
tasks.push(got(baseUrl + gtinPath(gtin, client) + 'index.jpg', headers))
}

// if not all number
if (!/^\d+$/.test(gtin)) {
count++;
// search national with the provided gtin
tasks.push(got(baseUrl + gtinPath(gtin) + 'index.jpg', headers))
} else if (isNational(gtin)) {
count++;
// search national with real gtin
tasks.push(got(baseUrl + gtinPath(rgtin) + 'index.jpg', headers))

// check if we already queued the national product research
if (queuePath) {
const queueUrl = `https://s3.amazonaws.com/${queuePath}`
hasQueue = true
tasks.push(got(`${queueUrl}${rgtin}.jpg`, headers))
}
}

const rsts = await Promise.all(tasks.map(p => p.catch(e => e)))
rsts.forEach((item) => {
if (imageUrl) {
rsts.forEach((item, i) => {
if (imageUrl || i >= count) {
return
}

Expand All @@ -54,6 +72,19 @@ export default async (event, context, callback) => {
}
})

// if queue url results
if (hasQueue && rsts[count]) {
// check queue url status code
if (rsts[count].statusCode !== 200) {
try {
await queueS3(queuePath, rgtin)
} catch(e) {
// don't care, just debug it
debug('queue error', e)
}
}
}

rspHandler(imageUrl, imageUrl ? 302 : 404, imageUrl ? { Location: imageUrl } : null)

return imageUrl
Expand Down
35 changes: 35 additions & 0 deletions src/queueS3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import AWS from 'aws-sdk'

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

export default (queuePath, gtin, contentType = 'image/jpeg') => {
return new Promise((resolve, reject) => {
const parts = queuePath.split('/')
const bucket = parts[0]
const rest = queuePath.substr(bucket.length).replace(/^\/+|\/+$/g, '')

const params = {
Bucket: bucket,
Body: ' ',
Key: `${rest}/${gtin}.jpg`
}
// console.log(path)

if (contentType) {
params.ContentType = contentType
}

debug(`queuing research ${bucket}/${params.Key}`)

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

return resolve(data)
}
)
})
}
2 changes: 1 addition & 1 deletion src/saveToS3.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default (path, data, contentType = 'image/jpeg') => {

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

Expand Down
21 changes: 21 additions & 0 deletions tests/imageHandler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,25 @@ describe('image-handler-tests', () => {

expect(rst).toBe(baseUrl + '008/100/003/00008100003983/index.jpg')
})

// this is commented out because, if it has already been executed
// then product is no longer new
/* test('test request queuing of new national product', async () => {
const rst = await handler(
{
pathParameters: {
gtin: '00725439999687'
},
queryStringParameters: {
nocheck: 0
}
},
null,
(err) => {
expect(err).toBeNull()
}
)
expect(rst).toBe(null)
})*/
})

0 comments on commit 20ee76b

Please sign in to comment.