From 47b934f67ead3102d7fc13b8f57aa5b4d06c0ee8 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Wed, 25 May 2016 17:34:26 -0400 Subject: [PATCH] Fix direct saving of PDFs via connector --- chrome/content/zotero/xpcom/server.js | 8 ++--- .../content/zotero/xpcom/server_connector.js | 22 +++++-------- test/tests/server_connectorTest.js | 33 +++++++++++++++++++ 3 files changed, 45 insertions(+), 18 deletions(-) diff --git a/chrome/content/zotero/xpcom/server.js b/chrome/content/zotero/xpcom/server.js index 7a32e5052..7e8ce949d 100755 --- a/chrome/content/zotero/xpcom/server.js +++ b/chrome/content/zotero/xpcom/server.js @@ -400,7 +400,10 @@ Zotero.Server.DataListener.prototype._processEndpoint = function(method, postDat } // pass to endpoint - if((endpoint.init.length ? endpoint.init.length : endpoint.init.arity) === 3) { + if (endpoint.init.length === 2) { + endpoint.init(decodedData, sendResponseCallback); + } + else { const uaRe = /[\r\n]User-Agent: +([^\r\n]+)/i; var m = uaRe.exec(this.header); var url = { @@ -408,10 +411,7 @@ Zotero.Server.DataListener.prototype._processEndpoint = function(method, postDat "query":this.query ? Zotero.Server.decodeQueryString(this.query.substr(1)) : {}, "userAgent":m && m[1] }; - endpoint.init(url, decodedData, sendResponseCallback); - } else { - endpoint.init(decodedData, sendResponseCallback); } } catch(e) { Zotero.debug(e); diff --git a/chrome/content/zotero/xpcom/server_connector.js b/chrome/content/zotero/xpcom/server_connector.js index cccc42a4c..e2e69b9f0 100644 --- a/chrome/content/zotero/xpcom/server_connector.js +++ b/chrome/content/zotero/xpcom/server_connector.js @@ -409,7 +409,7 @@ Zotero.Server.Connector.SaveSnapshot.prototype = { * @param {String} data POST data or GET query string * @param {Function} sendResponseCallback function to send HTTP response */ - "init":function(url, data, sendResponseCallback) { + init: Zotero.Promise.coroutine(function* (url, data, sendResponseCallback) { Zotero.Server.Connector.Data[data["url"]] = ""+data["html"]+""; var zp = Zotero.getActiveZoteroPane(); @@ -439,20 +439,14 @@ Zotero.Server.Connector.SaveSnapshot.prototype = { delete Zotero.Server.Connector.Data[data.url]; try { - // TODO: Async - Zotero.Attachments.importFromURL( - data.url, - null, - null, - null, - collection ? [collection.id] : null, - "application/pdf", + yield Zotero.Attachments.importFromURL({ libraryID, - function () { - sendResponseCallback(201); - }, + url: data.url, + collections: collection ? [collection.id] : undefined, + contentType: "application/pdf", cookieSandbox - ); + }); + sendResponseCallback(201) } catch (e) { sendResponseCallback(500); @@ -496,7 +490,7 @@ Zotero.Server.Connector.SaveSnapshot.prototype = { null, null, false, cookieSandbox ); } - } + }) } /** diff --git a/test/tests/server_connectorTest.js b/test/tests/server_connectorTest.js index 8482d9989..2591ccc3c 100644 --- a/test/tests/server_connectorTest.js +++ b/test/tests/server_connectorTest.js @@ -145,5 +145,38 @@ describe("Connector Server", function () { assert.isTrue(item.isImportedAttachment()); assert.equal(item.getField('title'), 'Title'); }); + + it("should save a PDF to the current selected collection", function* () { + var collection = yield createDataObject('collection'); + yield waitForItemsLoad(win); + + var file = getTestDataDirectory(); + file.append('test.pdf'); + httpd.registerFile("/test.pdf", file); + + var ids; + var promise = waitForItemEvent('add'); + yield Zotero.HTTP.request( + 'POST', + connectorServerPath + "/connector/saveSnapshot", + { + headers: { + "Content-Type": "application/json" + }, + body: JSON.stringify({ + url: testServerPath + "/test.pdf", + pdf: true + }) + } + ); + + var ids = yield promise; + + assert.lengthOf(ids, 1); + var item = Zotero.Items.get(ids[0]); + assert.isTrue(item.isImportedAttachment()); + assert.equal(item.attachmentContentType, 'application/pdf'); + assert.isTrue(collection.hasItem(item.id)); + }); }); });