1- // Copyright (c) Jupyter Development Team.
2- // Distributed under the terms of the Modified BSD License.
3-
1+ import { JupyterFrontEnd } from '@jupyterlab/application' ;
42import { Signal , ISignal } from '@lumino/signaling' ;
53import { Contents , ServerConnection } from '@jupyterlab/services' ;
6- import { IDriveInfo } from './token' ;
4+ import { IDriveInfo , IRegisteredFileTypes } from './token' ;
75import { getContents , mountDrive } from './requests' ;
86
97let 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
614665export namespace Drive {
0 commit comments