Repository: zotero/zotero (JavaScript)

Issue:
Don't rename files during Retrieve Metadata/Create Parent Item if renaming not enabled for file type
Following up on https://github.com/zotero/zotero/pull/4474#issuecomment-2263338123:

> If the file is stored, or linked file renaming is enabled, regardless of the file's content type (I don't totally understand why Retrieve Metadata works that way, but it seems intentional), the file is renamed and the attachment gets an automatic title.

Re: the parenthetical, do you mean that we rename the file even if renaming for the file type is unchecked? I don't think that's actually intentional, and it seems wrong. If you disable renaming for a given file type, it seems like you have your own system or whatever and we shouldn't touch it.

Right now I'm also seeing a weird behavior where the file is renamed in all cases but the default title is only set when renaming is enabled for the type:

1. Have a recognizable standalone PDF
2. Title it "foo" and rename it to "bar.pdf"
3. Disable renaming for PDFs
4. Retrieve Metadata

The file is renamed based on the metadata, and the title is set to the filename minus the extension. The former seems wrong, and the latter is definitely wrong. I think both the filename and the title should be left as is in this case?

And same for "foo" and filename "foo.pdf". I think there's a stronger case there for still setting "PDF", but I think if people are opting out of renaming, they just want to do their own thing, and they may want to see their artisanally crafted filenames in the items list.

If PDF renaming is enabled, the file is renamed but the title is set to "PDF". These are correct.



Fix (patch):
diff --git a/chrome/content/zotero/xpcom/attachments.js b/chrome/content/zotero/xpcom/attachments.js
index 5e9d1c9f9dc..855f410ca0f 100644
--- a/chrome/content/zotero/xpcom/attachments.js
+++ b/chrome/content/zotero/xpcom/attachments.js
@@ -2503,6 +2503,12 @@ Zotero.Attachments = new function () {
 	};
 	
 	
+	this.shouldAutoRenameAttachment = function (attachment) {
+		return Zotero.Attachments.shouldAutoRenameFile(attachment.attachmentLinkMode == Zotero.Attachments.LINK_MODE_LINKED_FILE)
+			&& Zotero.Attachments.isRenameAllowedForType(attachment.attachmentContentType);
+	};
+	
+	
 	this.getRenamedFileBaseNameIfAllowedType = async function (parentItem, file) {
 		var contentType = file.endsWith('.pdf')
 			// Don't bother reading file if there's a .pdf extension
diff --git a/chrome/content/zotero/xpcom/data/item.js b/chrome/content/zotero/xpcom/data/item.js
index 3b84c6ec44e..55d65c481c4 100644
--- a/chrome/content/zotero/xpcom/data/item.js
+++ b/chrome/content/zotero/xpcom/data/item.js
@@ -3907,9 +3907,7 @@ Zotero.Item.prototype.setAutoAttachmentTitle = function ({ ignoreAutoRenamePrefs
 	// file is being renamed, give it a default title ("PDF", "Webpage", etc.)
 	let isFirstOfType = this.parentItemID
 		&& this.parentItem.numFileAttachmentsWithContentType(this.attachmentContentType) <= 1;
-	let isBeingRenamed = ignoreAutoRenamePrefs
-		|| (Zotero.Attachments.shouldAutoRenameFile(this.isLinkedFileAttachment())
-			&& Zotero.Attachments.isRenameAllowedForType(this.attachmentContentType));
+	let isBeingRenamed = ignoreAutoRenamePrefs || Zotero.Attachments.shouldAutoRenameAttachment(this);
 	if (isFirstOfType && isBeingRenamed) {
 		let defaultTitle = this._getDefaultTitleForAttachmentContentType();
 		if (defaultTitle !== null) {
diff --git a/chrome/content/zotero/xpcom/recognizeDocument.js b/chrome/content/zotero/xpcom/recognizeDocument.js
index a15b37b71da..e950da1ee83 100644
--- a/chrome/content/zotero/xpcom/recognizeDocument.js
+++ b/chrome/content/zotero/xpcom/recognizeDocument.js
@@ -292,7 +292,7 @@ Zotero.RecognizeDocument = new function () {
 		var originalFilename = PathUtils.filename(path);
 		
 		// Rename attachment file to match new metadata
-		if (Zotero.Attachments.shouldAutoRenameFile(attachment.attachmentLinkMode == Zotero.Attachments.LINK_MODE_LINKED_FILE)) {
+		if (Zotero.Attachments.shouldAutoRenameAttachment(attachment)) {
 			let ext = Zotero.File.getExtension(path);
 			let fileBaseName = Zotero.Attachments.getFileBaseNameFromItem(parentItem, { attachmentTitle: originalTitle });
 			let newName = fileBaseName + (ext ? '.' + ext : '');
@@ -300,11 +300,9 @@ Zotero.RecognizeDocument = new function () {
 			if (result !== true) {
 				throw new Error("Error renaming " + path);
 			}
+			attachment.setAutoAttachmentTitle({ ignoreAutoRenamePrefs: true });
+			await attachment.saveTx();
 		}
-		
-		// Rename attachment title
-		attachment.setAutoAttachmentTitle();
-		await attachment.saveTx();
 
 		try {
 			let win = Zotero.getMainWindow();
diff --git a/chrome/content/zotero/zoteroPane.js b/chrome/content/zotero/zoteroPane.js
index 590e149663f..428d555e299 100644
--- a/chrome/content/zotero/zoteroPane.js
+++ b/chrome/content/zotero/zoteroPane.js
@@ -5343,7 +5343,7 @@ var ZoteroPane = new function()
 		}
 
 		for (let item of items) {
-			if (Zotero.Attachments.shouldAutoRenameFile(item.attachmentLinkMode == Zotero.Attachments.LINK_MODE_LINKED_FILE)) {
+			if (Zotero.Attachments.shouldAutoRenameAttachment(item)) {
 				let path = item.getFilePath();
 				if (!path) {
 					Zotero.debug('No path for attachment ' + item.key);