From efae0502712b902cda1b979b4478f8b30f02c75b Mon Sep 17 00:00:00 2001 From: Simon Schuster Date: Tue, 3 Dec 2019 13:12:31 +0100 Subject: [PATCH 1/3] Expose attachment urls via API Generally, it is not useful to expose regular Zotero attachments via the API. However, url attachments are an exception: url resources are of use to the api user, as they help locating various linked online artifacts that can be readily accessed (in contrast to other attachment types residing within Zotero's store). Therefore, this commit exports them as linked items into the API-response. --- src/utilities.js | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/utilities.js b/src/utilities.js index 5f88951..61da6d4 100644 --- a/src/utilities.js +++ b/src/utilities.js @@ -65,8 +65,8 @@ Zotero.Utilities.itemToAPIJSON = function(item) { var fieldID, itemFieldID; for(var field in item) { - if(field === "complete" || field === "itemID" || field === "attachments" - || field === "seeAlso") continue; + if(field === "complete" || field === "itemID" || field === "seeAlso") + continue; var val = item[field]; @@ -149,6 +149,25 @@ Zotero.Utilities.itemToAPIJSON = function(item) { note: note.toString() }); } + } else if(field === "attachments") { + var n = val.length; + for(var j=0; j Date: Wed, 4 Dec 2019 18:06:20 +0100 Subject: [PATCH 2/3] Adapt tests to correctly handle url attachments The previous commit introduced url attachments, which have to be corretly handled/expected within the tests, which this commit rectifies. --- test/search_test.js | 12 ++++++++++-- test/web_test.js | 28 ++++++++++++++++++++++------ 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/test/search_test.js b/test/search_test.js index 621ab0e..a154f41 100644 --- a/test/search_test.js +++ b/test/search_test.js @@ -120,9 +120,13 @@ describe("/search", function () { assert.equal(response.statusCode, 200); var json = response.body; - assert.lengthOf(json, 1); + assert.lengthOf(json, 2); assert.equal(json[0].itemType, 'journalArticle'); assert.equal(json[0].title, articleTitle1); + // This item contains an attachment (URL) + assert.equal(json[1].itemType, 'attachment'); + assert.equal(json[1].parentItem, json[0].key); + assert.equal(json[1].mimeType, 'text/html'); }); it("should translate a PMID with 'pmid:' prefix", async function () { @@ -133,8 +137,12 @@ describe("/search", function () { assert.equal(response.statusCode, 200); var json = response.body; - assert.lengthOf(json, 1); + assert.lengthOf(json, 2); assert.equal(json[0].itemType, 'journalArticle'); assert.equal(json[0].title, articleTitle1); + // This item contains an attachment (URL) + assert.equal(json[1].itemType, 'attachment'); + assert.equal(json[1].parentItem, json[0].key); + assert.equal(json[1].mimeType, 'text/html'); }); }); diff --git a/test/web_test.js b/test/web_test.js index 73fc82c..71026c8 100644 --- a/test/web_test.js +++ b/test/web_test.js @@ -24,10 +24,14 @@ describe("/web", function () { .send(url); assert.equal(response.statusCode, 200); var json = response.body; - - assert.lengthOf(json, 1); + assert.lengthOf(json, 2); assert.equal(json[0].itemType, 'journalArticle'); assert.equal(json[0].title, 'Title'); + // This item contains an snapshot + assert.equal(json[1].itemType, 'attachment'); + assert.equal(json[1].parentItem, json[0].key); + assert.equal(json[1].mimeType, 'text/html'); + assert.equal(json[1].url, url); }); @@ -50,9 +54,11 @@ describe("/web", function () { .send(json); assert.equal(response.statusCode, 200); json = response.body; - assert.lengthOf(json, 2); + assert.lengthOf(json, 3); assert.equal(json[0].title, 'A'); - assert.equal(json[1].title, 'C'); + assert.equal(json[1].parentItem, json[0].key); + assert.equal(json[1].url, url); + assert.equal(json[2].title, 'C'); }); @@ -66,10 +72,15 @@ describe("/web", function () { assert.equal(response.statusCode, 200); var json = response.body; - assert.lengthOf(json, 1); + assert.lengthOf(json, 2); assert.equal(json[0].itemType, 'journalArticle'); assert.equal(json[0].title, 'Title'); assert.equal(json[0].url, finalURL); + // This item contains an snapshot + assert.equal(json[1].itemType, 'attachment'); + assert.equal(json[1].parentItem, json[0].key); + assert.equal(json[1].mimeType, 'text/html'); + assert.equal(json[1].url, finalURL); }); @@ -110,9 +121,14 @@ describe("/web", function () { assert.equal(response.statusCode, 200); var json = response.body; - assert.lengthOf(json, 1); + assert.lengthOf(json, 2); assert.equal(json[0].itemType, 'journalArticle'); assert.equal(json[0].title, 'Titre'); + // This item contains an snapshot + assert.equal(json[1].itemType, 'attachment'); + assert.equal(json[1].parentItem, json[0].key); + assert.equal(json[1].mimeType, 'text/html'); + assert.equal(json[1].url, url); }); it("should reject non-HTML/XML upstream content types", async function () { From a056f7674f54381199e469b7288cd5f0ca53c59d Mon Sep 17 00:00:00 2001 From: Antonin Delpeuch Date: Sun, 24 Mar 2019 19:06:24 +0100 Subject: [PATCH 3/3] Expose attachments, as suggested by noctux --- src/utilities.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/utilities.js b/src/utilities.js index 5f88951..86fc1ed 100644 --- a/src/utilities.js +++ b/src/utilities.js @@ -65,8 +65,8 @@ Zotero.Utilities.itemToAPIJSON = function(item) { var fieldID, itemFieldID; for(var field in item) { - if(field === "complete" || field === "itemID" || field === "attachments" - || field === "seeAlso") continue; + if(field === "complete" || field === "itemID" || field === "seeAlso") + continue; var val = item[field]; @@ -149,6 +149,22 @@ Zotero.Utilities.itemToAPIJSON = function(item) { note: note.toString() }); } + } else if(field === "attachments") { + var n = val.length; + for(var j=0; j