Showing with 38 additions and 4 deletions.
  1. +2 −2 chrome/content/zotero/xpcom/db.js
  2. +19 −0 chrome/content/zotero/xpcom/file.js
  3. +2 −2 chrome/content/zotero/xpcom/zotero.js
  4. +15 −0 test/tests/fileTest.js
@@ -1217,7 +1217,7 @@ Zotero.DBConnection.prototype._getConnectionAsync = async function (options) {
// Save damaged filed
this._debug('Saving damaged DB file with .damaged extension', 1);
let damagedFile = this._dbPath + '.damaged';
Zotero.moveToUnique(file, damagedFile);
await Zotero.File.moveToUnique(file, damagedFile);

// Create new main database
this._connection = store.openDatabase(file);
@@ -1237,7 +1237,7 @@ Zotero.DBConnection.prototype._getConnectionAsync = async function (options) {
// Save damaged file
this._debug('Saving damaged DB file with .damaged extension', 1);
let damagedFile = this._dbPath + '.damaged';
Zotero.moveToUnique(file, damagedFile);
await Zotero.File.moveToUnique(file, damagedFile);

// Test the backup file
try {
@@ -894,6 +894,25 @@ Zotero.File = new function(){
}


/**
* @param {String} file
* @param {String} newFile
* @return {String} - Path of new file
*/
this.moveToUnique = async function (file, newFile) {
var targetDir = OS.Path.dirname(newFile);

var newNSIFile = this.pathToFile(newFile);
newNSIFile.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0o644);
var newName = newNSIFile.leafName;
newNSIFile.remove(null);

newFile = OS.Path.join(targetDir, newName);
await OS.File.move(file, newFile);
return newFile;
}


this.copyToUnique = function (file, newFile) {
file = this.pathToFile(file);
newFile = this.pathToFile(newFile);
@@ -49,7 +49,6 @@ Services.scriptloader.loadSubScript("resource://zotero/polyfill.js");
this.flattenArguments = flattenArguments;
this.getAncestorByTagName = getAncestorByTagName;
this.randomString = randomString;
this.moveToUnique = moveToUnique;
this.reinit = reinit; // defined in zotero-service.js

// Public properties
@@ -1711,7 +1710,8 @@ Services.scriptloader.loadSubScript("resource://zotero/polyfill.js");
}


function moveToUnique(file, newFile){
this.moveToUnique = function (file, newFile) {
Zotero.debug("Zotero.moveToUnique() is deprecated -- use Zotero.File.moveToUnique()", 2);
newFile.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0o644);
var newName = newFile.leafName;
newFile.remove(null);
@@ -158,6 +158,21 @@ describe("Zotero.File", function () {
});


describe("#moveToUnique", function () {
it("should move a file to a unique filename", async function () {
var tmpDir = Zotero.getTempDirectory().path;
var sourceFile = OS.Path.join(tmpDir, "1");
var tmpTargetDir = OS.Path.join(tmpDir, "targetDirectory")
var targetFile = OS.Path.join(tmpTargetDir, "file.txt");
await OS.File.makeDir(tmpTargetDir);
await Zotero.File.putContentsAsync(sourceFile, "");
await Zotero.File.putContentsAsync(targetFile, "");
var newFile = await Zotero.File.moveToUnique(sourceFile, targetFile);
assert.equal(OS.Path.join(tmpTargetDir, 'file-1.txt'), newFile);
});
});


describe("#copyDirectory()", function () {
it("should copy all files within a directory", function* () {
var tmpDir = Zotero.getTempDirectory().path;