-
Notifications
You must be signed in to change notification settings - Fork 12
[PB-5111] Feat: Upload Facade #432
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,58 @@ | ||
| import { basename } from 'path'; | ||
|
Check warning on line 1 in src/services/network/upload/upload-facade.service.ts
|
||
| import { CLIUtils } from '../../../utils/cli.utils'; | ||
| import { logger } from '../../../utils/logger.utils'; | ||
| import { LocalFilesystemService } from '../../local-filesystem/local-filesystem.service'; | ||
| import { UploadFolderParams } from './upload.types'; | ||
| import { UploadFolderService } from './upload-folder.service'; | ||
| import { UploadFileService } from './upload-file.service'; | ||
|
|
||
| export class UploadFacade { | ||
| static readonly instance = new UploadFacade(); | ||
| async uploadFolder({ localPath, destinationFolderUuid, loginUserDetails, jsonFlag, onProgress }: UploadFolderParams) { | ||
| const timer = CLIUtils.timer(); | ||
| const network = CLIUtils.prepareNetwork({ jsonFlag, loginUserDetails }); | ||
| const scanResult = await LocalFilesystemService.instance.scanLocalDirectory(localPath); | ||
| logger.info( | ||
| `Scanned folder ${localPath}: found ${scanResult.totalItems} items, total size ${scanResult.totalBytes} bytes.`, | ||
| ); | ||
|
|
||
| const currentProgress = { itemsUploaded: 0, bytesUploaded: 0 }; | ||
| const emitProgress = () => { | ||
| const itemProgress = currentProgress.itemsUploaded / scanResult.totalItems; | ||
| const sizeProgress = currentProgress.bytesUploaded / scanResult.totalBytes; | ||
| const percentage = Math.floor((itemProgress * 0.5 + sizeProgress * 0.5) * 100); | ||
| onProgress({ percentage }); | ||
| }; | ||
|
|
||
| const folderMap = await UploadFolderService.instance.createFolders({ | ||
| foldersToCreate: scanResult.folders, | ||
| destinationFolderUuid, | ||
| currentProgress, | ||
| emitProgress, | ||
| }); | ||
|
|
||
| if (folderMap.size === 0) { | ||
| return { error: new Error('Failed to create folders, cannot upload files') }; | ||
| } | ||
|
|
||
| const totalBytes = await UploadFileService.instance.uploadFilesInChunks({ | ||
| network, | ||
| filesToUpload: scanResult.files, | ||
| folderMap, | ||
| bucket: loginUserDetails.bucket, | ||
| destinationFolderUuid, | ||
| currentProgress, | ||
| emitProgress, | ||
| }); | ||
|
|
||
| const rootFolderName = basename(localPath); | ||
| const rootFolderId = folderMap.get(rootFolderName) ?? ''; | ||
| return { | ||
| data: { | ||
| totalBytes, | ||
| rootFolderId, | ||
| uploadTimeMs: timer.stop(), | ||
| }, | ||
| }; | ||
| } | ||
| } | ||
This file contains hidden or 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
184 changes: 184 additions & 0 deletions
184
test/services/network/upload/upload-facade.service.test.ts
This file contains hidden or 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,184 @@ | ||
| import { beforeEach, describe, it, vi, expect } from 'vitest'; | ||
| import { UploadFacade } from '../../../../src/services/network/upload/upload-facade.service'; | ||
| import { CLIUtils } from '../../../../src/utils/cli.utils'; | ||
| import { logger } from '../../../../src/utils/logger.utils'; | ||
| import { LocalFilesystemService } from '../../../../src/services/local-filesystem/local-filesystem.service'; | ||
| import { UploadFolderService } from '../../../../src/services/network/upload/upload-folder.service'; | ||
| import { UploadFileService } from '../../../../src/services/network/upload/upload-file.service'; | ||
| import { NetworkFacade } from '../../../../src/services/network/network-facade.service'; | ||
| import { LoginUserDetails } from '../../../../src/types/command.types'; | ||
| import { createFileSystemNodeFixture } from './upload.service.helpers'; | ||
|
|
||
| vi.mock('../../../../src/utils/cli.utils', () => ({ | ||
| CLIUtils: { | ||
| timer: vi.fn(), | ||
| prepareNetwork: vi.fn(), | ||
| }, | ||
| })); | ||
|
|
||
| vi.mock('../../../../src/utils/logger.utils', () => ({ | ||
| logger: { | ||
| info: vi.fn(), | ||
| }, | ||
| })); | ||
|
|
||
| vi.mock('../../../../src/services/local-filesystem/local-filesystem.service', () => ({ | ||
| LocalFilesystemService: { | ||
| instance: { | ||
| scanLocalDirectory: vi.fn(), | ||
| }, | ||
| }, | ||
| })); | ||
|
|
||
| vi.mock('../../../../src/services/network/upload/upload-folder.service', () => ({ | ||
| UploadFolderService: { | ||
| instance: { | ||
| createFolders: vi.fn(), | ||
| }, | ||
| }, | ||
| })); | ||
|
|
||
| vi.mock('../../../../src/services/network/upload/upload-file.service', () => ({ | ||
| UploadFileService: { | ||
| instance: { | ||
| uploadFilesInChunks: vi.fn(), | ||
| }, | ||
| }, | ||
| })); | ||
|
|
||
| describe('UploadFacade', () => { | ||
| let sut: UploadFacade; | ||
|
|
||
| const mockNetworkFacade = {} as NetworkFacade; | ||
|
|
||
| const mockLoginUserDetails = { | ||
| bridgeUser: 'test-bridge-user', | ||
| userId: 'test-user-id', | ||
| mnemonic: 'test-mnemonic', | ||
| bucket: 'test-bucket', | ||
| } as LoginUserDetails; | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| sut = UploadFacade.instance; | ||
| vi.mocked(CLIUtils.prepareNetwork).mockReturnValue(mockNetworkFacade); | ||
| vi.mocked(CLIUtils.timer).mockReturnValue({ | ||
| stop: vi.fn().mockReturnValue(1000), | ||
| }); | ||
| }); | ||
|
|
||
| describe('uploadFolder', () => { | ||
| const localPath = '/local/test-folder'; | ||
| const destinationFolderUuid = 'dest-uuid'; | ||
| const onProgress = vi.fn(); | ||
|
|
||
| it('should properly return an error if createFolders returns an empty map', async () => { | ||
| vi.mocked(LocalFilesystemService.instance.scanLocalDirectory).mockResolvedValue({ | ||
| folders: [createFileSystemNodeFixture({ type: 'folder', name: 'test', relativePath: 'test' })], | ||
| files: [], | ||
| totalItems: 1, | ||
| totalBytes: 0, | ||
| }); | ||
|
|
||
| vi.mocked(UploadFolderService.instance.createFolders).mockResolvedValue(new Map()); | ||
|
|
||
| const result = await sut.uploadFolder({ | ||
| localPath, | ||
| destinationFolderUuid, | ||
| loginUserDetails: mockLoginUserDetails, | ||
| jsonFlag: false, | ||
| onProgress, | ||
| }); | ||
|
|
||
| expect(result.error).toBeInstanceOf(Error); | ||
| expect(result.error?.message).toBe('Failed to create folders, cannot upload files'); | ||
| expect(UploadFolderService.instance.createFolders).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should properly handle the upload of folder and the creation of file and return proper result', async () => { | ||
| const folderName = 'test-folder'; | ||
| vi.mocked(LocalFilesystemService.instance.scanLocalDirectory).mockResolvedValue({ | ||
| folders: [createFileSystemNodeFixture({ type: 'folder', name: folderName, relativePath: folderName })], | ||
| files: [ | ||
| createFileSystemNodeFixture({ | ||
| type: 'file', | ||
| name: 'file1.txt', | ||
| relativePath: `${folderName}/file1.txt`, | ||
| size: 500, | ||
| }), | ||
| ], | ||
| totalItems: 2, | ||
| totalBytes: 500, | ||
| }); | ||
|
|
||
| const folderMap = new Map([['test-folder', 'folder-uuid-123']]); | ||
| vi.mocked(UploadFolderService.instance.createFolders).mockResolvedValue(folderMap); | ||
| vi.mocked(UploadFileService.instance.uploadFilesInChunks).mockResolvedValue(500); | ||
|
|
||
| const result = await sut.uploadFolder({ | ||
| localPath, | ||
| destinationFolderUuid, | ||
| loginUserDetails: mockLoginUserDetails, | ||
| jsonFlag: false, | ||
| onProgress, | ||
| }); | ||
|
|
||
| expect(result.error).toBeUndefined(); | ||
| expect(result.data).toBeDefined(); | ||
| expect(result.data?.totalBytes).toBe(500); | ||
| expect(result.data?.rootFolderId).toBe('folder-uuid-123'); | ||
| expect(result.data?.uploadTimeMs).toBe(1000); | ||
| expect(UploadFolderService.instance.createFolders).toHaveBeenCalled(); | ||
| expect(UploadFileService.instance.uploadFilesInChunks).toHaveBeenCalled(); | ||
| expect(logger.info).toHaveBeenCalledWith(`Scanned folder ${localPath}: found 2 items, total size 500 bytes.`); | ||
| }); | ||
|
|
||
| it('should report progress correctly during upload', async () => { | ||
| const folderName = 'test-folder'; | ||
| vi.mocked(LocalFilesystemService.instance.scanLocalDirectory).mockResolvedValue({ | ||
| folders: [createFileSystemNodeFixture({ type: 'folder', name: folderName, relativePath: folderName })], | ||
| files: [ | ||
| createFileSystemNodeFixture({ | ||
| type: 'file', | ||
| name: 'file1.txt', | ||
| relativePath: `${folderName}/file1.txt`, | ||
| size: 400, | ||
| }), | ||
| ], | ||
| totalItems: 2, | ||
| totalBytes: 400, | ||
| }); | ||
|
|
||
| const folderMap = new Map([['test-folder', 'folder-uuid-123']]); | ||
|
|
||
| vi.mocked(UploadFolderService.instance.createFolders).mockImplementation( | ||
| async ({ currentProgress, emitProgress }) => { | ||
| currentProgress.itemsUploaded = 1; | ||
| emitProgress(); | ||
| return folderMap; | ||
| }, | ||
| ); | ||
|
|
||
| vi.mocked(UploadFileService.instance.uploadFilesInChunks).mockImplementation( | ||
| async ({ currentProgress, emitProgress }) => { | ||
| currentProgress.itemsUploaded = 2; | ||
| currentProgress.bytesUploaded = 400; | ||
| emitProgress(); | ||
| return 400; | ||
| }, | ||
| ); | ||
|
|
||
| await sut.uploadFolder({ | ||
| localPath, | ||
| destinationFolderUuid, | ||
| loginUserDetails: mockLoginUserDetails, | ||
| jsonFlag: false, | ||
| onProgress, | ||
| }); | ||
|
|
||
| expect(onProgress).toHaveBeenCalledTimes(2); | ||
| expect(onProgress).toHaveBeenNthCalledWith(1, { percentage: 25 }); | ||
| expect(onProgress).toHaveBeenNthCalledWith(2, { percentage: 100 }); | ||
| }); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.