From e8d4b3e840627303a2a03a5801a4bf2bd14de319 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Wed, 29 Apr 2015 17:22:31 -0400 Subject: [PATCH] Add Zotero.Item.prototype.attachmentFilename --- chrome/content/zotero/xpcom/data/item.js | 39 ++++++++++++++++++++++++ test/tests/itemTest.js | 32 +++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/chrome/content/zotero/xpcom/data/item.js b/chrome/content/zotero/xpcom/data/item.js index b2888a22f..93a5fe03b 100644 --- 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 index 86848e4b8..534508968 100644 --- 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); + }); + }); });