Add Zotero.Item.prototype.attachmentFilename
This commit is contained in:
parent
40e86147a4
commit
e8d4b3e840
|
@ -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()) {
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue
Block a user