Add Zotero.File.moveToUnique(), replacing Zotero.moveToUnique()
New function takes string paths instead of nsIFile instances
This commit is contained in:
parent
f3a62f5a63
commit
4554998798
3 changed files with 36 additions and 2 deletions
|
@ -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;
|
||||
|
|
Loading…
Add table
Reference in a new issue