Skip to content

Commit ae4d58d

Browse files
committed
Implement rename and copy
1 parent 06f3e91 commit ae4d58d

File tree

3 files changed

+65
-80
lines changed

3 files changed

+65
-80
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,7 @@
9090
},
9191
"jupyterlab": {
9292
"extension": true,
93-
"outputDir": "jupyterlab_filesystem_access/labextension",
94-
"schemaDir": "schema"
93+
"outputDir": "jupyterlab_filesystem_access/labextension"
9594
},
9695
"jupyter-releaser": {
9796
"hooks": {

schema/plugin.json

Lines changed: 0 additions & 67 deletions
This file was deleted.

src/drive.ts

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ export class FileSystemDrive implements Contents.IDrive {
7272
path: '',
7373
created: new Date().toISOString(),
7474
last_modified: new Date().toISOString(),
75-
format: 'json',
75+
format: null,
76+
mimetype: '',
7677
content: null,
7778
writable: true,
7879
type: 'directory',
79-
mimetype: 'application/json'
8080
};
8181
}
8282

@@ -109,11 +109,11 @@ export class FileSystemDrive implements Contents.IDrive {
109109
path: PathExt.join(parentPath, localPath, value.name),
110110
created: '',
111111
last_modified: '',
112-
format: 'json',
112+
format: null,
113+
mimetype: '',
113114
content: null,
114115
writable: true,
115116
type: 'directory',
116-
mimetype: 'application/json'
117117
});
118118
}
119119
}
@@ -123,8 +123,8 @@ export class FileSystemDrive implements Contents.IDrive {
123123
path: PathExt.join(parentPath, localPath),
124124
last_modified: '',
125125
created: '',
126-
format: 'json',
127-
mimetype: 'application/json',
126+
format: null,
127+
mimetype: '',
128128
content,
129129
size: undefined,
130130
writable: true,
@@ -197,8 +197,26 @@ export class FileSystemDrive implements Contents.IDrive {
197197
});
198198
}
199199

200-
rename(oldPath: string, newPath: string): Promise<Contents.IModel> {
201-
throw new Error('Method not implemented.');
200+
async rename(oldPath: string, newPath: string): Promise<Contents.IModel> {
201+
// Best effort, we are lacking proper APIs for renaming
202+
const toCopy = await this.get(oldPath);
203+
const newName = PathExt.basename(newPath);
204+
205+
const copy: Partial<Contents.IModel> = {
206+
name: newName,
207+
path: newPath,
208+
content: toCopy.content,
209+
format: toCopy.format,
210+
mimetype: toCopy.mimetype,
211+
type: toCopy.type,
212+
writable: toCopy.writable,
213+
};
214+
215+
await this.save(newPath, copy);
216+
217+
await this.delete(oldPath);
218+
219+
return this.get(newPath);
202220
}
203221

204222
async save(
@@ -207,7 +225,13 @@ export class FileSystemDrive implements Contents.IDrive {
207225
): Promise<Contents.IModel> {
208226
const parentHandle = await this.getParentHandle(path);
209227

210-
const handle = await parentHandle.getFileHandle(PathExt.basename(path));
228+
if (options?.type === 'directory') {
229+
await parentHandle.getDirectoryHandle(PathExt.basename(path), { create: true });
230+
231+
return this.get(path);
232+
}
233+
234+
const handle = await parentHandle.getFileHandle(PathExt.basename(path), { create: true });
211235
const writable = await handle.createWritable({});
212236

213237
const format = options?.format;
@@ -223,8 +247,37 @@ export class FileSystemDrive implements Contents.IDrive {
223247
return this.get(path);
224248
}
225249

226-
copy(path: string, toLocalDir: string): Promise<Contents.IModel> {
227-
throw new Error('Method not implemented.');
250+
async copy(path: string, toLocalDir: string): Promise<Contents.IModel> {
251+
// Best effort, we are lacking proper APIs for copying
252+
const toCopy = await this.get(path);
253+
const parentPath = PathExt.dirname(path);
254+
255+
let newName = toCopy.name;
256+
if (parentPath === toLocalDir) {
257+
const ext = PathExt.extname(toCopy.name);
258+
259+
if (ext) {
260+
newName = `${newName.slice(0, newName.length - ext.length)} (Copy)${ext}`;
261+
} else {
262+
newName = `${newName} (Copy)`;
263+
}
264+
}
265+
266+
const newPath = PathExt.join(toLocalDir, newName);
267+
268+
const copy: Partial<Contents.IModel> = {
269+
name: newName,
270+
path: newPath,
271+
content: toCopy.content,
272+
format: toCopy.format,
273+
mimetype: toCopy.mimetype,
274+
type: toCopy.type,
275+
writable: toCopy.writable,
276+
};
277+
278+
await this.save(newPath, copy);
279+
280+
return this.get(newPath);
228281
}
229282

230283
async createCheckpoint(path: string): Promise<Contents.ICheckpointModel> {

0 commit comments

Comments
 (0)