diff --git a/chrome/content/zotero/zoteroPane.js b/chrome/content/zotero/zoteroPane.js index 8a503baa4d..d0a35563b1 100644 --- a/chrome/content/zotero/zoteroPane.js +++ b/chrome/content/zotero/zoteroPane.js @@ -5633,7 +5633,7 @@ var ZoteroPane = new function() }; - this.renameSelectedAttachmentsFromParents = Zotero.Promise.coroutine(function* () { + this.renameSelectedAttachmentsFromParents = async function () { // TEMP: fix if (!this.canEdit()) { @@ -5650,32 +5650,39 @@ var ZoteroPane = new function() throw('Item ' + itemID + ' is not a child file attachment in ZoteroPane_Local.renameAttachmentFromParent()'); } - var file = item.getFile(); + var file = await item.getFilePathAsync(); if (!file) { continue; } let parentItemID = item.parentItemID; - let parentItem = yield Zotero.Items.getAsync(parentItemID); + let parentItem = await Zotero.Items.getAsync(parentItemID); var newName = Zotero.Attachments.getFileBaseNameFromItem(parentItem); - var ext = file.leafName.match(/\.[^\.]+$/); + let extRE = /\.[^\.]+$/; + let origFilename = file.split("/").pop(); + let ext = origFilename.match(extRE); if (ext) { - newName = newName + ext; + newName = newName + ext[0]; } + let origFilenameNoExt = origFilename.replace(extRE, '') - var renamed = yield item.renameAttachmentFile(newName, false, true); + var renamed = await item.renameAttachmentFile(newName, false, true); if (renamed !== true) { Zotero.debug("Could not rename file (" + renamed + ")"); continue; } - item.setField('title', newName); - yield item.saveTx(); + // If the attachment title matched the filename, change it now + let origTitle = item.getField('title'); + if (origTitle == origFilename || origTitle == origFilenameNoExt) { + item.setField('title', newName); + await item.saveTx(); + } } return true; - }); + }; this.convertLinkedFilesToStoredFiles = async function () { diff --git a/test/tests/zoteroPaneTest.js b/test/tests/zoteroPaneTest.js index 47091c3622..fdc70b1e41 100644 --- a/test/tests/zoteroPaneTest.js +++ b/test/tests/zoteroPaneTest.js @@ -614,6 +614,51 @@ describe("ZoteroPane", function() { assert.equal(OS.Path.basename(path), uniqueFilename) await OS.File.exists(path); }); + + it("shouldn't change attachment title if different from filename", async function () { + var item = createUnsavedDataObject('item'); + item.setField('title', 'Title'); + await item.saveTx(); + + var attachment = await importFileAttachment('test.png', { parentItemID: item.id }); + attachment.setField('title', 'Image'); + await attachment.saveTx(); + await zp.selectItem(attachment.id); + + assert.isTrue(await zp.renameSelectedAttachmentsFromParents()); + assert.equal(attachment.attachmentFilename, 'Title.png'); + assert.equal(attachment.getField('title'), 'Image') + }); + + it("should change attachment title if the same as filename", async function () { + var item = createUnsavedDataObject('item'); + item.setField('title', 'Title'); + await item.saveTx(); + + var attachment = await importFileAttachment('test.png', { parentItemID: item.id }); + attachment.setField('title', 'test.png'); + await attachment.saveTx(); + await zp.selectItem(attachment.id); + + assert.isTrue(await zp.renameSelectedAttachmentsFromParents()); + assert.equal(attachment.attachmentFilename, 'Title.png'); + assert.equal(attachment.getField('title'), 'Title.png') + }); + + it("should change attachment title if the same as filename without extension", async function () { + var item = createUnsavedDataObject('item'); + item.setField('title', 'Title'); + await item.saveTx(); + + var attachment = await importFileAttachment('test.png', { parentItemID: item.id }); + attachment.setField('title', 'test'); + await attachment.saveTx(); + await zp.selectItem(attachment.id); + + assert.isTrue(await zp.renameSelectedAttachmentsFromParents()); + assert.equal(attachment.attachmentFilename, 'Title.png'); + assert.equal(attachment.getField('title'), 'Title.png') + }); });