private static async Task DownloadFile()

in context/scripts/Web.cs [59:176]


        private static async Task<bool> DownloadFile(string sourceUrl, string destinationFile, int attempts, TimeSpan delay)
        {
            string name = Path.GetFileName(destinationFile);
            bool success = false;
            for (int i = 0; i < attempts; i++)
            {
                try
                {
                    string[] sourceParts = sourceUrl.Split('#');
                    HashAlgorithm hashAlgorithm = null;
                    string hashAlgorithmName = string.Empty;
                    string hashValue = string.Empty;
                    byte[] hash = null;
                    if (sourceParts.Length == 3) // has hash
                    {
                        sourceUrl = sourceParts[0];
                        hashAlgorithmName = sourceParts[1].ToUpperInvariant();
                        hashValue = sourceParts[2].ToLowerInvariant();
                        try
                        {
                            List<byte> hashBytes = new List<byte>();
                            for (int j = 0; j < hashValue.Length; j += 2)
                            {
                                hashBytes.Add(Convert.ToByte(hashValue.Substring(j, 2), 16));
                            }

                            hash = hashBytes.ToArray();
                        }
                        catch
                        {
                            throw new InvalidOperationException("Invalid hash: " + hashValue);
                        }

                        switch (hashAlgorithmName)
                        {
                            case "SHA384":
                                hashAlgorithm = SHA384.Create();
                                break;

                            case "SHA512":
                                hashAlgorithm = SHA512.Create();
                                break;

                            case "SHA256":
                                hashAlgorithm = SHA256.Create();
                                break;

                            case "SHA1":
                                hashAlgorithm = SHA1.Create();
                                break;

                            case "MD5":
                                hashAlgorithm = MD5.Create();
                                break;

                            default:
                                throw new InvalidOperationException("Invalid hash algorithm: " + hashAlgorithmName);
                        }
                    }

                    if (i > 0)
                    {
                        WriteLine("{0}\tattempt #{1}\t after {2}s", name, i + 1, delay.TotalSeconds);
                        await Task.Delay(delay);
                    }
                    else
                    {
                        WriteLine("{0}\tdownloading from \"{1}\"", name, sourceUrl);
                    }

                    WriteLine("[Download file task] Starting the download of the file ...");
                    if (await DownloadFile(name, sourceUrl, destinationFile))
                    {
                        if (hashAlgorithm != null)
                        {
                            WriteLine("[Download file task] Opening the file for read ...");
                            using (FileStream fileStream = File.OpenRead(destinationFile))
                            {
                                byte[] computedHash = hashAlgorithm.ComputeHash(fileStream);
                                bool hashIsEqual = StructuralComparisons.StructuralEqualityComparer.Equals(hash, computedHash);
                                if (!hashIsEqual)
                                {
                                    StringBuilder computedHashStr = new StringBuilder(computedHash.Length * 2);
                                    foreach (byte b in computedHash)
                                    {
                                        computedHashStr.AppendFormat("{0:x2}", b);
                                    }

                                    throw new InvalidOperationException("Hashes are not match. Expected: " + hashValue + ". Actual: " + computedHashStr);
                                }

                                WriteLine(destinationFile + " checksum(" + hashAlgorithmName + ") " + hashValue + " is valid.");
                            }
                        }

                        success = true;
                        break;
                    }

                    WriteErrorLine("{0}\tabort", name);
                }
                catch (Exception ex)
                {
                    WriteErrorLine("{0}\terror: \"{1}\"", name, ex.Message);
                    if (ex is InvalidOperationException)
                    {
                        throw;
                    }
                }
            }

            if (!success)
            {
                throw new Exception(sourceUrl + " - download failed.");
            }

            return success;
        }