示例#1
0
        private void Execute(FileSorting sorting)
        {
            _logger.Debug("Executing next FileSorting");
            var files = sorting.FileFinder.SelectMany(s =>
            {
                return(s.FindFiles().Select(f => new FileSortingExecution(f)));
            });

            var options = new ParallelOptions {
                MaxDegreeOfParallelism = 1
            };
            var results = new List <FileSortingExecution>();
            var loop    = Parallel.ForEach(files, options, x =>
            {
                try
                {
                    Execute(x, sorting);
                }
                catch (Exception ex)
                {
                    _logger.Error(ex, "Moving of file {0} not possible due to:", x.SourceFile.FullPath);
                    x.Error = ex;
                }
                results.Add(x);
            });

            foreach (var entry in results)
            {
                var logPath = Path.Combine(entry.SourceFile.BasePath, MoveLogFileName);
                File.AppendAllText(logPath, DumpResult(entry));
            }

            foreach (var finder in sorting.FileFinder)
            {
                finder.RemoveEmptyFolders();
            }

            _logger.Debug("Finsihed executing of FileSorting");
        }
示例#2
0
 public void AddFileSorting(FileSorting fileSorting)
 {
     _fileSortings.Add(fileSorting);
 }
示例#3
0
        private void Execute(FileSortingExecution execution, FileSorting sorting)
        {
            _logger.Debug("Executing file filter");
            var fileFilterWasMatching = true;

            foreach (var filter in sorting.FileFilter)
            {
                if (filter.MatchFile(execution.SourceFile))
                {
                    execution.MatchingFileFilterName = filter.Name ?? string.Empty;
                    fileFilterWasMatching            = true;
                    break;
                }
                fileFilterWasMatching = false;
            }

            if (!fileFilterWasMatching)
            {
                _logger.Debug("No Filter matching for file {0}", execution.SourceFile.FullPath);
                execution.FileWasProcessed = false;
                return;
            }

            execution.FileWasProcessed = true;

            _logger.Debug("Extracting informations from file {0}", execution.SourceFile.FullPath);
            var fileInformations = sorting.FileInformationExtractor.Select(x => x.ExtractInformation(execution.SourceFile));

            execution.FileInformation = MergeFileInformation(fileInformations);

            _logger.Debug("Mapping informations from file {0}", execution.SourceFile.FullPath);
            foreach (var mapper in sorting.FileInformationMapper)
            {
                execution.FileInformation = mapper.MapInformation(execution.FileInformation);
            }

            _logger.Debug("Executing fileInformation filter");
            var fileInformationFilterWasMatching = true;

            foreach (var filter in sorting.FileInformationFilter)
            {
                if (filter.MatchFile(execution.SourceFile, execution.FileInformation))
                {
                    execution.MatchingFileInformationFilterName = filter.Name ?? string.Empty;
                    fileInformationFilterWasMatching            = true;
                    break;
                }
                fileInformationFilterWasMatching = false;
            }

            if (!fileInformationFilterWasMatching)
            {
                _logger.Debug("No fileInformation Filter matching for file {0}", execution.SourceFile.FullPath);
                return;
            }

            _logger.Debug("Creating new file path for file {0}", execution.SourceFile.FullPath);
            execution.DestinationPath = sorting.FilePathCreator.CreateDestinationPath(execution.FileInformation, execution.SourceFile);

            if (sorting.BackupFilePathCreator != null)
            {
                _logger.Debug("Creating backup file path for file {0}", execution.SourceFile.FullPath);
                execution.BackupPath = sorting.BackupFilePathCreator.CreateDestinationPath(execution.FileInformation, execution.SourceFile);

                _logger.Debug("Backuping file {0}", execution.SourceFile.FullPath);
                sorting.FileMover.ProcessFileBackup(execution.SourceFile, execution.BackupPath);
            }

            _logger.Debug("Moving file {0}", execution.SourceFile.FullPath);
            sorting.FileMover.ProcessFileSort(execution.SourceFile, execution.DestinationPath);

            _logger.Debug("Change content of file {0}", execution.DestinationPath.FullPath);
            foreach (var contentChanger in sorting.FileContentChanger)
            {
                contentChanger.ChangeContent(execution.DestinationPath);
            }

            _logger.Debug("Indexing file {0}", execution.DestinationPath.FullPath);
            foreach (var indexer in sorting.FileIndexer)
            {
                indexer.IndexFile(execution.DestinationPath);
            }
        }