Asyncify Zotero.Attachments.getTotalFileSize()
This commit is contained in:
parent
2df630e83c
commit
fc1137b769
|
@ -1050,15 +1050,13 @@ Zotero.Attachments = new function(){
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {Zotero.Item} item
|
* @param {Zotero.Item} item
|
||||||
* @param {Boolean} [skipHidden=FALSE] Don't count hidden files
|
* @param {Boolean} [skipHidden=true] - Don't count hidden files
|
||||||
* @return {Integer} Total file size in bytes
|
* @return {Promise<Integer>} - Promise for the total file size in bytes
|
||||||
*/
|
*/
|
||||||
this.getTotalFileSize = function (item, skipHidden) {
|
this.getTotalFileSize = Zotero.Promise.coroutine(function* (item, skipHidden = true) {
|
||||||
var funcName = "Zotero.Attachments.getTotalFileSize()";
|
|
||||||
|
|
||||||
if (!item.isAttachment()) {
|
if (!item.isAttachment()) {
|
||||||
throw ("Item is not an attachment in " + funcName);
|
throw new Error("Item is not an attachment");
|
||||||
}
|
}
|
||||||
|
|
||||||
var linkMode = item.attachmentLinkMode;
|
var linkMode = item.attachmentLinkMode;
|
||||||
|
@ -1069,31 +1067,37 @@ Zotero.Attachments = new function(){
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
throw ("Invalid attachment link mode in " + funcName);
|
throw new Error("Invalid attachment link mode");
|
||||||
}
|
}
|
||||||
|
|
||||||
var file = item.getFile();
|
var path = yield item.getFilePathAsync();
|
||||||
if (!file) {
|
if (!path) {
|
||||||
throw ("File not found in " + funcName);
|
throw new Error("File not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (linkMode == Zotero.Attachments.LINK_MODE_LINKED_FILE) {
|
if (linkMode == Zotero.Attachments.LINK_MODE_LINKED_FILE) {
|
||||||
return item.fileSize;
|
return (yield OS.File.stat(path)).size;
|
||||||
}
|
}
|
||||||
|
|
||||||
var parentDir = file.parent;
|
|
||||||
var files = parentDir.directoryEntries;
|
|
||||||
var size = 0;
|
var size = 0;
|
||||||
while (files.hasMoreElements()) {
|
var parent = OS.Path.dirname(path);
|
||||||
file = files.getNext();
|
let iterator = new OS.File.DirectoryIterator(parent);
|
||||||
file.QueryInterface(Components.interfaces.nsIFile);
|
try {
|
||||||
if (skipHidden && file.leafName.indexOf('.') == 0) {
|
yield iterator.forEach(function (entry) {
|
||||||
continue;
|
if (skipHidden && entry.name.startsWith('.')) {
|
||||||
}
|
return;
|
||||||
size += file.fileSize;
|
}
|
||||||
|
return OS.File.stat(entry.path)
|
||||||
|
.then(function (info) {
|
||||||
|
size += info.size;
|
||||||
|
});
|
||||||
|
})
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
iterator.close();
|
||||||
}
|
}
|
||||||
return size;
|
return size;
|
||||||
}
|
});
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -111,4 +111,18 @@ describe("Zotero.Attachments", function() {
|
||||||
// Should create a group library for use by all tests
|
// Should create a group library for use by all tests
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe("#getTotalFileSize", function () {
|
||||||
|
it("should return the size for a single-file attachment", function* () {
|
||||||
|
var file = getTestDataDirectory();
|
||||||
|
file.append('test.png');
|
||||||
|
|
||||||
|
// Create attachment and compare content
|
||||||
|
var item = yield Zotero.Attachments.importFromFile({
|
||||||
|
file: file
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.equal((yield Zotero.Attachments.getTotalFileSize(item)), file.fileSize);
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue
Block a user