commit 536cd867d729b40e4448bef447f94881d44c193b
parent f96341317004a14a1555e6b04d82885a133089ef
Author: Dan Stillman <dstillman@zotero.org>
Date: Thu, 6 Aug 2015 15:55:37 -0400
Fix toJSON for attachment items
Diffstat:
3 files changed, 56 insertions(+), 6 deletions(-)
diff --git a/chrome/content/zotero/xpcom/attachments.js b/chrome/content/zotero/xpcom/attachments.js
@@ -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}'`);
+ }
}
diff --git a/chrome/content/zotero/xpcom/data/item.js b/chrome/content/zotero/xpcom/data/item.js
@@ -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
diff --git a/test/tests/itemTest.js b/test/tests/itemTest.js
@@ -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 () {