Fix toJSON for attachment items

This commit is contained in:
Dan Stillman 2015-08-06 15:55:37 -04:00
parent f963413170
commit 536cd867d7
3 changed files with 56 additions and 6 deletions

View File

@ -1397,4 +1397,29 @@ Zotero.Attachments = new function(){
}
return false;
}
this.linkModeToName = function (linkMode) {
switch (linkMode) {
case this.LINK_MODE_IMPORTED_FILE:
return 'imported_file';
case this.LINK_MODE_IMPORTED_URL:
return 'imported_url';
case this.LINK_MODE_LINKED_FILE:
return 'linked_file';
case this.LINK_MODE_LINKED_URL:
return 'linked_url';
default:
throw new Error(`Invalid link mode ${linkMode}`);
}
}
this.linkModeFromName = function (linkModeName) {
var prop = "LINK_MODE_" + linkModeName.toUpperCase();
if (this[prop] !== undefined) {
return this[prop];
}
throw new Error(`Invalid link mode name '${linkModeName}'`);
}
}

View File

@ -2879,7 +2879,7 @@ Zotero.defineProperty(Zotero.Item.prototype, 'attachmentSyncState', {
*/
Zotero.defineProperty(Zotero.Item.prototype, 'attachmentModificationTime', {
get: Zotero.Promise.coroutine(function* () {
if (!this.isAttachment()) {
if (!this.isFileAttachment()) {
return undefined;
}
@ -2892,7 +2892,7 @@ Zotero.defineProperty(Zotero.Item.prototype, 'attachmentModificationTime', {
return undefined;
}
var fmtime = OS.File.stat(path).lastModificationDate;
var fmtime = ((yield OS.File.stat(path)).lastModificationDate).getTime();
if (fmtime < 1) {
Zotero.debug("File mod time " + fmtime + " is less than 1 -- interpreting as 1", 2);
@ -4039,10 +4039,22 @@ Zotero.Item.prototype.toJSON = Zotero.Promise.coroutine(function* (options = {})
// Attachment fields
if (this.isAttachment()) {
obj.linkMode = this.attachmentLinkMode;
let linkMode = this.attachmentLinkMode;
obj.linkMode = Zotero.Attachments.linkModeToName(linkMode);
obj.contentType = this.attachmentContentType;
obj.charset = this.attachmentCharset;
obj.path = this.attachmentPath;
if (linkMode == Zotero.Attachments.LINK_MODE_LINKED_FILE) {
obj.path = this.attachmentPath;
}
else {
obj.filename = this.attachmentFilename;
}
if (this.isFileAttachment()) {
obj.md5 = this.attachmentHash;
obj.mtime = yield this.attachmentModificationTime;
}
}
// Notes and embedded attachment notes
@ -4051,8 +4063,6 @@ Zotero.Item.prototype.toJSON = Zotero.Promise.coroutine(function* (options = {})
if (note !== "" || mode == 'full' || (mode == 'new' && this.isNote())) {
obj.note = note;
}
// TODO: md5, hash?
}
// Tags

View File

@ -800,6 +800,21 @@ describe("Zotero.Item", function () {
assert.isTrue(json.deleted);
})
})
// TODO: Expand to all fields
it("should handle attachment fields", function* () {
var file = getTestDataDirectory();
file.append('test.png');
var item = yield Zotero.Attachments.importFromFile({
file: file
});
var json = yield item.toJSON();
assert.equal(json.linkMode, 'imported_file');
assert.equal(json.filename, 'test.png');
assert.isUndefined(json.path);
assert.equal(json.md5, '93da8f1e5774c599f0942dcecf64b11c');
assert.typeOf(json.mtime, 'number');
})
})
describe("#fromJSON()", function () {