Skip to content

Commit

Permalink
Fixed some code smells
Browse files Browse the repository at this point in the history
  • Loading branch information
ArceDanielShok committed Jul 18, 2024
1 parent 2ba3879 commit 8e91019
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 101 deletions.
4 changes: 2 additions & 2 deletions src/apps/main/realtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ function cleanAndStartRemoteNotifications() {
socket.on('event', (data) => {
logger.log('Notification received: ', JSON.stringify(data, null, 2));

broadcastToWindows('remote-changes', undefined);
eventBus.emit('RECEIVED_REMOTE_CHANGES');
// broadcastToWindows('remote-changes', undefined);
// eventBus.emit('RECEIVED_REMOTE_CHANGES');
});
}

Expand Down
16 changes: 8 additions & 8 deletions src/apps/main/remote-sync/RemoteSyncManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,14 @@ export class RemoteSyncManager {
Logger.info('Starting remote to local sync');
Logger.info('Checking if we are in a valid state to start the sync');

// const testPassed = this.smokeTest();

// if (!testPassed) {
// return {
// files: [],
// folders: [],
// };
// }
const testPassed = this.smokeTest();

if (!testPassed && !folderId) {
return {
files: [],
folders: [],
};
}
this.totalFilesSynced = 0;
this.totalFilesUnsynced = [];
this.totalFoldersSynced = 0;
Expand Down
12 changes: 6 additions & 6 deletions src/apps/main/remote-sync/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ ipcMain.handle('get-remote-sync-status', () =>
remoteSyncManager.getSyncStatus()
);

export async function updateRemoteSync(folderId?: number): Promise<void> {
export async function updateRemoteSync(): Promise<void> {
// Wait before checking for updates, could be possible
// that we received the notification, but if we check
// for new data we don't receive it
Expand All @@ -156,7 +156,10 @@ export async function updateRemoteSync(folderId?: number): Promise<void> {
Logger.info('Remote sync is already running');
return;
}
await sleep(5_000);
const userData = configStore.get('userData');
const lastFilesSyncAt = await remoteSyncManager.getFileCheckpoint();
Logger.info('Last files sync at', lastFilesSyncAt);
const folderId = lastFilesSyncAt ? undefined : userData?.root_folder_id;
await startRemoteSync(folderId);
updateSyncEngine();
}
Expand All @@ -170,10 +173,7 @@ ipcMain.handle('SYNC_MANUALLY', async () => {
Logger.info('[Manual Sync] Received manual sync event');
const isSyncing = await checkSyncEngineInProcess(5_000);
if (isSyncing) return;

const userData = configStore.get('userData');

await updateRemoteSync(userData?.root_folder_id);
await updateRemoteSync();
await fallbackRemoteSync();
});

Expand Down
8 changes: 4 additions & 4 deletions src/apps/sync-engine/BindingManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class BindingsManager {
}

async start(version: string, providerId: string) {
await ipcRendererSyncEngine.send('SYNCING');
ipcRendererSyncEngine.send('SYNCING');
await this.stop();
await this.pollingStart();

Expand Down Expand Up @@ -278,10 +278,10 @@ export class BindingsManager {
await this.container.virtualDrive.connectSyncRoot();

await runner([this.load.bind(this), this.polling.bind(this)]);
await ipcRendererSyncEngine.send('SYNCED');
ipcRendererSyncEngine.send('SYNCED');
}

watch() {
async watch() {
const queueManager = new QueueManager({
handleAdd: async (task: QueueItem) => {
try {
Expand Down Expand Up @@ -361,7 +361,7 @@ export class BindingsManager {
queueManager,
logWatcherPath
);
queueManager.processAll();
await queueManager.processAll();
}

async stop() {
Expand Down
40 changes: 24 additions & 16 deletions src/context/virtual-drive/contents/application/ContentsUploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { PlatformPathConverter } from '../../shared/application/PlatformPathConv
import { RelativePathToAbsoluteConverter } from '../../shared/application/RelativePathToAbsoluteConverter';
import { SyncEngineIpc } from '../../../../apps/sync-engine/ipcRendererSyncEngine';
import { ipcRenderer } from 'electron';

import Logger from 'electron-log';
export class ContentsUploader {
constructor(
private readonly remoteContentsManagersFactory: ContentsManagersFactory,
Expand Down Expand Up @@ -64,27 +64,35 @@ export class ContentsUploader {
}

async run(posixRelativePath: string): Promise<RemoteFileContents> {
const win32RelativePath =
PlatformPathConverter.posixToWin(posixRelativePath);
try {
const win32RelativePath =
PlatformPathConverter.posixToWin(posixRelativePath);

const absolutePath =
this.relativePathToAbsoluteConverter.run(win32RelativePath);

const absolutePath =
this.relativePathToAbsoluteConverter.run(win32RelativePath);
Logger.debug('[DEBUG UPLOAD]:', posixRelativePath, absolutePath);

const { contents, abortSignal } = await this.contentProvider.provide(
absolutePath
);
const { contents, abortSignal } = await this.contentProvider.provide(
absolutePath
);
Logger.debug('[DEBUG UPLOAD STEEP 1]: ');

const uploader = this.remoteContentsManagersFactory.uploader(
contents,
abortSignal
);
const uploader = this.remoteContentsManagersFactory.uploader(
contents,
abortSignal
);

this.registerEvents(uploader, contents);
this.registerEvents(uploader, contents);

const contentsId = await uploader.upload(contents.stream, contents.size);
const contentsId = await uploader.upload(contents.stream, contents.size);

const fileContents = RemoteFileContents.create(contentsId, contents.size);
const fileContents = RemoteFileContents.create(contentsId, contents.size);

return fileContents;
return fileContents;
} catch (error: unknown) {
Logger.error('[ERROR DEBUG]', error);
throw error;
}
}
}
18 changes: 17 additions & 1 deletion src/context/virtual-drive/files/application/FileCreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { OfflineFile } from '../domain/OfflineFile';
import { SyncEngineIpc } from '../../../../apps/sync-engine/ipcRendererSyncEngine';
import { FileStatuses } from '../domain/FileStatus';
import { ipcRenderer } from 'electron';
import Logger from 'electron-log';

export class FileCreator {
constructor(
Expand Down Expand Up @@ -39,18 +40,30 @@ export class FileCreator {
await this.fileDeleter.run(existingFile.contentsId);
}
}

Logger.debug('[DEBUG IN FILECREATOR STEEP 1]' + filePath.value);
const size = new FileSize(contents.size);

const folder = this.folderFinder.findFromFilePath(filePath);

Logger.debug('[DEBUG IN FILECREATOR STEEP 2]' + filePath.value);

const offline = OfflineFile.create(contents.id, folder, size, filePath);

Logger.debug('[DEBUG IN FILECREATOR STEEP 3]' + filePath.value);

const existingPersistedFile = await this.remote.checkStatusFile(contents.id);

Logger.debug('[DEBUG IN FILECREATOR STEEP 3.1]' + existingPersistedFile);

const persistedAttributes = await this.remote.persist(offline);

Logger.debug('[DEBUG IN FILECREATOR STEEP 4]' + filePath.value);
const file = File.from(persistedAttributes);

Logger.debug('[DEBUG IN FILECREATOR STEEP 5]' + filePath.value);
await this.repository.add(file);

Logger.debug('[DEBUG IN FILECREATOR STEEP 6]' + filePath.value);
await this.eventBus.publish(offline.pullDomainEvents());
this.ipc.send('FILE_CREATED', {
name: file.name,
Expand All @@ -59,10 +72,13 @@ export class FileCreator {
});
ipcRenderer.send('CHECK_SYNC');

Logger.debug('[DEBUG IN FILECREATOR STEEP 7]' + filePath.value);
return file;
} catch (error: unknown) {
const message = error instanceof Error ? error.message : 'unknown error';

Logger.debug('[DEBUG ERROR IN FILECREATOR]' + filePath.value, error);

this.ipc.send('FILE_UPLOAD_ERROR', {
name: filePath.name(),
extension: filePath.extension(),
Expand Down
104 changes: 40 additions & 64 deletions src/context/virtual-drive/items/application/RemoteItemsGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { DriveFile } from '../../../../apps/main/database/entities/DriveFile';
import { DriveFolder } from '../../../../apps/main/database/entities/DriveFolder';
import { SyncEngineIpc } from '../../../../apps/sync-engine/ipcRendererSyncEngine';
import {
ServerFile,
Expand All @@ -11,45 +13,49 @@ import {
export class RemoteItemsGenerator {
constructor(private readonly ipc: SyncEngineIpc) {}

private mapFile(updatedFile: DriveFile): ServerFile {
return {
bucket: updatedFile.bucket,
createdAt: updatedFile.createdAt,
encrypt_version: '03-aes',
fileId: updatedFile.fileId,
folderId: updatedFile.folderId,
id: updatedFile.id,
modificationTime: updatedFile.modificationTime,
name: updatedFile.name,
plainName: updatedFile.plainName,
size: updatedFile.size,
type: updatedFile.type ?? null,
updatedAt: updatedFile.updatedAt,
userId: updatedFile.userId,
status: updatedFile.status as ServerFileStatus,
uuid: updatedFile.uuid,
};
}

private mapFolder(updatedFolder: DriveFolder): ServerFolder {
return {
bucket: updatedFolder.bucket ?? null,
createdAt: updatedFolder.createdAt,
id: updatedFolder.id,
name: updatedFolder.name,
parentId: updatedFolder.parentId ?? null,
updatedAt: updatedFolder.updatedAt,
plain_name: updatedFolder.plainName ?? null,
status: updatedFolder.status as ServerFolderStatus,
uuid: updatedFolder.uuid,
};
}

async getAll(): Promise<{ files: ServerFile[]; folders: ServerFolder[] }> {
const updatedRemoteItems = await this.ipc.invoke(
'GET_UPDATED_REMOTE_ITEMS'
);

const files = updatedRemoteItems.files.map<ServerFile>((updatedFile) => {
return {
bucket: updatedFile.bucket,
createdAt: updatedFile.createdAt,
encrypt_version: '03-aes',
fileId: updatedFile.fileId,
folderId: updatedFile.folderId,
id: updatedFile.id,
modificationTime: updatedFile.modificationTime,
name: updatedFile.name,
plainName: updatedFile.plainName,
size: updatedFile.size,
type: updatedFile.type ?? null,
updatedAt: updatedFile.updatedAt,
userId: updatedFile.userId,
status: updatedFile.status as ServerFileStatus,
uuid: updatedFile.uuid,
};
});
const files = updatedRemoteItems.files.map<ServerFile>(this.mapFile);

const folders = updatedRemoteItems.folders.map<ServerFolder>(
(updatedFolder) => {
return {
bucket: updatedFolder.bucket ?? null,
createdAt: updatedFolder.createdAt,
id: updatedFolder.id,
name: updatedFolder.name,
parentId: updatedFolder.parentId ?? null,
updatedAt: updatedFolder.updatedAt,
plain_name: updatedFolder.plainName ?? null,
status: updatedFolder.status as ServerFolderStatus,
uuid: updatedFolder.uuid,
};
}
this.mapFolder
);

return { files, folders };
Expand All @@ -63,40 +69,10 @@ export class RemoteItemsGenerator {
folderId
);

const files = updatedRemoteItems.files.map<ServerFile>((updatedFile) => {
return {
bucket: updatedFile.bucket,
createdAt: updatedFile.createdAt,
encrypt_version: '03-aes',
fileId: updatedFile.fileId,
folderId: updatedFile.folderId,
id: updatedFile.id,
modificationTime: updatedFile.modificationTime,
name: updatedFile.name,
plainName: updatedFile.plainName,
size: updatedFile.size,
type: updatedFile.type ?? null,
updatedAt: updatedFile.updatedAt,
userId: updatedFile.userId,
status: updatedFile.status as ServerFileStatus,
uuid: updatedFile.uuid,
};
});
const files = updatedRemoteItems.files.map<ServerFile>(this.mapFile);

const folders = updatedRemoteItems.folders.map<ServerFolder>(
(updatedFolder) => {
return {
bucket: updatedFolder.bucket ?? null,
createdAt: updatedFolder.createdAt,
id: updatedFolder.id,
name: updatedFolder.name,
parentId: updatedFolder.parentId ?? null,
updatedAt: updatedFolder.updatedAt,
plain_name: updatedFolder.plainName ?? null,
status: updatedFolder.status as ServerFolderStatus,
uuid: updatedFolder.uuid,
};
}
this.mapFolder
);

return { files, folders };
Expand Down

0 comments on commit 8e91019

Please sign in to comment.