Fix direct saving of PDFs via connector

This commit is contained in:
Dan Stillman 2016-05-25 17:34:26 -04:00
parent 6a97de8911
commit 47b934f67e
3 changed files with 45 additions and 18 deletions

View file

@ -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);

View file

@ -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
);
}
}
})
}
/**

View file

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