mirror of
https://github.com/yeongpin/cursor-free-vip.git
synced 2025-08-02 12:47:34 +08:00
Merge pull request #1033 from LoveDoLove/lovedolove
修復 install.ps1 下載狀態條的問題
This commit is contained in:
commit
1c36ff010e
@ -125,75 +125,61 @@ function Install-CursorFreeVIP {
|
|||||||
|
|
||||||
Write-Styled "No existing installation file found, starting download..." -Color $Theme.Primary -Prefix "Download"
|
Write-Styled "No existing installation file found, starting download..." -Color $Theme.Primary -Prefix "Download"
|
||||||
|
|
||||||
# Create WebClient and add progress event
|
# Use HttpWebRequest for chunked download with real-time progress bar
|
||||||
$webClient = New-Object System.Net.WebClient
|
$url = $asset.browser_download_url
|
||||||
$webClient.Headers.Add("User-Agent", "PowerShell Script")
|
$outputFile = $downloadPath
|
||||||
|
Write-Styled "Downloading from: $url" -Color $Theme.Info -Prefix "URL"
|
||||||
|
Write-Styled "Saving to: $outputFile" -Color $Theme.Info -Prefix "Path"
|
||||||
|
|
||||||
# Define progress variables
|
$request = [System.Net.HttpWebRequest]::Create($url)
|
||||||
$Global:downloadedBytes = 0
|
$request.UserAgent = "PowerShell Script"
|
||||||
$Global:totalBytes = 0
|
$response = $request.GetResponse()
|
||||||
$Global:lastProgress = 0
|
$totalLength = $response.ContentLength
|
||||||
$Global:lastBytes = 0
|
$responseStream = $response.GetResponseStream()
|
||||||
$Global:lastTime = Get-Date
|
$fileStream = [System.IO.File]::OpenWrite($outputFile)
|
||||||
|
$buffer = New-Object byte[] 8192
|
||||||
# Download progress event
|
$bytesRead = 0
|
||||||
$eventId = [guid]::NewGuid()
|
$totalRead = 0
|
||||||
Register-ObjectEvent -InputObject $webClient -EventName DownloadProgressChanged -Action {
|
$lastProgress = -1
|
||||||
$Global:downloadedBytes = $EventArgs.BytesReceived
|
$startTime = Get-Date
|
||||||
$Global:totalBytes = $EventArgs.TotalBytesToReceive
|
try {
|
||||||
$progress = [math]::Round(($Global:downloadedBytes / $Global:totalBytes) * 100, 1)
|
do {
|
||||||
|
$bytesRead = $responseStream.Read($buffer, 0, $buffer.Length)
|
||||||
# Only update display when progress changes by more than 1%
|
if ($bytesRead -gt 0) {
|
||||||
if ($progress -gt $Global:lastProgress + 1) {
|
$fileStream.Write($buffer, 0, $bytesRead)
|
||||||
$Global:lastProgress = $progress
|
$totalRead += $bytesRead
|
||||||
$downloadedMB = [math]::Round($Global:downloadedBytes / 1MB, 2)
|
$progress = [math]::Round(($totalRead / $totalLength) * 100, 1)
|
||||||
$totalMB = [math]::Round($Global:totalBytes / 1MB, 2)
|
if ($progress -ne $lastProgress) {
|
||||||
|
$elapsed = (Get-Date) - $startTime
|
||||||
# Calculate download speed
|
$speed = if ($elapsed.TotalSeconds -gt 0) { $totalRead / $elapsed.TotalSeconds } else { 0 }
|
||||||
$currentTime = Get-Date
|
|
||||||
$timeSpan = ($currentTime - $Global:lastTime).TotalSeconds
|
|
||||||
if ($timeSpan -gt 0) {
|
|
||||||
$bytesChange = $Global:downloadedBytes - $Global:lastBytes
|
|
||||||
$speed = $bytesChange / $timeSpan
|
|
||||||
|
|
||||||
# Choose appropriate unit based on speed
|
|
||||||
$speedDisplay = if ($speed -gt 1MB) {
|
$speedDisplay = if ($speed -gt 1MB) {
|
||||||
"$([math]::Round($speed / 1MB, 2)) MB/s"
|
"{0:N2} MB/s" -f ($speed / 1MB)
|
||||||
} elseif ($speed -gt 1KB) {
|
} elseif ($speed -gt 1KB) {
|
||||||
"$([math]::Round($speed / 1KB, 2)) KB/s"
|
"{0:N2} KB/s" -f ($speed / 1KB)
|
||||||
} else {
|
} else {
|
||||||
"$([math]::Round($speed, 2)) B/s"
|
"{0:N2} B/s" -f $speed
|
||||||
}
|
}
|
||||||
|
$downloadedMB = [math]::Round($totalRead / 1MB, 2)
|
||||||
Write-Host "`rDownloading: $downloadedMB MB / $totalMB MB ($progress%) - $speedDisplay" -NoNewline -ForegroundColor Cyan
|
$totalMB = [math]::Round($totalLength / 1MB, 2)
|
||||||
|
Write-Progress -Activity "Downloading CursorFreeVIP" -Status "$downloadedMB MB / $totalMB MB ($progress%) - $speedDisplay" -PercentComplete $progress
|
||||||
# Update last data
|
$lastProgress = $progress
|
||||||
$Global:lastBytes = $Global:downloadedBytes
|
|
||||||
$Global:lastTime = $currentTime
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} | Out-Null
|
} while ($bytesRead -gt 0)
|
||||||
|
} finally {
|
||||||
# Download completed event
|
$fileStream.Close()
|
||||||
Register-ObjectEvent -InputObject $webClient -EventName DownloadFileCompleted -Action {
|
$responseStream.Close()
|
||||||
Write-Host "`r" -NoNewline
|
$response.Close()
|
||||||
|
}
|
||||||
|
Write-Progress -Activity "Downloading CursorFreeVIP" -Completed
|
||||||
|
# Check file exists and is not zero size
|
||||||
|
if (!(Test-Path $outputFile) -or ((Get-Item $outputFile).Length -eq 0)) {
|
||||||
|
throw "Download failed or file is empty."
|
||||||
|
}
|
||||||
Write-Styled "Download completed!" -Color $Theme.Success -Prefix "Complete"
|
Write-Styled "Download completed!" -Color $Theme.Success -Prefix "Complete"
|
||||||
Unregister-Event -SourceIdentifier $eventId
|
Write-Styled "File location: $outputFile" -Color $Theme.Info -Prefix "Location"
|
||||||
} | Out-Null
|
|
||||||
|
|
||||||
# Start download
|
|
||||||
$webClient.DownloadFileAsync([Uri]$asset.browser_download_url, $downloadPath)
|
|
||||||
|
|
||||||
# Wait for download to complete
|
|
||||||
while ($webClient.IsBusy) {
|
|
||||||
Start-Sleep -Milliseconds 100
|
|
||||||
}
|
|
||||||
|
|
||||||
Write-Styled "File location: $downloadPath" -Color $Theme.Info -Prefix "Location"
|
|
||||||
Write-Styled "Starting program..." -Color $Theme.Primary -Prefix "Launch"
|
Write-Styled "Starting program..." -Color $Theme.Primary -Prefix "Launch"
|
||||||
|
Start-Process $outputFile
|
||||||
# Run program
|
|
||||||
Start-Process $downloadPath
|
|
||||||
}
|
}
|
||||||
catch {
|
catch {
|
||||||
Write-Styled $_.Exception.Message -Color $Theme.Error -Prefix "Error"
|
Write-Styled $_.Exception.Message -Color $Theme.Error -Prefix "Error"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user