From 8e91019336ae91a5852fa229b58de3813249a2f2 Mon Sep 17 00:00:00 2001 From: ArceDanielShok Date: Thu, 18 Jul 2024 12:25:05 -0300 Subject: [PATCH] Fixed some code smells --- src/apps/main/realtime.ts | 4 +- .../main/remote-sync/RemoteSyncManager.ts | 16 +-- src/apps/main/remote-sync/handlers.ts | 12 +- src/apps/sync-engine/BindingManager.ts | 8 +- .../contents/application/ContentsUploader.ts | 40 ++++--- .../files/application/FileCreator.ts | 18 ++- .../items/application/RemoteItemsGenerator.ts | 104 +++++++----------- 7 files changed, 101 insertions(+), 101 deletions(-) diff --git a/src/apps/main/realtime.ts b/src/apps/main/realtime.ts index 756b2a231..e24d3bdae 100644 --- a/src/apps/main/realtime.ts +++ b/src/apps/main/realtime.ts @@ -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'); }); } diff --git a/src/apps/main/remote-sync/RemoteSyncManager.ts b/src/apps/main/remote-sync/RemoteSyncManager.ts index 51c18a0a9..d8a396160 100644 --- a/src/apps/main/remote-sync/RemoteSyncManager.ts +++ b/src/apps/main/remote-sync/RemoteSyncManager.ts @@ -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; diff --git a/src/apps/main/remote-sync/handlers.ts b/src/apps/main/remote-sync/handlers.ts index 573103b38..9c10f6ec3 100644 --- a/src/apps/main/remote-sync/handlers.ts +++ b/src/apps/main/remote-sync/handlers.ts @@ -146,7 +146,7 @@ ipcMain.handle('get-remote-sync-status', () => remoteSyncManager.getSyncStatus() ); -export async function updateRemoteSync(folderId?: number): Promise { +export async function updateRemoteSync(): Promise { // 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 @@ -156,7 +156,10 @@ export async function updateRemoteSync(folderId?: number): Promise { 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(); } @@ -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(); }); diff --git a/src/apps/sync-engine/BindingManager.ts b/src/apps/sync-engine/BindingManager.ts index 3a8bd6d8c..f6b4656f6 100644 --- a/src/apps/sync-engine/BindingManager.ts +++ b/src/apps/sync-engine/BindingManager.ts @@ -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(); @@ -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 { @@ -361,7 +361,7 @@ export class BindingsManager { queueManager, logWatcherPath ); - queueManager.processAll(); + await queueManager.processAll(); } async stop() { diff --git a/src/context/virtual-drive/contents/application/ContentsUploader.ts b/src/context/virtual-drive/contents/application/ContentsUploader.ts index a5cd724cb..ca94c2468 100644 --- a/src/context/virtual-drive/contents/application/ContentsUploader.ts +++ b/src/context/virtual-drive/contents/application/ContentsUploader.ts @@ -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, @@ -64,27 +64,35 @@ export class ContentsUploader { } async run(posixRelativePath: string): Promise { - 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; + } } } diff --git a/src/context/virtual-drive/files/application/FileCreator.ts b/src/context/virtual-drive/files/application/FileCreator.ts index 9c179a85d..341d5693d 100644 --- a/src/context/virtual-drive/files/application/FileCreator.ts +++ b/src/context/virtual-drive/files/application/FileCreator.ts @@ -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( @@ -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, @@ -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(), diff --git a/src/context/virtual-drive/items/application/RemoteItemsGenerator.ts b/src/context/virtual-drive/items/application/RemoteItemsGenerator.ts index 249410fb1..00d1712e2 100644 --- a/src/context/virtual-drive/items/application/RemoteItemsGenerator.ts +++ b/src/context/virtual-drive/items/application/RemoteItemsGenerator.ts @@ -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, @@ -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((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(this.mapFile); const folders = updatedRemoteItems.folders.map( - (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 }; @@ -63,40 +69,10 @@ export class RemoteItemsGenerator { folderId ); - const files = updatedRemoteItems.files.map((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(this.mapFile); const folders = updatedRemoteItems.folders.map( - (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 };