diff --git a/chrome/content/zotero/xpcom/attachments.js b/chrome/content/zotero/xpcom/attachments.js index 4d1c03d5f..798e4a90b 100644 --- a/chrome/content/zotero/xpcom/attachments.js +++ b/chrome/content/zotero/xpcom/attachments.js @@ -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 ? "\\" : "/"); } diff --git a/chrome/content/zotero/xpcom/schema.js b/chrome/content/zotero/xpcom/schema.js index 046d7d3c7..50adb1abc 100644 --- a/chrome/content/zotero/xpcom/schema.js +++ b/chrome/content/zotero/xpcom/schema.js @@ -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); diff --git a/test/tests/attachmentsTest.js b/test/tests/attachmentsTest.js index 6e5735327..255451809 100644 --- a/test/tests/attachmentsTest.js +++ b/test/tests/attachmentsTest.js @@ -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();