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
This commit is contained in:
Dan Stillman 2024-04-28 07:50:04 -04:00
parent 41726fef84
commit 81ed1f6ebb
3 changed files with 20 additions and 1 deletions

View file

@ -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();
}

View file

@ -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, '');

View file

@ -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)