Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [
Expand Down Expand Up @@ -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, '/');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
15 changes: 15 additions & 0 deletions packages/nodes-base/nodes/Files/ReadWriteFile/test/utils.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
Loading