Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mrtcode committed Dec 5, 2018
1 parent 75a7af0 commit f642d1f
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 14 deletions.
14 changes: 13 additions & 1 deletion lambda_template.yaml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}

Expand Down Expand Up @@ -223,7 +223,7 @@ Zotero.HTTP = new function() {
}
}
}
else if(
else if (
options.fallbackToPDF &&
mimeType.essence === 'application/pdf'
) {
Expand Down
2 changes: 1 addition & 1 deletion src/lambda.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
18 changes: 15 additions & 3 deletions src/recognizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
};
2 changes: 1 addition & 1 deletion src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
20 changes: 14 additions & 6 deletions src/webSession.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,19 +164,27 @@ WebSession.prototype.handleURL = async function () {
headers
}
);
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
translate.getTranslators(true);
}
// 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;
Expand Down

0 comments on commit f642d1f

Please sign in to comment.