Fix revealing parent directory of missing file

Updates Zotero.File.getClosestDirectory() to take a path rather than an
nsIFile
This commit is contained in:
Dan Stillman 2017-07-21 18:33:36 -04:00
parent b033dedddc
commit 6d2c72fb54
3 changed files with 48 additions and 13 deletions

View File

@ -32,7 +32,6 @@ Zotero.File = new function(){
Components.utils.import("resource://gre/modules/FileUtils.jsm"); Components.utils.import("resource://gre/modules/FileUtils.jsm");
this.getExtension = getExtension; this.getExtension = getExtension;
this.getClosestDirectory = getClosestDirectory;
this.getContentsFromURL = getContentsFromURL; this.getContentsFromURL = getContentsFromURL;
this.putContents = putContents; this.putContents = putContents;
this.getValidFileName = getValidFileName; this.getValidFileName = getValidFileName;
@ -88,21 +87,31 @@ Zotero.File = new function(){
} }
/* /**
* Traverses up the filesystem from a file until it finds an existing * Traverses up the filesystem from a file until it finds an existing
* directory, or false if it hits the root * directory, or false if it hits the root
*/ */
function getClosestDirectory(file) { this.getClosestDirectory = async function (file) {
var dir = file.parent; try {
let stat = await OS.File.stat(file);
while (dir && !dir.exists()) { // If file is an existing directory, return it
var dir = dir.parent; if (stat.isDir) {
return file;
}
}
catch (e) {
if (e.becauseNoSuchFile) {}
else {
throw e;
}
} }
if (dir && dir.exists()) { var dir = OS.Path.dirname(file);
return dir; while (dir && !await OS.File.exists(dir)) {
dir = OS.Path.dirname(dir);
} }
return false;
return dir || false;
} }

View File

@ -4569,10 +4569,10 @@ var ZoteroPane = new function()
Zotero.debug("Invalid path", 2); Zotero.debug("Invalid path", 2);
break; break;
} }
var dir = Zotero.File.getClosestDirectory(file);
var dir = yield Zotero.File.getClosestDirectory(file);
if (dir) { if (dir) {
dir.QueryInterface(Components.interfaces.nsILocalFile); fp.displayDirectory = Zotero.File.pathToFile(dir);
fp.displayDirectory = dir;
} }
fp.appendFilters(Components.interfaces.nsIFilePicker.filterAll); fp.appendFilters(Components.interfaces.nsIFilePicker.filterAll);

View File

@ -96,6 +96,32 @@ describe("Zotero.File", function () {
}); });
}); });
describe("#getClosestDirectory()", function () {
it("should return directory for file that exists", function* () {
var tmpDir = yield getTempDirectory();
var closest = yield Zotero.File.getClosestDirectory(tmpDir);
assert.equal(closest, tmpDir);
});
it("should return parent directory for missing file", function* () {
var tmpDir = yield getTempDirectory();
var closest = yield Zotero.File.getClosestDirectory(OS.Path.join(tmpDir, 'a'));
assert.equal(closest, tmpDir);
});
it("should find an existing directory three levels up from a missing file", function* () {
var tmpDir = yield getTempDirectory();
var closest = yield Zotero.File.getClosestDirectory(OS.Path.join(tmpDir, 'a', 'b', 'c'));
assert.equal(closest, tmpDir);
});
it("should return false for a path that doesn't exist at all", function* () {
assert.isFalse(yield Zotero.File.getClosestDirectory('/a/b/c'));
});
});
describe("#copyDirectory()", function () { describe("#copyDirectory()", function () {
it("should copy all files within a directory", function* () { it("should copy all files within a directory", function* () {
var tmpDir = Zotero.getTempDirectory().path; var tmpDir = Zotero.getTempDirectory().path;