From 9bfe19d773394e246aa3ae62fd59471f6aa5c1ce Mon Sep 17 00:00:00 2001 From: chinonso098 Date: Tue, 30 Jul 2024 00:09:50 -0400 Subject: [PATCH] I seem to be back on track --- src/app/shared/system-service/file.service.ts | 92 +++++++------------ .../fileexplorer/fileexplorer.component.ts | 9 +- .../filemanager/filemanager.component.ts | 9 +- .../system-apps/terminal/terminal.commands.ts | 2 +- 4 files changed, 47 insertions(+), 65 deletions(-) diff --git a/src/app/shared/system-service/file.service.ts b/src/app/shared/system-service/file.service.ts index 49305b16..82cd941b 100644 --- a/src/app/shared/system-service/file.service.ts +++ b/src/app/shared/system-service/file.service.ts @@ -151,7 +151,6 @@ export class FileService{ }); } - public async copyFilesAsync(sourcepaths:string[], destinationpath:string):Promise{ await this.initBrowserFsAsync(); @@ -189,30 +188,31 @@ export class FileService{ public async createFolderAsync(directory:string, fileName:string):Promise{ await this.initBrowserFsAsync(); return new Promise((resolve, reject) =>{ - - this._fileSystem.exists(`${directory}/${fileName}`, (exists) =>{ - if(exists){ - console.log('createFolderAsync: folder already exists',exists); - resolve(false); - }else{ - this._fileSystem.mkdir(`${directory}/${fileName}`,0o777,(err) =>{ + this._fileSystem.mkdir(`${directory}/${fileName}`,0o777,(err) =>{ + if(err?.code === 'EEXIST' ){ + console.log('createFolderAsync Error:folder already exists',err); + const itrName = this.iterateFileName(`${directory}/${fileName}`); + this._fileSystem.mkdir(itrName,0o777,(err) =>{ if(err){ - console.log('createFolderAsync Error: folder creation',err); - reject(false); + console.log('createFolderAsync Error:',err); + reject(err); } - //this.dirFilesUpdateNotify.next(); resolve(true); }); + }else{ + console.log(`err:${err}`); + this._fileExistsMap.set(`${directory}/${fileName}`,0); + resolve(true); } }); - }) + }); } - public async deleteFolderAsync(directory:string, fileName:string):Promise{ + public async deleteFolderAsync(directory:string):Promise{ return new Promise((resolve, reject) =>{ - this._fileSystem.exists(`${directory}/${fileName}`, (err) =>{ + this._fileSystem.exists(`${directory}/`, (err) =>{ if(err){ - this._fileSystem.rmdir(`${directory}/${fileName}`,(err) =>{ + this._fileSystem.rmdir(`${directory}/`,(err) =>{ if(err){ console.log('deleteFolderAsync Error: folder delete failed',err); reject(false); @@ -498,7 +498,6 @@ export class FileService{ }); } - public async getShortCutFromURLAsync(path:string):Promise{ await this.initBrowserFsAsync(); @@ -577,26 +576,6 @@ export class FileService{ }); } - public async renameFolderAsync(directory:string, oldfileName:string, newFileName:string):Promise{ - await this.initBrowserFsAsync(); - - return new Promise((resolve, reject) =>{ - this._fileSystem.exists(`${directory}/${newFileName}`, (err) =>{ - if(err){ - console.log('renameFolderAsync Error: folder already exists',err); - }else{ - this._fileSystem.rename(`${directory}/${oldfileName}`,`${directory}/${newFileName}`,(err) =>{ - if(err){ - console.log('renameFolderAsync Error: folder rename',err); - reject(false); - } - resolve(true); - }); - } - }); - }); - } - public async renameFileAsync_TBD(path:string, newFileName:string): Promise { await this.initBrowserFsAsync(); @@ -630,37 +609,30 @@ export class FileService{ }); } - public async renameFileAsync(path:string, newFileName:string): Promise { + public async renameAsync(path:string, newFileName:string, isFile:boolean): Promise { await this.initBrowserFsAsync(); - return new Promise((resolve, reject) =>{ - this._fileSystem.readFile(path,(err, contents = Buffer.from('')) =>{ - if(err){ - console.log('getFile in renameFileAsync error:',err) - reject(false) - }else{ - this._fileSystem.writeFile(`${dirname(path)}/${newFileName}${extname(path)}`,contents,(err)=>{ + return new Promise((resolve, reject) =>{ + let rename = ''; let type = '' + if(isFile){ rename = `${dirname(path)}/${newFileName}${extname(path)}`; type = 'file'; + }else{ rename = `${dirname(path)}/${newFileName}`; type = 'folder'; } + + this._fileSystem.exists(`${rename}`, (err) =>{ + if(err){ + console.log(`renameAsync Error: ${type} already exists`,err); + reject(false); + }else{ + this._fileSystem.rename(`${path}`,rename,(err) =>{ if(err){ - console.log('writeFile in renameFileAsync error:',err); + console.log(`renameAsync Error: ${type} rename`,err); reject(false); - }else{ - this._fileSystem.unlink(path,(err) =>{ - if(err){ - console.log('unlink file error:',err) - reject(err) - } - console.log('successfully unlinked') - resolve(true); - }); - console.log('successfully renamed') - resolve(true); } + resolve(true); }); - console.log('successfully fetched') - resolve(true); - } - }); + } + }); }); + } diff --git a/src/app/system-apps/fileexplorer/fileexplorer.component.ts b/src/app/system-apps/fileexplorer/fileexplorer.component.ts index 3a00a7ed..b15fc705 100644 --- a/src/app/system-apps/fileexplorer/fileexplorer.component.ts +++ b/src/app/system-apps/fileexplorer/fileexplorer.component.ts @@ -1005,7 +1005,12 @@ export class FileExplorerComponent implements BaseComponent, OnInit, AfterViewIn } async onDeleteFile():Promise{ - const result = await this._fileService.deleteFileAsync(this.selectedFile.getCurrentPath) + let result = false; + if(this.selectedFile.getIsFile){ + result = await this._fileService.deleteFileAsync(this.selectedFile.getCurrentPath); + }else{ + result = await this._fileService.deleteFolderAsync(this.selectedFile.getCurrentPath) + } if(result){ await this.loadFilesInfoAsync(); @@ -1262,7 +1267,7 @@ export class FileExplorerComponent implements BaseComponent, OnInit, AfterViewIn const renameText = this.renameForm.value.renameInput as string; if(renameText !== '' && renameText.length !== 0 && renameText !== this.currentIconName){ - const result = await this._fileService.renameFileAsync(this.selectedFile.getCurrentPath, renameText); + const result = await this._fileService.renameAsync(this.selectedFile.getCurrentPath, renameText, this.selectedFile.getIsFile); if(result){ // renamFileAsync, doesn't trigger a reload of the file directory, so to give the user the impression that the file has been updated, the code below diff --git a/src/app/system-apps/filemanager/filemanager.component.ts b/src/app/system-apps/filemanager/filemanager.component.ts index 96cdcbc0..711551fa 100644 --- a/src/app/system-apps/filemanager/filemanager.component.ts +++ b/src/app/system-apps/filemanager/filemanager.component.ts @@ -410,7 +410,12 @@ export class FileManagerComponent implements BaseComponent, OnInit, AfterViewIni } async onDeleteFile():Promise{ - const result = await this._fileService.deleteFileAsync(this.selectedFile.getCurrentPath) + let result = false; + if(this.selectedFile.getIsFile){ + result = await this._fileService.deleteFileAsync(this.selectedFile.getCurrentPath); + }else{ + result = await this._fileService.deleteFolderAsync(this.selectedFile.getCurrentPath) + } if(result){ await this.loadFilesInfoAsync(); @@ -505,7 +510,7 @@ export class FileManagerComponent implements BaseComponent, OnInit, AfterViewIni const renameText = this.renameForm.value.renameInput as string; if(renameText !== '' && renameText.length !== 0 && renameText !== this.currentIconName ){ - const result = await this._fileService.renameFileAsync(this.selectedFile.getCurrentPath, renameText); + const result = await this._fileService.renameAsync(this.selectedFile.getCurrentPath, renameText, this.selectedFile.getIsFile); if(result){ // renamFileAsync, doesn't trigger a reload of the file directory, so to give the user the impression that the file has been updated, the code below diff --git a/src/app/system-apps/terminal/terminal.commands.ts b/src/app/system-apps/terminal/terminal.commands.ts index 8daac3ca..3b193196 100644 --- a/src/app/system-apps/terminal/terminal.commands.ts +++ b/src/app/system-apps/terminal/terminal.commands.ts @@ -718,7 +718,7 @@ Mandatory argument to long options are mandotory for short options too. // Delete the current directory after all its contents have been processed const folderName = this.getFileName(sourceArg); - const result = await this._fileService.deleteFolderAsync(sourceArg, folderName); + const result = await this._fileService.deleteFolderAsync(`${sourceArg}/${folderName}`); if (result) { console.log(`Directory: ${sourceArg} deleted successfully`);