forked from amkovkov/GranuSightSoftware2
91 lines
6.3 KiB
PowerShell
91 lines
6.3 KiB
PowerShell
$uri = "https://builds.dotnet.microsoft.com/dotnet/Sdk/10.0.300/dotnet-sdk-10.0.300-linux-arm64.tar.gz";
|
|
$targetDir = ".\dotnet"
|
|
$tarballFile = ".\dotnet\dotnet-sdk.tar.gz"
|
|
|
|
if (Test-Path $targetDir) {
|
|
Remove-Item $targetDir -Recurse -Force
|
|
}
|
|
New-Item -ItemType Directory -Path $targetDir | Out-Null
|
|
|
|
function Invoke-DownloadWithProgress {
|
|
[CmdletBinding()]
|
|
param (
|
|
[Parameter(Mandatory = $true)]
|
|
[string]$Uri,
|
|
|
|
[Parameter(Mandatory = $true)]
|
|
[string]$OutFile,
|
|
|
|
[Parameter(Mandatory = $false)]
|
|
[string]$ActivityName = "Скачивание файла"
|
|
)
|
|
|
|
Add-Type -AssemblyName System.Net.Http
|
|
$httpClient = New-Object System.Net.Http.HttpClient
|
|
|
|
try {
|
|
$response = $httpClient.GetAsync($Uri, [System.Net.Http.HttpCompletionOption]::ResponseHeadersRead).GetAwaiter().GetResult()
|
|
|
|
if (-not $response.IsSuccessStatusCode) {
|
|
throw "Сервер вернул код ошибки: $([int]$response.StatusCode) ($($response.StatusCode))"
|
|
}
|
|
|
|
$totalBytes = $response.Content.Headers.ContentLength
|
|
$totalMB = if ($totalBytes) { [Math]::Round($totalBytes / 1MB, 3) } else { "Неизвестно" }
|
|
|
|
$downloadStream = $response.Content.ReadAsStreamAsync().GetAwaiter().GetResult()
|
|
$fileStream = [System.IO.File]::Create($OutFile)
|
|
$fileStream.Close()
|
|
|
|
$buffer = New-Object byte[] 8192
|
|
$bytesRead = 0
|
|
$totalBytesRead = 0
|
|
|
|
while (($bytesRead = $downloadStream.Read($buffer, 0, $buffer.Length)) -gt 0) {
|
|
$fileStream = [System.IO.File]::Open($OutFile, [System.IO.FileMode]::Append)
|
|
$fileStream.Write($buffer, 0, $bytesRead)
|
|
$fileStream.Close();
|
|
|
|
$totalBytesRead += $bytesRead
|
|
|
|
$currentMB = [Math]::Round($totalBytesRead / 1MB, 3)
|
|
|
|
$progressParams = @{
|
|
Activity = $ActivityName
|
|
Status = "Скачано: " + $currentMB.ToString("F3", [System.Globalization.CultureInfo]::InvariantCulture) + " МБ из " + $totalMB.ToString("F3", [System.Globalization.CultureInfo]::InvariantCulture) + " МБ"
|
|
}
|
|
|
|
if ($totalBytes) {
|
|
$progressParams.PercentComplete = [int](($totalBytesRead / $totalBytes) * 100)
|
|
}
|
|
|
|
Write-Progress @progressParams
|
|
}
|
|
|
|
$downloadStream.Close()
|
|
|
|
return [PSCustomObject]@{
|
|
StatusCode = [int]$response.StatusCode
|
|
StatusDescription = $response.StatusCode.ToString()
|
|
ContentLength = $totalBytes
|
|
Path = $OutFile
|
|
}
|
|
}
|
|
catch {
|
|
if (Test-Path $OutFile) { Remove-Item $OutFile -Force }
|
|
Write-Error $_.Exception.Message
|
|
return $null
|
|
}
|
|
finally {
|
|
if ($httpClient) { $httpClient.Dispose() }
|
|
Write-Progress -Activity $ActivityName -Completed
|
|
}
|
|
}
|
|
|
|
$res = Invoke-DownloadWithProgress -Uri $uri -OutFile $tarballFile -ActivityName "Скачивание"
|
|
if (-not ($res -and $res.StatusCode -eq 200)) {
|
|
Write-Error "Не удалось скачать файл или сервер вернул ошибку."
|
|
} else {
|
|
tar -xvzf $tarballFile -C $targetDir
|
|
}
|