www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | Submodules | README | LICENSE

commit 61300e4dd750d72381346bb3aace3d29809ae97b
parent b8ce002df3d328621b2ff15ecdfc24b6528ebe09
Author: Simon Kornblith <simon@simonster.com>
Date:   Thu, 18 Apr 2013 02:30:16 -0400

Try to parse paths as paths in addition to URIs

Diffstat:
Mchrome/content/zotero/xpcom/translation/translate_item.js | 101++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------------
1 file changed, 72 insertions(+), 29 deletions(-)

diff --git a/chrome/content/zotero/xpcom/translation/translate_item.js b/chrome/content/zotero/xpcom/translation/translate_item.js @@ -285,45 +285,88 @@ Zotero.Translate.ItemSaver.prototype = { return newItem; }, - - "_parsePath":function(path) { - // generate nsIFile - var IOService = Components.classes["@mozilla.org/network/io-service;1"]. - getService(Components.interfaces.nsIIOService); + + "_parsePathURI":function(path) { try { - var uri = IOService.newURI(Zotero.File.encodeFilePath(path), "", this._baseURI); + var uri = Services.io.newURI(path, "", this._baseURI); + var file = uri.QueryInterface(Components.interfaces.nsIFileURL).file; + if(file.path != '/' && file.exists()) return file; } catch (e) { - var msg = "Error parsing attachment path: " + path + "\n" + e.message; - Zotero.logError(msg); - Zotero.debug("Translate: " + msg, 2); - return false; + Zotero.logError(e); } - + return false; + }, + + "_parseAbsolutePath":function(path) { try { - var file = uri.QueryInterface(Components.interfaces.nsIFileURL).file; - if (file.path == '/') { - var msg = "Error parsing attachment path: " + path + "\nRoot path returned."; - Zotero.logError(msg); - Zotero.debug("Translate: " + msg, 2); - return false; + // First, try to parse absolute paths using initWithPath + var file = Components.classes["@mozilla.org/file/local;1"]. + createInstance(Components.interfaces.nsILocalFile); + file.initWithPath(path); + if(file.exists()) return file; + } catch(e) { + Zotero.logError(e); + } + return false; + }, + + "_parseRelativePath":function(path) { + try { + var file = this._baseURI.QueryInterface(Components.interfaces.nsIFileURL).file.parent; + var splitPath = path.split(/[\/\\]/g); + for(var i=0; i<splitPath.length; i++) { + if(splitPath[i] !== "") file.append(splitPath[i]); } + Zotero.debug("Testing "+file.path); + if(file.exists()) return file; + } catch(e) { + Zotero.logError(e); } - catch (e) { - var msg = "Error getting file from attachment path: " + path + "\n" + e.message; - Zotero.logError(msg); - Zotero.debug("Translate: " + msg, 2); - return false; + return false; + }, + + "_parsePath":function(path) { + var file; + + // First, try to parse as absolute path + if(((/[a-zA-Z]:\\/.test(path) && Zotero.isWin) || (path[0] === "/" && !Zotero.isWin)) + && (file = this._parseAbsolutePath(path))) { + Zotero.debug("Translate: Got file "+path+" as absolute path"); + return file; } - - if(file.exists()) { + + // Next, try to parse as URI + if((file = this._parsePathURI(path))) { + Zotero.debug("Translate: Got "+path+" as URI") return file; - } else if(path[0] !== "/" && path.substr(0, 5).toLowerCase() !== "file:") { - // This looks like a relative path, but it might actually be an absolute path, because - // some people are not quite there. - var newFile = this._parsePath("/"+path); - if(newFile && newFile.exists()) return newFile; + } else if(path.substr(0, 7) !== "file://") { + // If it was a fully qualified file URI, we can give up now + + // Next, try to parse as relative path + if((file = this._parseRelativePath(path))) { + Zotero.debug("Translate: Got file "+path+" as relative path"); + return file; + } + + if(path[0] !== "/") { + // Next, try to parse a path with no / as an absolute URI or path + if((file = this._parsePathURI("/"+path))) { + Zotero.debug("Translate: Got file "+path+" as broken URI"); + return file; + } + + if((file = this._parseAbsolutePath("/"+path))) { + Zotero.debug("Translate: Got file "+path+" as broken absolute path"); + return file; + } + + } } + + // Give up + Zotero.debug("Translate: Could not find file "+path) + return false; },