Exemplo n.º 1
0
        private async Task DeleteOrphanedRemoteStates()
        {
            // files
            foreach (var remoteState in _context.RemoteStates.Where(current => !current.IsLocal && current.Type == DriveItemType.File))
            {
                _logger.LogInformation($"Delete orphaned file '{remoteState.GetItemPath()}' from drive '{_remoteDrive.Name}'.");
                await _remoteDrive.DeleteAsync(remoteState.ToDriveItem());
            }

            // folders
            foreach (var remoteState in _context.RemoteStates.Where(current => !current.IsLocal && current.Type == DriveItemType.Folder))
            {
                _logger.LogInformation($"Delete orphaned folder '{remoteState.GetItemPath()}' from drive '{_remoteDrive.Name}'.");
                await _remoteDrive.DeleteAsync(remoteState.ToDriveItem());
            }
        }
Exemplo n.º 2
0
        private async Task <(DriveItem UpdateDriveItem, WatcherChangeTypes ChangeType)> SyncDriveItem(
            IDriveProxy sourceDrive,
            IDriveProxy targetDrive,
            DriveItem oldDriveItem,
            DriveItem newDriveItem)
        {
            DriveItem updatedDriveItem;

            (var itemName1, var itemName2) = this.GetItemNames(newDriveItem);
            var changeType = newDriveItem.GetChangeType(oldDriveItem);

            switch (changeType)
            {
            case WatcherChangeTypes.Changed:
            case WatcherChangeTypes.Created:

                // Item was created or modified on source drive
                // actions: create or modify on target drive
                _logger.LogInformation($"{itemName1} was created or modified on drive '{sourceDrive.Name}'. Action(s): Create or modify {itemName2} on drive '{targetDrive.Name}'.");
                updatedDriveItem = await this.TransferDriveItem(sourceDrive, targetDrive, newDriveItem);

                break;

            case WatcherChangeTypes.Deleted:

                // item was deleted on source drive
                // actions: delete item on target drive
                _logger.LogInformation($"{itemName1} was deleted on drive '{sourceDrive.Name}'. Action(s): Delete {itemName2} on drive '{targetDrive.Name}'.");

                if (await targetDrive.ExistsAsync(newDriveItem))
                {
                    updatedDriveItem = await targetDrive.DeleteAsync(newDriveItem);
                }
                else
                {
                    _logger.LogWarning($"Cannot delete {itemName2} because it does not exist on drive '{targetDrive.Name}'.");
                    throw new InvalidOperationException($"Cannot delete {itemName2} because it does not exist on drive '{targetDrive.Name}'.");
                }

                break;

            case WatcherChangeTypes.Renamed:

                // item was renamed / moved on source drive
                // actions: rename / move item on target drive
                _logger.LogInformation($"{itemName1} was renamed / moved on drive '{sourceDrive.Name}'. Action(s): Rename / move {itemName2} on drive '{targetDrive.Name}'.");

                if (await targetDrive.ExistsAsync(newDriveItem))
                {
                    _logger.LogWarning($"Cannot move {itemName2} because the target {itemName2} already exists on drive '{targetDrive.Name}'.");
                    throw new InvalidOperationException($"Cannot move {itemName2} because the target {itemName2} already exists on drive '{targetDrive.Name}'.");
                }
                else
                {
                    updatedDriveItem = await targetDrive.MoveAsync(oldDriveItem, newDriveItem);
                }

                break;

            default:
                updatedDriveItem = newDriveItem;
                break;
            }

            return(updatedDriveItem, changeType);
        }