Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
JoanVicens committed Feb 28, 2024
1 parent 612da91 commit 479a10f
Show file tree
Hide file tree
Showing 16 changed files with 82 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/apps/main/fordwardToWindows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ MainProcessSyncEngineIPC.on('FILE_DOWNLOADING', (_, payload) => {
});
});

ipcMainDrive.on('FILE_PREPARING', (_, payload) => {
MainProcessSyncEngineIPC.on('FILE_PREPARING', (_, payload) => {
const { nameWithExtension, processInfo } = payload;

broadcastToWindows('sync-info-update', {
Expand All @@ -30,7 +30,7 @@ ipcMainDrive.on('FILE_PREPARING', (_, payload) => {
});
});

ipcMainDrive.on('FILE_DOWNLOADED', (_, payload) => {
MainProcessSyncEngineIPC.on('FILE_DOWNLOADED', (_, payload) => {
const { nameWithExtension } = payload;

broadcastToWindows('sync-info-update', {
Expand Down
4 changes: 2 additions & 2 deletions src/apps/main/tray/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ MainProcessSyncEngineIPC.on('FILE_DOWNLOADING', () => {
setTrayStatus('SYNCING');
});

ipcMainDrive.on('FILE_PREPARING', () => {
MainProcessSyncEngineIPC.on('FILE_PREPARING', () => {
setTrayStatus('SYNCING');
});

ipcMainDrive.on('FILE_DOWNLOADED', () => {
MainProcessSyncEngineIPC.on('FILE_DOWNLOADED', () => {
setTrayStatus('IDLE');
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ import { TreeEvents } from './backgroundEvents/tree';

export type BackgroundProcessVirtualDriveEvents = FilesEvents &
FolderEvents &
TreeEvents;
TreeEvents & {
CHECK_SYNC: () => void;
};
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,6 @@ export type FilesEvents = {
FILE_OVERWRITTEN: (payload: { nameWithExtension: string }) => void;

FILE_CLONED: (payload: FileUpdatePayload) => void;

FILE_PREPARING: (payload: FileUpdatePayload) => void;
};
2 changes: 1 addition & 1 deletion src/apps/shared/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
type DriveOperationInProgress = {
action: 'UPLOADING' | 'DOWNLOADING' | 'RENAMING' | 'DELETING';
action: 'UPLOADING' | 'DOWNLOADING' | 'RENAMING' | 'DELETING' | 'PREPARING';
progress: number;
name: string;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ export class DeleteController extends CallbackController {
throw new Error(`Placeholder Id not identified: ${trimmedId}`);
}

private CleanQueuesByFolder(folderUuid: Folder['uuid']) {
private async CleanQueuesByFolder(folderUuid: Folder['uuid']) {
// always remove files from the filesQueue if a folder is added
this.CleanQueueFile(folderUuid);
// remove child folders from the queue if a parent folder exists
this.CleanQueueFolder(folderUuid);
await this.CleanQueueFolder(folderUuid);
}

private CleanQueueFile(folderUuid: Folder['uuid']) {
Expand All @@ -88,16 +88,18 @@ export class DeleteController extends CallbackController {
});
}

private CleanQueueFolder(folderUuid: Folder['uuid']) {
private async CleanQueueFolder(folderUuid: Folder['uuid']) {
const reversedFolders = this.foldersQueue.reversedValues;
reversedFolders.forEach((folder) => {
const isParentFolder = this.folderContainerDetector.run(
const allDone = reversedFolders.map(async (folder) => {
const isParentFolder = await this.folderContainerDetector.run(
folder,
folderUuid
);
if (isParentFolder) {
this.foldersQueue.removeOne(folder);
}
});

await Promise.all(allDone);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { CreateFilePlaceholderOnDeletionFailed } from '../../../../context/virtual-drive/files/application/CreateFilePlaceholderOnDeletionFailed';
import { FileCheckerStatusInRoot } from '../../../../context/virtual-drive/files/application/FileCheckerStatusInRoot';
import { FileCreator } from '../../../../context/virtual-drive/files/application/FileCreator';
import { FileDeleter } from '../../../../context/virtual-drive/files/application/FileDeleter';
import { FileFolderContainerDetector } from '../../../../context/virtual-drive/files/application/FileFolderContainerDetector';
Expand Down Expand Up @@ -26,4 +27,5 @@ export interface FilesContainer {
filesPlaceholderCreator: FilesPlaceholderCreator;
filesPlaceholderUpdater: FilesPlaceholderUpdater;
singleFileMatchingFinder: SingleFileMatchingFinder;
filesCheckerStatusInRoot: FileCheckerStatusInRoot;
}
1 change: 1 addition & 0 deletions src/apps/sync-engine/dependency-injection/files/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ export async function buildFilesContainer(
filesPlaceholderCreator,
filesPlaceholderUpdater,
singleFileMatchingFinder,
filesCheckerStatusInRoot,
};

return { container, subscribers: [] };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { FolderPlaceholderConverter } from '../../../../context/virtual-drive/fo
import { FolderSyncStatusUpdater } from '../../../../context/virtual-drive/folders/application/FolderSyncStatusUpdater';
import { FoldersFatherSyncStatusUpdater } from '../../../../context/virtual-drive/folders/application/FoldersFatherSyncStatusUpdater';
import { FolderFinder } from '../../../../context/virtual-drive/folders/application/FolderFinder';
import { FolderContainerDetector } from '../../../../context/virtual-drive/folders/application/FolderContainerDetector';

export interface FoldersContainer {
folderCreator: FolderCreatorFromOfflineFolder;
Expand All @@ -36,4 +37,5 @@ export interface FoldersContainer {
folderSyncStatusUpdater: FolderSyncStatusUpdater;
foldersFatherSyncStatusUpdater: FoldersFatherSyncStatusUpdater;
folderFinder: FolderFinder;
folderContainerDetector: FolderContainerDetector;
}
Original file line number Diff line number Diff line change
Expand Up @@ -173,5 +173,6 @@ export async function buildFoldersContainer(
folderPlaceholderConverter,
folderSyncStatusUpdater,
foldersFatherSyncStatusUpdater,
folderContainerDetector,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export class FuseEnvironmentContentFileDownloader
this.state = null;
}

removeListeners(): void {
// no-op
}

forceStop(): void {
//@ts-ignore
// Logger.debug('Finish emitter type', this.state?.type);
Expand Down
22 changes: 13 additions & 9 deletions src/context/virtual-drive/files/application/FileSyncronizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,25 +83,29 @@ export class FileSynchronizer {
posixRelativePath: string,
filePath: FilePath,
upload: (path: string) => Promise<RemoteFileContents>,
attemps = 3
attempts = 3
) => {
try {
await new Promise((resolve) => setTimeout(resolve, 2000));
const fileContents = await upload(posixRelativePath);
const createdFile = await this.fileCreator.run(filePath, fileContents);
const createdFile = await this.fileCreator.run(
filePath,
fileContents.id,
fileContents.size
);
await this.convertAndUpdateSyncStatus(createdFile);
} catch (error: unknown) {
Logger.error('Error creating file:', error);
if (error instanceof FolderNotFoundError) {
await this.createFolderFather(posixRelativePath);
}
if (attemps > 0) {
if (attempts > 0) {
await new Promise((resolve) => setTimeout(resolve, 2000));
await this.retryCreation(
posixRelativePath,
filePath,
upload,
attemps - 1
attempts - 1
);
return;
}
Expand Down Expand Up @@ -141,8 +145,8 @@ export class FileSynchronizer {
}
}

private hasDifferentSize(file: File, absoulthePath: string) {
const stats = fs.statSync(absoulthePath);
private hasDifferentSize(file: File, absolutePath: string) {
const stats = fs.statSync(absolutePath);
return Math.abs(file.size - stats.size) > 0.001;
}

Expand All @@ -153,14 +157,14 @@ export class FileSynchronizer {
]);
}

private retryFolderCreation = async (posixDir: string, attemps = 3) => {
private retryFolderCreation = async (posixDir: string, attempts = 3) => {
try {
await new Promise((resolve) => setTimeout(resolve, 4000));
await this.runFolderCreator(posixDir);
} catch (error) {
Logger.error('Error creating folder father creation:', error);
if (attemps > 0) {
await this.retryFolderCreation(posixDir, attemps - 1);
if (attempts > 0) {
await this.retryFolderCreation(posixDir, attempts - 1);
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,39 @@
import {
PinState,
SyncState,
} from '../../../../apps/shared/types/PlaceholderStates';
import { RelativePathToAbsoluteConverter } from '../../shared/application/RelativePathToAbsoluteConverter';
import { File } from '../domain/File';
import { PlaceholderState } from '../domain/PlaceholderState';
import { LocalFileSystem } from '../domain/file-systems/LocalFileSystem';
import fs from 'fs/promises';

export class FuseLocalFileSystem implements LocalFileSystem {
constructor(
private readonly relativePathToAbsoluteConverter: RelativePathToAbsoluteConverter
) {}

async updateSyncStatus(_file: File): Promise<void> {
// no-op
}

async convertToPlaceholder(_file: File): Promise<void> {
// no-op
}

async getPlaceholderState(_file: File): Promise<void> {
// no-op
}

async getPlaceholderStateByRelativePath(
_relativePath: string
): Promise<PlaceholderState> {
return {
pinState: PinState.Inherited,
syncState: SyncState.InSync,
};
}

async createPlaceHolder(_file: File): Promise<void> {
// no-op
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@ import { FolderRepository } from '../domain/FolderRepository';
export class FolderContainerDetector {
constructor(private readonly repository: FolderRepository) {}

run(
fodlerContentId: Folder['uuid'],
async run(
folderContentId: Folder['uuid'],
parentFolderContentId: Folder['uuid']
): boolean {
const folder = this.repository.searchByPartial({ uuid: fodlerContentId });
): Promise<boolean> {
const folder = await this.repository.searchByUuid(folderContentId);

if (!folder) {
throw new Error('Folder not found');
}

const parent = this.repository.searchByPartial({
id: folder.parentId as number,
});
const parent = await this.repository.searchById(folder.parentId as number);

if (!parent) {
throw new Error('Parent folder not found');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class FoldersFatherSyncStatusUpdater {
if (posixDir === '/') {
return;
}
const folder = await this.repository.searchByPartial({ path: posixDir });
const folder = this.repository.matchingPartial({ path: posixDir })[0];
if (folder) {
Logger.debug(`Updating sync status for ${folder.path}`);
await this.localFileSystem.updateSyncStatus(folder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ import { Folder } from '../domain/Folder';
import { LocalFileSystem } from '../domain/file-systems/LocalFileSystem';

export class FuseLocalFileSystem implements LocalFileSystem {
async updateSyncStatus(_folder: Folder): Promise<void> {
//no-op
}
async convertToPlaceholder(_folder: Folder): Promise<void> {
//no-op
}
async getPlaceholderState(_folder: Folder): Promise<void> {
//no-op
}
async createPlaceHolder(_folder: Folder): Promise<void> {
//no-op
}
Expand Down

0 comments on commit 479a10f

Please sign in to comment.