commit 47b934f67ead3102d7fc13b8f57aa5b4d06c0ee8
parent 6a97de8911000e66e9b25106e40462aea84a5bfa
Author: Dan Stillman <dstillman@zotero.org>
Date: Wed, 25 May 2016 17:34:26 -0400
Fix direct saving of PDFs via connector
Diffstat:
3 files changed, 45 insertions(+), 18 deletions(-)
diff --git 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
@@ -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"]] = "<html>"+data["html"]+"</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
@@ -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));
+ });
});
});