From 81ed1f6ebb5ece4729fa71c377fcf3a9e9253361 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Sun, 28 Apr 2024 07:50:04 -0400 Subject: [PATCH] Strip line and paragraph separators in filenames And don't fail on existing filenames with these characters in `Item::attachmentFilename` https://forums.zotero.org/discussion/114025/pdf-files-renaming-casuing-syncing-issue --- chrome/content/zotero/xpcom/data/item.js | 5 ++++- chrome/content/zotero/xpcom/file.js | 2 ++ test/tests/itemTest.js | 14 ++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/chrome/content/zotero/xpcom/data/item.js b/chrome/content/zotero/xpcom/data/item.js index 3fd270e8cf..a3f3d94859 100644 --- a/chrome/content/zotero/xpcom/data/item.js +++ b/chrome/content/zotero/xpcom/data/item.js @@ -3186,7 +3186,10 @@ Zotero.defineProperty(Zotero.Item.prototype, 'attachmentFilename', { if (!path) { return ''; } - var prefixedPath = path.match(/^(?:attachments|storage):(.*)$/); + // Include /s (DOTALL) to handle \u2028 (line separator) and \u2029 (paragraph separator), + // which we're now stripping in File.getValidFileName() but didn't previously + // https://forums.zotero.org/discussion/114025/pdf-files-renaming-casuing-syncing-issue + var prefixedPath = path.match(/^(?:attachments|storage):(.*)$/s); if (prefixedPath) { return prefixedPath[1].split('/').pop(); } diff --git a/chrome/content/zotero/xpcom/file.js b/chrome/content/zotero/xpcom/file.js index bb787c3450..607bf25e22 100644 --- a/chrome/content/zotero/xpcom/file.js +++ b/chrome/content/zotero/xpcom/file.js @@ -1290,6 +1290,8 @@ Zotero.File = new function(){ fileName = fileName.replace(/[\u2000-\u200A]/g, ' '); // Replace zero-width spaces fileName = fileName.replace(/[\u200B-\u200E]/g, ''); + // Replace line and paragraph separators + fileName = fileName.replace(/[\u2028-\u2029]/g, ' '); if (!skipXML) { // Strip characters not valid in XML, since they won't sync and they're probably unwanted fileName = fileName.replace(/[\u0000-\u0008\u000b\u000c\u000e-\u001f\ud800-\udfff\ufffe\uffff]/g, ''); diff --git a/test/tests/itemTest.js b/test/tests/itemTest.js index 7dfa4a84f8..aa48c3a6cb 100644 --- a/test/tests/itemTest.js +++ b/test/tests/itemTest.js @@ -1069,6 +1069,20 @@ describe("Zotero.Item", function () { assert.equal(item.getFilePath(), file.path); }); + it("should handle line and paragraph separators in filenames", async function () { + var filename = "Line 1\u2028Line 2\u2029Line 3.txt"; + + var item = await createDataObject('item'); + + var attachment = new Zotero.Item("attachment"); + attachment.attachmentLinkMode = Zotero.Attachments.LINK_MODE_IMPORTED_FILE; + attachment.parentID = item.id; + attachment.attachmentFilename = filename; + await attachment.saveTx(); + + assert.equal(attachment.attachmentFilename, filename); + }); + it("should get a filename for a base-dir-relative file", function () { var dir = getTestDataDirectory().path; Zotero.Prefs.set('saveRelativeAttachmentPath', true)