From cdda5727288d91eb015e7d2451e56e4134d27074 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Fri, 27 Jul 2018 00:52:44 -0400 Subject: [PATCH] Support additional data from OA PDF lookup service Handle an array of objects with 'url' and 'version' rather than just an array of URLs. Also: - Don't throw an error from addOpenAccessPDF() if there's an error from getOpenAccessPDFURLs() - Make addPDFFromURLs() a separate function so URL lookup can be done separately from download --- chrome/content/zotero/xpcom/attachments.js | 28 +++++++++++++++++-- .../zotero/xpcom/utilities_internal.js | 2 ++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/chrome/content/zotero/xpcom/attachments.js b/chrome/content/zotero/xpcom/attachments.js index eef40d535a..a2ef52972b 100644 --- a/chrome/content/zotero/xpcom/attachments.js +++ b/chrome/content/zotero/xpcom/attachments.js @@ -889,6 +889,7 @@ Zotero.Attachments = new function(){ */ this.downloadFirstAvailableFile = async function (urls, path, options) { var url; + urls = [...urls]; while (url = urls.shift()) { try { await this.downloadFile(url, path, options); @@ -918,15 +919,34 @@ Zotero.Attachments = new function(){ return false; } - var urls = await Zotero.Utilities.Internal.getOpenAccessPDFURLs(doi); - if (!urls.length) { + try { + var urlObjects = await Zotero.Utilities.Internal.getOpenAccessPDFURLs(doi); + } + catch (e) { + Zotero.logError(e); + return false; + } + if (!urlObjects.length) { return false; } + return this.addPDFFromURLs(item, urlObjects); + }; + + + /** + * Try to add a PDF to an item from a set of possible URLs + * + * @param {Zotero.Item} item + * @param {Object[]} urlObjects - Array of objects with 'url' and 'version' + * @return {Zotero.Item|false} - New attachment item, or false if unsuccessful + */ + this.addPDFFromURLs = async function (item, urlObjects) { var fileBaseName = this.getFileBaseNameFromItem(item); var tmpDir; var tmpFile; var attachmentItem = false; + var urls = urlObjects.map(o => o.url); try { tmpDir = (await this.createTemporaryStorageDirectory()).path; tmpFile = OS.Path.join(tmpDir, fileBaseName + '.pdf'); @@ -934,13 +954,15 @@ Zotero.Attachments = new function(){ urls, tmpFile, { isPDF: true } ); if (url) { + let version = urlObjects[urls.indexOf(url)].version; attachmentItem = await this.createURLAttachmentFromTemporaryStorageDirectory({ directory: tmpDir, libraryID: item.libraryID, filename: OS.Path.basename(tmpFile), url, contentType: 'application/pdf', - parentItemID: item.id + parentItemID: item.id, + articleVersion: version }); } else { diff --git a/chrome/content/zotero/xpcom/utilities_internal.js b/chrome/content/zotero/xpcom/utilities_internal.js index 60afa71948..bf71572828 100644 --- a/chrome/content/zotero/xpcom/utilities_internal.js +++ b/chrome/content/zotero/xpcom/utilities_internal.js @@ -955,6 +955,8 @@ Zotero.Utilities.Internal = { }); var urls = req.response; Zotero.debug(`Found ${urls.length} ${Zotero.Utilities.pluralize(urls.length, ['URL', 'URLs'])}`); + // Handle older URL-only format + urls = urls.map(o => typeof o == 'string' ? { url: o } : o); return urls; },