forked from schmurtzm/Onion-Desktop-Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Onion_Install_Download.ps1
300 lines (234 loc) · 11.7 KB
/
Onion_Install_Download.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
$ScriptPath = $MyInvocation.MyCommand.Path
$ScriptDirectory = Split-Path $ScriptPath -Parent
Set-Location -Path $ScriptDirectory
[Environment]::CurrentDirectory = Get-Location
Add-Type -AssemblyName System.Windows.Forms
$sysdir = ".\update"
$GITHUB_REPOSITORY = "OnionUI/Onion"
function GetVersion($version) {
($version -replace "[a-zA-Z]" -split '\.' | ForEach-Object { [int]$_ }) -join ''
}
# Function to fetch version information and populate the form
function Populate-Version {
$global:Beta_assets_info = (Invoke-RestMethod -Uri "https://api.github.com/repos/$GITHUB_REPOSITORY/releases" -Method Get -UseBasicParsing) | Where-Object { $_.prerelease } | Select-Object -First 1
$Beta_asset = $global:Beta_assets_info.assets | Where-Object { $_.name -like "*Onion-v*" }
$Beta_FullVersion = $Beta_asset.name -replace "^Onion-v|" -replace "\.zip$"
#$Beta_Version = $Beta_FullVersion -replace "-dev.*$"
$Beta_SizeMB = [math]::Round($Beta_asset.size / 1MB, 2)
$Beta_DescriptionUrl = $global:Beta_assets_info.html_url.Trim('"')
$Beta_url = $Beta_asset.browser_download_url.Trim('"')
$Beta_FileName = $Beta_url.Split("/")[-1]
$url = $Beta_asset.browser_download_url.Trim('"')
$Beta_Version = ($url -split "Onion-v")[-1] -replace ".zip" -replace "-dev"
$global:Stable_assets_info = Invoke-RestMethod -Uri "https://api.github.com/repos/$GITHUB_REPOSITORY/releases/latest" -Method Get -UseBasicParsing
$Stable_asset = $global:Stable_assets_info.assets | Where-Object { $_.name -like "*Onion-v*" }
$Stable_FullVersion = $Stable_asset.name -replace "^Onion-v|" -replace "\.zip$"
$Stable_Version = $Stable_FullVersion -replace "-dev.*$"
$Stable_SizeMB = [math]::Round($Stable_asset.size / 1MB, 2)
$Stable_DescriptionUrl = $global:Stable_assets_info.html_url.Trim('"')
$Stable_url = $Stable_asset.browser_download_url.Trim('"')
$Stable_FileName = $Stable_url.Split("/")[-1]
$BetaRadioButton.Text = "Beta (Version: $Beta_Version, Size: $Beta_SizeMB MB)"
$StableRadioButton.Text = "Stable (Version: $Stable_Version, Size: $Stable_SizeMB MB)"
$BetaInfoButton.Tag = $Beta_DescriptionUrl
$StableInfoButton.Tag = $Stable_DescriptionUrl
if (Test-Path downloads\$Beta_FileName) {
$Downloaded_size = Get-Item -Path downloads\$Beta_FileName | Select-Object -ExpandProperty Length
if ($Downloaded_size -eq $Beta_asset.size) {
Write-Host "Beta file size already OK ! ($Downloaded_size Bytes)"
$BetaRadioButton.Enabled = 0
$BetaRadioButton.Text = $BetaRadioButton.Text + " (already downloaded)"
}
else {
Write-Host "`n`nExisting beta file has wrong size:`n` $Downloaded_size instead of $size`nDownloading...`n"
}
}
if (Test-Path downloads\$Stable_FileName) {
$Downloaded_size = Get-Item -Path downloads\$Stable_FileName | Select-Object -ExpandProperty Length
if ($Downloaded_size -eq $Stable_asset.size) {
Write-Host "Stable file size already OK ! ($Downloaded_size Bytes)"
$StableRadioButton.Enabled = 0
}
else {
Write-Host "`n`nExisting Stable file has wrong size:`n` $Downloaded_size instead of $size`nDownloading...`n"
}
}
if ($BetaRadioButton.Enabled -eq 0 -and $StableRadioButton.Enabled -eq 0) {
$DL_Lbl.Text = "All Onion versions are up to date."
$DownloadButton.Enabled = 0
$timer.Start()
}
}
# Function to handle the Download button click event
function Download-Button_Click {
$DownloadButton.enabled = 0
$Downloaded_size = 0
if ($BetaRadioButton.Checked) {
$selectedVersion = "beta"
}
else {
$selectedVersion = "stable"
}
# Fetch the information of the selected version
if ($selectedVersion -eq "beta") {
#$assets_info = (Invoke-RestMethod -Uri "https://api.github.com/repos/$GITHUB_REPOSITORY/releases" -Method Get -UseBasicParsing) | Where-Object { $_.prerelease } | Select-Object -First 1
$assets_info = $global:Beta_assets_info
}
else {
#$assets_info = Invoke-RestMethod -Uri "https://api.github.com/repos/$GITHUB_REPOSITORY/releases/latest" -Method Get -UseBasicParsing
$assets_info = $global:Stable_assets_info
}
$asset = $assets_info.assets | Where-Object { $_.name -like "*Onion-v*" }
$url = $asset.browser_download_url.Trim('"')
$FullVersion = $asset.name -replace "^Onion-v|" -replace "\.zip$"
$Version = $FullVersion -replace "-dev.*$"
$size = $asset.size
$SizeMB = [math]::Round($size / 1MB, 2)
$info = $assets_info.body
# Display the selected version information
#$message = "Version : $Version (Channel: $selectedVersion)`nSize : $SizeMB MB`nURL : $url"
#[System.Windows.Forms.MessageBox]::Show($message, "Download Information", "OK", "Information")
Write-Host "`n`n== Downloading Onion $Version ($selectedVersion channel) =="
#$ProgressPreference = 'SilentlyContinue'
#Invoke-WebRequest -Uri $url -OutFile (Join-Path $downloadPath "$Version.zip")
#$command = "Invoke-WebRequest -Uri '$url' -OutFile (Join-Path '$downloadPath' '$Version.zip')"
#Write-Host $command
# Invoke-Expression -Command $command
##############################
# Déterminer le nom du fichier à partir de l'URL
$downloadPath = Join-Path $ScriptDirectory "downloads"
New-Item -ItemType Directory -Force -Path $downloadPath | Out-Null
$Update_FileName = $url.Split("/")[-1]
Write-Host "url $url"
if (Test-Path downloads\$Update_FileName) {
$Downloaded_size = Get-Item -Path downloads\$Update_FileName | Select-Object -ExpandProperty Length
}
else { $Downloaded_size = 0 }
if ($Downloaded_size -eq $size) {
Write-Host "File size already OK ! ($Downloaded_size Bytes)"
$DL_Lbl.Text = "File size already OK ! ($Downloaded_size Bytes)"
$DownloadButton.enabled = 1
return
#Start-Sleep -Seconds 3
}
else {
Write-Host "`n`nExisting file has wrong size:`n` $Downloaded_size instead of $size`nDownloading...`n"
}
# Lancer wget en arriére-plan pour télécharger le fichier
# invisible download window
# $wgetProcess = Start-Process -FilePath "wget" -ArgumentList "q", "-nv", "-O", $fileName, $url -WindowStyle Hidden -PassThru
#mini download window :
#$wgetProcess = Start-Process -FilePath "cmd" -ArgumentList " /c mode con:cols=150 lines=1 & tools\wget" , "-P .\downloads" , "-O $fileName", $url -PassThru
$progressBar.visible = 1
$wgetProcess = Start-Process -FilePath "cmd" -ArgumentList "/c mode con:cols=150 lines=1 & tools\wget", "-O `"downloads\$Update_FileName`"", $url -PassThru
# Tant que wget est en arriére-plan, afficher la taille du fichier actuel
while (!$wgetProcess.HasExited) {
#$fileSize = $Update_FullPath | Select-Object -ExpandProperty Length
if (Test-Path downloads\$Update_FileName) {
$fileSize = Get-Item -Path downloads\$Update_FileName | Select-Object -ExpandProperty Length
}
else { $fileSize = 0 }
#$fileSize = downloads\$Update_FileName.Length
$progress = [math]::Round(($fileSize / $size) * 100) # Calculer la progression en pourcentage
$progressBar.Value = $progress # Mettre à jour la valeur de la barre de progression
Start-Sleep -Milliseconds 1000 # Attendre une demi-seconde
}
##############################
Write-Host "`n`n=================== Download done ===================`n"
$DownloadButton.enabled = 1
$progressBar.Value = 0
$progressBar.visible = 0
if (Test-Path downloads\$Update_FileName) {
$Downloaded_size = Get-Item -Path downloads\$Update_FileName | Select-Object -ExpandProperty Length
}
else { $Downloaded_size = 0 }
if ($Downloaded_size -eq $size) {
Write-Host "File size OK! ($Downloaded_size)"
$DL_Lbl.Text = "Download successful."
#Start-Sleep -Seconds 3
$timer.Start()
}
else {
Write-Host "`n`nError: Wrong download size:`n` $Downloaded_size instead of $size`n"
$DL_Lbl.Text = "Download failed."
}
}
# Create a new instance of Windows Form
$form = New-Object System.Windows.Forms.Form
$form.Text = "Onion Downloader"
$form.Size = New-Object System.Drawing.Size(600, 200)
$form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle
$form.MaximizeBox = $false
$form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
# Create the Stable radio button
$StableRadioButton = New-Object System.Windows.Forms.RadioButton
$StableRadioButton.Text = "Stable"
$StableRadioButton.Location = New-Object System.Drawing.Point(20, 20)
$StableRadioButton.Size = New-Object System.Drawing.Size(500, 20)
$StableRadioButton.Checked = $true
$form.Controls.Add($StableRadioButton)
# Create the Beta radio button
$BetaRadioButton = New-Object System.Windows.Forms.RadioButton
$BetaRadioButton.Text = "Beta"
$BetaRadioButton.Location = New-Object System.Drawing.Point(20, 50)
$BetaRadioButton.Size = New-Object System.Drawing.Size(500, 20)
$form.Controls.Add($BetaRadioButton)
# Create the Stable info button
$StableInfoButton = New-Object System.Windows.Forms.Button
$StableInfoButton.Text = "i"
$StableInfoButton.Location = New-Object System.Drawing.Point(530, 20)
$StableInfoButton.Size = New-Object System.Drawing.Size(20, 20)
$StableInfoButton.Font = New-Object System.Drawing.Font("Arial", 10, [System.Drawing.FontStyle]::Bold)
$StableInfoButton.Add_Click({ Start-Process $StableInfoButton.Tag })
$form.Controls.Add($StableInfoButton)
# Create the Beta info button
$BetaInfoButton = New-Object System.Windows.Forms.Button
$BetaInfoButton.Text = "i"
$BetaInfoButton.Location = New-Object System.Drawing.Point(530, 50)
$BetaInfoButton.Size = New-Object System.Drawing.Size(20, 20)
$BetaInfoButton.Font = New-Object System.Drawing.Font("Arial", 10, [System.Drawing.FontStyle]::Bold)
$BetaInfoButton.Add_Click({ Start-Process $BetaInfoButton.Tag })
$form.Controls.Add($BetaInfoButton)
# Create the Download button
$DownloadButton = New-Object System.Windows.Forms.Button
$DownloadButton.Text = "Download"
$DownloadButton.Location = New-Object System.Drawing.Point(20, 80)
$DownloadButton.Size = New-Object System.Drawing.Size(100, 23)
$DownloadButton.Add_Click({ Download-Button_Click })
$form.Controls.Add($DownloadButton)
# Créer la barre de progression dans le formulaire
$progressBar = New-Object System.Windows.Forms.ProgressBar
$progressBar.Location = New-Object System.Drawing.Point(20, 110)
$progressBar.Size = New-Object System.Drawing.Size(500, 23)
$progressBar.visible = 0
$form.Controls.Add($progressBar)
$DL_Lbl = New-Object System.Windows.Forms.Label
$DL_Lbl.Location = New-Object System.Drawing.Point(20, 110)
$DL_Lbl.Size = New-Object System.Drawing.Size(500, 46)
$form.Controls.Add($DL_Lbl)
$tooltip = New-Object System.Windows.Forms.ToolTip
$tooltip.SetToolTip($StableInfoButton, "Show release notes.")
$tooltip.SetToolTip($BetaInfoButton, "Show release notes.")
$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = 1000
$timer.Add_Tick({
# Utiliser le modificateur script pour accéder à la variable globale
$script:counter -= 1
$DL_Lbl.Text = "All Onion versions are up to date.`nClosing in $script:counter s"
if ($script:counter -eq 0) {
$timer.Stop()
$form.Close()
}
})
#Write-Host "Beta Release Name: $global:Beta_assets_info"
$script:counter = 6
# Populate version information
Populate-Version
$DL_Lbl.Text = "Close this Window if you don't want to download any new version."
# Show the form
$iconPath = Join-Path -Path $PSScriptRoot -ChildPath "tools\res\onion.ico"
$icon = New-Object System.Drawing.Icon($iconPath)
$form.Icon = $icon
$result = $form.ShowDialog()
$form.Dispose()
$timer.Stop()