-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #442 from internxt/fix/lopp-deleting-files
[PB-1513]: Fix/loop-when-delete-files
- Loading branch information
Showing
15 changed files
with
214 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/context/virtual-drive/files/application/FileFolderContainerDetector.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { FolderFinder } from '../../folders/application/FolderFinder'; | ||
import { File } from '../../files/domain/File'; | ||
import { Folder } from '../../folders/domain/Folder'; | ||
import { FileRepository } from '../domain/FileRepository'; | ||
import { FileNotFoundError } from '../domain/errors/FileNotFoundError'; | ||
|
||
export class FileFolderContainerDetector { | ||
constructor( | ||
private readonly repository: FileRepository, | ||
private readonly folderFinder: FolderFinder | ||
) {} | ||
|
||
run(contentId: File['contentsId'], folderContentId: Folder['uuid']): boolean { | ||
const file = this.repository.searchByPartial({ | ||
contentsId: contentId, | ||
}); | ||
if (!file) { | ||
throw new FileNotFoundError(contentId); | ||
} | ||
const folder = this.folderFinder.findFromId(file.folderId); | ||
return folder.uuid === folderContentId; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/context/virtual-drive/folders/application/FolderContainerDetector.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Folder } from '../domain/Folder'; | ||
import { FolderRepository } from '../domain/FolderRepository'; | ||
|
||
export class FolderContainerDetector { | ||
constructor(private readonly repository: FolderRepository) {} | ||
|
||
run( | ||
fodlerContentId: Folder['uuid'], | ||
parentFolderContentId: Folder['uuid'] | ||
): boolean { | ||
const folder = this.repository.searchByPartial({ uuid: fodlerContentId }); | ||
|
||
if (!folder) { | ||
throw new Error('Folder not found'); | ||
} | ||
|
||
const parent = this.repository.searchByPartial({ | ||
id: folder.parentId as number, | ||
}); | ||
|
||
if (!parent) { | ||
throw new Error('Parent folder not found'); | ||
} | ||
|
||
return parent.uuid === parentFolderContentId; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/context/virtual-drive/folders/application/RetryFolderDeleter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import Logger from 'electron-log'; | ||
import { FolderDeleter } from './FolderDeleter'; | ||
import { MaxRetriesDeletingFolderError } from '../domain/errors/MaxRetriesDeletingFolderError'; | ||
|
||
export class RetryFolderDeleter { | ||
private static NUMBER_OF_RETRIES = 2; | ||
private static MILLISECOND_BETWEEN_TRIES = 1_000; | ||
private static INITIAL_DELAY = 100; | ||
constructor(private readonly deleter: FolderDeleter) {} | ||
async retryDeleter(asyncFunction: () => Promise<any>) { | ||
let retryCount = 0; | ||
|
||
while (retryCount <= RetryFolderDeleter.NUMBER_OF_RETRIES) { | ||
try { | ||
const result = await asyncFunction(); | ||
return result; | ||
} catch (error: unknown) { | ||
if (error instanceof Error) { | ||
Logger.warn( | ||
`Folder deleter attempt ${retryCount + 1} failed: ${error.message}` | ||
); | ||
} else { | ||
Logger.warn( | ||
`Folder deleter attempt ${ | ||
retryCount + 1 | ||
} failed with an unknown error.` | ||
); | ||
} | ||
|
||
await new Promise((resolve) => { | ||
setTimeout(resolve, RetryFolderDeleter.MILLISECOND_BETWEEN_TRIES); | ||
}); | ||
|
||
retryCount++; | ||
} | ||
} | ||
throw new MaxRetriesDeletingFolderError( | ||
RetryFolderDeleter.NUMBER_OF_RETRIES | ||
); | ||
} | ||
|
||
async run(folder: string): Promise<any> { | ||
await new Promise((resolve) => { | ||
setTimeout(resolve, RetryFolderDeleter.INITIAL_DELAY); | ||
}); | ||
|
||
const deleter = () => this.deleter.run(folder); | ||
return this.retryDeleter(deleter); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/context/virtual-drive/folders/domain/errors/MaxRetriesDeletingFolderError.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export class MaxRetriesDeletingFolderError extends Error { | ||
constructor(retriesNumber: number) { | ||
super(`Max retries (${retriesNumber}) reached. Deleter still failed.`); | ||
} | ||
} |
Oops, something went wrong.