Async Zotero.File.copyDirectory()
This commit is contained in:
parent
e1355cef2f
commit
5e1c25f4b5
3 changed files with 53 additions and 14 deletions
|
@ -1128,9 +1128,9 @@ Zotero.Attachments = new function(){
|
||||||
|
|
||||||
// Copy over files if they exist
|
// Copy over files if they exist
|
||||||
if (newAttachment.isImportedAttachment() && attachment.getFile()) {
|
if (newAttachment.isImportedAttachment() && attachment.getFile()) {
|
||||||
var dir = Zotero.Attachments.getStorageDirectory(attachment);
|
let dir = Zotero.Attachments.getStorageDirectory(attachment);
|
||||||
var newDir = yield Zotero.Attachments.createDirectoryForItem(newAttachment);
|
let newDir = yield Zotero.Attachments.createDirectoryForItem(newAttachment);
|
||||||
Zotero.File.copyDirectory(dir, newDir);
|
yield Zotero.File.copyDirectory(dir, newDir);
|
||||||
}
|
}
|
||||||
|
|
||||||
yield newAttachment.addLinkedItem(attachment);
|
yield newAttachment.addLinkedItem(attachment);
|
||||||
|
|
|
@ -583,18 +583,26 @@ Zotero.File = new function(){
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Copies all files from dir into newDir
|
* Copies all files from dir into newDir
|
||||||
|
*
|
||||||
|
* @param {String|nsIFile} source - Source directory
|
||||||
|
* @param {String|nsIFile} target - Target directory
|
||||||
*/
|
*/
|
||||||
this.copyDirectory = function (dir, newDir) {
|
this.copyDirectory = Zotero.Promise.coroutine(function* (source, target) {
|
||||||
if (!dir.exists()) {
|
if (source instanceof Ci.nsIFile) source = source.path;
|
||||||
throw ("Directory doesn't exist in Zotero.File.copyDirectory()");
|
if (target instanceof Ci.nsIFile) target = target.path;
|
||||||
}
|
|
||||||
var otherFiles = dir.directoryEntries;
|
yield OS.File.makeDir(target, {
|
||||||
while (otherFiles.hasMoreElements()) {
|
ignoreExisting: true,
|
||||||
var file = otherFiles.getNext();
|
unixMode: 0o755
|
||||||
file.QueryInterface(Components.interfaces.nsIFile);
|
});
|
||||||
file.copyTo(newDir, null);
|
|
||||||
}
|
return this.iterateDirectory(source, function* (iterator) {
|
||||||
}
|
while (true) {
|
||||||
|
let entry = yield iterator.next();
|
||||||
|
yield OS.File.copy(entry.path, OS.Path.join(target, entry.name));
|
||||||
|
}
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
this.createDirectoryIfMissing = function (dir) {
|
this.createDirectoryIfMissing = function (dir) {
|
||||||
|
|
31
test/tests/fileTest.js
Normal file
31
test/tests/fileTest.js
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
describe("Zotero.File", function () {
|
||||||
|
describe("#copyDirectory()", function () {
|
||||||
|
it("should copy all files within a directory", function* () {
|
||||||
|
var tmpDir = Zotero.getTempDirectory().path;
|
||||||
|
var tmpCopyDir = OS.Path.join(tmpDir, "copyDirectory")
|
||||||
|
var source = OS.Path.join(tmpCopyDir, "1");
|
||||||
|
var target = OS.Path.join(tmpCopyDir, "2");
|
||||||
|
yield OS.File.makeDir(source, {
|
||||||
|
from: tmpDir
|
||||||
|
});
|
||||||
|
|
||||||
|
yield Zotero.File.putContentsAsync(OS.Path.join(source, "A"), "Test 1");
|
||||||
|
yield Zotero.File.putContentsAsync(OS.Path.join(source, "B"), "Test 2");
|
||||||
|
|
||||||
|
yield OS.File.removeDir(target, {
|
||||||
|
ignoreAbsent: true
|
||||||
|
});
|
||||||
|
|
||||||
|
yield Zotero.File.copyDirectory(source, target);
|
||||||
|
|
||||||
|
assert.equal(
|
||||||
|
(yield Zotero.File.getContentsAsync(OS.Path.join(target, "A"))),
|
||||||
|
"Test 1"
|
||||||
|
);
|
||||||
|
assert.equal(
|
||||||
|
(yield Zotero.File.getContentsAsync(OS.Path.join(target, "B"))),
|
||||||
|
"Test 2"
|
||||||
|
);
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
Loading…
Add table
Add a link
Reference in a new issue