www

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

commit dbd36a935ff7c765ccff264e9d735b99a320a40e
parent ecb8cb140d16651440a3a14fc952051d501c9145
Author: Simon Kornblith <simon@simonster.com>
Date:   Thu, 18 Apr 2013 09:45:40 -0700

Merge pull request #304 from simonster/file-fixes

Try to parse paths as paths in addition to as URIs
Diffstat:
Mchrome/content/zotero/xpcom/translation/translate_item.js | 106+++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------
1 file changed, 77 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,93 @@ 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]); } + 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, replacing backslashes with slashes + if((file = this._parseRelativePath(path.replace(/\\/g, "/")))) { + Zotero.debug("Translate: Got file "+path+" as relative path"); + return file; + } + + // Next, try to parse as relative path, without replacing backslashes with slashes + 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; },