Skip to content

Commit 7425a64

Browse files
committed
Clean given path by removing drive prefix
1 parent 3943209 commit 7425a64

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

src/drive.ts

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import { PathExt } from '@jupyterlab/coreutils';
44

55
import { ISignal, Signal } from '@lumino/signaling';
66

7+
export const DRIVE_NAME = 'FileSystem';
8+
const DRIVE_PREFIX = `${DRIVE_NAME}:`;
9+
710
function arrayBufferToBase64(buffer: ArrayBuffer): string {
811
let binary = '';
912
const bytes = new Uint8Array(buffer);
@@ -41,7 +44,7 @@ export class FileSystemDrive implements Contents.IDrive {
4144
}
4245

4346
get name(): string {
44-
return 'FileSystem';
47+
return DRIVE_NAME;
4548
}
4649

4750
get serverSettings(): ServerConnection.ISettings {
@@ -64,6 +67,8 @@ export class FileSystemDrive implements Contents.IDrive {
6467
path: string,
6568
options?: Contents.IFetchOptions
6669
): Promise<Contents.IModel> {
70+
path = this.cleanPath(path);
71+
6772
const root = this._rootHandle;
6873

6974
if (!root) {
@@ -140,16 +145,20 @@ export class FileSystemDrive implements Contents.IDrive {
140145
async newUntitled(
141146
options?: Contents.ICreateOptions
142147
): Promise<Contents.IModel> {
148+
let parentPath = '';
149+
if (options && options.path) {
150+
parentPath = this.cleanPath(options.path);
151+
}
152+
143153
const type = options?.type || 'directory';
144154
const path = PathExt.join(
145-
options?.path || '',
155+
parentPath,
146156
type === 'directory' ? 'Untitled Folder' : 'untitled'
147157
);
148158
const ext = options?.ext || 'txt';
149159

150160
const parentHandle = await this.getParentHandle(path);
151161

152-
const parentPath = PathExt.dirname(path);
153162
let localPath = PathExt.basename(path);
154163
const name = localPath;
155164

@@ -186,6 +195,8 @@ export class FileSystemDrive implements Contents.IDrive {
186195
}
187196

188197
async delete(path: string): Promise<void> {
198+
path = this.cleanPath(path);
199+
189200
const parentHandle = await this.getParentHandle(path);
190201

191202
await parentHandle.removeEntry(PathExt.basename(path), { recursive: true });
@@ -199,6 +210,9 @@ export class FileSystemDrive implements Contents.IDrive {
199210

200211
async rename(oldPath: string, newPath: string): Promise<Contents.IModel> {
201212
// Best effort, we are lacking proper APIs for renaming
213+
oldPath = this.cleanPath(oldPath);
214+
newPath = this.cleanPath(newPath);
215+
202216
await this.doCopy(oldPath, newPath);
203217

204218
await this.delete(oldPath);
@@ -210,6 +224,8 @@ export class FileSystemDrive implements Contents.IDrive {
210224
path: string,
211225
options?: Partial<Contents.IModel>
212226
): Promise<Contents.IModel> {
227+
path = this.cleanPath(path);
228+
213229
const parentHandle = await this.getParentHandle(path);
214230

215231
if (options?.type === 'directory') {
@@ -240,6 +256,8 @@ export class FileSystemDrive implements Contents.IDrive {
240256

241257
async copy(path: string, toLocalDir: string): Promise<Contents.IModel> {
242258
// Best effort, we are lacking proper APIs for copying
259+
path = this.cleanPath(path);
260+
243261
const toCopy = await this.get(path);
244262
const parentPath = PathExt.dirname(path);
245263

@@ -373,7 +391,7 @@ export class FileSystemDrive implements Contents.IDrive {
373391
}
374392

375393
private async doCopy(oldPath: string, newPath: string): Promise<void> {
376-
// Best effort, we are lacking proper APIs for copying
394+
// Best effort, we are lacking proper APIs for copying
377395
const oldParentHandle = await this.getParentHandle(oldPath);
378396

379397
const oldLocalPath = PathExt.basename(oldPath);
@@ -414,6 +432,13 @@ export class FileSystemDrive implements Contents.IDrive {
414432
}
415433
}
416434

435+
private cleanPath(path: string): string {
436+
if (path.includes(DRIVE_PREFIX)) {
437+
return path.replace(DRIVE_PREFIX, '');
438+
}
439+
return path;
440+
}
441+
417442
private _isDisposed = false;
418443
private _fileChanged = new Signal<Contents.IDrive, Contents.IChangedArgs>(
419444
this

0 commit comments

Comments
 (0)