commit e8d4b3e840627303a2a03a5801a4bf2bd14de319
parent 40e86147a4aade87368b68f83c632be8c991d933
Author: Dan Stillman <dstillman@zotero.org>
Date: Wed, 29 Apr 2015 17:22:31 -0400
Add Zotero.Item.prototype.attachmentFilename
Diffstat:
2 files changed, 71 insertions(+), 0 deletions(-)
diff --git a/chrome/content/zotero/xpcom/data/item.js b/chrome/content/zotero/xpcom/data/item.js
@@ -2487,6 +2487,8 @@ Zotero.Item.prototype._updateAttachmentStates = function (exists) {
Zotero.Item.prototype.getFilename = function () {
+ Zotero.debug("getFilename() deprecated -- use .attachmentFilename");
+ return this.attachmentFilename;
if (!this.isAttachment()) {
throw new Error("getFileName() can only be called on attachment items");
}
@@ -2802,6 +2804,43 @@ Zotero.defineProperty(Zotero.Item.prototype, 'attachmentCharset', {
}
});
+
+/**
+ * Get or set the filename of file attachments
+ *
+ * This will return the filename for all file attachments, but the filename can only be set
+ * for stored file attachments. Linked file attachments should be set using .attachmentPath.
+ */
+Zotero.defineProperty(Zotero.Item.prototype, 'attachmentFilename', {
+ get: function () {
+ if (!this.isAttachment()) {
+ return undefined;
+ }
+ var file = this.getFile();
+ if (!file) {
+ return '';
+ }
+ return file.leafName;
+ },
+ set: function (val) {
+ if (!this.isAttachment()) {
+ throw new Error("Attachment filename can only be set for attachment items");
+ }
+ var linkMode = this.attachmentLinkMode;
+ if (linkMode == Zotero.Attachments.LINK_MODE_LINKED_FILE
+ || linkMode == Zotero.Attachments.LINK_MODE_LINKED_URL) {
+ throw new Error("Attachment filename can only be set for stored files");
+ }
+
+ if (!val) {
+ throw new Error("Attachment filename cannot be blank");
+ }
+
+ this.attachmentPath = 'storage:' + val;
+ }
+});
+
+
Zotero.defineProperty(Zotero.Item.prototype, 'attachmentPath', {
get: function() {
if (!this.isAttachment()) {
diff --git a/test/tests/itemTest.js b/test/tests/itemTest.js
@@ -34,4 +34,36 @@ describe("Zotero.Item", function() {
}.bind(this));
});
});
+
+ describe("#attachmentFilename", function () {
+ it("should get and set a filename for a stored file", function* () {
+ var filename = "test.txt";
+
+ // Create parent item
+ var item = new Zotero.Item("book");
+ var parentItemID = yield item.save();
+
+ // Create attachment item
+ var item = new Zotero.Item("attachment");
+ item.attachmentLinkMode = Zotero.Attachments.LINK_MODE_IMPORTED_FILE;
+ item.parentID = parentItemID;
+ var itemID = yield item.save();
+
+ // Should be empty when unset
+ assert.equal(item.attachmentFilename, '');
+
+ // Set filename
+ item.attachmentFilename = filename;
+ yield item.save();
+ item = yield Zotero.Items.getAsync(itemID);
+
+ // Check filename
+ assert.equal(item.attachmentFilename, filename);
+
+ // Check full path
+ var file = Zotero.Attachments.getStorageDirectory(item);
+ file.append(filename);
+ assert.equal(item.getFile().path, file.path);
+ });
+ });
});