From ecaa599bec32e999dbbbdef1a433295955856629 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Sun, 13 Dec 2009 04:14:31 +0000 Subject: [PATCH] - Renaming an attachment file with certain extended characters could cause it to be deleted - When relinking a file, automatically rename file with cross-platform invalid characters (e.g., "/") filtered out --- .../content/zotero/bindings/attachmentbox.xml | 6 ++++- chrome/content/zotero/xpcom/data/item.js | 25 +++++++++++++++---- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/chrome/content/zotero/bindings/attachmentbox.xml b/chrome/content/zotero/bindings/attachmentbox.xml index 757385a740..dd8c67e8c5 100644 --- a/chrome/content/zotero/bindings/attachmentbox.xml +++ b/chrome/content/zotero/bindings/attachmentbox.xml @@ -409,7 +409,11 @@ if (checkState.value) { var renamed = item.renameAttachmentFile(newTitle.value); if (renamed == -1) { - var confirmed = confirm(newTitle.value + ' exists. Overwrite existing file?'); + var confirmed = nsIPS.confirm( + window, + '', + newTitle.value + ' exists. Overwrite existing file?' + ); if (confirmed) { item.renameAttachmentFile(newTitle.value, true); break; diff --git a/chrome/content/zotero/xpcom/data/item.js b/chrome/content/zotero/xpcom/data/item.js index b84ab9e370..5baadfaa19 100644 --- a/chrome/content/zotero/xpcom/data/item.js +++ b/chrome/content/zotero/xpcom/data/item.js @@ -2592,15 +2592,18 @@ Zotero.Item.prototype.renameAttachmentFile = function(newName, overwrite) { try { newName = Zotero.File.getValidFileName(newName); - if (file.leafName == newName) { - return true; - } - var dest = file.parent; dest.append(newName); + // Ignore if no change + // + // Note: Just comparing file.leafName to newName isn't reliable + if (file.leafName == dest.leafName) { + return true; + } + if (overwrite) { - dest.remove(null); + dest.remove(false); } else if (dest.exists()) { return -1; @@ -2635,9 +2638,21 @@ Zotero.Item.prototype.relinkAttachmentFile = function(file) { throw('Cannot relink linked URL in Zotero.Items.relinkAttachmentFile()'); } + var newName = Zotero.File.getValidFileName(file.leafName); + if (!newName) { + throw ("No valid characters in filename after filtering in Zotero.Item.relinkAttachmentFile()"); + } + + // Rename file to filtered name if necessary + if (file.leafName != newName) { + Zotero.debug("Renaming file '" + file.leafName + "' to '" + newName + "'"); + file.moveTo(null, newName); + } + var path = Zotero.Attachments.getPath(file, linkMode); this.attachmentPath = path; this.save(); + return false; }