From 13df30660f7e7f905df0f68822af714a52331d61 Mon Sep 17 00:00:00 2001 From: Martynas Bagdonas Date: Sat, 1 Dec 2018 14:31:57 +0200 Subject: [PATCH] Fixes --- lambda_template.yaml.j2 | 14 +++++++++++++- src/http.js | 4 ++-- src/lambda.js | 2 +- src/recognizer.js | 18 +++++++++++++++--- src/server.js | 2 +- src/webSession.js | 20 ++++++++++++++------ 6 files changed, 46 insertions(+), 14 deletions(-) diff --git a/lambda_template.yaml.j2 b/lambda_template.yaml.j2 index 560d1fe..4dbc884 100644 --- a/lambda_template.yaml.j2 +++ b/lambda_template.yaml.j2 @@ -14,13 +14,25 @@ Resources: MemorySize: 2048 Timeout: 30 Policies: + - Statement: + Action: + - s3:PutObject + - s3:DeleteObject + Effect: Allow + Resource: !ImportValue RecognizerUploadBucketArn - LambdaInvokePolicy: FunctionName: {{ identifier_search_function_name }} - LambdaInvokePolicy: FunctionName: {{ recognizer_function_name }} Events: # API Gateway - API: + GetAPI: + Type: Api + Properties: + # Proxy all GET requests to Lambda function + Path: /{proxy+} + Method: get + PostAPI: Type: Api Properties: # Proxy all POST requests to Lambda function diff --git a/src/http.js b/src/http.js index c42aa83..51a27d6 100644 --- a/src/http.js +++ b/src/http.js @@ -153,7 +153,7 @@ Zotero.HTTP = new function() { return reject(error); } - if(!response.headers['content-type']) { + if (!response.headers['content-type']) { return reject(new Error('Missing content-type header')); } @@ -223,7 +223,7 @@ Zotero.HTTP = new function() { } } } - else if( + else if ( options.fallbackToPDF && mimeType.essence === 'application/pdf' ) { diff --git a/src/lambda.js b/src/lambda.js index a107938..d311778 100644 --- a/src/lambda.js +++ b/src/lambda.js @@ -43,7 +43,7 @@ app.use(bodyParser({enableTypes: ['text', 'json']})); app.use(_.post('/web', WebEndpoint.handle.bind(WebEndpoint))); app.use(_.post('/search', SearchEndpoint.handle.bind(SearchEndpoint))); app.use(_.post('/export', ExportEndpoint.handle.bind(ExportEndpoint))); -app.use(_.post('/recognize/getUploadParams', Recognizer.handleUpload.bind(Recognizer))); +app.use(_.get('/recognize/getUploadParams', Recognizer.handleUpload.bind(Recognizer))); app.use(_.post('/recognize/process', Recognizer.handleProcess.bind(Recognizer))); Debug.init(1); diff --git a/src/recognizer.js b/src/recognizer.js index dc51118..f4375a8 100644 --- a/src/recognizer.js +++ b/src/recognizer.js @@ -194,8 +194,20 @@ let Recognizer = module.exports = { */ handleProcess: async function (ctx) { let uploadID = ctx.request.body; - let item = await this.recognize(uploadID); - ctx.body = Zotero.Utilities.itemToAPIJSON(item); - await this.remove(uploadID); + + if (!uploadID) { + ctx.throw(400, "uploadID not provided\n"); + } + + try { + let item = await this.recognize(uploadID); + ctx.body = Zotero.Utilities.itemToAPIJSON(item); + } + catch(e) { + throw e; + } + finally { + await this.remove(uploadID); + } } }; diff --git a/src/server.js b/src/server.js index 6d1749f..2fd33f7 100644 --- a/src/server.js +++ b/src/server.js @@ -44,7 +44,7 @@ app.use(bodyParser({ enableTypes: ['text', 'json']})); app.use(_.post('/web', WebEndpoint.handle.bind(WebEndpoint))); app.use(_.post('/search', SearchEndpoint.handle.bind(SearchEndpoint))); app.use(_.post('/export', ExportEndpoint.handle.bind(ExportEndpoint))); -app.use(_.post('/recognize/getUploadParams', Recognizer.handleUpload.bind(Recognizer))); +app.use(_.get('/recognize/getUploadParams', Recognizer.handleUpload.bind(Recognizer))); app.use(_.post('/recognize/process', Recognizer.handleProcess.bind(Recognizer))); Debug.init(1); diff --git a/src/webSession.js b/src/webSession.js index ba4d8b9..def2469 100644 --- a/src/webSession.js +++ b/src/webSession.js @@ -152,7 +152,7 @@ WebSession.prototype.handleURL = async function () { cookieSandbox: this._cookieSandbox } ); - if(req.type === 'document') { + if (req.type === 'document') { translate.setDocument(req.response); // This could be optimized by only running detect on secondary translators // if the first fails, but for now just run detect on all @@ -160,11 +160,19 @@ WebSession.prototype.handleURL = async function () { } // If PDF received else { - let uploadID = await Recognizer.upload(req.response); - let item = await Recognizer.recognize(uploadID); - this.ctx.response.body = Zotero.Utilities.itemToAPIJSON(item); - await Recognizer.remove(uploadID); - resolve(); + let uploadID; + try { + uploadID = await Recognizer.upload(req.response); + let item = await Recognizer.recognize(uploadID); + this.ctx.response.body = Zotero.Utilities.itemToAPIJSON(item); + resolve(); + } + catch (e) { + throw e; + } + finally { + if (uploadID) await Recognizer.remove(uploadID); + } } return promise;