Merge pull request #277 from aurimasv/escape-path
Escape special characters in file paths when attaching files
This commit is contained in:
commit
fcce6327ae
|
@ -43,6 +43,21 @@ Zotero.File = new function(){
|
|||
this.getCharsetFromFile = getCharsetFromFile;
|
||||
this.addCharsetListener = addCharsetListener;
|
||||
|
||||
/**
|
||||
* Encode special characters in file paths that might cause problems,
|
||||
* like # (but preserve slashes or colons)
|
||||
*
|
||||
* @param {String} path File path
|
||||
* @return {String} Encoded file path
|
||||
*/
|
||||
this.encodeFilePath = function(path) {
|
||||
var parts = path.split(/([\\\/:]+)/);
|
||||
// Every other item is the separator
|
||||
for (var i=0, n=parts.length; i<n; i+=2) {
|
||||
parts[i] = encodeURIComponent(parts[i]);
|
||||
}
|
||||
return parts.join('');
|
||||
}
|
||||
|
||||
function getExtension(file){
|
||||
var pos = file.leafName.lastIndexOf('.');
|
||||
|
|
|
@ -291,10 +291,10 @@ Zotero.Translate.ItemSaver.prototype = {
|
|||
var IOService = Components.classes["@mozilla.org/network/io-service;1"].
|
||||
getService(Components.interfaces.nsIIOService);
|
||||
try {
|
||||
var uri = IOService.newURI(path, "", this._baseURI);
|
||||
var uri = IOService.newURI(Zotero.File.encodeFilePath(path), "", this._baseURI);
|
||||
}
|
||||
catch (e) {
|
||||
var msg = "Error parsing attachment path: " + path;
|
||||
var msg = "Error parsing attachment path: " + path + "\n" + e.message;
|
||||
Zotero.logError(msg);
|
||||
Zotero.debug("Translate: " + msg, 2);
|
||||
return false;
|
||||
|
@ -303,14 +303,14 @@ Zotero.Translate.ItemSaver.prototype = {
|
|||
try {
|
||||
var file = uri.QueryInterface(Components.interfaces.nsIFileURL).file;
|
||||
if (file.path == '/') {
|
||||
var msg = "Error parsing attachment path: " + path;
|
||||
var msg = "Error parsing attachment path: " + path + "\nRoot path returned.";
|
||||
Zotero.logError(msg);
|
||||
Zotero.debug("Translate: " + msg, 2);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
catch (e) {
|
||||
var msg = "Error getting file from attachment path: " + path;
|
||||
var msg = "Error getting file from attachment path: " + path + "\n" + e.message;
|
||||
Zotero.logError(msg);
|
||||
Zotero.debug("Translate: " + msg, 2);
|
||||
return false;
|
||||
|
|
Loading…
Reference in New Issue
Block a user