Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose attachments #99

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down Expand Up @@ -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<n; j++) {
var attachment = val[j];
if(typeof attachment !== "object" || !attachment.url) {
Zotero.debug("itemToAPIJSON: Discarded attachment: not an URL");
continue;
}
var apiItem = {
itemType: "attachment",
parentItem: newItem.key,
mimeType: attachment.mimeType.toString(),
url: attachment.url.toString(),
};
if (attachment.title) { // Optional field member, not all attachments have a title
apiItem['title'] = attachment.title.toString();
}
newItems.push(apiItem);
}
} else if((fieldID = Zotero.ItemFields.getID(field))) {
// if content is not a string, either stringify it or delete it
if(typeof val !== "string") {
Expand Down
12 changes: 10 additions & 2 deletions test/search_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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');
});
});
28 changes: 22 additions & 6 deletions test/web_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});


Expand All @@ -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');
});


Expand All @@ -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);
});


Expand Down Expand Up @@ -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 () {
Expand Down