From 174bde74516f7a5a739845d69f6d4233ca0ecb60 Mon Sep 17 00:00:00 2001 From: Abe Jellinek Date: Fri, 12 Jul 2024 00:58:38 -0400 Subject: [PATCH] Find Available PDF: Use translator-provided title (#4377) --- chrome/content/zotero/xpcom/attachments.js | 14 +++++++------- chrome/content/zotero/xpcom/utilities_internal.js | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/chrome/content/zotero/xpcom/attachments.js b/chrome/content/zotero/xpcom/attachments.js index 6bc1b611c2..5c1809da01 100644 --- a/chrome/content/zotero/xpcom/attachments.js +++ b/chrome/content/zotero/xpcom/attachments.js @@ -1730,7 +1730,7 @@ Zotero.Attachments = new function () { try { tmpDir = (await this.createTemporaryStorageDirectory()).path; tmpFile = OS.Path.join(tmpDir, fileBaseName + '.pdf'); - let { url, props } = await this.downloadFirstAvailableFile( + let { title, url, props } = await this.downloadFirstAvailableFile( urlResolvers, tmpFile, { @@ -1746,7 +1746,7 @@ Zotero.Attachments = new function () { directory: tmpDir, libraryID: item.libraryID, filename: PathUtils.filename(tmpFile), - title: _getPDFTitleFromVersion(props.articleVersion), + title: title || _getPDFTitleFromVersion(props.articleVersion), url, contentType: 'application/pdf', parentItemID: item.id @@ -1807,8 +1807,8 @@ Zotero.Attachments = new function () { * @param {Function} [options.onAfterRequest] - Function that runs after a request * @param {Function} [options.onRequestError] - Function that runs when a request fails. * Return true to retry request and false to skip. - * @return {Object|false} - Object with successful 'url' and 'props' from the associated urlResolver, - * or false if no file could be downloaded + * @return {Object|false} - Object with successful 'title' (when available from translator), 'url', and 'props' + * from the associated urlResolver, or false if no file could be downloaded */ this.downloadFirstAvailableFile = async function (urlResolvers, path, options) { const maxURLs = 6; @@ -1883,7 +1883,6 @@ Zotero.Attachments = new function () { let url = urlResolver.url; let pageURL = urlResolver.pageURL; - let fromPage = false; // Force URLs to HTTPS. If a request fails because of that, too bad. if (!Zotero.test) { @@ -1938,6 +1937,7 @@ Zotero.Attachments = new function () { // If URL wasn't available or failed, try to get a URL from a page if (pageURL) { url = null; + let title = null; let responseURL; try { Zotero.debug(`Looking for PDF on ${pageURL}`); @@ -2073,7 +2073,7 @@ Zotero.Attachments = new function () { } // Otherwise translate the Document we parsed above else if (doc) { - url = await Zotero.Utilities.Internal.getPDFFromDocument(doc); + ({ title, url } = await Zotero.Utilities.Internal.getPDFFromDocument(doc)); } } catch (e) { @@ -2100,7 +2100,7 @@ Zotero.Attachments = new function () { await beforeRequest(url); await this.downloadFile(url, path, downloadOptions); afterRequest(url); - return { url, props: urlResolver }; + return { title, url, props: urlResolver }; } catch (e) { Zotero.debug(`Error downloading ${url}: ${e}\n\n${e.stack}`); diff --git a/chrome/content/zotero/xpcom/utilities_internal.js b/chrome/content/zotero/xpcom/utilities_internal.js index 142d06b13f..20464b4f87 100644 --- a/chrome/content/zotero/xpcom/utilities_internal.js +++ b/chrome/content/zotero/xpcom/utilities_internal.js @@ -1335,7 +1335,7 @@ Zotero.Utilities.Internal = { * Run translation on a Document to try to find a PDF URL * * @param {doc} Document - * @return {String|false} - PDF URL, or false if none found + * @return {{ title: string, url: string } | false} - PDF attachment title and URL, or false if none found */ getPDFFromDocument: async function (doc) { let translate = new Zotero.Translate.Web(); @@ -1356,7 +1356,7 @@ Zotero.Utilities.Internal = { } for (let attachment of newItems[0].attachments) { if (attachment.mimeType == 'application/pdf') { - return attachment.url; + return { title: attachment.title, url: attachment.url }; } } return false;