Translation: Treat EPUBs as primary attachments (#4321)

This commit is contained in:
Abe Jellinek 2024-07-03 01:18:23 -04:00 committed by GitHub
parent 931d89468f
commit 5f47a3d41d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -76,6 +76,10 @@ Zotero.Translate.ItemSaver = function(options) {
Zotero.Translate.ItemSaver.ATTACHMENT_MODE_IGNORE = 0; Zotero.Translate.ItemSaver.ATTACHMENT_MODE_IGNORE = 0;
Zotero.Translate.ItemSaver.ATTACHMENT_MODE_DOWNLOAD = 1; Zotero.Translate.ItemSaver.ATTACHMENT_MODE_DOWNLOAD = 1;
Zotero.Translate.ItemSaver.ATTACHMENT_MODE_FILE = 2; Zotero.Translate.ItemSaver.ATTACHMENT_MODE_FILE = 2;
Zotero.Translate.ItemSaver.PRIMARY_ATTACHMENT_TYPES = new Set([
'application/pdf',
'application/epub+zip',
]);
Zotero.Translate.ItemSaver.prototype = { Zotero.Translate.ItemSaver.prototype = {
/** /**
@ -152,18 +156,18 @@ Zotero.Translate.ItemSaver.prototype = {
// handle attachments // handle attachments
if (jsonItem.attachments) { if (jsonItem.attachments) {
let attachmentsToSave = []; let attachmentsToSave = [];
let foundPrimaryPDF = false; let foundPrimary = false;
for (let jsonAttachment of jsonItem.attachments) { for (let jsonAttachment of jsonItem.attachments) {
if (!this._canSaveAttachment(jsonAttachment)) { if (!this._canSaveAttachment(jsonAttachment)) {
continue; continue;
} }
// The first PDF is the primary one. If that one fails to download, // The first PDF/EPUB is the primary one. If that one fails to download,
// we might check for an open-access PDF below. // we might check for an open-access PDF below.
let isPrimaryPDF = false; if (Zotero.Translate.ItemSaver.PRIMARY_ATTACHMENT_TYPES.has(jsonAttachment.mimeType)
if (jsonAttachment.mimeType == 'application/pdf' && !foundPrimaryPDF) { && !foundPrimary) {
jsonAttachment.isPrimaryPDF = true; jsonAttachment.isPrimary = true;
foundPrimaryPDF = true; foundPrimary = true;
} }
attachmentsToSave.push(jsonAttachment); attachmentsToSave.push(jsonAttachment);
attachmentCallback(jsonAttachment, 0); attachmentCallback(jsonAttachment, 0);
@ -217,7 +221,7 @@ Zotero.Translate.ItemSaver.prototype = {
// Skip items with translated PDF attachments // Skip items with translated PDF attachments
if (jsonItem.attachments if (jsonItem.attachments
&& jsonItem.attachments.some(x => x.mimeType == 'application/pdf')) { && jsonItem.attachments.some(x => Zotero.Translate.ItemSaver.PRIMARY_ATTACHMENT_TYPES.has(x.mimeType))) {
continue; continue;
} }
@ -245,21 +249,21 @@ Zotero.Translate.ItemSaver.prototype = {
} }
// Save translated child attachments, and keep track of whether the save was successful // Save translated child attachments, and keep track of whether the save was successful
var itemIDsWithPDFAttachments = new Set(); var itemIDsWithPrimaryAttachments = new Set();
for (let [jsonAttachment, parentItemID] of childAttachments) { for (let [jsonAttachment, parentItemID] of childAttachments) {
let attachment = await this._saveAttachment( let attachment = await this._saveAttachment(
jsonAttachment, jsonAttachment,
parentItemID, parentItemID,
function (attachment, progress, error) { function (attachment, progress, error) {
// Don't cancel failed primary PDFs until we've tried other methods // Don't cancel failed primary PDFs until we've tried other methods
if (progress === false && attachment.isPrimaryPDF && shouldDownloadOAPDF) { if (progress === false && attachment.isPrimary && shouldDownloadOAPDF) {
return; return;
} }
attachmentCallback(...arguments); attachmentCallback(...arguments);
} }
); );
if (attachment && jsonAttachment.isPrimaryPDF) { if (attachment && jsonAttachment.isPrimary) {
itemIDsWithPDFAttachments.add(parentItemID); itemIDsWithPrimaryAttachments.add(parentItemID);
} }
} }
@ -267,16 +271,18 @@ Zotero.Translate.ItemSaver.prototype = {
// one or there was but it failed, look for another PDF (if enabled) // one or there was but it failed, look for another PDF (if enabled)
if (shouldDownloadOAPDF) { if (shouldDownloadOAPDF) {
for (let item of items) { for (let item of items) {
// Already have a PDF from translation // Already have a primary attachment from translation
if (itemIDsWithPDFAttachments.has(item.id)) { if (itemIDsWithPrimaryAttachments.has(item.id)) {
continue; continue;
} }
let jsonItem = jsonByItem.get(item); let jsonItem = jsonByItem.get(item);
// Reuse the existing status line if there is one. This could be a failed // Reuse the existing status line if there is one. This could be a failed
// translator attachment or a possible OA PDF found above. // translator attachment or a possible OA PDF found above.
// Explicitly check that the attachment is a PDF, not just any primary type,
// since we're reusing it for a PDF attachment.
let jsonAttachment = jsonItem.attachments && jsonItem.attachments.find( let jsonAttachment = jsonItem.attachments && jsonItem.attachments.find(
x => x.mimeType == 'application/pdf' && x.isPrimaryPDF x => x.mimeType == 'application/pdf' && x.isPrimary
); );
// If no translated, no OA, and no custom, don't show a line // If no translated, no OA, and no custom, don't show a line
@ -383,7 +389,7 @@ Zotero.Translate.ItemSaver.prototype = {
parent: parentID, parent: parentID,
title, title,
mimeType: 'application/pdf', mimeType: 'application/pdf',
isPrimaryPDF: true isPrimary: true
}; };
}, },