fix(Read/Write Files from Disk Node): Escape parenthesis when reading file (#11753)

This commit is contained in:
Michael Kret 2024-11-20 15:48:15 +02:00 committed by GitHub
parent 9cc5bc1aef
commit 285534e6d0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 25 additions and 1 deletions

View file

@ -7,7 +7,7 @@ import type {
} from 'n8n-workflow';
import glob from 'fast-glob';
import { errorMapper } from '../helpers/utils';
import { errorMapper, escapeSpecialCharacters } from '../helpers/utils';
import { updateDisplayOptions } from '@utils/utilities';
export const properties: INodeProperties[] = [
@ -82,6 +82,8 @@ export async function execute(this: IExecuteFunctions, items: INodeExecutionData
try {
fileSelector = String(this.getNodeParameter('fileSelector', itemIndex));
fileSelector = escapeSpecialCharacters(fileSelector);
if (/^[a-zA-Z]:/.test(fileSelector)) {
fileSelector = fileSelector.replace(/\\\\/g, '/');
}

View file

@ -30,3 +30,10 @@ export function errorMapper(
return new NodeOperationError(this.getNode(), error, { itemIndex, message, description });
}
export function escapeSpecialCharacters(str: string) {
// Escape parentheses
str = str.replace(/[()]/g, '\\$&');
return str;
}

View file

@ -0,0 +1,15 @@
import { escapeSpecialCharacters } from '../helpers/utils';
describe('Read/Write Files from Disk, escapeSpecialCharacters', () => {
it('should escape parentheses in a string', () => {
const input = '/home/michael/Desktop/test(1).txt';
const expectedOutput = '/home/michael/Desktop/test\\(1\\).txt';
expect(escapeSpecialCharacters(input)).toBe(expectedOutput);
});
it('should not modify strings that do not contain parentheses', () => {
const input = '/home/michael/Desktop/test.txt';
const expectedOutput = '/home/michael/Desktop/test.txt';
expect(escapeSpecialCharacters(input)).toBe(expectedOutput);
});
});