Ejemplo n.º 1
0
        private static void SynchronizeFile(Sync sync, IFileHandler contentFileHandler, IFileHandler outputFileHandler)
        {
            switch (sync.SyncType)
            {
            case SyncType.SourceToTarget:
            {
                var fileDiff = GetFileDiff(sync, contentFileHandler, outputFileHandler);
                if (fileDiff == FileDiff.Equal)
                {
                    _logger.Debug(@"{SyncLogType}: [{TargetPath}] [{SyncType}]", SyncLogType.CopyToTarget.GetDescription(), sync.TargetPath, sync.SyncType);
                    _logger.Debug(@"{SyncLogType} : (files are equal!) [{SourcePath}] [{SyncType}]", SyncLogType.CopyFromSource.GetDescription(), sync.SourcePath, sync.TargetPath, sync.SyncType);
                }
                else
                {
                    if (fileDiff == FileDiff.DiffTargetMissing || fileDiff == FileDiff.DiffSourceNewer)
                    {
                        _logger.Information(@"{SyncLogType}: [{TargetPath}] [{SyncType}] [{FileDiff}]", SyncLogType.CopyToTarget.GetDescription(), sync.TargetPath, sync.SyncType, fileDiff);
                        _logger.Information(@"{SyncLogType}: [{SourcePath}] [{SyncType}] [{FileDiff}]", SyncLogType.CopyFromSource.GetDescription(), sync.SourcePath, sync.SyncType, fileDiff);
                    }
                    else
                    {
                        _logger.Warning(@"{SyncLogType}: [{TargetPath}] [{SyncType}] [{FileDiff}]", SyncLogType.CopyToTarget.GetDescription(), sync.TargetPath, sync.SyncType, fileDiff);
                        _logger.Warning(@"{SyncLogType}: [{SourcePath}] [{SyncType}] [{FileDiff}]", SyncLogType.CopyFromSource.GetDescription(), sync.SourcePath, sync.SyncType, fileDiff);
                    }

                    outputFileHandler.FileCopy(contentFileHandler, sync.SourcePath, sync.TargetPath);
                }

                break;
            }

            case SyncType.TargetToSource:
            {
                var fileDiff = GetFileDiff(sync, contentFileHandler, outputFileHandler);
                {
                    switch (fileDiff)
                    {
                    case FileDiff.Equal:
                    {
                        _logger.Debug(@"{SyncLogType}: [{SourcePath}] [{SyncType}]", SyncLogType.CopyToSource.GetDescription(), sync.SourcePath, sync.SyncType);
                        _logger.Debug(@"{SyncLogType}: (files are equal!) [{TargetPath}] [{SyncType}]", SyncLogType.CopyFromTarget.GetDescription(), sync.TargetPath, sync.SyncType);
                        break;
                    }

                    case FileDiff.DiffSourceMissing:
                    case FileDiff.DiffTargetNewer:
                    {
                        _logger.Information(@"{SyncLogType}: [{SourcePath}] [{SyncType}] [{FileDiff}]", SyncLogType.CopyToSource.GetDescription(), sync.SourcePath, sync.SyncType, fileDiff);
                        _logger.Information(@"{SyncLogType}: [{TargetPath}] [{SyncType}] [{FileDiff}]", SyncLogType.CopyFromTarget.GetDescription(), sync.TargetPath, sync.SyncType, fileDiff);
                        contentFileHandler.FileCopyBack(sync.SourcePath, outputFileHandler, sync.TargetPath);
                        break;
                    }

                    case FileDiff.DiffTargetMissing:
                    {
                        _logger.Information(@"{SyncLogType}: [{SourcePath}] [{SyncType}] [{FileDiff}]", SyncLogType.CopyToTarget.GetDescription(), sync.TargetPath, sync.SyncType, fileDiff);
                        _logger.Information(@"{SyncLogType}: (invserse) [{TargetPath}] [{SyncType}] [{FileDiff}]", SyncLogType.CopyFromSource.GetDescription(), sync.SourcePath, sync.SyncType, fileDiff);
                        outputFileHandler.FileCopy(contentFileHandler, sync.SourcePath, sync.TargetPath);
                        break;
                    }

                    case FileDiff.DiffContent:
                    case FileDiff.DiffSourceNewer:
                    default:
                    {
                        _logger.Warning(@"{SyncLogType}: [{SourcePath}] [{SyncType}] [{FileDiff}]", SyncLogType.CopyToSource.GetDescription(), sync.SourcePath, sync.SyncType, fileDiff);
                        _logger.Warning(@"{SyncLogType}: [{TargetPath}] [{SyncType}] [{FileDiff}]", SyncLogType.CopyFromTarget.GetDescription(), sync.TargetPath, sync.SyncType, fileDiff);
                        contentFileHandler.FileCopyBack(sync.SourcePath, outputFileHandler, sync.TargetPath);
                        //outputFileHandler.FileCopy(contentFileHandler, sync.SourcePath, sync.TargetPath);
                        break;
                    }
                    }
                }
                break;
            }

            case SyncType.DeleteTarget:
            {
                if (outputFileHandler.FileExists(sync.TargetPath))
                {
                    _logger.Information(@"{SyncLogType}: [{TargetPath}] [{SyncType}]", SyncLogType.DeleteTarget.GetDescription(), sync.TargetPath, sync.SyncType);
                    outputFileHandler.FileDelete(sync.TargetPath);
                }
                else
                {
                    _logger.Debug(@"{SyncLogType}: (nothing to delete!) [{TargetPath}] [{SyncType}]", SyncLogType.DeleteTarget.GetDescription(), sync.TargetPath, sync.SyncType);
                }
                break;
            }

            case SyncType.Unknown:
            default:
                throw new ArgumentOutOfRangeException();
            }
        }