Detect more EPUBs in viewAttachment() content type fixer (#3433)

This commit is contained in:
Abe Jellinek 2023-09-23 03:09:23 -04:00 committed by GitHub
parent c45405190a
commit 79ef266f2f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 17 deletions

View file

@ -4710,23 +4710,28 @@ var ZoteroPane = new function()
if(typeof itemIDs != "object") itemIDs = [itemIDs]; if(typeof itemIDs != "object") itemIDs = [itemIDs];
var launchFile = async (path, contentType, itemID) => { var launchFile = async (path, item) => {
// Fix blank PDF MIME type and incorrect EPUB MIME type let contentType = item.attachmentContentType;
if (!contentType || contentType === 'application/epub') { // Fix blank/incorrect EPUB and PDF content types
let item = await Zotero.Items.getAsync(itemID); let sniffType = async () => {
let path = await item.getFilePathAsync(); let path = await item.getFilePathAsync();
let type = contentType === 'application/epub' return Zotero.MIME.sniffForMIMEType(await Zotero.File.getSample(path));
? 'application/epub+zip' };
: 'application/pdf'; if (!contentType || contentType === 'application/octet-stream') {
if (Zotero.MIME.sniffForMIMEType(await Zotero.File.getSample(path)) == type) { let sniffedType = await sniffType();
contentType = type; if (sniffedType === 'application/pdf' || sniffedType === 'application/epub+zip') {
item.attachmentContentType = type; contentType = sniffedType;
await item.saveTx();
} }
} }
else if (contentType === 'application/epub' && await sniffType() === 'application/epub+zip') {
contentType = 'application/epub+zip';
}
if (item.attachmentContentType !== contentType) {
item.attachmentContentType = contentType;
await item.saveTx();
}
if (['application/pdf', 'application/epub+zip', 'text/html'].includes(contentType)) { if (['application/pdf', 'application/epub+zip', 'text/html'].includes(contentType)) {
let item = await Zotero.Items.getAsync(itemID);
let library = Zotero.Libraries.get(item.libraryID);
let type; let type;
if (contentType === 'application/pdf') { if (contentType === 'application/pdf') {
type = 'pdf'; type = 'pdf';
@ -4747,7 +4752,7 @@ var ZoteroPane = new function()
openInWindow = !openInWindow; openInWindow = !openInWindow;
} }
await Zotero.Reader.open( await Zotero.Reader.open(
itemID, item.id,
extraData && extraData.location, extraData && extraData.location,
{ {
openInWindow, openInWindow,
@ -4827,7 +4832,7 @@ var ZoteroPane = new function()
let iCloudPath = Zotero.File.getEvictedICloudPath(path); let iCloudPath = Zotero.File.getEvictedICloudPath(path);
if (await OS.File.exists(iCloudPath)) { if (await OS.File.exists(iCloudPath)) {
Zotero.debug("Triggering download of iCloud file"); Zotero.debug("Triggering download of iCloud file");
await launchFile(iCloudPath, item.attachmentContentType, itemID); await launchFile(iCloudPath, item);
let time = new Date(); let time = new Date();
let maxTime = 5000; let maxTime = 5000;
let revealed = false; let revealed = false;
@ -4887,7 +4892,7 @@ var ZoteroPane = new function()
if (fileExists && !redownload) { if (fileExists && !redownload) {
Zotero.debug("Opening " + path); Zotero.debug("Opening " + path);
Zotero.Notifier.trigger('open', 'file', item.id); Zotero.Notifier.trigger('open', 'file', item.id);
await launchFile(path, item.attachmentContentType, item.id); await launchFile(path, item);
continue; continue;
} }
@ -4933,7 +4938,7 @@ var ZoteroPane = new function()
Zotero.debug("Opening " + path); Zotero.debug("Opening " + path);
Zotero.Notifier.trigger('open', 'file', item.id); Zotero.Notifier.trigger('open', 'file', item.id);
await launchFile(path, item.attachmentContentType, item.id); await launchFile(path, item);
} }
}); });

View file

@ -318,6 +318,29 @@ describe("ZoteroPane", function() {
Zotero.Sync.Storage.Local.downloadOnSync(Zotero.Libraries.userLibraryID, true); Zotero.Sync.Storage.Local.downloadOnSync(Zotero.Libraries.userLibraryID, true);
yield downloadOnDemand(); yield downloadOnDemand();
}); });
it("should update a PDF with a blank MIME type", async function () {
let attachment = await importFileAttachment('test.pdf');
// Can't use contentType argument to importFileAttachment() because blank string is ignored
attachment.attachmentContentType = '';
await attachment.saveTx();
await zp.viewAttachment(attachment.id);
assert.equal(attachment.attachmentContentType, 'application/pdf');
});
it("should update an EPUB with an 'application/epub' MIME type", async function () {
let attachment = await importFileAttachment('stub.epub', { contentType: 'application/epub' });
assert.equal(attachment.attachmentContentType, 'application/epub');
await zp.viewAttachment(attachment.id);
assert.equal(attachment.attachmentContentType, 'application/epub+zip');
});
it("should update an EPUB with an 'application/octet-stream' MIME type", async function () {
let attachment = await importFileAttachment('stub.epub', { contentType: 'application/octet-stream' });
assert.equal(attachment.attachmentContentType, 'application/octet-stream');
await zp.viewAttachment(attachment.id);
assert.equal(attachment.attachmentContentType, 'application/epub+zip');
});
}) })