Fix 'false' filename after case-only rename on Macs
Zotero.File.move() now forces `overwrite` if the old and new filenames differ only by case, since otherwise on a case-insensitive filesystem OS.File.move() does an existence check and thinks that the target file exists.
This commit is contained in:
parent
595ba396ef
commit
81e2bee452
3 changed files with 35 additions and 2 deletions
|
@ -473,7 +473,8 @@ Zotero.File = new function(){
|
||||||
* @param {Object} [options]
|
* @param {Object} [options]
|
||||||
* @param {Boolean} [options.overwrite=false] - Overwrite file if one exists
|
* @param {Boolean} [options.overwrite=false] - Overwrite file if one exists
|
||||||
* @param {Boolean} [options.unique=false] - Add suffix to create unique filename if necessary
|
* @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 = {}) {
|
this.rename = async function (file, newName, options = {}) {
|
||||||
var overwrite = options.overwrite || false;
|
var overwrite = options.overwrite || false;
|
||||||
|
@ -489,6 +490,12 @@ Zotero.File = new function(){
|
||||||
return origName;
|
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 parentDir = OS.Path.dirname(origPath);
|
||||||
var destPath = OS.Path.join(parentDir, newName);
|
var destPath = OS.Path.join(parentDir, newName);
|
||||||
var destName = OS.Path.basename(destPath);
|
var destName = OS.Path.basename(destPath);
|
||||||
|
|
|
@ -132,6 +132,17 @@ describe("Zotero.File", function () {
|
||||||
assert.isTrue(await OS.File.exists(destFile));
|
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 () {
|
it("should overwrite an existing file if `overwrite` is true", async function () {
|
||||||
var tmpDir = await getTempDirectory();
|
var tmpDir = await getTempDirectory();
|
||||||
var sourceFile = OS.Path.join(tmpDir, 'a');
|
var sourceFile = OS.Path.join(tmpDir, 'a');
|
||||||
|
|
|
@ -985,7 +985,22 @@ describe("Zotero.Item", function () {
|
||||||
// DEBUG: Is this necessary?
|
// DEBUG: Is this necessary?
|
||||||
assert.equal(item.attachmentSyncState, Zotero.Sync.Storage.Local.SYNC_STATE_TO_UPLOAD);
|
assert.equal(item.attachmentSyncState, Zotero.Sync.Storage.Local.SYNC_STATE_TO_UPLOAD);
|
||||||
assert.isNull(item.attachmentSyncedHash);
|
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* () {
|
it("should rename a linked file", function* () {
|
||||||
var filename = 'test.png';
|
var filename = 'test.png';
|
||||||
|
|
Loading…
Add table
Reference in a new issue