Skip to content

Commit

Permalink
Merge pull request #2 from flexpilot-ai/bugfix/sanitize-fs-path
Browse files Browse the repository at this point in the history
bug-fix: sanitize path for windows web
  • Loading branch information
mohankumarelec authored Feb 1, 2025
2 parents e9d9588 + 5afe418 commit 0158566
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 22 deletions.
1 change: 1 addition & 0 deletions extensions/flexpilot/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 42 additions & 22 deletions extensions/flexpilot/src/fs-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ interface GitTree {
// Define the UID for the real and dummy files
enum FileUID { Real = 1, Dummy = 2 }

const sanitizePath = (fsPath: string) => fsPath.replace(/\\/g, '/');

/**
* Implementation of the file system provider for the GitHub repository
*/
Expand Down Expand Up @@ -157,13 +159,16 @@ export class GitHubFileSystemProvider implements vscode.FileSystemProvider, vsco
}

stat(uri: vscode.Uri): vscode.FileStat | Thenable<vscode.FileStat> {
// Get the sanitized path for windows path style
const fsPath = sanitizePath(uri.fsPath);

// Check if the file exists in the file system
if (!fs.existsSync(uri.fsPath)) {
if (!fs.existsSync(fsPath)) {
throw vscode.FileSystemError.FileNotFound(uri);
}

// Get the file stats and return the file stat object
const stat = fs.statSync(uri.fsPath);
const stat = fs.statSync(fsPath);
const fileStat: vscode.FileStat = {
ctime: stat.ctimeMs,
size: stat.size,
Expand All @@ -174,8 +179,11 @@ export class GitHubFileSystemProvider implements vscode.FileSystemProvider, vsco
}

readDirectory(uri: vscode.Uri): [string, vscode.FileType][] | Thenable<[string, vscode.FileType][]> {
// Get the sanitized path for windows path style
const fsPath = sanitizePath(uri.fsPath);

// Check if the directory exists in the file system
if (!fs.existsSync(uri.fsPath)) {
if (!fs.existsSync(fsPath)) {
throw vscode.FileSystemError.FileNotFound(uri);
}

Expand All @@ -199,42 +207,51 @@ export class GitHubFileSystemProvider implements vscode.FileSystemProvider, vsco
}

createDirectory(uri: vscode.Uri): void | Thenable<void> {
// Get the sanitized path for windows path style
const fsPath = sanitizePath(uri.fsPath);

// Create the directory if it does not exist in the file system
if (!fs.existsSync(uri.fsPath)) {
fs.mkdirSync(uri.fsPath, { recursive: true });
if (!fs.existsSync(fsPath)) {
fs.mkdirSync(fsPath, { recursive: true });
}

// Emit the file change event
this.emitter.fire([{ type: vscode.FileChangeType.Created, uri }]);
}

async readFile(uri: vscode.Uri): Promise<Uint8Array> {
// Get the sanitized path for windows path style
const fsPath = sanitizePath(uri.fsPath);

// Check if the file exists in the file system
if (!fs.existsSync(uri.fsPath)) {
if (!fs.existsSync(fsPath)) {
throw vscode.FileSystemError.FileNotFound(uri);
}

// Fetch the file from the GitHub API if it is not a real file
if (fs.statSync(uri.fsPath).uid !== FileUID.Real) {
if (fs.statSync(fsPath).uid !== FileUID.Real) {
const originalUrl = `https://raw.githubusercontent.com${uri.path}`;
const originalStats = fs.statSync(uri.fsPath);
const originalStats = fs.statSync(fsPath);
const response = await fetch(corsEnableUrl(originalUrl));
fs.writeFileSync(uri.fsPath, Buffer.from(await response.arrayBuffer()));
fs.chownSync(uri.fsPath, FileUID.Real, FileUID.Real);
fs.utimesSync(uri.fsPath, originalStats.atime, originalStats.mtime);
fs.writeFileSync(fsPath, Buffer.from(await response.arrayBuffer()));
fs.chownSync(fsPath, FileUID.Real, FileUID.Real);
fs.utimesSync(fsPath, originalStats.atime, originalStats.mtime);
}

// Read the file content and return it as a Uint8Array
const content = fs.readFileSync(uri.fsPath, { encoding: 'buffer' });
const content = fs.readFileSync(fsPath, { encoding: 'buffer' });
if (content instanceof Buffer) {
return new Uint8Array(content.buffer);
}
throw vscode.FileSystemError.Unavailable(uri);
}

writeFile(uri: vscode.Uri, content: Uint8Array, options: { create: boolean; overwrite: boolean }): void | Thenable<void> {
// Get the sanitized path for windows path style
const fsPath = sanitizePath(uri.fsPath);

// Check if the file exists in the file system
const exists = fs.existsSync(uri.fsPath);
const exists = fs.existsSync(fsPath);

// Check if the file can be created or overwritten
if (!exists && !options.create) {
Expand All @@ -244,8 +261,8 @@ export class GitHubFileSystemProvider implements vscode.FileSystemProvider, vsco
}

// Write the file content to the file system
fs.writeFileSync(uri.fsPath, content);
fs.chownSync(uri.fsPath, FileUID.Real, FileUID.Real);
fs.writeFileSync(fsPath, content);
fs.chownSync(fsPath, FileUID.Real, FileUID.Real);

// Emit the file change event based on the file status
if (!exists) {
Expand All @@ -256,17 +273,20 @@ export class GitHubFileSystemProvider implements vscode.FileSystemProvider, vsco
}

delete(uri: vscode.Uri, options: { recursive: boolean }): void | Thenable<void> {
// Get the sanitized path for windows path style
const fsPath = sanitizePath(uri.fsPath);

// Check if the file exists in the file system
if (!fs.existsSync(uri.fsPath)) {
if (!fs.existsSync(fsPath)) {
throw vscode.FileSystemError.FileNotFound(uri);
}

// Delete the file or directory based on the recursive option
const stat = fs.statSync(uri.fsPath);
const stat = fs.statSync(fsPath);
if (stat.isDirectory()) {
fs.rmdirSync(uri.fsPath, { recursive: !!options.recursive });
fs.rmdirSync(fsPath, { recursive: !!options.recursive });
} else {
fs.unlinkSync(uri.fsPath);
fs.unlinkSync(fsPath);
}

// Emit the file change event based on the file status
Expand All @@ -275,17 +295,17 @@ export class GitHubFileSystemProvider implements vscode.FileSystemProvider, vsco

rename(oldUri: vscode.Uri, newUri: vscode.Uri, options: { overwrite: boolean }): void | Thenable<void> {
// Check if the old file exists in the file system
const exists = fs.existsSync(newUri.fsPath);
const exists = fs.existsSync(sanitizePath(newUri.fsPath));

// Rename the file if it exists and overwrite is enabled
if (exists && !options.overwrite) {
throw vscode.FileSystemError.FileExists(newUri);
} else if (!fs.existsSync(oldUri.fsPath)) {
} else if (!fs.existsSync(sanitizePath(oldUri.fsPath))) {
throw vscode.FileSystemError.FileNotFound(oldUri);
}

// Rename the file in the file system
fs.renameSync(oldUri.fsPath, newUri.fsPath);
fs.renameSync(sanitizePath(oldUri.fsPath), sanitizePath(newUri.fsPath));

// Emit the file change event based on the file status
this.emitter.fire([
Expand Down

0 comments on commit 0158566

Please sign in to comment.