From fc2de19cb0d659ea98b19cf3f2a788b1f44d2cfd Mon Sep 17 00:00:00 2001 From: joan vicens Date: Thu, 21 Mar 2024 13:52:54 +0100 Subject: [PATCH] chore: improve file creator api --- .../application/FileCreationOrchestrator.ts | 2 +- .../files/application/FileCreator.ts | 16 +++++++--------- .../CreateFileOnOfflineFileUplodaded.ts | 4 +--- .../virtual-drive/files/domain/OfflineFile.ts | 11 +++-------- .../files/application/FileCreator.test.ts | 6 +++--- 5 files changed, 15 insertions(+), 24 deletions(-) diff --git a/src/context/virtual-drive/boundaryBridge/application/FileCreationOrchestrator.ts b/src/context/virtual-drive/boundaryBridge/application/FileCreationOrchestrator.ts index f39291c51..98a033698 100644 --- a/src/context/virtual-drive/boundaryBridge/application/FileCreationOrchestrator.ts +++ b/src/context/virtual-drive/boundaryBridge/application/FileCreationOrchestrator.ts @@ -25,7 +25,7 @@ export class FileCreationOrchestrator { const fileContents = await this.contentsUploader.run(posixRelativePath); const createdFile = await this.fileCreator.run( - path, + path.value, fileContents.id, fileContents.size ); diff --git a/src/context/virtual-drive/files/application/FileCreator.ts b/src/context/virtual-drive/files/application/FileCreator.ts index 7cc6565c3..b8936e621 100644 --- a/src/context/virtual-drive/files/application/FileCreator.ts +++ b/src/context/virtual-drive/files/application/FileCreator.ts @@ -12,7 +12,8 @@ import { FileStatuses } from '../domain/FileStatus'; import { DriveDesktopError } from '../../../shared/domain/errors/DriveDesktopError'; import Logger from 'electron-log'; import { ParentFolderFinder } from '../../folders/application/ParentFolderFinder'; - +import { ContentsId } from '../../contents/domain/ContentsId'; +import { basename } from 'path'; export class FileCreator { constructor( private readonly remote: RemoteFileSystem, @@ -23,14 +24,10 @@ export class FileCreator { private readonly notifier: SyncFileMessenger ) {} - async run( - filePath: FilePath, - contentsId: string, - size: number - ): Promise { + async run(path: string, contentsId: string, size: number): Promise { try { const existingFiles = this.repository.matchingPartial({ - path: PlatformPathConverter.winToPosix(filePath.value), + path: PlatformPathConverter.winToPosix(path), status: FileStatuses.EXISTS, }); @@ -43,11 +40,12 @@ export class FileCreator { } const fileSize = new FileSize(size); + const filePath = new FilePath(path); const folder = await this.parentFolderFinder.run(filePath); const offline = OfflineFile.create( - contentsId, + new ContentsId(contentsId), folder, fileSize, filePath @@ -73,7 +71,7 @@ export class FileCreator { await this.notifier.issues({ error: 'UPLOAD_ERROR', cause, - name: filePath.nameWithExtension(), + name: basename(path), }); throw error; diff --git a/src/context/virtual-drive/files/application/event-subsribers/CreateFileOnOfflineFileUplodaded.ts b/src/context/virtual-drive/files/application/event-subsribers/CreateFileOnOfflineFileUplodaded.ts index 55c32b89d..593d16576 100644 --- a/src/context/virtual-drive/files/application/event-subsribers/CreateFileOnOfflineFileUplodaded.ts +++ b/src/context/virtual-drive/files/application/event-subsribers/CreateFileOnOfflineFileUplodaded.ts @@ -1,7 +1,6 @@ import { OfflineContentsUploadedDomainEvent } from '../../../../offline-drive/contents/domain/events/OfflineContentsUploadedDomainEvent'; import { DomainEventClass } from '../../../../shared/domain/DomainEvent'; import { DomainEventSubscriber } from '../../../../shared/domain/DomainEventSubscriber'; -import { FilePath } from '../../domain/FilePath'; import { FileCreator } from '../FileCreator'; import Logger from 'electron-log'; import { FileToOverrideProvider } from '../FileToOverrideProvider'; @@ -28,8 +27,7 @@ export class CreateFileOnOfflineFileUploaded return; } - const filePath = new FilePath(event.path); - await this.creator.run(filePath, event.aggregateId, event.size); + await this.creator.run(event.path, event.aggregateId, event.size); } catch (err) { Logger.error('[CreateFileOnOfflineFileUploaded]:', err); } diff --git a/src/context/virtual-drive/files/domain/OfflineFile.ts b/src/context/virtual-drive/files/domain/OfflineFile.ts index f789359c7..3434054d3 100644 --- a/src/context/virtual-drive/files/domain/OfflineFile.ts +++ b/src/context/virtual-drive/files/domain/OfflineFile.ts @@ -64,21 +64,16 @@ export class OfflineFile extends AggregateRoot { } static create( - contentsId: string, + contentsId: ContentsId, folder: Folder, size: FileSize, path: FilePath ): OfflineFile { - const file = new OfflineFile( - new ContentsId(contentsId), - folder.id, - path, - size - ); + const file = new OfflineFile(contentsId, folder.id, path, size); file.record( new FileCreatedDomainEvent({ - aggregateId: contentsId, + aggregateId: contentsId.value, size: file.size, type: path.extension(), path: file.path, diff --git a/tests/context/virtual-drive/files/application/FileCreator.test.ts b/tests/context/virtual-drive/files/application/FileCreator.test.ts index c1f015117..5f46a4909 100644 --- a/tests/context/virtual-drive/files/application/FileCreator.test.ts +++ b/tests/context/virtual-drive/files/application/FileCreator.test.ts @@ -53,7 +53,7 @@ describe('File Creator', () => { remoteFileSystemMock.persistMock.mockResolvedValueOnce(fileAttributes); - await SUT.run(path, contents.id, contents.size); + await SUT.run(path.value, contents.id, contents.size); expect(fileRepository.addMock).toBeCalledWith( expect.objectContaining({ @@ -76,7 +76,7 @@ describe('File Creator', () => { remoteFileSystemMock.persistMock.mockResolvedValueOnce(fileAttributes); - await SUT.run(path, contents.id, contents.size); + await SUT.run(path.value, contents.id, contents.size); expect(eventBus.publishMock.mock.calls[0][0][0].eventName).toBe( 'file.created' @@ -109,7 +109,7 @@ describe('File Creator', () => { // returns Promise }); - await SUT.run(path, contents.id, contents.size); + await SUT.run(path.value, contents.id, contents.size); expect(deleterSpy).toBeCalledWith(existingFile.contentsId);