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' ;
64import { PathExt } from '@jupyterlab/coreutils' ;
7- import { IDriveInfo } from './token' ;
8- import { saveFile , mountDrive } from './requests' ;
5+
6+ import { IDriveInfo , IRegisteredFileTypes } from './token' ;
7+ import { saveFile , getContents , mountDrive } from './requests' ;
98
109let data : Contents . IModel = {
1110 name : '' ,
@@ -120,6 +119,20 @@ export class Drive implements Contents.IDrive {
120119 return this . _serverSettings ;
121120 }
122121
122+ /**
123+ * The registered file types
124+ */
125+ get registeredFileTypes ( ) : IRegisteredFileTypes {
126+ return this . _registeredFileTypes ;
127+ }
128+
129+ /**
130+ * The registered file types
131+ */
132+ set registeredFileTypes ( fileTypes : IRegisteredFileTypes ) {
133+ this . _registeredFileTypes = fileTypes ;
134+ }
135+
123136 /**
124137 * A signal emitted when a file operation takes place.
125138 */
@@ -185,40 +198,41 @@ export class Drive implements Contents.IDrive {
185198 ) : Promise < Contents . IModel > {
186199 let relativePath = '' ;
187200 if ( localPath !== '' ) {
188- if ( localPath . includes ( this . name ) ) {
189- relativePath = localPath . split ( this . name + '/' ) [ 1 ] ;
190- } else {
191- relativePath = localPath ;
192- }
193-
194201 // extract current drive name
195- const currentDrive = this . drivesList . filter ( x => x . name === localPath ) [ 0 ] ;
202+ const currentDrive = this . _drivesList . filter (
203+ x =>
204+ x . name ===
205+ ( localPath . indexOf ( '/' ) !== - 1
206+ ? localPath . substring ( 0 , localPath . indexOf ( '/' ) )
207+ : localPath )
208+ ) [ 0 ] ;
209+
196210 // when accessed the first time, mount drive
197- if ( ! currentDrive . mounted ) {
211+ if ( currentDrive . mounted === false ) {
198212 try {
199213 await mountDrive ( localPath , {
200214 provider : currentDrive . provider ,
201215 region : currentDrive . region
202216 } ) ;
203- currentDrive . mounted = true ;
217+ this . _drivesList . filter ( x => x . name === localPath ) [ 0 ] . mounted = true ;
204218 } catch ( e ) {
205219 console . log ( e ) ;
206220 }
207221 }
208222
209- data = {
210- name : PathExt . basename ( localPath ) ,
211- path : PathExt . basename ( localPath ) ,
212- last_modified : '' ,
213- created : '' ,
214- content : [ ] ,
215- format : 'json' ,
216- mimetype : '' ,
217- size : undefined ,
218- writable : true ,
219- type : 'directory'
220- } ;
223+ // eliminate drive name from path
224+ relativePath =
225+ localPath . indexOf ( '/' ) !== - 1
226+ ? localPath . substring ( localPath . indexOf ( '/' ) + 1 )
227+ : '' ;
228+
229+ data = await getContents ( currentDrive . name , {
230+ path : relativePath ,
231+ registeredFileTypes : this . _registeredFileTypes
232+ } ) ;
221233 } else {
234+ // retriving list of contents from root
235+ // in our case: list available drives
222236 const drivesList : Contents . IModel [ ] = [ ] ;
223237 for ( const drive of this . _drivesList ) {
224238 drivesList . push ( {
@@ -248,7 +262,6 @@ export class Drive implements Contents.IDrive {
248262 type : 'directory'
249263 } ;
250264 }
251- console . log ( 'GET: ' , relativePath ) ;
252265
253266 Contents . validateContentsModel ( data ) ;
254267 return data ;
@@ -565,7 +578,11 @@ export class Drive implements Contents.IDrive {
565578 * checkpoint is created.
566579 */
567580 createCheckpoint ( path : string ) : Promise < Contents . ICheckpointModel > {
568- return Promise . reject ( 'Repository is read only' ) ;
581+ const emptyCheckpoint : Contents . ICheckpointModel = {
582+ id : '' ,
583+ last_modified : ''
584+ } ;
585+ return Promise . resolve ( emptyCheckpoint ) ;
569586 }
570587
571588 /**
@@ -606,6 +623,40 @@ export class Drive implements Contents.IDrive {
606623 return Promise . reject ( 'Read only' ) ;
607624 }
608625
626+ /**
627+ * Get all registered file types and store them accordingly with their file
628+ * extension (e.g.: .txt, .pdf, .jpeg), file mimetype (e.g.: text/plain, application/pdf)
629+ * and file format (e.g.: base64, text).
630+ *
631+ * @param app
632+ */
633+ getRegisteredFileTypes ( app : JupyterFrontEnd ) {
634+ // get called when instating the toolbar
635+ const registeredFileTypes = app . docRegistry . fileTypes ( ) ;
636+
637+ for ( const fileType of registeredFileTypes ) {
638+ // check if we are dealing with a directory
639+ if ( fileType . extensions . length === 0 ) {
640+ this . _registeredFileTypes [ '' ] = {
641+ fileType : 'directory' ,
642+ fileFormat : 'json' ,
643+ fileMimeTypes : [ 'text/directory' ]
644+ } ;
645+ }
646+
647+ // store the mimetype and fileformat for each file extension
648+ fileType . extensions . forEach ( extension => {
649+ if ( ! this . _registeredFileTypes [ extension ] ) {
650+ this . _registeredFileTypes [ extension ] = {
651+ fileType : fileType . name ,
652+ fileMimeTypes : [ ...fileType . mimeTypes ] ,
653+ fileFormat : fileType . fileFormat ?? ''
654+ } ;
655+ }
656+ } ) ;
657+ }
658+ }
659+
609660 /**
610661 * Get a REST url for a file given a path.
611662 */
@@ -626,6 +677,7 @@ export class Drive implements Contents.IDrive {
626677 private _fileChanged = new Signal < this, Contents . IChangedArgs > ( this ) ;
627678 private _isDisposed : boolean = false ;
628679 private _disposed = new Signal < this, void > ( this ) ;
680+ private _registeredFileTypes : IRegisteredFileTypes = { } ;
629681}
630682
631683export namespace Drive {
0 commit comments