From 68073b8b32136e438760f5a460fdcf93ee3e3581 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Wed, 4 Apr 2012 04:44:04 -0400 Subject: [PATCH] Properly rename attachments when filenames differ only in case This might have been limited to case-insensitive filesystems. Also fix renaming when the previous filename didn't include an extension. Before, a file named "test" would be renamed from parent with ".test" as the extension. --- chrome/content/zotero/xpcom/data/item.js | 18 ++++++++++++------ chrome/content/zotero/zoteroPane.js | 4 ++-- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/chrome/content/zotero/xpcom/data/item.js b/chrome/content/zotero/xpcom/data/item.js index 64248dfdba..9faf5d97e3 100644 --- a/chrome/content/zotero/xpcom/data/item.js +++ b/chrome/content/zotero/xpcom/data/item.js @@ -2742,15 +2742,21 @@ Zotero.Item.prototype.renameAttachmentFile = function(newName, overwrite) { // Ignore if no change // // Note: Just comparing file.leafName to newName isn't reliable - if (file.leafName == dest.leafName) { + if (file.leafName === dest.leafName) { return true; } - if (overwrite) { - dest.remove(false); - } - else if (dest.exists()) { - return -1; + // If old and new names differ only in case, let's bank on this + // just being a case change and not bother checking for existing + // files, since dest.exists() will just show true on a case-insensitive + // filesystem anyway. + if (file.leafName.toLowerCase() != dest.leafName.toLowerCase()) { + if (overwrite) { + dest.remove(false); + } + else if (dest.exists()) { + return -1; + } } file.moveTo(null, newName); diff --git a/chrome/content/zotero/zoteroPane.js b/chrome/content/zotero/zoteroPane.js index 70bcc46887..da39c09d2f 100644 --- a/chrome/content/zotero/zoteroPane.js +++ b/chrome/content/zotero/zoteroPane.js @@ -3684,9 +3684,9 @@ var ZoteroPane = new function() var parentItemID = item.getSource(); var newName = Zotero.Attachments.getFileBaseNameFromItem(parentItemID); - var ext = file.leafName.match(/[^\.]+$/); + var ext = file.leafName.match(/\.[^\.]+$/); if (ext) { - newName = newName + '.' + ext; + newName = newName + ext; } var renamed = item.renameAttachmentFile(newName);