Adjust attachment-title handling on Rename File from Parent Metadata
Don't change the attachment title on a manual rename from metadata if it doesn't match the current filename, with or without the extension Closes #3220
This commit is contained in:
parent
d95c10e06c
commit
ed2cae9236
2 changed files with 61 additions and 9 deletions
|
@ -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 () {
|
||||
|
|
|
@ -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')
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue