Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PB-1309]: Feat/Replace files when overwritting #435

Draft
wants to merge 8 commits into
base: windows
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/apps/main/fordwardToWindows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,24 @@ ipcMainDrive.on('FILE_CREATED', (_, payload) => {
});
});

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

broadcastToWindows('sync-info-update', {
action: 'UPDATED',
name: nameWithExtension,
});
});

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

broadcastToWindows('sync-info-update', {
action: 'UPDATING',
name: nameWithExtension,
});
});

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

Expand Down Expand Up @@ -130,3 +148,12 @@ ipcMainDrive.on('FILE_DELETION_ERROR', (_, payload: FileErrorInfo) => {
name: nameWithExtension,
});
});

ipcMainDrive.on('FILE_UPDATE_ERROR', (_, payload: FileErrorInfo) => {
const { nameWithExtension } = payload;

broadcastToWindows('sync-info-update', {
action: 'UPDATE_ERROR',
name: nameWithExtension,
});
});
12 changes: 12 additions & 0 deletions src/apps/main/tray/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ ipcMainDrive.on('FILE_UPLOADED', () => {
setTrayStatus('IDLE');
});

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

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

ipcMainDrive.on('FILE_UPLOAD_ERROR', () => {
setTrayStatus('ALERT');
});
Expand All @@ -75,3 +83,7 @@ ipcMainDrive.on('FILE_RENAME_ERROR', () => {
ipcMainDrive.on('FILE_DELETION_ERROR', () => {
setTrayStatus('ALERT');
});

ipcMainDrive.on('FILE_UPDATE_ERROR', () => {
setTrayStatus('ALERT');
});
4 changes: 3 additions & 1 deletion src/apps/renderer/hooks/useSyncInfoSubscriber.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export function useSyncInfoSubscriber() {

const itemIsAnError = [
'UPLOAD_ERROR',
'UPDATE_ERROR',
'DOWNLOAD_ERROR',
'RENAME_ERROR',
'DELETE_ERROR',
Expand All @@ -33,7 +34,7 @@ export function useSyncInfoSubscriber() {
function clearItems() {
setProcessInfoUpdatedPayload((current) =>
current.filter((item) =>
['UPLOADING', 'DOWNLOADING', 'RENAMING', 'DELETING'].includes(
['UPLOADING', 'DOWNLOADING', 'RENAMING', 'DELETING', 'UPDATING'].includes(
item.action
)
)
Expand All @@ -47,6 +48,7 @@ export function useSyncInfoSubscriber() {
item.action !== 'UPLOADING' &&
item.action !== 'DOWNLOADING' &&
item.action !== 'RENAMING' &&
item.action !== 'UPDATING' &&
item.action !== 'DELETING'
);
});
Expand Down
23 changes: 15 additions & 8 deletions src/apps/shared/IPC/events/drive.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
type FolderEvents = {
FOLDER_CREATING: (payload: { name: string }) => void;
FOLDER_CREATED: (payload: { name: string }) => void;
FOLDER_RENAMING: (payload: { oldName: string; newName: string }) => void;
FOLDER_RENAMED: (payload: { oldName: string; newName: string }) => void;
};

export type FileInfo = {
name: string;
extension: string;
Expand Down Expand Up @@ -33,6 +26,12 @@ type UploadEvents = {
FILE_UPLOAD_ERROR: (payload: FileErrorInfo) => void;
};

type UpdateEvents = {
FILE_UPDATING: (payload: FileInfo) => void;
FILE_UPDATED: (payload: FileInfo) => void;
FILE_UPDATE_ERROR: (payload: FileErrorInfo) => void;
};

type DownloadEvents = {
FILE_DOWNLOADING: (payload: FileProgressInfo) => void;
FILE_DOWNLOADED: (payload: FileProgressInfo) => void;
Expand All @@ -57,6 +56,13 @@ type RenameEvents = {
FILE_RENAME_ERROR: (payload: FileErrorInfo) => void;
};

type FolderEvents = {
FOLDER_CREATING: (payload: { name: string }) => void;
FOLDER_CREATED: (payload: { name: string }) => void;
FOLDER_RENAMING: (payload: { oldName: string; newName: string }) => void;
FOLDER_RENAMED: (payload: { oldName: string; newName: string }) => void;
};

type OverwriteEvents = {
FILE_OVERWRITED: (payload: { nameWithExtension: string }) => void;
};
Expand All @@ -78,6 +84,7 @@ type FileEvents = UploadEvents &
RenameEvents &
OverwriteEvents &
MoveEvents &
CloneEvents;
CloneEvents &
UpdateEvents;

export type DriveEvents = FolderEvents & FileEvents;
21 changes: 21 additions & 0 deletions src/apps/shared/IPC/events/sync-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,27 @@ export type FilesEvents = {
FILE_OVERWRITTEN: (payload: { nameWithExtension: string }) => void;

FILE_CLONED: (payload: FileUpdatePayload) => void;

FILE_UPDATING: (payload: {
name: string;
extension: string;
nameWithExtension: string;
size: number;
}) => void;

FILE_UPDATED: (payload: {
name: string;
extension: string;
nameWithExtension: string;
size: number;
}) => void;

FILE_UPDATE_ERROR: (payload: {
name: string;
extension: string;
nameWithExtension: string;
error: string;
}) => void;
};

export type SyncEngineInvocableFunctions = {
Expand Down
5 changes: 3 additions & 2 deletions src/apps/shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ export type ProcessIssue = ProcessInfoBase & {
| 'DOWNLOAD_ERROR'
| 'RENAME_ERROR'
| 'DELETE_ERROR'
| 'UPDATE_ERROR'
| 'METADATA_READ_ERROR';

errorName: ProcessErrorName;
Expand All @@ -223,11 +224,11 @@ export type ProcessInfoUpdatePayload =
| (ProcessInfoBase &
(
| {
action: 'UPLOADING' | 'DOWNLOADING' | 'RENAMING' | 'DELETING';
action: 'UPLOADING' | 'DOWNLOADING' | 'RENAMING' | 'DELETING' | 'UPDATING';
progress: number;
}
| {
action: 'UPLOADED' | 'DOWNLOADED' | 'RENAMED' | 'DELETED';
action: 'UPLOADED' | 'DOWNLOADED' | 'RENAMED' | 'DELETED' | 'UPDATED';
}
))
| ProcessIssue;
Expand Down
6 changes: 6 additions & 0 deletions src/apps/sync-engine/BindingManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,12 @@ export class BindingsManager {
callback(false);
}
},
notifyFileUpdatedCallback: (
absolutePath: string,
callback: (acknowledge: boolean, id: string) => boolean
) => {
controllers.updateFile.execute(absolutePath, callback);
},
validateDataCallback: () => {
Logger.debug('validateDataCallback');
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { DeleteController } from './controllers/DeleteController';
import { DownloadFileController } from './controllers/DownloadFileController';
import { NotifyPlaceholderHydrationFinished } from './controllers/NotifyPlaceholderHydrationFinished';
import { RenameOrMoveController } from './controllers/RenameOrMoveController';
import { UpdateFileController } from './controllers/UpdateFileController';
import { OfflineRenameOrMoveController } from './controllers/offline/OfflineRenameOrMoveController';

export function buildControllers(container: DependencyContainer) {
Expand All @@ -14,6 +15,11 @@ export function buildControllers(container: DependencyContainer) {
container.offline.folderCreator
);

const updateFileController = new UpdateFileController(
container.fileCreationOrchestrator,
container.fileUpdater
);

const deleteController = new DeleteController(
container.fileDeleter,
container.folderDeleter
Expand Down Expand Up @@ -44,6 +50,7 @@ export function buildControllers(container: DependencyContainer) {
return {
addFile: addFileController,
renameOrMove: renameOrMoveController,
updateFile: updateFileController,
delete: deleteController,
downloadFile: downloadFileController,
offline: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { FileCreationOrchestrator } from '../../../../context/virtual-drive/boundaryBridge/application/FileCreationOrchestrator';
import { FileUpdater } from '../../../../context/virtual-drive/files/application/FileUpdater';
import { createFilePlaceholderId } from '../../../../context/virtual-drive/files/domain/PlaceholderId';
import { CallbackController } from './CallbackController';
import Logger from 'electron-log';


type UpdateCallback = (acknowledge: boolean, id: string) => void;

export class UpdateFileController extends CallbackController {

constructor(
private readonly fileCreationOrchestrator: FileCreationOrchestrator,
private readonly fileUpdater: FileUpdater,
) {
super();
}

private updateFile = async (
oldContentsId: string,
callback: (acknowledge: boolean, id: string) => void
) => {
try {
const newContentsId = await this.fileCreationOrchestrator.run(oldContentsId);
await this.fileUpdater.run(oldContentsId, newContentsId);
return callback(true, createFilePlaceholderId(newContentsId));
} catch (error: unknown) {
Logger.error('Error when updating a file: ', error);
callback(false, '');
}
};

async execute(contentsId: string, callback: UpdateCallback) {
const trimmedId = this.trim(contentsId);

if (this.isFilePlaceholder(trimmedId)) {
const [_, contentsId] = trimmedId.split(':');
Logger.debug(`Updating file: ${contentsId}`);
this.updateFile(contentsId, callback);
return;
}

throw new Error(`Placeholder Id not identified: ${trimmedId}`);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { FileDeleter } from '../../../../context/virtual-drive/files/application
import { FileFinderByContentsId } from '../../../../context/virtual-drive/files/application/FileFinderByContentsId';
import { FilePathUpdater } from '../../../../context/virtual-drive/files/application/FilePathUpdater';
import { FilePlaceholderCreatorFromContentsId } from '../../../../context/virtual-drive/files/application/FilePlaceholderCreatorFromContentsId';
import { FileUpdater } from '../../../../context/virtual-drive/files/application/FileUpdater';
import { FilesPlaceholderUpdater } from '../../../../context/virtual-drive/files/application/FilesPlaceholderUpdater';
import { FilesPlaceholderCreator } from '../../../../context/virtual-drive/files/application/FilesPlaceholdersCreator';
import { RepositoryPopulator } from '../../../../context/virtual-drive/files/application/RepositoryPopulator';
Expand All @@ -22,4 +23,5 @@ export interface FilesContainer {
repositoryPopulator: RepositoryPopulator;
filesPlaceholderCreator: FilesPlaceholderCreator;
filesPlaceholderUpdater: FilesPlaceholderUpdater;
fileUpdater: FileUpdater;
}
20 changes: 16 additions & 4 deletions src/apps/sync-engine/dependency-injection/files/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import { InMemoryFileRepository } from '../../../../context/virtual-drive/files/
import { SDKRemoteFileSystem } from '../../../../context/virtual-drive/files/infrastructure/SDKRemoteFileSystem';
import { NodeWinLocalFileSystem } from '../../../../context/virtual-drive/files/infrastructure/NodeWinLocalFileSystem';
import { LocalFileIdProvider } from '../../../../context/virtual-drive/shared/application/LocalFileIdProvider';
import { DependencyInjectionHttpClientsProvider } from '../common/clients';
import { FileUpdater } from '../../../../context/virtual-drive/files/application/FileUpdater';
import { HttpRemoteFileSystem } from '../../../../context/virtual-drive/files/infrastructure/HttpRemoteFileSystem';

export async function buildFilesContainer(
folderContainer: FoldersContainer,
Expand All @@ -36,8 +39,10 @@ export async function buildFilesContainer(
const eventHistory = DependencyInjectionEventRepository.get();
const { virtualDrive } = DependencyInjectionVirtualDrive;
const sdk = await DependencyInjectionStorageSdk.get();
const clients = DependencyInjectionHttpClientsProvider.get();

const remoteFileSystem = new SDKRemoteFileSystem(sdk, crypt, user.bucket);
const remoteSDKFileSystem = new SDKRemoteFileSystem(sdk, crypt, user.bucket);
const remoteHTTPFileSystem = new HttpRemoteFileSystem(clients.newDrive);
const localFileSystem = new NodeWinLocalFileSystem(
virtualDrive,
sharedContainer.relativePathToAbsoluteConverter
Expand All @@ -48,21 +53,27 @@ export async function buildFilesContainer(
const fileFinderByContentsId = new FileFinderByContentsId(repository);

const fileDeleter = new FileDeleter(
remoteFileSystem,
remoteSDKFileSystem,
localFileSystem,
repository,
folderContainer.allParentFoldersStatusIsExists,
ipcRendererSyncEngine
);

const fileUpdater = new FileUpdater(
remoteHTTPFileSystem,
repository,
ipcRendererSyncEngine
);

const sameFileWasMoved = new SameFileWasMoved(
repository,
localFileSystem,
eventHistory
);

const filePathUpdater = new FilePathUpdater(
remoteFileSystem,
remoteSDKFileSystem,
localFileSystem,
repository,
fileFinderByContentsId,
Expand All @@ -72,7 +83,7 @@ export async function buildFilesContainer(
);

const fileCreator = new FileCreator(
remoteFileSystem,
remoteSDKFileSystem,
repository,
folderContainer.folderFinder,
fileDeleter,
Expand Down Expand Up @@ -120,6 +131,7 @@ export async function buildFilesContainer(
repositoryPopulator: repositoryPopulator,
filesPlaceholderCreator,
filesPlaceholderUpdater,
fileUpdater,
};

return { container, subscribers: [] };
Expand Down
8 changes: 4 additions & 4 deletions src/apps/sync-engine/dependency-injection/folders/builder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NodeWinLocalFileSystem } from '../../../../context/virtual-drive/folders/infrastructure/NodeWinLocalFileSystem';
import { NodeWinLocalFolderSystem } from '../../../../context/virtual-drive/folders/infrastructure/NodeWinLocalFolderSystem';
import { AllParentFoldersStatusIsExists } from '../../../../context/virtual-drive/folders/application/AllParentFoldersStatusIsExists';
import { FolderByPartialSearcher } from '../../../../context/virtual-drive/folders/application/FolderByPartialSearcher';
import { FolderCreator } from '../../../../context/virtual-drive/folders/application/FolderCreator';
Expand All @@ -17,7 +17,7 @@ import { RetrieveAllFolders } from '../../../../context/virtual-drive/folders/ap
import { SynchronizeOfflineModifications } from '../../../../context/virtual-drive/folders/application/SynchronizeOfflineModifications';
import { SynchronizeOfflineModificationsOnFolderCreated } from '../../../../context/virtual-drive/folders/application/SynchronizeOfflineModificationsOnFolderCreated';
import { FolderPlaceholderUpdater } from '../../../../context/virtual-drive/folders/application/UpdatePlaceholderFolder';
import { HttpRemoteFileSystem } from '../../../../context/virtual-drive/folders/infrastructure/HttpRemoteFileSystem';
import { HttpRemoteFolderSystem } from '../../../../context/virtual-drive/folders/infrastructure/HttpRemoteFolderSystem';
import { InMemoryFolderRepository } from '../../../../context/virtual-drive/folders/infrastructure/InMemoryFolderRepository';
import { InMemoryOfflineFolderRepository } from '../../../../context/virtual-drive/folders/infrastructure/InMemoryOfflineFolderRepository';
import { ipcRendererSyncEngine } from '../../ipcRendererSyncEngine';
Expand All @@ -38,8 +38,8 @@ export async function buildFoldersContainer(

const repository = new InMemoryFolderRepository();

const localFileSystem = new NodeWinLocalFileSystem(virtualDrive);
const remoteFileSystem = new HttpRemoteFileSystem(
const localFileSystem = new NodeWinLocalFolderSystem(virtualDrive);
const remoteFileSystem = new HttpRemoteFolderSystem(
clients.drive,
clients.newDrive
);
Expand Down
Loading
Loading