Example #1
0
 public Program()
 {
     mConfiguration    = ProcessConfiguration();
     mAppConfiguration = GetAppConfiguration(mConfiguration);
     mConnectionString = mConfiguration.GetConnectionString(mAppConfiguration.ProducerConnectionStringName);
     mAppSetup         = new FileHasherAppSetup(mConnectionString);
 }
        private ISourceFileRepository GenerateFiles(FileHasherAppConfig appConfig)
        {
            Faker faker =
                new Faker();

            List <FileHandle> sourceFiles =
                new List <FileHandle>();

            string workingDirectory = GetMappedWorkingDirectory(appConfig);

            if (!Directory.Exists(workingDirectory))
            {
                Directory.CreateDirectory(workingDirectory);
            }

            int fileCount = faker.GenerateFileCount(appConfig);

            for (int i = 0; i < fileCount; i++)
            {
                Guid   fileId   = Guid.NewGuid();
                string filePath = ComputeFilePath(workingDirectory, fileId);

                byte[] fileContents = faker.GenerateFileContents(appConfig);
                File.WriteAllBytes(filePath, fileContents);

                FileHandle fileHandle = new FileHandle(fileId, filePath);
                sourceFiles.Add(fileHandle);
            }

            return(new InMemorySourceFileRepository(sourceFiles));
        }
        public Task <ISourceFileRepository> GenerateSourceFilesAsync(FileHasherAppConfig appConfig)
        {
            if (appConfig == null)
            {
                throw new ArgumentNullException(nameof(appConfig));
            }

            return(Task.Run(() => GenerateFiles(appConfig)));
        }
        public Task CleanupSourceFilesAsync(FileHasherAppConfig appConfig)
        {
            if (appConfig == null)
            {
                throw new ArgumentNullException(nameof(appConfig));
            }

            return(Task.Run(() => CleanupSourceFiles(appConfig)));
        }
        private string GetMappedWorkingDirectory(FileHasherAppConfig appConfig)
        {
            string workingDirectory = CurrentDirectoryPlaceholder;

            if (!string.IsNullOrWhiteSpace(appConfig.WorkingDirectory))
            {
                workingDirectory = appConfig.WorkingDirectory;
            }

            return(workingDirectory.Replace(CurrentDirectoryPlaceholder,
                                            Directory.GetCurrentDirectory()));
        }
        public static int GenerateFileSizeBytes(this Faker faker, FileHasherAppConfig appConfig)
        {
            if (faker == null)
            {
                throw new ArgumentNullException(nameof(faker));
            }

            if (appConfig == null)
            {
                throw new ArgumentNullException(nameof(appConfig));
            }

            return(faker.Random.Int(
                       appConfig.FileSizeBytes.Min,
                       appConfig.FileSizeBytes.Max
                       ));
        }
        public static byte[] GenerateFileContents(this Faker faker, FileHasherAppConfig appConfig)
        {
            if (faker == null)
            {
                throw new ArgumentNullException(nameof(faker));
            }

            if (appConfig == null)
            {
                throw new ArgumentNullException(nameof(appConfig));
            }

            int fileSizeBytes = faker
                                .GenerateFileSizeBytes(appConfig);

            return(faker.Random
                   .Bytes(fileSizeBytes));
        }
        private void CleanupSourceFiles(FileHasherAppConfig appConfig)
        {
            string workingDirectory = GetMappedWorkingDirectory(appConfig);

            if (!Directory.Exists(workingDirectory))
            {
                return;
            }

            string[] sourceFiles = Directory.GetFiles(workingDirectory, "*.dat");

            if (sourceFiles != null && sourceFiles.Length > 0)
            {
                foreach (string sourceFile in sourceFiles)
                {
                    File.Delete(sourceFile);
                }
            }
        }