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 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]
* @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];
}