From 20ee76bff834c377e0a17a5ffa160875acde749d Mon Sep 17 00:00:00 2001 From: noogen Date: Wed, 15 May 2019 14:45:23 -0500 Subject: [PATCH] add auto research queue --- env.yml.example | 2 ++ package.json | 2 +- src/imageHandler.js | 39 ++++++++++++++++++++++++++++++++++---- src/queueS3.js | 35 ++++++++++++++++++++++++++++++++++ src/saveToS3.js | 2 +- tests/imageHandler.test.js | 21 ++++++++++++++++++++ 6 files changed, 95 insertions(+), 6 deletions(-) create mode 100644 src/queueS3.js diff --git a/env.yml.example b/env.yml.example index ddce3e4..2a48349 100644 --- a/env.yml.example +++ b/env.yml.example @@ -16,6 +16,7 @@ dev: DIGITEYES_APPKEY: DIGITEYES_AUTHKEY: CDN_BASE: + QUEUE_PATH: prod: AWS_BUCKET: my-bucket @@ -28,3 +29,4 @@ prod: DIGITEYES_APPKEY: DIGITEYES_AUTHKEY: CDN_BASE: + QUEUE_PATH: diff --git a/package.json b/package.json index 11327e5..0d66a13 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gtin-cloud", - "version": "1.0.6", + "version": "1.0.7", "description": "GTIN cloud storage strategy", "main": "handler.js", "scripts": { diff --git a/src/imageHandler.js b/src/imageHandler.js index 0a20156..2cd174a 100644 --- a/src/imageHandler.js +++ b/src/imageHandler.js @@ -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 @@ -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 } @@ -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 diff --git a/src/queueS3.js b/src/queueS3.js new file mode 100644 index 0000000..84c8d01 --- /dev/null +++ b/src/queueS3.js @@ -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) + } + ) + }) +} diff --git a/src/saveToS3.js b/src/saveToS3.js index f9084bd..c3e6b2b 100644 --- a/src/saveToS3.js +++ b/src/saveToS3.js @@ -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) } diff --git a/tests/imageHandler.test.js b/tests/imageHandler.test.js index 519c72b..43d11a3 100644 --- a/tests/imageHandler.test.js +++ b/tests/imageHandler.test.js @@ -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) + })*/ })