Update MIME.getMIMETypeFromURL() for non-Bluebird HTTP.request()

This commit is contained in:
Dan Stillman 2019-05-25 07:34:41 -04:00
parent e8dd1f7824
commit 7aa68a0f75
2 changed files with 37 additions and 30 deletions

View file

@ -509,7 +509,8 @@ Zotero.Attachments = new function(){
return process(contentType, Zotero.MIME.hasNativeHandler(contentType)); 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);
}); });

View file

@ -327,36 +327,42 @@ Zotero.MIME = new function(){
* @param {Zotero.CookieSandbox} [cookieSandbox] * @param {Zotero.CookieSandbox} [cookieSandbox]
* @return {Promise} * @return {Promise}
*/ */
this.getMIMETypeFromURL = function (url, cookieSandbox) { this.getMIMETypeFromURL = async function (url, cookieSandbox) {
return Zotero.HTTP.promise("HEAD", url, { cookieSandbox: cookieSandbox, successCodes: false }) var xmlhttp = await Zotero.HTTP.request(
.then(function (xmlhttp) { "HEAD",
if (xmlhttp.status != 200 && xmlhttp.status != 204) { url,
Zotero.debug("Attachment HEAD request returned with status code " {
+ xmlhttp.status + " in Zotero.MIME.getMIMETypeFromURL()", 2); cookieSandbox,
var mimeType = ''; successCodes: false
} }
else { );
var mimeType = xmlhttp.channel.contentType;
} if (xmlhttp.status != 200 && xmlhttp.status != 204) {
Zotero.debug("Attachment HEAD request returned with status code "
var nsIURL = Components.classes["@mozilla.org/network/standard-url;1"] + xmlhttp.status + " in Zotero.MIME.getMIMETypeFromURL()", 2);
.createInstance(Components.interfaces.nsIURL); var mimeType = '';
nsIURL.spec = url; }
else {
// Override MIME type to application/pdf if extension is .pdf -- var mimeType = xmlhttp.channel.contentType;
// workaround for sites that respond to the HEAD request with an }
// invalid MIME type (https://www.zotero.org/trac/ticket/460)
// var nsIURL = Components.classes["@mozilla.org/network/standard-url;1"]
// Downloaded file is inspected in attachment code and deleted if actually HTML .createInstance(Components.interfaces.nsIURL);
if (nsIURL.fileName.match(/pdf$/) || url.match(/pdf$/)) { nsIURL.spec = url;
mimeType = 'application/pdf';
} // Override MIME type to application/pdf if extension is .pdf --
// workaround for sites that respond to the HEAD request with an
var ext = nsIURL.fileExtension; // invalid MIME type (https://www.zotero.org/trac/ticket/460)
var hasNativeHandler = Zotero.MIME.hasNativeHandler(mimeType, ext); //
// Downloaded file is inspected in attachment code and deleted if actually HTML
return [mimeType, hasNativeHandler]; 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];
} }