Windows file path fixes

- Fix upgrading of Mozilla-style attachments/storage file paths on upgrade
  (requires re-upgrade)
- Save relative paths using forward slashes for consistency, and convert
  to platform-appropriate slashes on use
This commit is contained in:
Dan Stillman 2016-05-09 02:30:00 -04:00
parent 317b1dfa0f
commit cb8b2bda1b
3 changed files with 28 additions and 10 deletions

View file

@ -874,7 +874,12 @@ Zotero.Attachments = new function(){
if (Zotero.File.directoryContains(basePath, path)) {
basePath = OS.Path.normalize(basePath);
path = OS.Path.normalize(path);
path = this.BASE_PATH_PLACEHOLDER + path.substr(basePath.length + 1)
path = this.BASE_PATH_PLACEHOLDER
+ path.substr(basePath.length + 1)
// Since stored paths can be synced to other platforms, use
// forward slashes for consistency. resolveRelativePath() will
// convert to the appropriate platform-specific slash on use.
.replace(/\\/g, "/");
}
return path;
@ -898,10 +903,15 @@ Zotero.Attachments = new function(){
return false;
}
return OS.Path.join(
return this.fixPathSlashes(OS.Path.join(
OS.Path.normalize(basePath),
path.substr(Zotero.Attachments.BASE_PATH_PLACEHOLDER.length)
);
));
}
this.fixPathSlashes = function (path) {
return path.replace(Zotero.isWin ? /\//g : /\\/g, Zotero.isWin ? "\\" : "/");
}

View file

@ -2274,7 +2274,10 @@ Zotero.Schema = new function(){
var _migrateUserData_80_filePaths = Zotero.Promise.coroutine(function* () {
var rows = yield Zotero.DB.queryAsync("SELECT itemID, libraryID, key, linkMode, path FROM items JOIN itemAttachments USING (itemID) WHERE path != ''");
var tmpDirFile = Zotero.getTempDirectory();
var tmpFilePath = OS.Path.normalize(tmpDirFile.path);
var tmpFilePath = OS.Path.normalize(tmpDirFile.path)
// Since relative paths can be applied on different platforms,
// just use "/" everywhere for oonsistency, and convert on use
.replace(/\\/g, '/');
for (let i = 0; i < rows.length; i++) {
let row = rows[i];
@ -2286,7 +2289,7 @@ Zotero.Schema = new function(){
let relPath = path.substr(prefix.length)
let file = tmpDirFile.clone();
file.setRelativeDescriptor(file, relPath);
path = OS.Path.normalize(file.path);
path = OS.Path.normalize(file.path).replace(/\\/g, '/');
// setRelativeDescriptor() silently uses the parent directory on Windows
// if the filename contains certain characters, so strip them —
@ -2308,12 +2311,9 @@ Zotero.Schema = new function(){
}
}
// Normalize path, and then convert '\' to '/'. As long as normalize() is run on the
// path before use, it doesn't matter which separator it uses, but we might as well
// be consistent.
path = path.replace(/\\/g, '/');
if (!path.startsWith(tmpFilePath)) {
Zotero.logError(path + " does not start with temp path -- not converting relative path for item " + libraryKey);
Zotero.logError(path + " does not start with " + tmpFilePath
+ " -- not converting relative path for item " + libraryKey);
continue;
}
path = prefix + path.substr(tmpFilePath.length + 1);

View file

@ -177,6 +177,14 @@ describe("Zotero.Attachments", function() {
})
})
describe("#getBaseDirectoryRelativePath()", function () {
it("should convert backslashes to forward slashes", function () {
Zotero.Prefs.set('baseAttachmentPath', "C:\\foo\\bar");
var path = Zotero.Attachments.getBaseDirectoryRelativePath("C:\\foo\\bar\\test\\file.txt");
assert.equal(path, Zotero.Attachments.BASE_PATH_PLACEHOLDER + "test/file.txt");
});
});
describe("#getTotalFileSize", function () {
it("should return the size for a single-file attachment", function* () {
var file = getTestDataDirectory();