From 4190412ee4afd8a38148f51a773fb5746af67d6a Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Sat, 5 Mar 2016 01:20:42 -0500 Subject: [PATCH] Allow PDF saving via connectors If 'pdf' flag is included in object POSTed to saveSnapshot, import the PDF directly and save as top-level item. Currently the PDF is redownloaded -- there might be a better way to get the PDF data over without redownloading. (It uses passed cookies, though, so gated PDFs should still work.) --- .../content/zotero/xpcom/server_connector.js | 114 +++++++++++------- .../zotero/treeitem-attachment-pdf@2x.png | Bin 0 -> 2258 bytes 2 files changed, 71 insertions(+), 43 deletions(-) create mode 100644 chrome/skin/default/zotero/treeitem-attachment-pdf@2x.png diff --git a/chrome/content/zotero/xpcom/server_connector.js b/chrome/content/zotero/xpcom/server_connector.js index ecdf6db33c..304b958b45 100644 --- a/chrome/content/zotero/xpcom/server_connector.js +++ b/chrome/content/zotero/xpcom/server_connector.js @@ -405,51 +405,79 @@ Zotero.Server.Connector.SaveSnapshot.prototype = { */ "init":function(url, data, sendResponseCallback) { Zotero.Server.Connector.Data[data["url"]] = ""+data["html"]+""; - Zotero.HTTP.processDocuments(["zotero://connector/"+encodeURIComponent(data["url"])], - function(doc) { - delete Zotero.Server.Connector.Data[data["url"]]; - - // figure out where to save - var libraryID = null; - var collectionID = null; - var zp = Zotero.getActiveZoteroPane(); - try { - var libraryID = zp.getSelectedLibraryID(); - var collection = zp.getSelectedCollection(); - } catch(e) {} - - try { - // create new webpage item - var item = new Zotero.Item("webpage"); - item.libraryID = libraryID; - item.setField("title", doc.title); - item.setField("url", data.url); - item.setField("accessDate", "CURRENT_TIMESTAMP"); - var itemID = item.save(); - if(collection) collection.addItem(itemID); + + // figure out where to save + var libraryID = null; + var collectionID = null; + var zp = Zotero.getActiveZoteroPane(); + try { + var libraryID = zp.getSelectedLibraryID(); + var collection = zp.getSelectedCollection(); + } catch(e) {} + + // determine whether snapshot can be saved + var filesEditable; + if (libraryID) { + let group = Zotero.Groups.getByLibraryID(libraryID); + filesEditable = group.filesEditable; + } + else { + filesEditable = true; + } + + var cookieSandbox = new Zotero.CookieSandbox(null, data["url"], data["cookie"], url.userAgent); + + if (data.pdf && filesEditable) { + delete Zotero.Server.Connector.Data[data.url]; + + try { + Zotero.Attachments.importFromURL( + data.url, + null, + null, + null, + collection ? [collection.id] : null, + "application/pdf", + libraryID, + function () { + sendResponseCallback(201); + }, + cookieSandbox + ); + } + catch (e) { + sendResponseCallback(500); + throw e; + } + } + else { + Zotero.HTTP.processDocuments(["zotero://connector/"+encodeURIComponent(data["url"])], + function(doc) { + delete Zotero.Server.Connector.Data[data["url"]]; - // determine whether snapshot can be saved - var filesEditable; - if (libraryID) { - var group = Zotero.Groups.getByLibraryID(libraryID); - filesEditable = group.filesEditable; - } else { - filesEditable = true; + try { + // create new webpage item + var item = new Zotero.Item("webpage"); + item.libraryID = libraryID; + item.setField("title", doc.title); + item.setField("url", data.url); + item.setField("accessDate", "CURRENT_TIMESTAMP"); + var itemID = item.save(); + if(collection) collection.addItem(itemID); + + // save snapshot + if (filesEditable && !data.skipSnapshot) { + Zotero.Attachments.importFromDocument(doc, itemID); + } + + sendResponseCallback(201); + } catch(e) { + sendResponseCallback(500); + throw e; } - - // save snapshot - if (filesEditable && !data.skipSnapshot) { - Zotero.Attachments.importFromDocument(doc, itemID); - } - - sendResponseCallback(201); - } catch(e) { - sendResponseCallback(500); - throw e; - } - }, - null, null, false, - new Zotero.CookieSandbox(null, data["url"], data["cookie"], url.userAgent)); + }, + null, null, false, cookieSandbox); + } } } diff --git a/chrome/skin/default/zotero/treeitem-attachment-pdf@2x.png b/chrome/skin/default/zotero/treeitem-attachment-pdf@2x.png new file mode 100644 index 0000000000000000000000000000000000000000..899804924f909bb1ca81348b30bb31869d99cdd5 GIT binary patch literal 2258 zcmeAS@N?(olHy`uVBq!ia0vp^3LwnE1|*BCs=fdz#^NA%Cx&(BWL^R}oCO|{#S9GG z!XV7ZFl&wk0|SdnW=KRygs+cPa(=E}VoH8es$NBI0Z=sqgH44MkeQoW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