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();
            }
        }
Ejemplo n.º 2
0
        private static void AddContentToSyncList(string sourceBasePath, IFileHandler outputFileHandler, IList <Package> packages,
                                                 string contentFolderName, SyncType syncType, List <Sync> syncList, AliasService aliasService)
        {
            int packageCnt = 0;

            foreach (var package in packages)
            {
                ProgressBar.DrawProgressBar($"AddContentToSyncList [{contentFolderName}]", packageCnt++, packages.Count);

                var packageContentFolderBasePath = _pathService.Combine(sourceBasePath, package.Path, contentFolderName);
                var packageContentLhaBasePath    = _pathService.Combine(sourceBasePath, package.Path, contentFolderName + ".lha");
                if (Directory.Exists(packageContentFolderBasePath) && File.Exists(packageContentLhaBasePath))
                {
                    throw new Exception($"Package [{package.Path}] has both [content] folder and [content.lha].");
                }

                string packageContentBasePath;
                if (File.Exists(packageContentLhaBasePath))
                {
                    packageContentBasePath = packageContentLhaBasePath;
                }
                else if (Directory.Exists(packageContentFolderBasePath))
                {
                    packageContentBasePath = packageContentFolderBasePath;
                }
                else
                {
                    continue;
                }

                using (var contentFileHandler = FileHandlerFactory.Create(_logger, packageContentBasePath))
                {
                    var packageOutputPath = "";
                    var sourcePath        = "";
                    var packageEntries    = contentFileHandler.DirectoryGetFileSystemEntriesRecursive(sourcePath);
                    foreach (var packageEntry in packageEntries)
                    {
                        var outputPath = aliasService.TargetAliasToOutputPath(packageEntry);

                        var packageEntryFileName = _pathService.GetFileName(outputPath);
                        if (ShouldContentReverseAll(packageEntryFileName))
                        {
                            var contentReversePackageEntryPath = _pathService.GetDirectoryName(outputPath);
                            var contentReversePackageSubPath   = RemoveRoot(sourcePath, contentReversePackageEntryPath);
                            var contentReverseFileOutputPath   = _pathService.Combine(packageOutputPath, contentReversePackageSubPath);
                            if (outputFileHandler.DirectoryExists(contentReverseFileOutputPath))
                            {
                                var innerContentReversePackageEntries = outputFileHandler.DirectoryGetFileSystemEntriesRecursive(contentReverseFileOutputPath);
                                foreach (var innerContentReversePackageEntry in innerContentReversePackageEntries)
                                {
                                    var innerContentReversePackageSubPath = RemoveRoot(packageOutputPath, innerContentReversePackageEntry);
                                    var innerContentReverseSourcePath     = _pathService.Combine(sourcePath, innerContentReversePackageSubPath);

                                    var innerSync = new Sync
                                    {
                                        PackageContentBasePath = packageContentBasePath,
                                        SourcePath             = innerContentReverseSourcePath,
                                        TargetPath             = innerContentReversePackageEntry,
                                        SyncType = syncType,
                                        FileType = outputFileHandler.GetFileType(innerContentReversePackageEntry)
                                    };

                                    syncList.Add(innerSync);
                                }
                            }
                        }
                        else
                        {
                            var packageSubPath = RemoveRoot(sourcePath, outputPath);
                            var fileOutputPath = _pathService.Combine(packageOutputPath, packageSubPath);
                            var sync           = new Sync
                            {
                                PackageContentBasePath = packageContentBasePath,
                                SourcePath             = packageEntry,
                                TargetPath             = fileOutputPath,
                                SyncType = syncType,
                                FileType = contentFileHandler.GetFileType(packageEntry)
                            };

                            syncList.Add(sync);
                        }
                    }
                }
            }
        }