Replace spaces with underscores in uploaded files

This commit is contained in:
Alex Gleason committed 2025-09-20 19:05:53 -05:00
1 parent 75d4d13737
commit a3c204f44f
2 files changed
+41 -1

No files matched your search

+38
View File
@@ -153,5 +153,43 @@ describe('fileUtils', () => {
const savedContent = await mockFS.readFile('/tmp/test_1.txt', 'utf8');
expect(savedContent).toBe(fileContent);
});
it('should convert spaces to underscores in filename', async () => {
const fileContent = 'Hello, world!';
const arrayBuffer = new TextEncoder().encode(fileContent).buffer;
// Create a mock File object with spaces in the name
const file = {
name: 'my file with spaces.txt',
arrayBuffer: async () => arrayBuffer
} as File;
const savedPath = await saveFileToTmp(mockFS, file);
expect(savedPath).toBe('/tmp/my_file_with_spaces.txt');
// Verify file was actually saved with sanitized name
const savedContent = await mockFS.readFile('/tmp/my_file_with_spaces.txt', 'utf8');
expect(savedContent).toBe(fileContent);
});
it('should convert multiple consecutive spaces to single underscore', async () => {
const fileContent = 'test content';
const arrayBuffer = new TextEncoder().encode(fileContent).buffer;
// Create a mock File object with multiple spaces
const file = {
name: 'file with multiple spaces.txt',
arrayBuffer: async () => arrayBuffer
} as File;
const savedPath = await saveFileToTmp(mockFS, file);
expect(savedPath).toBe('/tmp/file_with_multiple_spaces.txt');
// Verify file was actually saved
const savedContent = await mockFS.readFile('/tmp/file_with_multiple_spaces.txt', 'utf8');
expect(savedContent).toBe(fileContent);
});
});
});
+3 -1
View File
@@ -53,7 +53,9 @@ export async function generateUniqueFilename(
*/
export async function saveFileToTmp(fs: JSRuntimeFS, file: File): Promise<string> {
const tmpDir = '/tmp';
const uniqueFilename = await generateUniqueFilename(fs, tmpDir, file.name);
// Convert spaces to underscores in the filename
const sanitizedFilename = file.name.replace(/\s+/g, '_');
const uniqueFilename = await generateUniqueFilename(fs, tmpDir, sanitizedFilename);
const fullPath = `${tmpDir}/${uniqueFilename}`;
// Read file content as array buffer and convert to Uint8Array