Skip to content

Commit

Permalink
chore: improve file creator api
Browse files Browse the repository at this point in the history
  • Loading branch information
JoanVicens committed Mar 21, 2024
1 parent 040ba56 commit fc2de19
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down
16 changes: 7 additions & 9 deletions src/context/virtual-drive/files/application/FileCreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -23,14 +24,10 @@ export class FileCreator {
private readonly notifier: SyncFileMessenger
) {}

async run(
filePath: FilePath,
contentsId: string,
size: number
): Promise<File> {
async run(path: string, contentsId: string, size: number): Promise<File> {
try {
const existingFiles = this.repository.matchingPartial({
path: PlatformPathConverter.winToPosix(filePath.value),
path: PlatformPathConverter.winToPosix(path),
status: FileStatuses.EXISTS,
});

Expand All @@ -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
Expand All @@ -73,7 +71,7 @@ export class FileCreator {
await this.notifier.issues({
error: 'UPLOAD_ERROR',
cause,
name: filePath.nameWithExtension(),
name: basename(path),
});

throw error;
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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);
}
Expand Down
11 changes: 3 additions & 8 deletions src/context/virtual-drive/files/domain/OfflineFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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'
Expand Down Expand Up @@ -109,7 +109,7 @@ describe('File Creator', () => {
// returns Promise<void>
});

await SUT.run(path, contents.id, contents.size);
await SUT.run(path.value, contents.id, contents.size);

expect(deleterSpy).toBeCalledWith(existingFile.contentsId);

Expand Down

0 comments on commit fc2de19

Please sign in to comment.