Fix linked attachment base directory handling at drive root

The first letter of the relative path was being removed on save if the
base directory was set to the drive root (e.g. D:\ instead of D:\foo).
This commit is contained in:
Dan Stillman 2017-08-18 16:04:22 +02:00
parent acb990c75d
commit 7386b376f3
2 changed files with 15 additions and 8 deletions

View file

@ -882,14 +882,15 @@ Zotero.Attachments = new function(){
} }
if (Zotero.File.directoryContains(basePath, path)) { if (Zotero.File.directoryContains(basePath, path)) {
basePath = OS.Path.normalize(basePath); // Since stored paths can be synced to other platforms, use forward slashes for consistency.
path = OS.Path.normalize(path); // resolveRelativePath() will convert to the appropriate platform-specific slash on use.
path = this.BASE_PATH_PLACEHOLDER basePath = OS.Path.normalize(basePath).replace(/\\/g, "/");
+ path.substr(basePath.length + 1) path = OS.Path.normalize(path).replace(/\\/g, "/");
// Since stored paths can be synced to other platforms, use // Normalize D:\ vs. D:\foo
// forward slashes for consistency. resolveRelativePath() will if (!basePath.endsWith('/')) {
// convert to the appropriate platform-specific slash on use. basePath += '/';
.replace(/\\/g, "/"); }
path = this.BASE_PATH_PLACEHOLDER + path.substr(basePath.length)
} }
return path; return path;

View file

@ -210,6 +210,12 @@ describe("Zotero.Attachments", function() {
}); });
describe("#getBaseDirectoryRelativePath()", function () { describe("#getBaseDirectoryRelativePath()", function () {
it("should handle base directory at Windows drive root", function () {
Zotero.Prefs.set('baseAttachmentPath', "C:\\");
var path = Zotero.Attachments.getBaseDirectoryRelativePath("C:\\file.txt");
assert.equal(path, Zotero.Attachments.BASE_PATH_PLACEHOLDER + "file.txt");
});
it("should convert backslashes to forward slashes", function () { it("should convert backslashes to forward slashes", function () {
Zotero.Prefs.set('baseAttachmentPath', "C:\\foo\\bar"); Zotero.Prefs.set('baseAttachmentPath', "C:\\foo\\bar");
var path = Zotero.Attachments.getBaseDirectoryRelativePath("C:\\foo\\bar\\test\\file.txt"); var path = Zotero.Attachments.getBaseDirectoryRelativePath("C:\\foo\\bar\\test\\file.txt");