Skip to content

Commit

Permalink
add logic for registered file types retrieval and usage
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisaCG committed Nov 20, 2024
1 parent 09d0f60 commit ae54abf
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 15 deletions.
73 changes: 62 additions & 11 deletions src/contents.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.

import { JupyterFrontEnd } from '@jupyterlab/application';
import { Signal, ISignal } from '@lumino/signaling';
import { Contents, ServerConnection } from '@jupyterlab/services';
import { IDriveInfo } from './token';
import { IDriveInfo, IRegisteredFileTypes } from './token';
import { getContents, mountDrive } from './requests';

let data: Contents.IModel = {
Expand Down Expand Up @@ -119,6 +117,20 @@ export class Drive implements Contents.IDrive {
return this._serverSettings;
}

/**
* The registered file types
*/
get registeredFileTypes(): IRegisteredFileTypes {
return this._registeredFileTypes;
}

/**
* The registered file types
*/
set registeredFileTypes(fileTypes: IRegisteredFileTypes) {
this._registeredFileTypes = fileTypes;
}

/**
* A signal emitted when a file operation takes place.
*/
Expand Down Expand Up @@ -182,13 +194,14 @@ export class Drive implements Contents.IDrive {
localPath: string,
options?: Contents.IFetchOptions
): Promise<Contents.IModel> {
let relativePath = '';
const relativePath = '';
console.log('GET localpath: ', localPath);
if (localPath !== '') {
if (localPath.includes(this.name)) {
relativePath = localPath.split(this.name + '/')[1];
} else {
relativePath = localPath;
}
// if (localPath.includes(this.name)) {
// relativePath = localPath.split(this.name + '/')[1];
// } else {
// relativePath = localPath;
// }

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

data = await getContents(currentDrive.name, { path: '' });
data = await getContents(currentDrive.name, {
path: '',
registeredFileTypes: this._registeredFileTypes
});
} else {
const drivesList: Contents.IModel[] = [];
for (const drive of this._drivesList) {
Expand Down Expand Up @@ -589,6 +605,40 @@ export class Drive implements Contents.IDrive {
return Promise.reject('Read only');
}

/**
* Get all registered file types and store them accordingly with their file
* extension (e.g.: .txt, .pdf, .jpeg), file mimetype (e.g.: text/plain, application/pdf)
* and file format (e.g.: base64, text).
*
* @param app
*/
getRegisteredFileTypes(app: JupyterFrontEnd) {
// get called when instating the toolbar
const registeredFileTypes = app.docRegistry.fileTypes();

for (const fileType of registeredFileTypes) {
// check if we are dealing with a directory
if (fileType.extensions.length === 0) {
this._registeredFileTypes[''] = {
fileType: 'directory',
fileFormat: 'json',
fileMimeTypes: ['text/directory']
};
}

// store the mimetype and fileformat for each file extension
fileType.extensions.forEach(extension => {
if (!this._registeredFileTypes[extension]) {
this._registeredFileTypes[extension] = {
fileType: fileType.name,
fileMimeTypes: [...fileType.mimeTypes],
fileFormat: fileType.fileFormat ? fileType.fileFormat : ''
};
}
});
}
}

/**
* Get a REST url for a file given a path.
*/
Expand All @@ -609,6 +659,7 @@ export class Drive implements Contents.IDrive {
private _fileChanged = new Signal<this, Contents.IChangedArgs>(this);
private _isDisposed: boolean = false;
private _disposed = new Signal<this, void>(this);
private _registeredFileTypes: IRegisteredFileTypes = {};
}

export namespace Drive {
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,9 @@ const driveFileBrowser: JupyterFrontEndPlugin<void> = {

app.serviceManager.contents.addDrive(drive);

// get registered file types
drive.getRegisteredFileTypes(app);

// Manually restore and load the drive file browser.
const driveBrowser = fileBrowserFactory.createFileBrowser('drivebrowser', {
auto: false,
Expand Down
14 changes: 10 additions & 4 deletions src/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Contents } from '@jupyterlab/services';
import { PathExt } from '@jupyterlab/coreutils';

import { requestAPI } from './handler';
import { getFileType, IRegisteredFileTypes } from './token';

let data: Contents.IModel = {
name: '',
Expand Down Expand Up @@ -47,7 +48,7 @@ export async function mountDrive(

export async function getContents(
driveName: string,
options: { path: string }
options: { path: string; registeredFileTypes: IRegisteredFileTypes }
) {
const response = await requestAPI<any>(
'drives/' + driveName + '/' + options.path,
Expand All @@ -60,17 +61,22 @@ export async function getContents(
response.data.forEach((row: any) => {
const fileName = PathExt.basename(row.path);

const [fileType, fileMimeType, fileFormat] = getFileType(
PathExt.extname(PathExt.basename(fileName)),
options.registeredFileTypes
);

fileList[fileName] = fileList[fileName] ?? {
name: fileName,
path: driveName + '/' + row.path,
last_modified: row.last_modified,
created: '',
content: !fileName.split('.')[1] ? [] : null,
format: null, //fileFormat as Contents.FileFormat,
mimetype: 'null', //fileMimeType,
format: fileFormat as Contents.FileFormat,
mimetype: fileMimeType,
size: row.size,
writable: true,
type: 'directory' //fileType
type: fileType
};
});

Expand Down
38 changes: 38 additions & 0 deletions src/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,41 @@ export interface IDriveInfo {
*/
mounted: boolean;
}

/**
* An interface that stores the registered file type, mimetype and format for each file extension.
*/
export interface IRegisteredFileTypes {
[fileExtension: string]: {
fileType: string;
fileMimeTypes: string[];
fileFormat: string;
};
}

/**
* Helping function to define file type, mimetype and format based on file extension.
* @param extension file extension (e.g.: txt, ipynb, csv)
* @returns
*/
export function getFileType(
extension: string,
registeredFileTypes: IRegisteredFileTypes
) {
let fileType: string = 'text';
let fileMimetype: string = 'text/plain';
let fileFormat: string = 'text';

if (registeredFileTypes[extension]) {
fileType = registeredFileTypes[extension].fileType;
fileMimetype = registeredFileTypes[extension].fileMimeTypes[0];
fileFormat = registeredFileTypes[extension].fileFormat;
}

// the file format for notebooks appears as json, but should be text
if (extension === '.ipynb') {
fileFormat = 'text';
}

return [fileType, fileMimetype, fileFormat];
}

0 comments on commit ae54abf

Please sign in to comment.