From 7aa68a0f75a48e4f2b82440bc01f5886d9b838a0 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Sat, 25 May 2019 07:34:41 -0400 Subject: [PATCH] Update MIME.getMIMETypeFromURL() for non-Bluebird HTTP.request() --- chrome/content/zotero/xpcom/attachments.js | 3 +- chrome/content/zotero/xpcom/mime.js | 64 ++++++++++++---------- 2 files changed, 37 insertions(+), 30 deletions(-) diff --git a/chrome/content/zotero/xpcom/attachments.js b/chrome/content/zotero/xpcom/attachments.js index 00b203a954..c779deb86c 100644 --- a/chrome/content/zotero/xpcom/attachments.js +++ b/chrome/content/zotero/xpcom/attachments.js @@ -509,7 +509,8 @@ Zotero.Attachments = new function(){ return process(contentType, Zotero.MIME.hasNativeHandler(contentType)); } - return Zotero.MIME.getMIMETypeFromURL(url, cookieSandbox).spread(process); + var args = yield Zotero.MIME.getMIMETypeFromURL(url, cookieSandbox); + return process(...args); }); diff --git a/chrome/content/zotero/xpcom/mime.js b/chrome/content/zotero/xpcom/mime.js index f972d8d724..155d1edb51 100644 --- a/chrome/content/zotero/xpcom/mime.js +++ b/chrome/content/zotero/xpcom/mime.js @@ -327,36 +327,42 @@ Zotero.MIME = new function(){ * @param {Zotero.CookieSandbox} [cookieSandbox] * @return {Promise} */ - this.getMIMETypeFromURL = function (url, cookieSandbox) { - return Zotero.HTTP.promise("HEAD", url, { cookieSandbox: cookieSandbox, successCodes: false }) - .then(function (xmlhttp) { - if (xmlhttp.status != 200 && xmlhttp.status != 204) { - Zotero.debug("Attachment HEAD request returned with status code " - + xmlhttp.status + " in Zotero.MIME.getMIMETypeFromURL()", 2); - var mimeType = ''; + this.getMIMETypeFromURL = async function (url, cookieSandbox) { + var xmlhttp = await Zotero.HTTP.request( + "HEAD", + url, + { + cookieSandbox, + successCodes: false } - else { - var mimeType = xmlhttp.channel.contentType; - } - - var nsIURL = Components.classes["@mozilla.org/network/standard-url;1"] - .createInstance(Components.interfaces.nsIURL); - nsIURL.spec = url; - - // Override MIME type to application/pdf if extension is .pdf -- - // workaround for sites that respond to the HEAD request with an - // invalid MIME type (https://www.zotero.org/trac/ticket/460) - // - // Downloaded file is inspected in attachment code and deleted if actually HTML - if (nsIURL.fileName.match(/pdf$/) || url.match(/pdf$/)) { - mimeType = 'application/pdf'; - } - - var ext = nsIURL.fileExtension; - var hasNativeHandler = Zotero.MIME.hasNativeHandler(mimeType, ext); - - return [mimeType, hasNativeHandler]; - }); + ); + + if (xmlhttp.status != 200 && xmlhttp.status != 204) { + Zotero.debug("Attachment HEAD request returned with status code " + + xmlhttp.status + " in Zotero.MIME.getMIMETypeFromURL()", 2); + var mimeType = ''; + } + else { + var mimeType = xmlhttp.channel.contentType; + } + + var nsIURL = Components.classes["@mozilla.org/network/standard-url;1"] + .createInstance(Components.interfaces.nsIURL); + nsIURL.spec = url; + + // Override MIME type to application/pdf if extension is .pdf -- + // workaround for sites that respond to the HEAD request with an + // invalid MIME type (https://www.zotero.org/trac/ticket/460) + // + // Downloaded file is inspected in attachment code and deleted if actually HTML + if (nsIURL.fileName.match(/pdf$/) || url.match(/pdf$/)) { + mimeType = 'application/pdf'; + } + + var ext = nsIURL.fileExtension; + var hasNativeHandler = Zotero.MIME.hasNativeHandler(mimeType, ext); + + return [mimeType, hasNativeHandler]; }