From cbf4876173974398ee921a4739210e4159d092ab Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Fri, 7 Aug 2015 15:36:46 -0400 Subject: [PATCH] Fix attachment renaming Fixes #822 --- .../content/zotero/bindings/attachmentbox.xml | 30 ++++++++++++++- chrome/content/zotero/xpcom/data/item.js | 23 ++++++------ chrome/content/zotero/xpcom/storage.js | 37 +++++++++---------- chrome/content/zotero/zoteroPane.js | 2 +- test/tests/itemPaneTest.js | 16 ++++++++ test/tests/itemTest.js | 22 +++++++++++ 6 files changed, 96 insertions(+), 34 deletions(-) diff --git a/chrome/content/zotero/bindings/attachmentbox.xml b/chrome/content/zotero/bindings/attachmentbox.xml index dceb08079..6d98e7a5f 100644 --- a/chrome/content/zotero/bindings/attachmentbox.xml +++ b/chrome/content/zotero/bindings/attachmentbox.xml @@ -135,7 +135,35 @@ - + + + + + + + + + + + + + + + + } - File hash, null if never synced, if false if + * file doesn't exist */ - this.getSyncedHash = function (itemID) { + this.getSyncedHash = Zotero.Promise.coroutine(function* (itemID) { var sql = "SELECT storageHash FROM itemAttachments WHERE itemID=?"; - var hash = Zotero.DB.valueQuery(sql, itemID); + var hash = yield Zotero.DB.valueQueryAsync(sql, itemID); if (hash === false) { - throw "Item " + itemID + " not found in " - + "Zotero.Sync.Storage.getSyncedHash()"; + throw new Error("Item " + itemID + " not found"); } return hash; - } + }) /** @@ -592,24 +591,22 @@ Zotero.Sync.Storage = new function () { * @param {Boolean} [updateItem=FALSE] Update dateModified field of * attachment item */ - this.setSyncedHash = function (itemID, hash, updateItem) { + this.setSyncedHash = Zotero.Promise.coroutine(function* (itemID, hash, updateItem) { if (hash !== null && hash.length != 32) { throw ("Invalid file hash '" + hash + "' in Zotero.Storage.setSyncedHash()"); } - Zotero.DB.beginTransaction(); + Zotero.DB.requireTransaction(); var sql = "UPDATE itemAttachments SET storageHash=? WHERE itemID=?"; - Zotero.DB.valueQuery(sql, [hash, itemID]); + yield Zotero.DB.queryAsync(sql, [hash, itemID]); if (updateItem) { // Update item date modified so the new mod time will be synced var sql = "UPDATE items SET clientDateModified=? WHERE itemID=?"; - Zotero.DB.query(sql, [Zotero.DB.transactionDateTime, itemID]); + yield Zotero.DB.queryAsync(sql, [Zotero.DB.transactionDateTime, itemID]); } - - Zotero.DB.commitTransaction(); - } + }); /** diff --git a/chrome/content/zotero/zoteroPane.js b/chrome/content/zotero/zoteroPane.js index 14ad0a520..d3e3997d5 100644 --- a/chrome/content/zotero/zoteroPane.js +++ b/chrome/content/zotero/zoteroPane.js @@ -4104,7 +4104,7 @@ var ZoteroPane = new function() newName = newName + ext; } - var renamed = item.renameAttachmentFile(newName); + var renamed = yield item.renameAttachmentFile(newName); if (renamed !== true) { Zotero.debug("Could not rename file (" + renamed + ")"); continue; diff --git a/test/tests/itemPaneTest.js b/test/tests/itemPaneTest.js index f7260b6e6..6ac2e4b62 100644 --- a/test/tests/itemPaneTest.js +++ b/test/tests/itemPaneTest.js @@ -29,6 +29,22 @@ describe("Item pane", function () { }) }) + describe("Attachment pane", function () { + it("should refresh on file rename", function* () { + var file = getTestDataDirectory(); + file.append('test.png'); + var item = yield Zotero.Attachments.importFromFile({ + file: file + }); + var newName = 'test2.png'; + yield item.renameAttachmentFile(newName); + + var itemBox = doc.getElementById('zotero-attachment-box'); + var label = itemBox._id('fileName'); + assert.equal(label.value, newName); + }) + }) + describe("Note pane", function () { it("should refresh on note update", function* () { var item = new Zotero.Item('note'); diff --git a/test/tests/itemTest.js b/test/tests/itemTest.js index a8ba7354d..3401e0fc3 100644 --- a/test/tests/itemTest.js +++ b/test/tests/itemTest.js @@ -539,6 +539,28 @@ describe("Zotero.Item", function () { }); }) + describe("#renameAttachmentFile()", function () { + it("should rename an attached file", function* () { + var file = getTestDataDirectory(); + file.append('test.png'); + var item = yield Zotero.Attachments.importFromFile({ + file: file + }); + var newName = 'test2.png'; + yield item.renameAttachmentFile(newName); + assert.equal(item.attachmentFilename, newName); + var path = yield item.getFilePathAsync(); + assert.equal(OS.Path.basename(path), newName) + yield OS.File.exists(path); + + assert.equal( + (yield Zotero.Sync.Storage.getSyncState(item.id)), + Zotero.Sync.Storage.SYNC_STATE_TO_UPLOAD + ); + assert.isNull(yield Zotero.Sync.Storage.getSyncedHash(item.id)); + }) + }) + describe("#setTags", function () { it("should save an array of tags in API JSON format", function* () { var tags = [