Skip to content

Commit

Permalink
wip: find last acceded file when uploading contents
Browse files Browse the repository at this point in the history
  • Loading branch information
JoanVicens committed Mar 21, 2024
1 parent b43a5b6 commit fe23037
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/apps/fuse/callbacks/WriteCallback.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { OfflineDriveDependencyContainer } from '../dependency-injection/offline/OfflineDriveDependencyContainer';
import Logger from 'electron-log';

export class WriteCallback {
constructor(private readonly container: OfflineDriveDependencyContainer) {}
Expand All @@ -8,9 +9,11 @@ export class WriteCallback {
_fd: string,
buffer: Buffer,
len: number,
_pos: number,
pos: number,
cb: (a: number) => void
) {
Logger.debug('WRITE: ', path, len, pos);

await this.container.offlineContentsAppender.run(path, buffer);

return cb(len);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { CreateFileOnOfflineFileUploaded } from '../../../../../context/virtual-
import { FileRepositoryInitializer } from '../../../../../context/virtual-drive/files/application/FileRepositoryInitializer';
import { SyncFileMessenger } from '../../../../../context/virtual-drive/files/domain/SyncFileMessenger';
import { FilesSearcherByPartialMatch } from '../../../../../context/virtual-drive/files/application/search-all/FilesSearcherByPartialMatch';
import { FileToOverrideProvider } from '../../../../../context/virtual-drive/files/application/FileToOverrideProvider';

export interface FilesContainer {
filesByFolderPathNameLister: FilesByFolderPathSearcher;
Expand All @@ -19,6 +20,7 @@ export interface FilesContainer {
repositoryPopulator: FileRepositoryInitializer;
syncFileMessenger: SyncFileMessenger;
filesSearcherByPartialMatch: FilesSearcherByPartialMatch;
fileToOverrideProvider: FileToOverrideProvider;
// event handler
createFileOnOfflineFileUploaded: CreateFileOnOfflineFileUploaded;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { FilesContainer } from './FilesContainer';
import { InMemoryFileRepositorySingleton } from '../../../../shared/dependency-injection/virtual-drive/files/InMemoryFileRepositorySingleton';
import { SingleFileMatchingSearcher } from '../../../../../context/virtual-drive/files/application/SingleFileMatchingSearcher';
import { FilesSearcherByPartialMatch } from '../../../../../context/virtual-drive/files/application/search-all/FilesSearcherByPartialMatch';
import { FileToOverrideProvider } from '../../../../../context/virtual-drive/files/application/FileToOverrideProvider';

export async function buildFilesContainer(
initialFiles: Array<File>,
Expand Down Expand Up @@ -92,8 +93,14 @@ export async function buildFilesContainer(
syncFileMessenger
);

const fileToOverrideProvider = new FileToOverrideProvider(
eventRepository,
repository
);

const createFileOnOfflineFileUploaded = new CreateFileOnOfflineFileUploaded(
fileCreator
fileCreator,
fileToOverrideProvider
);

const filesSearcherByPartialMatch = new FilesSearcherByPartialMatch(
Expand All @@ -110,6 +117,7 @@ export async function buildFilesContainer(
repositoryPopulator,
syncFileMessenger,
filesSearcherByPartialMatch,
fileToOverrideProvider,
// event handlers
createFileOnOfflineFileUploaded,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { LocalFileContents } from '../domain/LocalFileContents';
import { LocalFileSystem } from '../domain/LocalFileSystem';
import { ContentFileDownloader } from '../domain/contentHandlers/ContentFileDownloader';
import Logger from 'electron-log';
import { ContentsAccededDomainEvent } from '../domain/events/ContentsAccededDomainEvent';

export class DownloadContentsToPlainFile {
constructor(
Expand Down Expand Up @@ -36,6 +37,13 @@ export class DownloadContentsToPlainFile {
async run(file: File): Promise<void> {
const contentsId = new ContentsId(file.contentsId);

await this.eventBus.publish([
new ContentsAccededDomainEvent({
aggregateId: file.contentsId,
path: file.path,
}),
]);

const metadata = await this.local.metadata(contentsId);

if (metadata) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { DomainEvent } from '../../../../shared/domain/DomainEvent';

export class ContentsAccededDomainEvent extends DomainEvent {
static readonly EVENT_NAME = 'contents.acceded';

readonly path: string;

constructor({ aggregateId, path }: { aggregateId: string; path: string }) {
super({
eventName: ContentsAccededDomainEvent.EVENT_NAME,
aggregateId,
});

this.path = path;
}

toPrimitives() {
return {
aggregateId: this.aggregateId,
eventId: this.eventId,
path: this.path,
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,28 @@ import { DomainEventSubscriber } from '../../../shared/domain/DomainEventSubscri
import { FilePath } from '../domain/FilePath';
import { FileCreator } from './FileCreator';
import Logger from 'electron-log';
import { FileToOverrideProvider } from './FileToOverrideProvider';

export class CreateFileOnOfflineFileUploaded
implements DomainEventSubscriber<OfflineContentsUploadedDomainEvent>
{
constructor(private readonly creator: FileCreator) {}
constructor(
private readonly creator: FileCreator,
private readonly fileToOverrideProvider: FileToOverrideProvider
) {}

subscribedTo(): DomainEventClass[] {
return [OfflineContentsUploadedDomainEvent];
}

async on(event: OfflineContentsUploadedDomainEvent): Promise<void> {
try {
const hasToOverride = await this.fileToOverrideProvider.run();
const filePath = new FilePath(event.path);

Logger.debug('!!!!!!!!!!!!!!!!!!!!');
Logger.debug('IT SHOULD OVERRIDE:', hasToOverride);

await this.creator.run(filePath, event.aggregateId, event.size);
} catch (err) {
Logger.error('[CreateFileOnOfflineFileUploaded]:', err);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Optional } from '../../../../shared/types/Optional';
import { ContentsAccededDomainEvent } from '../../contents/domain/events/ContentsAccededDomainEvent';
import { EventRepository } from '../../shared/domain/EventRepository';
import { File } from '../domain/File';
import { FileRepository } from '../domain/FileRepository';
import { FileNotFoundError } from '../domain/errors/FileNotFoundError';

export class FileToOverrideProvider {
constructor(
private readonly eventRepository: EventRepository,
private readonly repository: FileRepository
) {}

async run(): Promise<Optional<File>> {
const eventOptional =
await this.eventRepository.last<ContentsAccededDomainEvent>(
ContentsAccededDomainEvent.EVENT_NAME
);

if (!eventOptional.isPresent()) {
return Optional.empty();
}

const event = eventOptional.get();

const file = await this.repository.searchByContentsId(event.aggregateId);

if (!file) {
throw new FileNotFoundError(event.aggregateId);
}

return Optional.of(file);
}
}
4 changes: 4 additions & 0 deletions src/context/virtual-drive/shared/domain/EventRepository.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { Optional } from '../../../../shared/types/Optional';
import { DomainEvent } from '../../../shared/domain/DomainEvent';

export interface EventRepository {
store(event: DomainEvent): Promise<void>;
search(aggregateId: string): Promise<Array<DomainEvent>>;
last<Event extends DomainEvent>(
event: Event['eventName']
): Promise<Optional<Event>>;
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Optional } from '../../../../shared/types/Optional';
import { DomainEvent } from '../../../shared/domain/DomainEvent';
import { EventRepository } from '../domain/EventRepository';

Expand Down Expand Up @@ -33,4 +34,12 @@ export class InMemoryEventRepository implements EventRepository {

return filtered as unknown as Event[];
}

async last<Event extends DomainEvent>(
eventName: Event['eventName']
): Promise<Optional<Event>> {
const event = this.events.findLast((e) => e.eventName === eventName);

return new Optional(event as Event);
}
}
2 changes: 1 addition & 1 deletion src/shared/types/Optional.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export class Optional<T> {
private constructor(private readonly value: T | undefined) {}
constructor(private readonly value: T | undefined) {}

static of<T>(value: T): Optional<T> {
return new Optional(value);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
import { DomainEvent } from '../../../../../src/context/shared/domain/DomainEvent';
import { EventRepository } from '../../../../../src/context/virtual-drive/shared/domain/EventRepository';
import { Optional } from '../../../../../src/shared/types/Optional';

export class EventRepositoryMock implements EventRepository {
public readonly storeMock = jest.fn();
public readonly searchMock = jest.fn();
public readonly lastMock = jest.fn();

store(event: DomainEvent): Promise<void> {
return this.storeMock(event);
}

search(aggregateId: string): Promise<DomainEvent[]> {
return this.searchMock(aggregateId);
}

last<Event extends DomainEvent>(
event: Event['eventName']
): Promise<Optional<Event>> {
return this.lastMock(event);
}
}

0 comments on commit fe23037

Please sign in to comment.