@@ -29,7 +29,7 @@ import { AsyncReturnType } from '../utils/TypeHelper';
29
29
import { safeTakeEvery as takeEvery , safeTakeLatest as takeLatest } from './SafeEffects' ;
30
30
import { FSModule } from 'browserfs/dist/node/core/FS' ;
31
31
import { retrieveFilesInWorkspaceAsRecord , rmFilesInDirRecursively , writeFileRecursively } from '../fileSystem/FileSystemUtils' ;
32
- import { refreshFileView } from '../fileSystemView/FileSystemViewList' ;
32
+ import { refreshFileView } from '../fileSystemView/FileSystemViewList' ; // TODO broken when folder is open when reading folders
33
33
34
34
const DISCOVERY_DOCS = [ 'https://www.googleapis.com/discovery/v1/apis/drive/v3/rest' ] ;
35
35
const SCOPES =
@@ -91,12 +91,14 @@ export function* persistenceSaga(): SagaIterator {
91
91
let toastKey : string | undefined ;
92
92
try {
93
93
yield call ( ensureInitialisedAndAuthorised ) ;
94
- const { id, name, mimeType, picked } = yield call ( pickFile ,
94
+ const { id, name, mimeType, picked, parentId } = yield call ( pickFile ,
95
95
'Pick a file/folder to open' ,
96
96
{
97
97
pickFolders : true
98
98
}
99
99
) ; // id, name, picked gotten here
100
+
101
+ yield call ( console . log , parentId ) ;
100
102
if ( ! picked ) {
101
103
return ;
102
104
}
@@ -142,8 +144,6 @@ export function* persistenceSaga(): SagaIterator {
142
144
}
143
145
yield call ( console . log , "there is a filesystem" ) ;
144
146
145
- yield put ( actions . playgroundUpdatePersistenceFolder ( { id, name, lastSaved : new Date ( ) } ) ) ;
146
-
147
147
// rmdir everything TODO replace everything hardcoded with playground?
148
148
yield call ( rmFilesInDirRecursively , fileSystem , "/playground" ) ;
149
149
@@ -173,6 +173,10 @@ export function* persistenceSaga(): SagaIterator {
173
173
174
174
yield call ( showSuccessMessage , `Loaded folder ${ name } .` , 1000 ) ;
175
175
176
+ // TODO does not update playground on loading folder
177
+ yield call ( console . log , "ahfdaskjhfkjsadf" , parentId ) ;
178
+ yield put ( actions . playgroundUpdatePersistenceFolder ( { id, name, parentId, lastSaved : new Date ( ) } ) ) ;
179
+
176
180
return ;
177
181
}
178
182
@@ -348,10 +352,14 @@ export function* persistenceSaga(): SagaIterator {
348
352
yield takeEvery ( // TODO work on this
349
353
PERSISTENCE_SAVE_ALL ,
350
354
function * ( ) {
355
+ // TODO: when top level folder is renamed, save all just leaves the old folder alone and saves in a new folder if it exists
356
+ // Add checking to see if current folder already exists when renamed?
357
+ // Some way to keep track of when files/folders are renamed???????????
358
+ // TODO: if top level folder already exists in GDrive, to
359
+
351
360
// Case init: Don't care, delete everything in remote and save again
352
361
// Callable only if persistenceObject isFolder
353
-
354
- const [ currFolderObject ] = yield select (
362
+ const [ currFolderObject ] = yield select ( // TODO resolve type here?
355
363
( state : OverallState ) => [
356
364
state . playground . persistenceObject
357
365
]
@@ -360,6 +368,12 @@ export function* persistenceSaga(): SagaIterator {
360
368
yield call ( console . log , "no obj!" ) ;
361
369
return ;
362
370
}
371
+ if ( ! ( currFolderObject as PersistenceObject ) . isFolder ) {
372
+ yield call ( console . log , "folder not opened!" ) ;
373
+ return ;
374
+ }
375
+
376
+
363
377
364
378
console . log ( "currFolderObj" , currFolderObject ) ;
365
379
@@ -378,6 +392,44 @@ export function* persistenceSaga(): SagaIterator {
378
392
// behaviour of open folder for GDrive loads even empty folders
379
393
yield call ( console . log , "currfiles" , currFiles ) ;
380
394
395
+ const [ chapter , variant , external ] = yield select (
396
+ ( state : OverallState ) => [
397
+ state . workspaces . playground . context . chapter ,
398
+ state . workspaces . playground . context . variant ,
399
+ state . workspaces . playground . externalLibrary
400
+ ]
401
+ ) ;
402
+ const config : IPlaygroundConfig = {
403
+ chapter,
404
+ variant,
405
+ external
406
+ } ;
407
+
408
+ // check if top level folder has been renamed
409
+ // assuming only 1 top level folder exists, so get 1 file
410
+ const testPath = Object . keys ( currFiles ) [ 0 ] ;
411
+ const regexResult = / ^ ( .* [ \\ \/ ] ) ? ( \. * .* ?) ( \. [ ^ . ] + ?| ) $ / . exec ( testPath ) ;
412
+ if ( regexResult === null ) {
413
+ yield call ( console . log , "Regex null!" ) ;
414
+ return ; // should never come here
415
+ }
416
+ const topLevelFolderName = regexResult [ 1 ] . slice (
417
+ ( "/playground/" ) . length , - 1 ) . split ( "/" ) [ 0 ] ;
418
+
419
+ if ( topLevelFolderName === "" ) {
420
+ yield call ( console . log , "no top level folder?" ) ;
421
+ return ;
422
+ }
423
+
424
+ if ( topLevelFolderName !== currFolderObject . name ) {
425
+ // top level folder name has been renamed
426
+ yield call ( console . log , "TLFN changed from " , currFolderObject . name , " to " , topLevelFolderName ) ;
427
+ const newTopLevelFolderId : string = yield call ( getContainingFolderIdRecursively , [ topLevelFolderName ] ,
428
+ currFolderObject . parentId ! ) ; // try and find the folder if it exists
429
+ currFolderObject . name = topLevelFolderName ; // so that the new top level folder will be created below
430
+ currFolderObject . id = newTopLevelFolderId ; // so that new top level folder will be saved in root of gdrive
431
+ }
432
+
381
433
//const fileNameRegex = new RegExp('@"[^\\]+$"');
382
434
for ( const currFullFilePath of Object . keys ( currFiles ) ) {
383
435
const currFileContent = currFiles [ currFullFilePath ] ;
@@ -395,18 +447,34 @@ export function* persistenceSaga(): SagaIterator {
395
447
// /fold1/fold2/ becomes ["fold1", "fold2"]
396
448
// If in top level folder, becomes [""]
397
449
398
- yield call ( getContainingFolderIdRecursively , currFileParentFolders ,
450
+ const currFileParentFolderId : string = yield call ( getContainingFolderIdRecursively , currFileParentFolders ,
399
451
currFolderObject . id ) ;
400
452
401
- // yield call(
402
- // createFile,
403
- // fileName,
404
- // currFolderObject.id,
453
+ yield call ( console . log , "name" , currFileName , "content" , currFileContent
454
+ , "parent folder id" , currFileParentFolderId ) ;
405
455
406
- // );
456
+ const currFileId : string = yield call ( getFileFromFolder , currFileParentFolderId , currFileName ) ;
407
457
408
- yield call ( console . log , "name" , currFileName , "content" , currFileContent
409
- , "path" , currFileParentFolders ) ;
458
+ if ( currFileId === "" ) {
459
+ // file does not exist, create file
460
+ yield call ( console . log , "creating " , currFileName ) ;
461
+ yield call ( createFile , currFileName , currFileParentFolderId , MIME_SOURCE , currFileContent , config ) ;
462
+
463
+ } else {
464
+ // file exists, update file
465
+ yield call ( console . log , "updating " , currFileName , " id: " , currFileId ) ;
466
+ yield call ( updateFile , currFileId , currFileName , MIME_SOURCE , currFileContent , config ) ;
467
+ }
468
+ yield put ( actions . playgroundUpdatePersistenceFolder ( { id : currFolderObject . id , name : currFolderObject . name , parentId : currFolderObject . parentId , lastSaved : new Date ( ) } ) ) ;
469
+ yield call ( showSuccessMessage , `${ currFileName } successfully saved to Google Drive.` , 1000 ) ;
470
+
471
+ // TODO: create getFileIdRecursively, that uses currFileParentFolderId
472
+ // to query GDrive api to get a particular file's GDrive id OR modify reading func to save each obj's id somewhere
473
+ // Then use updateFile like in persistence_save_file to update files that exist
474
+ // on GDrive, or createFile if the file doesn't exist
475
+
476
+ // TODO: lazy loading of files?
477
+ // just show the folder structure, then load the file - to turn into an issue
410
478
}
411
479
412
480
@@ -663,6 +731,35 @@ async function getFilesOfFolder( // recursively get files
663
731
return ans ;
664
732
}
665
733
734
+ async function getFileFromFolder ( // returns string id or empty string if failed
735
+ parentFolderId : string ,
736
+ fileName : string
737
+ ) : Promise < string > {
738
+ let fileList : gapi . client . drive . File [ ] | undefined ;
739
+
740
+ await gapi . client . drive . files . list ( {
741
+ q : '\'' + parentFolderId + '\'' + ' in parents and trashed = false and name = \'' + fileName + '\'' ,
742
+ } ) . then ( res => {
743
+ fileList = res . result . files
744
+ } )
745
+
746
+ console . log ( fileList ) ;
747
+
748
+ if ( ! fileList || fileList . length === 0 ) {
749
+ // file does not exist
750
+ console . log ( "file not exist: " + fileName ) ;
751
+ return "" ;
752
+ }
753
+
754
+ //check if file is correct
755
+ if ( fileList ! [ 0 ] . name === fileName ) {
756
+ // file is correct
757
+ return fileList ! [ 0 ] . id ! ;
758
+ } else {
759
+ return "" ;
760
+ }
761
+ }
762
+
666
763
async function getContainingFolderIdRecursively ( // TODO memoize?
667
764
parentFolders : string [ ] ,
668
765
topFolderId : string ,
@@ -799,7 +896,7 @@ function createFolderAndReturnId(
799
896
headers,
800
897
body
801
898
} ) . then ( res => res . result . id )
802
- } ;
899
+ }
803
900
804
901
function createMultipartBody (
805
902
meta : any ,
0 commit comments