Skip to content

Commit ae54abf

Browse files
committed
add logic for registered file types retrieval and usage
1 parent 09d0f60 commit ae54abf

File tree

4 files changed

+113
-15
lines changed

4 files changed

+113
-15
lines changed

src/contents.ts

Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
// Copyright (c) Jupyter Development Team.
2-
// Distributed under the terms of the Modified BSD License.
3-
1+
import { JupyterFrontEnd } from '@jupyterlab/application';
42
import { Signal, ISignal } from '@lumino/signaling';
53
import { Contents, ServerConnection } from '@jupyterlab/services';
6-
import { IDriveInfo } from './token';
4+
import { IDriveInfo, IRegisteredFileTypes } from './token';
75
import { getContents, mountDrive } from './requests';
86

97
let data: Contents.IModel = {
@@ -119,6 +117,20 @@ export class Drive implements Contents.IDrive {
119117
return this._serverSettings;
120118
}
121119

120+
/**
121+
* The registered file types
122+
*/
123+
get registeredFileTypes(): IRegisteredFileTypes {
124+
return this._registeredFileTypes;
125+
}
126+
127+
/**
128+
* The registered file types
129+
*/
130+
set registeredFileTypes(fileTypes: IRegisteredFileTypes) {
131+
this._registeredFileTypes = fileTypes;
132+
}
133+
122134
/**
123135
* A signal emitted when a file operation takes place.
124136
*/
@@ -182,13 +194,14 @@ export class Drive implements Contents.IDrive {
182194
localPath: string,
183195
options?: Contents.IFetchOptions
184196
): Promise<Contents.IModel> {
185-
let relativePath = '';
197+
const relativePath = '';
198+
console.log('GET localpath: ', localPath);
186199
if (localPath !== '') {
187-
if (localPath.includes(this.name)) {
188-
relativePath = localPath.split(this.name + '/')[1];
189-
} else {
190-
relativePath = localPath;
191-
}
200+
// if (localPath.includes(this.name)) {
201+
// relativePath = localPath.split(this.name + '/')[1];
202+
// } else {
203+
// relativePath = localPath;
204+
// }
192205

193206
// extract current drive name
194207
const currentDrive = this._drivesList.filter(
@@ -207,7 +220,10 @@ export class Drive implements Contents.IDrive {
207220
}
208221
}
209222

210-
data = await getContents(currentDrive.name, { path: '' });
223+
data = await getContents(currentDrive.name, {
224+
path: '',
225+
registeredFileTypes: this._registeredFileTypes
226+
});
211227
} else {
212228
const drivesList: Contents.IModel[] = [];
213229
for (const drive of this._drivesList) {
@@ -589,6 +605,40 @@ export class Drive implements Contents.IDrive {
589605
return Promise.reject('Read only');
590606
}
591607

608+
/**
609+
* Get all registered file types and store them accordingly with their file
610+
* extension (e.g.: .txt, .pdf, .jpeg), file mimetype (e.g.: text/plain, application/pdf)
611+
* and file format (e.g.: base64, text).
612+
*
613+
* @param app
614+
*/
615+
getRegisteredFileTypes(app: JupyterFrontEnd) {
616+
// get called when instating the toolbar
617+
const registeredFileTypes = app.docRegistry.fileTypes();
618+
619+
for (const fileType of registeredFileTypes) {
620+
// check if we are dealing with a directory
621+
if (fileType.extensions.length === 0) {
622+
this._registeredFileTypes[''] = {
623+
fileType: 'directory',
624+
fileFormat: 'json',
625+
fileMimeTypes: ['text/directory']
626+
};
627+
}
628+
629+
// store the mimetype and fileformat for each file extension
630+
fileType.extensions.forEach(extension => {
631+
if (!this._registeredFileTypes[extension]) {
632+
this._registeredFileTypes[extension] = {
633+
fileType: fileType.name,
634+
fileMimeTypes: [...fileType.mimeTypes],
635+
fileFormat: fileType.fileFormat ? fileType.fileFormat : ''
636+
};
637+
}
638+
});
639+
}
640+
}
641+
592642
/**
593643
* Get a REST url for a file given a path.
594644
*/
@@ -609,6 +659,7 @@ export class Drive implements Contents.IDrive {
609659
private _fileChanged = new Signal<this, Contents.IChangedArgs>(this);
610660
private _isDisposed: boolean = false;
611661
private _disposed = new Signal<this, void>(this);
662+
private _registeredFileTypes: IRegisteredFileTypes = {};
612663
}
613664

614665
export namespace Drive {

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,9 @@ const driveFileBrowser: JupyterFrontEndPlugin<void> = {
224224

225225
app.serviceManager.contents.addDrive(drive);
226226

227+
// get registered file types
228+
drive.getRegisteredFileTypes(app);
229+
227230
// Manually restore and load the drive file browser.
228231
const driveBrowser = fileBrowserFactory.createFileBrowser('drivebrowser', {
229232
auto: false,

src/requests.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Contents } from '@jupyterlab/services';
33
import { PathExt } from '@jupyterlab/coreutils';
44

55
import { requestAPI } from './handler';
6+
import { getFileType, IRegisteredFileTypes } from './token';
67

78
let data: Contents.IModel = {
89
name: '',
@@ -47,7 +48,7 @@ export async function mountDrive(
4748

4849
export async function getContents(
4950
driveName: string,
50-
options: { path: string }
51+
options: { path: string; registeredFileTypes: IRegisteredFileTypes }
5152
) {
5253
const response = await requestAPI<any>(
5354
'drives/' + driveName + '/' + options.path,
@@ -60,17 +61,22 @@ export async function getContents(
6061
response.data.forEach((row: any) => {
6162
const fileName = PathExt.basename(row.path);
6263

64+
const [fileType, fileMimeType, fileFormat] = getFileType(
65+
PathExt.extname(PathExt.basename(fileName)),
66+
options.registeredFileTypes
67+
);
68+
6369
fileList[fileName] = fileList[fileName] ?? {
6470
name: fileName,
6571
path: driveName + '/' + row.path,
6672
last_modified: row.last_modified,
6773
created: '',
6874
content: !fileName.split('.')[1] ? [] : null,
69-
format: null, //fileFormat as Contents.FileFormat,
70-
mimetype: 'null', //fileMimeType,
75+
format: fileFormat as Contents.FileFormat,
76+
mimetype: fileMimeType,
7177
size: row.size,
7278
writable: true,
73-
type: 'directory' //fileType
79+
type: fileType
7480
};
7581
});
7682

src/token.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,41 @@ export interface IDriveInfo {
3232
*/
3333
mounted: boolean;
3434
}
35+
36+
/**
37+
* An interface that stores the registered file type, mimetype and format for each file extension.
38+
*/
39+
export interface IRegisteredFileTypes {
40+
[fileExtension: string]: {
41+
fileType: string;
42+
fileMimeTypes: string[];
43+
fileFormat: string;
44+
};
45+
}
46+
47+
/**
48+
* Helping function to define file type, mimetype and format based on file extension.
49+
* @param extension file extension (e.g.: txt, ipynb, csv)
50+
* @returns
51+
*/
52+
export function getFileType(
53+
extension: string,
54+
registeredFileTypes: IRegisteredFileTypes
55+
) {
56+
let fileType: string = 'text';
57+
let fileMimetype: string = 'text/plain';
58+
let fileFormat: string = 'text';
59+
60+
if (registeredFileTypes[extension]) {
61+
fileType = registeredFileTypes[extension].fileType;
62+
fileMimetype = registeredFileTypes[extension].fileMimeTypes[0];
63+
fileFormat = registeredFileTypes[extension].fileFormat;
64+
}
65+
66+
// the file format for notebooks appears as json, but should be text
67+
if (extension === '.ipynb') {
68+
fileFormat = 'text';
69+
}
70+
71+
return [fileType, fileMimetype, fileFormat];
72+
}

0 commit comments

Comments
 (0)