Improve attachment saving with regard to filenames
- Added a wrapper around nsIMIMEService.getPrimaryExtension() that works a bit more sanely (doesn't throw an error on failure, enforce a couple hard-coded extensions (PDF and HTML) even on Linux (where the MIME service one isn't implemented)) - Use the extension we got back from our improved getPrimaryExtension(), even if the filename already had one - If no filename, use last part of path before restorting to hostname
This commit is contained in:
parent
6fbebfae28
commit
ee9cf59e26
|
@ -400,16 +400,6 @@ Zotero.Attachments = new function(){
|
|||
|
||||
var fileName = _getFileNameFromURL(url, mimeType);
|
||||
|
||||
// This is a hack to make sure the file is opened in the browser when
|
||||
// we use loadURI(), since Firefox's internal detection mechanisms seem
|
||||
// to sometimes get confused
|
||||
// (see #192, https://chnm.gmu.edu/trac/zotero/ticket/192)
|
||||
if (mimeType=='text/html' &&
|
||||
(fileName.substr(fileName.length-5)!='.html'
|
||||
&& fileName.substr(fileName.length-4)!='.htm')){
|
||||
fileName += '.html';
|
||||
}
|
||||
|
||||
file.append(fileName);
|
||||
|
||||
wbp.progressListener = new Zotero.WebProgressFinishListener(function(){
|
||||
|
@ -509,21 +499,32 @@ Zotero.Attachments = new function(){
|
|||
.createInstance(Components.interfaces.nsIURL);
|
||||
nsIURL.spec = url;
|
||||
|
||||
if (nsIURL.fileName){
|
||||
return nsIURL.fileName;
|
||||
}
|
||||
var ext = Zotero.MIME.getPrimaryExtension(mimeType, nsIURL.fileExtension);
|
||||
|
||||
if (mimeType){
|
||||
try {
|
||||
var ext = Components.classes["@mozilla.org/mime;1"]
|
||||
.getService(Components.interfaces.nsIMIMEService)
|
||||
.getPrimaryExtension(mimeType, nsIURL.fileExtension);
|
||||
if (!nsIURL.fileName) {
|
||||
var matches = nsIURL.directory.match(/\/([^\/]+)\/$/);
|
||||
// If no filename, use the last part of the path if there is one
|
||||
if (matches) {
|
||||
nsIURL.fileName = matches[1];
|
||||
}
|
||||
// Or just use the host
|
||||
else {
|
||||
nsIURL.fileName = nsIURL.host;
|
||||
var tld = nsIURL.fileExtension;
|
||||
}
|
||||
// getPrimaryExtension doesn't work on Linux
|
||||
catch (e) {}
|
||||
}
|
||||
|
||||
return nsIURL.host + (ext ? '.' + ext : '');
|
||||
// If we found a better extension, use that
|
||||
if (ext && (!nsIURL.fileExtension || nsIURL.fileExtension != ext)) {
|
||||
nsIURL.fileExtension = ext;
|
||||
}
|
||||
|
||||
// If we replaced the TLD (which would've been interpreted as the extension), add it back
|
||||
if (tld && tld != nsIURL.fileExtension) {
|
||||
nsIURL.fileBaseName = nsIURL.fileBaseName + '.' + tld;
|
||||
}
|
||||
|
||||
return nsIURL.fileName;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -23,6 +23,7 @@
|
|||
Zotero.MIME = new function(){
|
||||
this.isTextType = isTextType;
|
||||
this.isExternalTextExtension = isExternalTextExtension;
|
||||
this.getPrimaryExtension = getPrimaryExtension;
|
||||
this.sniffForMIMEType = sniffForMIMEType;
|
||||
this.sniffForBinary = sniffForBinary;
|
||||
this.getMIMETypeFromData = getMIMETypeFromData;
|
||||
|
@ -82,6 +83,32 @@ Zotero.MIME = new function(){
|
|||
}
|
||||
|
||||
|
||||
/*
|
||||
* Our own wrapper around the MIME service's getPrimaryExtension() that
|
||||
* works a little better
|
||||
*/
|
||||
function getPrimaryExtension(mimeType, ext) {
|
||||
// Enforce some extensions
|
||||
switch (mimeType) {
|
||||
case 'text/html':
|
||||
return 'html';
|
||||
case 'application/pdf':
|
||||
return 'pdf';
|
||||
}
|
||||
|
||||
try {
|
||||
ext = Components.classes["@mozilla.org/mime;1"]
|
||||
.getService(Components.interfaces.nsIMIMEService)
|
||||
.getPrimaryExtension(mimeType, ext);
|
||||
}
|
||||
// nsIMIMEService.getPrimaryExtension() doesn't work on Linux and
|
||||
// throws an error if it can't find an extension
|
||||
catch (e) {}
|
||||
|
||||
return ext ? ext : '';
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Searches string for magic numbers
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue
Block a user