diff --git a/chrome/content/zotero/xpcom/attachments.js b/chrome/content/zotero/xpcom/attachments.js index 6c50c91680..51695be77f 100644 --- a/chrome/content/zotero/xpcom/attachments.js +++ b/chrome/content/zotero/xpcom/attachments.js @@ -896,6 +896,7 @@ Zotero.Attachments = new function(){ * * @param {Zotero.Item} item * @param {String[]} [methods=['doi', 'url', 'oa', 'custom']] + * @param {Boolean} [automatic=false] - Only include custom resolvers with `automatic: true` * @return {Object[]} - An array of urlResolvers (see downloadFirstAvailableFile()) */ this.getPDFResolvers = function (item, methods = ['doi', 'url', 'oa', 'custom'], automatic) { diff --git a/chrome/content/zotero/xpcom/connector/server_connector.js b/chrome/content/zotero/xpcom/connector/server_connector.js index 9f28d5c069..ab5734e0a2 100644 --- a/chrome/content/zotero/xpcom/connector/server_connector.js +++ b/chrome/content/zotero/xpcom/connector/server_connector.js @@ -1070,7 +1070,9 @@ Zotero.Server.Connector.Progress.prototype = { // TODO: Change to progressID instead of id once we stop prepending // the sessionID to support older connector versions if (attachment.id == progressID) { - return attachment.progress; + // TODO: Remove + return attachment.progress == -1 ? false : attachment.progress; + //return attachment.progress; } } } diff --git a/chrome/content/zotero/xpcom/translation/translate_item.js b/chrome/content/zotero/xpcom/translation/translate_item.js index a9b30039cc..6561bf7d71 100644 --- a/chrome/content/zotero/xpcom/translation/translate_item.js +++ b/chrome/content/zotero/xpcom/translation/translate_item.js @@ -196,16 +196,11 @@ Zotero.Translate.ItemSaver.prototype = { // possible downloads. // // TODO: Separate pref? - var pdfResolvers = new Map(); + var openAccessPDFURLs = new Map(); if (Zotero.Prefs.get('downloadAssociatedFiles') // TEMP: Limit to dev builds && Zotero.isDevBuild) { for (let item of items) { - let doi = item.getField('DOI'); - if (!doi) { - continue; - } - let jsonItem = jsonByItem.get(item); // Skip items with translated PDF attachments @@ -215,11 +210,15 @@ Zotero.Translate.ItemSaver.prototype = { } try { - let resolvers = this._getPDFResolvers(item); - pdfResolvers.set(item, resolvers); + let resolvers = Zotero.Attachments.getPDFResolvers(item, ['oa']); + if (!resolvers.length) { + continue; + } + let urlObjects = await resolvers[0](); + openAccessPDFURLs.set(item, urlObjects); // If there are possible URLs, create a status line for the PDF - if (resolvers.length) { - let title = Zotero.getString('findPDF.searchingForAvailablePDFs'); + if (urlObjects.length) { + let title = Zotero.getString('findPDF.openAccessPDF'); let jsonAttachment = this._makeJSONAttachment(jsonItem.id, title); jsonItem.attachments.push(jsonAttachment); attachmentCallback(jsonAttachment, 0); @@ -268,28 +267,48 @@ Zotero.Translate.ItemSaver.prototype = { x => x.mimeType == 'application/pdf' && x.isPrimaryPDF ); - // We might already have retrieved possible OA URLs above, if there wasn't a PDF - // from the translator. If not, get them now. - let resolvers = pdfResolvers.get(item); - if (!resolvers) { - resolvers = this._getPDFResolvers(item); - } - - if (!resolvers.length) { - // If there was an existing status line, use that - if (jsonAttachment) { - attachmentCallback(jsonAttachment, false); + // If no translated, no OA, and no custom, don't show a line + // If no translated and potential OA, show "Open-Access PDF" + // If no translated, no OA, but custom, show custom when it starts + // If translated fails and potential OA, show "Open-Access PDF" + // If translated fails, no OA, no custom, fail original + // If translated fails, no OA, but custom, change to custom when it starts + let resolvers = openAccessPDFURLs.get(item); + // No translated PDF, so we checked for OA PDFs above + if (resolvers) { + // Add custom resolvers + resolvers.push(...Zotero.Attachments.getPDFResolvers(item, ['custom'], true)); + + // No translated, no OA, no custom, no status line + if (!resolvers.length) { + continue; + } + + // No translated, no OA, just potential custom, so create a status line + if (!jsonAttachment) { + jsonAttachment = this._makeJSONAttachment( + jsonItem.id, Zotero.getString('findPDF.searchingForAvailablePDFs') + ); } - continue; } - - // If no status line, add one, since we have something to try - if (!jsonAttachment) { - jsonAttachment = this._makeJSONAttachment( - jsonItem.id, Zotero.getString('findPDF.searchingForAvailablePDFs') - ); + // There was a translated PDF, so we didn't check for OA PDFs yet and didn't + // update the status line + else { + // Look for OA PDFs now + resolvers = Zotero.Attachments.getPDFResolvers(item, ['oa']); + if (resolvers.length) { + resolvers = await resolvers[0](); + } + + // Add custom resolvers + resolvers.push(...Zotero.Attachments.getPDFResolvers(item, ['custom'], true)); + + // Failed translated, no OA, no custom, so fail the existing translator line + if (!resolvers.length) { + attachmentCallback(jsonAttachment, false); + continue; + } } - attachmentCallback(jsonAttachment, 0); let attachment; try { @@ -346,11 +365,6 @@ Zotero.Translate.ItemSaver.prototype = { }, - _getPDFResolvers: function (item) { - return Zotero.Attachments.getPDFResolvers(item, ['oa', 'custom']); - }, - - "saveCollections": Zotero.Promise.coroutine(function* (collections) { var collectionsToProcess = collections.slice(); // Use first collection passed to translate process as the root