Exemplo n.º 1
0
        private async Task BuildFileAsync(string hashDir, HashAlgorithm crypto, IFileInfo file)
        {
            Stream fileStream = file.Open(FileMode.Open, FileAccess.Read);
            Stream hashStream = null;

            byte[] hash;

            try
            {
                string fileName = file.GetRelativeName(_filePath, _fileType.DirectorySearchOption);
                if (_hashFileMap.TryGetValue(fileName, out IFileInfo hashFile))
                {
                    hashStream = hashFile.Open(FileMode.OpenOrCreate);
                    hash       = await _changeDetector.DetectAsync(fileStream, hashStream).ConfigureAwait(false);

                    _hashFileMap.TryRemove(fileName, out _);
                }
                else
                {
                    //new hash
                    _fileSystem.Directory.CreateDirectoriesIfNotExist(hashDir, fileName);
                    hashStream = _fileSystem.File.Create($"{Path.Combine(hashDir, fileName)}.hashfile");
                    hash       = crypto.ComputeHash(fileStream);
                }
                if (hash != null)
                {
                    await _fileType.SaveAsync(file, fileStream).ConfigureAwait(false);

                    await _changeDetector.WriteHashAsync(hash, hashStream).ConfigureAwait(false);
                }
            }
            finally
            {
                fileStream.Close();
                hashStream?.Close();
            }
        }