Skip to content
34 changes: 33 additions & 1 deletion src/file-system/controllers/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,39 @@ export const useFileSystemStore = createSelectors(
if (!fileName) return;
}

return get().createFile(dirPath, fileName);
// Split the input path into parts
const parts = fileName.split("/").filter(Boolean);

// Validate input
if (parts.length === 0) {
alert("Invalid file name");
return;
}

// Check for path traversal attempts
if (parts.some((part) => part === ".." || part === ".")) {
alert("Path traversal is not allowed");
return;
}

const finalFileName = parts.pop()!;
let currentPath = dirPath;

// Create intermediate folders if they don't exist
try {
for (const folder of parts) {
currentPath = await get().createDirectory(currentPath, folder);
}

// Finally create the file inside the deepest folder
return await get().createFile(currentPath, finalFileName);
} catch (error) {
console.error("Failed to create nested file:", error);
alert(
`Failed to create file: ${error instanceof Error ? error.message : "Unknown error"}`,
);
return;
}
},

handleCreateNewFolder: async () => {
Expand Down