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