Find Available PDF: Use translator-provided title (#4377)

This commit is contained in:
Abe Jellinek 2024-07-12 00:58:38 -04:00 committed by GitHub
parent f79dab8f3e
commit 174bde7451
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 9 additions and 9 deletions

View file

@ -1730,7 +1730,7 @@ Zotero.Attachments = new function () {
try { try {
tmpDir = (await this.createTemporaryStorageDirectory()).path; tmpDir = (await this.createTemporaryStorageDirectory()).path;
tmpFile = OS.Path.join(tmpDir, fileBaseName + '.pdf'); tmpFile = OS.Path.join(tmpDir, fileBaseName + '.pdf');
let { url, props } = await this.downloadFirstAvailableFile( let { title, url, props } = await this.downloadFirstAvailableFile(
urlResolvers, urlResolvers,
tmpFile, tmpFile,
{ {
@ -1746,7 +1746,7 @@ Zotero.Attachments = new function () {
directory: tmpDir, directory: tmpDir,
libraryID: item.libraryID, libraryID: item.libraryID,
filename: PathUtils.filename(tmpFile), filename: PathUtils.filename(tmpFile),
title: _getPDFTitleFromVersion(props.articleVersion), title: title || _getPDFTitleFromVersion(props.articleVersion),
url, url,
contentType: 'application/pdf', contentType: 'application/pdf',
parentItemID: item.id parentItemID: item.id
@ -1807,8 +1807,8 @@ Zotero.Attachments = new function () {
* @param {Function} [options.onAfterRequest] - Function that runs after a request * @param {Function} [options.onAfterRequest] - Function that runs after a request
* @param {Function} [options.onRequestError] - Function that runs when a request fails. * @param {Function} [options.onRequestError] - Function that runs when a request fails.
* Return true to retry request and false to skip. * Return true to retry request and false to skip.
* @return {Object|false} - Object with successful 'url' and 'props' from the associated urlResolver, * @return {Object|false} - Object with successful 'title' (when available from translator), 'url', and 'props'
* or false if no file could be downloaded * from the associated urlResolver, or false if no file could be downloaded
*/ */
this.downloadFirstAvailableFile = async function (urlResolvers, path, options) { this.downloadFirstAvailableFile = async function (urlResolvers, path, options) {
const maxURLs = 6; const maxURLs = 6;
@ -1883,7 +1883,6 @@ Zotero.Attachments = new function () {
let url = urlResolver.url; let url = urlResolver.url;
let pageURL = urlResolver.pageURL; let pageURL = urlResolver.pageURL;
let fromPage = false;
// Force URLs to HTTPS. If a request fails because of that, too bad. // Force URLs to HTTPS. If a request fails because of that, too bad.
if (!Zotero.test) { 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 URL wasn't available or failed, try to get a URL from a page
if (pageURL) { if (pageURL) {
url = null; url = null;
let title = null;
let responseURL; let responseURL;
try { try {
Zotero.debug(`Looking for PDF on ${pageURL}`); Zotero.debug(`Looking for PDF on ${pageURL}`);
@ -2073,7 +2073,7 @@ Zotero.Attachments = new function () {
} }
// Otherwise translate the Document we parsed above // Otherwise translate the Document we parsed above
else if (doc) { else if (doc) {
url = await Zotero.Utilities.Internal.getPDFFromDocument(doc); ({ title, url } = await Zotero.Utilities.Internal.getPDFFromDocument(doc));
} }
} }
catch (e) { catch (e) {
@ -2100,7 +2100,7 @@ Zotero.Attachments = new function () {
await beforeRequest(url); await beforeRequest(url);
await this.downloadFile(url, path, downloadOptions); await this.downloadFile(url, path, downloadOptions);
afterRequest(url); afterRequest(url);
return { url, props: urlResolver }; return { title, url, props: urlResolver };
} }
catch (e) { catch (e) {
Zotero.debug(`Error downloading ${url}: ${e}\n\n${e.stack}`); Zotero.debug(`Error downloading ${url}: ${e}\n\n${e.stack}`);

View file

@ -1335,7 +1335,7 @@ Zotero.Utilities.Internal = {
* Run translation on a Document to try to find a PDF URL * Run translation on a Document to try to find a PDF URL
* *
* @param {doc} Document * @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) { getPDFFromDocument: async function (doc) {
let translate = new Zotero.Translate.Web(); let translate = new Zotero.Translate.Web();
@ -1356,7 +1356,7 @@ Zotero.Utilities.Internal = {
} }
for (let attachment of newItems[0].attachments) { for (let attachment of newItems[0].attachments) {
if (attachment.mimeType == 'application/pdf') { if (attachment.mimeType == 'application/pdf') {
return attachment.url; return { title: attachment.title, url: attachment.url };
} }
} }
return false; return false;