diff --git a/chrome/content/zotero/xpcom/file.js b/chrome/content/zotero/xpcom/file.js index 6583f6275d..e111172bb8 100644 --- a/chrome/content/zotero/xpcom/file.js +++ b/chrome/content/zotero/xpcom/file.js @@ -473,7 +473,8 @@ Zotero.File = new function(){ * @param {Object} [options] * @param {Boolean} [options.overwrite=false] - Overwrite file if one exists * @param {Boolean} [options.unique=false] - Add suffix to create unique filename if necessary - * @return {String|false} - New filename, or false if destination file exists and `overwrite` not set + * @return {String|false} - New filename, or false if destination file exists and `overwrite` + * and `unique` not set */ this.rename = async function (file, newName, options = {}) { var overwrite = options.overwrite || false; @@ -489,6 +490,12 @@ Zotero.File = new function(){ return origName; } + // If only the case changed, we need to overwrite so move() doesn't think the destination + // file already exists + if (origName.toLowerCase() === newName.toLowerCase()) { + overwrite = true; + } + var parentDir = OS.Path.dirname(origPath); var destPath = OS.Path.join(parentDir, newName); var destName = OS.Path.basename(destPath); diff --git a/test/tests/fileTest.js b/test/tests/fileTest.js index ad05a17c1a..db0cf461ce 100644 --- a/test/tests/fileTest.js +++ b/test/tests/fileTest.js @@ -132,6 +132,17 @@ describe("Zotero.File", function () { assert.isTrue(await OS.File.exists(destFile)); }); + // Only relevant on a case-insensitive filesystem + it("should rename a file with a case-only change (Mac)", async function () { + var tmpDir = await getTempDirectory(); + var sourceFile = OS.Path.join(tmpDir, 'a'); + var destFile = OS.Path.join(tmpDir, 'A'); + await Zotero.File.putContentsAsync(sourceFile, 'foo'); + var newFilename = await Zotero.File.rename(sourceFile, 'A'); + assert.equal(newFilename, 'A'); + assert.equal(await Zotero.File.getContentsAsync(destFile), 'foo'); + }); + it("should overwrite an existing file if `overwrite` is true", async function () { var tmpDir = await getTempDirectory(); var sourceFile = OS.Path.join(tmpDir, 'a'); diff --git a/test/tests/itemTest.js b/test/tests/itemTest.js index c56dc38d57..a8a632551a 100644 --- a/test/tests/itemTest.js +++ b/test/tests/itemTest.js @@ -985,7 +985,22 @@ describe("Zotero.Item", function () { // DEBUG: Is this necessary? assert.equal(item.attachmentSyncState, Zotero.Sync.Storage.Local.SYNC_STATE_TO_UPLOAD); assert.isNull(item.attachmentSyncedHash); - }) + }); + + // Only relevant on a case-insensitive filesystem + it("should rename an attached file with a case-only change (Mac)", async function () { + var file = getTestDataDirectory(); + file.append('test.png'); + var item = await Zotero.Attachments.importFromFile({ + file: file + }); + var newName = 'Test.png'; + await item.renameAttachmentFile(newName); + assert.equal(item.attachmentFilename, newName); + var path = await item.getFilePathAsync(); + assert.equal(OS.Path.basename(path), newName) + await OS.File.exists(path); + }); it("should rename a linked file", function* () { var filename = 'test.png';