Skip to content

Commit 01d4b60

Browse files
committed
implement save file request and connect to frontend
1 parent 111c795 commit 01d4b60

File tree

2 files changed

+49
-14
lines changed

2 files changed

+49
-14
lines changed

src/contents.ts

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Signal, ISignal } from '@lumino/signaling';
55
import { Contents, ServerConnection } from '@jupyterlab/services';
66
import { PathExt } from '@jupyterlab/coreutils';
77
import { IDriveInfo } from './token';
8-
import { mountDrive } from './requests';
8+
import { saveFile, mountDrive } from './requests';
99

1010
let data: Contents.IModel = {
1111
name: '',
@@ -433,19 +433,26 @@ export class Drive implements Contents.IDrive {
433433
localPath: string,
434434
options: Partial<Contents.IModel> = {}
435435
): Promise<Contents.IModel> {
436-
/*const settings = this.serverSettings;
437-
const url = this._getUrl(localPath);
438-
const init = {
439-
method: 'PUT',
440-
body: JSON.stringify(options)
441-
};
442-
const response = await ServerConnection.makeRequest(url, init, settings);
443-
// will return 200 for an existing file and 201 for a new file
444-
if (response.status !== 200 && response.status !== 201) {
445-
const err = await ServerConnection.ResponseError.create(response);
446-
throw err;
447-
}
448-
const data = await response.json();*/
436+
// extract current drive name
437+
const currentDrive = this._drivesList.filter(
438+
x =>
439+
x.name ===
440+
(localPath.indexOf('/') !== -1
441+
? localPath.substring(0, localPath.indexOf('/'))
442+
: localPath)
443+
)[0];
444+
445+
// eliminate drive name from path
446+
const relativePath =
447+
localPath.indexOf('/') !== -1
448+
? localPath.substring(localPath.indexOf('/') + 1)
449+
: '';
450+
451+
const resp = await saveFile(currentDrive.name, {
452+
path: relativePath,
453+
param: options
454+
});
455+
console.log('contents resp: ', resp);
449456

450457
Contents.validateContentsModel(data);
451458
this._fileChanged.emit({

src/requests.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { ReadonlyJSONObject } from '@lumino/coreutils';
22
import { requestAPI } from './handler';
3+
import { Contents } from '@jupyterlab/services';
34

45
/**
56
* Fetch the list of available drives.
@@ -24,3 +25,30 @@ export async function mountDrive(
2425
};
2526
return await requestAPI<any>('drives', 'POST', body);
2627
}
28+
29+
export async function saveFile(
30+
driveName: string,
31+
options: {
32+
path: string;
33+
param: Partial<Contents.IModel>;
34+
}
35+
) {
36+
// const [fileType, fileMimeType, fileFormat] = getFileType(
37+
// PathExt.extname(PathExt.basename(options.path)),
38+
// options.registeredFileTypes
39+
// );
40+
41+
// const formattedBody = Private.formatBody(options.param, fileFormat, fileType, fileMimeType);
42+
// const body: ReadonlyJSONObject = {
43+
// content: formattedBody
44+
// };
45+
46+
const response = await requestAPI<any>(
47+
'drives/' + driveName + '/' + options.path,
48+
'PUT',
49+
{
50+
content: options.param.content
51+
}
52+
);
53+
console.log('response: ', response);
54+
}

0 commit comments

Comments
 (0)