Example #1
0
        /// <summary>
        /// Calculates the hash for the specified file and saves it to the database.
        /// </summary>
        /// <param name="File"></param>
        /// <param name="Stream"></param>
        private async Task UpdateFileHashInDatabaseAsync(BackupFile File, FileStream Stream)
        {
            if (File == null)
            {
                throw new ArgumentNullException(nameof(File));
            }
            if (Stream == null)
            {
                throw new ArgumentNullException(nameof(Stream));
            }

            var hashAlgo    = Hasher.GetDefaultHashAlgorithm(File.Priority);
            var currentHash = Hasher.GenerateFileHash(hashAlgo, Stream);

            if (currentHash.Length != 0)
            {
                File.SetFileHashWithAlgorithm(currentHash, hashAlgo);
                await Database.SetBackupFileHashAsync(File).ConfigureAwait(false);
            }
            else
            {
                throw new Exception("Failed to calculate the file hash.");
            }
        }