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 { PathExt } from '@jupyterlab/coreutils' ;
7- import { IDriveInfo } from './token' ;
8- import { mountDrive } from './requests' ;
4+ import { IDriveInfo , IRegisteredFileTypes } from './token' ;
5+ import { getContents , mountDrive } from './requests' ;
96
107let data : Contents . IModel = {
118 name : '' ,
@@ -120,6 +117,20 @@ export class Drive implements Contents.IDrive {
120117 return this . _serverSettings ;
121118 }
122119
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+
123134 /**
124135 * A signal emitted when a file operation takes place.
125136 */
@@ -185,40 +196,41 @@ export class Drive implements Contents.IDrive {
185196 ) : Promise < Contents . IModel > {
186197 let relativePath = '' ;
187198 if ( localPath !== '' ) {
188- if ( localPath . includes ( this . name ) ) {
189- relativePath = localPath . split ( this . name + '/' ) [ 1 ] ;
190- } else {
191- relativePath = localPath ;
192- }
193-
194199 // extract current drive name
195- const currentDrive = this . drivesList . filter ( x => x . name === localPath ) [ 0 ] ;
200+ const currentDrive = this . _drivesList . filter (
201+ x =>
202+ x . name ===
203+ ( localPath . indexOf ( '/' ) !== - 1
204+ ? localPath . substring ( 0 , localPath . indexOf ( '/' ) )
205+ : localPath )
206+ ) [ 0 ] ;
207+
196208 // when accessed the first time, mount drive
197- if ( ! currentDrive . mounted ) {
209+ if ( currentDrive . mounted === false ) {
198210 try {
199211 await mountDrive ( localPath , {
200212 provider : currentDrive . provider ,
201213 region : currentDrive . region
202214 } ) ;
203- currentDrive . mounted = true ;
215+ this . _drivesList . filter ( x => x . name === localPath ) [ 0 ] . mounted = true ;
204216 } catch ( e ) {
205217 console . log ( e ) ;
206218 }
207219 }
208220
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- } ;
221+ // eliminate drive name from path
222+ relativePath =
223+ localPath . indexOf ( '/' ) !== - 1
224+ ? localPath . substring ( localPath . indexOf ( '/' ) + 1 )
225+ : '' ;
226+
227+ data = await getContents ( currentDrive . name , {
228+ path : relativePath ,
229+ registeredFileTypes : this . _registeredFileTypes
230+ } ) ;
221231 } else {
232+ // retriving list of contents from root
233+ // in our case: list available drives
222234 const drivesList : Contents . IModel [ ] = [ ] ;
223235 for ( const drive of this . _drivesList ) {
224236 drivesList . push ( {
@@ -248,7 +260,6 @@ export class Drive implements Contents.IDrive {
248260 type : 'directory'
249261 } ;
250262 }
251- console . log ( 'GET: ' , relativePath ) ;
252263
253264 Contents . validateContentsModel ( data ) ;
254265 return data ;
@@ -558,7 +569,11 @@ export class Drive implements Contents.IDrive {
558569 * checkpoint is created.
559570 */
560571 createCheckpoint ( path : string ) : Promise < Contents . ICheckpointModel > {
561- return Promise . reject ( 'Repository is read only' ) ;
572+ const emptyCheckpoint : Contents . ICheckpointModel = {
573+ id : '' ,
574+ last_modified : ''
575+ } ;
576+ return Promise . resolve ( emptyCheckpoint ) ;
562577 }
563578
564579 /**
@@ -599,6 +614,40 @@ export class Drive implements Contents.IDrive {
599614 return Promise . reject ( 'Read only' ) ;
600615 }
601616
617+ /**
618+ * Get all registered file types and store them accordingly with their file
619+ * extension (e.g.: .txt, .pdf, .jpeg), file mimetype (e.g.: text/plain, application/pdf)
620+ * and file format (e.g.: base64, text).
621+ *
622+ * @param app
623+ */
624+ getRegisteredFileTypes ( app : JupyterFrontEnd ) {
625+ // get called when instating the toolbar
626+ const registeredFileTypes = app . docRegistry . fileTypes ( ) ;
627+
628+ for ( const fileType of registeredFileTypes ) {
629+ // check if we are dealing with a directory
630+ if ( fileType . extensions . length === 0 ) {
631+ this . _registeredFileTypes [ '' ] = {
632+ fileType : 'directory' ,
633+ fileFormat : 'json' ,
634+ fileMimeTypes : [ 'text/directory' ]
635+ } ;
636+ }
637+
638+ // store the mimetype and fileformat for each file extension
639+ fileType . extensions . forEach ( extension => {
640+ if ( ! this . _registeredFileTypes [ extension ] ) {
641+ this . _registeredFileTypes [ extension ] = {
642+ fileType : fileType . name ,
643+ fileMimeTypes : [ ...fileType . mimeTypes ] ,
644+ fileFormat : fileType . fileFormat ?? ''
645+ } ;
646+ }
647+ } ) ;
648+ }
649+ }
650+
602651 /**
603652 * Get a REST url for a file given a path.
604653 */
@@ -619,6 +668,7 @@ export class Drive implements Contents.IDrive {
619668 private _fileChanged = new Signal < this, Contents . IChangedArgs > ( this ) ;
620669 private _isDisposed : boolean = false ;
621670 private _disposed = new Signal < this, void > ( this ) ;
671+ private _registeredFileTypes : IRegisteredFileTypes = { } ;
622672}
623673
624674export namespace Drive {
0 commit comments