From 60cb299f40189af7b585fee34a6c59e4b2d5a7ab Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Fri, 25 Dec 2020 03:01:37 -0500 Subject: [PATCH] Fix uploading of embedded-image attachments --- chrome/content/zotero/xpcom/attachments.js | 3 +++ chrome/content/zotero/xpcom/data/item.js | 6 ++--- .../zotero/xpcom/storage/storageLocal.js | 8 +++--- test/tests/itemTest.js | 27 +++++++++++++++++++ 4 files changed, 37 insertions(+), 7 deletions(-) diff --git a/chrome/content/zotero/xpcom/attachments.js b/chrome/content/zotero/xpcom/attachments.js index 9cbdae1b8a..9129251c3f 100644 --- a/chrome/content/zotero/xpcom/attachments.js +++ b/chrome/content/zotero/xpcom/attachments.js @@ -2291,6 +2291,9 @@ Zotero.Attachments = new function(){ case Zotero.Attachments.LINK_MODE_IMPORTED_FILE: break; + case Zotero.Attachments.LINK_MODE_EMBEDDED_IMAGE: + return false; + default: throw new Error("Invalid attachment link mode"); } diff --git a/chrome/content/zotero/xpcom/data/item.js b/chrome/content/zotero/xpcom/data/item.js index f74db64889..4256d45386 100644 --- a/chrome/content/zotero/xpcom/data/item.js +++ b/chrome/content/zotero/xpcom/data/item.js @@ -2319,8 +2319,7 @@ Zotero.Item.prototype.getFilePath = function () { } // Imported file with relative path - if (linkMode == Zotero.Attachments.LINK_MODE_IMPORTED_URL || - linkMode == Zotero.Attachments.LINK_MODE_IMPORTED_FILE) { + if (this.isStoredFileAttachment()) { if (!path.includes("storage:")) { Zotero.logError("Invalid attachment path '" + path + "'"); this._updateAttachmentStates(false); @@ -3035,8 +3034,7 @@ Zotero.defineProperty(Zotero.Item.prototype, 'attachmentPath', { val = Zotero.Attachments.resolveRelativePath(val) || val; } } - else if (linkMode == Zotero.Attachments.LINK_MODE_IMPORTED_URL || - linkMode == Zotero.Attachments.LINK_MODE_IMPORTED_FILE) { + else if (this.isStoredFileAttachment()) { if (val && !val.startsWith('storage:')) { let storagePath = Zotero.Attachments.getStorageDirectory(this).path; if (!val.startsWith(storagePath)) { diff --git a/chrome/content/zotero/xpcom/storage/storageLocal.js b/chrome/content/zotero/xpcom/storage/storageLocal.js index 9350006f9b..cf084196cb 100644 --- a/chrome/content/zotero/xpcom/storage/storageLocal.js +++ b/chrome/content/zotero/xpcom/storage/storageLocal.js @@ -502,13 +502,14 @@ Zotero.Sync.Storage.Local = { */ getFilesToUpload: function (libraryID) { var sql = "SELECT itemID FROM itemAttachments JOIN items USING (itemID) " - + "WHERE libraryID=? AND syncState IN (?,?) AND linkMode IN (?,?)"; + + "WHERE libraryID=? AND syncState IN (?,?) AND linkMode IN (?,?,?)"; var params = [ libraryID, this.SYNC_STATE_TO_UPLOAD, this.SYNC_STATE_FORCE_UPLOAD, Zotero.Attachments.LINK_MODE_IMPORTED_FILE, - Zotero.Attachments.LINK_MODE_IMPORTED_URL + Zotero.Attachments.LINK_MODE_IMPORTED_URL, + Zotero.Attachments.LINK_MODE_EMBEDDED_IMAGE, ]; return Zotero.DB.columnQueryAsync(sql, params); }, @@ -566,12 +567,13 @@ Zotero.Sync.Storage.Local = { return Zotero.DB.executeTransaction(async function () { var sql = "SELECT itemID FROM items JOIN itemAttachments USING (itemID) " - + "WHERE libraryID=? AND itemTypeID=? AND linkMode IN (?, ?)"; + + "WHERE libraryID=? AND itemTypeID=? AND linkMode IN (?, ?, ?)"; var params = [ libraryID, Zotero.ItemTypes.getID('attachment'), Zotero.Attachments.LINK_MODE_IMPORTED_FILE, Zotero.Attachments.LINK_MODE_IMPORTED_URL, + Zotero.Attachments.LINK_MODE_EMBEDDED_IMAGE, ]; var itemIDs = await Zotero.DB.columnQueryAsync(sql, params); for (let itemID of itemIDs) { diff --git a/test/tests/itemTest.js b/test/tests/itemTest.js index a802bc7d4a..be9f01f746 100644 --- a/test/tests/itemTest.js +++ b/test/tests/itemTest.js @@ -854,6 +854,33 @@ describe("Zotero.Item", function () { }); }) + + describe("#getFilePath()", function () { + it("should return the absolute path for an embedded image", async function () { + var note = await createDataObject('item', { itemType: 'note' }); + + var path = OS.Path.join(getTestDataDirectory().path, 'test.png'); + var imageData = await Zotero.File.getBinaryContentsAsync(path); + var array = new Uint8Array(imageData.length); + for (let i = 0; i < imageData.length; i++) { + array[i] = imageData.charCodeAt(i); + } + + var blob = new Blob([array], { type: 'image/png' }); + var attachment = await Zotero.Attachments.importEmbeddedImage({ + blob, + parentItemID: note.id + }); + + var storageDir = Zotero.getStorageDirectory().path; + assert.equal( + OS.Path.join(storageDir, attachment.key, 'image.png'), + attachment.getFilePath() + ); + }); + }); + + describe("#attachmentCharset", function () { it("should get and set a value", function* () { var charset = 'utf-8';