Skip to content

Portable Tool for an Ideal Windows Setup

fixx direct link generation & improve UI download stability

Greedeks5e873165705a
diff --git a/.Source/GTweak/Assets/UserControls/ToolsetCard.xaml b/.Source/GTweak/Assets/UserControls/ToolsetCard.xaml
index d08be9ae..71f9a09c 100644
--- a/.Source/GTweak/Assets/UserControls/ToolsetCard.xaml
+++ b/.Source/GTweak/Assets/UserControls/ToolsetCard.xaml
@@ -139,6 +139,7 @@
</Grid>
<Grid VerticalAlignment="Center" HorizontalAlignment="Right" Margin="0,3,0,0">
+
<Button x:Name="CardButton" Content="{DynamicResource btn_download}" Command="{Binding DownloadCommand, RelativeSource={RelativeSource AncestorType=local:ToolsetCard}}">
<Button.Style>
<Style TargetType="Button" BasedOn="{StaticResource ButtonContent}">
@@ -169,8 +170,36 @@
</Grid.ColumnDefinitions>
<Grid Grid.Column="0">
- <TextBlock Panel.ZIndex="5" Text="{Binding Progress, StringFormat='{}{0:0}%', RelativeSource={RelativeSource AncestorType=local:ToolsetCard}}" Style="{StaticResource TextDefault}" FontFamily="{StaticResource Play}" Foreground="{StaticResource Brush_Highlighted_Inverted}" FontSize="12px" HorizontalAlignment="Center" VerticalAlignment="Center"/>
- <ProgressBar Value="{Binding Progress, RelativeSource={RelativeSource AncestorType=local:ToolsetCard}}" Foreground="{StaticResource Brush_Accent}" Minimum="0" Maximum="100" Height="20" MinWidth="150" VerticalAlignment="Center"/>
+
+ <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0,-2,0,0" FontSize="13px">
+ <TextBlock.Style>
+ <Style TargetType="TextBlock" BasedOn="{StaticResource TextDefault}">
+ <Setter Property="Visibility" Value="Collapsed"/>
+ <Style.Triggers>
+ <DataTrigger Binding="{Binding Progress, RelativeSource={RelativeSource AncestorType=local:ToolsetCard}}" Value="0">
+ <Setter Property="Visibility" Value="Visible"/>
+ </DataTrigger>
+ </Style.Triggers>
+ </Style>
+ </TextBlock.Style>
+ <Run Text="{DynamicResource connecting_ts}"/><Run Text="..."/>
+ </TextBlock>
+
+ <Grid>
+ <Grid.Style>
+ <Style TargetType="Grid">
+ <Setter Property="Visibility" Value="Visible"/>
+ <Style.Triggers>
+ <DataTrigger Binding="{Binding Progress, RelativeSource={RelativeSource AncestorType=local:ToolsetCard}}" Value="0">
+ <Setter Property="Visibility" Value="Collapsed"/>
+ </DataTrigger>
+ </Style.Triggers>
+ </Style>
+ </Grid.Style>
+
+ <TextBlock Panel.ZIndex="5" Text="{Binding Progress, StringFormat='{}{0:0}%', RelativeSource={RelativeSource AncestorType=local:ToolsetCard}}" Style="{StaticResource TextDefault}" FontFamily="{StaticResource Play}" Foreground="{StaticResource Brush_Highlighted_Inverted}" FontSize="12px" HorizontalAlignment="Center" VerticalAlignment="Center"/>
+ <ProgressBar Value="{Binding Progress, RelativeSource={RelativeSource AncestorType=local:ToolsetCard}}" Foreground="{StaticResource Brush_Accent}" Minimum="0" Maximum="100" Height="20" MinWidth="150" VerticalAlignment="Center"/>
+ </Grid>
</Grid>
<Button Grid.Column="1" Content="{DynamicResource btn_cancel}" Style="{StaticResource ButtonContent_Inverted}" Command="{Binding CancelCommand, RelativeSource={RelativeSource AncestorType=local:ToolsetCard}}" Margin="10,0,0,0"/>
diff --git a/.Source/GTweak/Core/Services/ToolsetDownloadService.cs b/.Source/GTweak/Core/Services/ToolsetDownloadService.cs
index 24a3b3dc..7c861b08 100644
--- a/.Source/GTweak/Core/Services/ToolsetDownloadService.cs
+++ b/.Source/GTweak/Core/Services/ToolsetDownloadService.cs
@@ -1,6 +1,8 @@
using System;
using System.Collections.Concurrent;
+using System.Collections.Generic;
using System.IO;
+using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
@@ -37,14 +39,27 @@ namespace GTweak.Core.Services
}
string htmlCode = await _httpClient.GetStringAsync(model.DownloadPath);
- Match match = Regex.Match(htmlCode, model.UrlPattern);
- return match.Success ? match.Value : null;
+ Match match = Regex.Match(htmlCode, model.UrlPattern, RegexOptions.IgnoreCase);
+
+ if (match.Success)
+ {
+ string url = match.Value;
+ if (Uri.TryCreate(url, UriKind.Relative, out _))
+ {
+ if (Uri.TryCreate(new Uri(model.DownloadPath), url, out Uri absoluteUri))
+ {
+ return absoluteUri.ToString();
+ }
+ }
+ return url;
+ }
+
+ throw new HttpRequestException();
}
if (model.Group.Equals("github", StringComparison.OrdinalIgnoreCase))
{
string apiUrl = $"https://api.github.com/repos/{model.DownloadPath}/releases/latest";
-
using HttpResponseMessage response = await _httpClient.GetAsync(apiUrl);
if (!response.IsSuccessStatusCode)
@@ -53,6 +68,7 @@ namespace GTweak.Core.Services
{
throw new Exception("GitHubRateLimit");
}
+
throw new HttpRequestException();
}
@@ -61,62 +77,89 @@ namespace GTweak.Core.Services
if (json["assets"] is JArray assets && assets.Count > 0)
{
- string firstFoundUrl = null;
- foreach (JToken asset in assets)
+ List<string> urls = assets.Select(a => a["browser_download_url"]?.ToString()).Where(u => !string.IsNullOrEmpty(u)).ToList();
+
+ if (!string.IsNullOrEmpty(model.FilePattern))
{
- string downloadUrl = asset["browser_download_url"]?.ToString();
- if (string.IsNullOrEmpty(downloadUrl))
+ string matchedUrl = urls.FirstOrDefault(u => Regex.IsMatch(u, model.FilePattern, RegexOptions.IgnoreCase));
+ if (matchedUrl != null)
{
- continue;
+ return matchedUrl;
}
+ }
- firstFoundUrl ??= downloadUrl;
+ string fallbackUrl = urls.FirstOrDefault(u => !u.EndsWith(".sig", StringComparison.OrdinalIgnoreCase) && !u.EndsWith(".sha256", StringComparison.OrdinalIgnoreCase) && !u.EndsWith(".asc", StringComparison.OrdinalIgnoreCase) && !u.EndsWith(".txt", StringComparison.OrdinalIgnoreCase));
- if (!string.IsNullOrEmpty(model.FilePattern) &&
- Regex.IsMatch(downloadUrl, model.FilePattern, RegexOptions.IgnoreCase))
- {
- return downloadUrl;
- }
- }
- return firstFoundUrl;
+ return fallbackUrl ?? urls.FirstOrDefault();
}
+ throw new HttpRequestException();
}
if (model.Group.Equals("sourceforge", StringComparison.OrdinalIgnoreCase))
{
string projectName = model.DownloadPath;
Match match = Regex.Match(projectName, @"projects/([^/]+)");
- if (match.Success)
+ projectName = match.Success ? match.Groups[1].Value : projectName.Trim('/');
+
+ string bestReleaseUrl = $"https://sourceforge.net/projects/{projectName}/best_release.json";
+ try
{
- projectName = match.Groups[1].Value;
+ using HttpResponseMessage response = await _httpClient.GetAsync(bestReleaseUrl);
+ if (response.IsSuccessStatusCode)
+ {
+ string content = await response.Content.ReadAsStringAsync();
+ JObject json = JObject.Parse(content);
+ string filename = json["release"]?["filename"]?.ToString();
+
+ if (!string.IsNullOrEmpty(filename))
+ {
+ if (string.IsNullOrEmpty(model.FilePattern) || Regex.IsMatch(filename, model.FilePattern, RegexOptions.IgnoreCase))
+ {
+ return $"https://downloads.sourceforge.net/project/{projectName}{filename}";
+ }
+ }
+ }
}
- else
+ catch (Exception ex)
{
- projectName = projectName.Trim('/');
+ ErrorLogging.LogDebug(ex);
}
- string apiUrl = $"https://sourceforge.net/projects/{projectName}/best_release.json";
+ try
+ {
+ string rssUrl = $"https://sourceforge.net/projects/{projectName}/rss?path=/";
+ string rssContent = await _httpClient.GetStringAsync(rssUrl);
- using HttpResponseMessage response = await _httpClient.GetAsync(apiUrl);
+ MatchCollection rssMatches = Regex.Matches(rssContent, $@"<link>https://sourceforge\.net/projects/{projectName}/files/([^<]+?)/download</link>", RegexOptions.IgnoreCase);
- if (!response.IsSuccessStatusCode)
- {
- throw new HttpRequestException();
- }
+ if (rssMatches.Count > 0)
+ {
+ if (!string.IsNullOrEmpty(model.FilePattern))
+ {
+ foreach (Match rssMatch in rssMatches)
+ {
+ string filePath = rssMatch.Groups[1].Value;
+ if (Regex.IsMatch(filePath, model.FilePattern, RegexOptions.IgnoreCase))
+ {
- string content = await response.Content.ReadAsStringAsync();
- JObject json = JObject.Parse(content);
+ return $"https://downloads.sourceforge.net/project/{projectName}/{filePath}";
+ }
+ }
+ }
- string filename = json["release"]?["filename"]?.ToString();
- if (!string.IsNullOrEmpty(filename))
+ string firstFilePath = rssMatches[0].Groups[1].Value;
+ return $"https://downloads.sourceforge.net/project/{projectName}/{firstFilePath}";
+ }
+ }
+ catch (Exception ex)
{
- return $"https://downloads.sourceforge.net/project/{projectName}{filename}";
+ ErrorLogging.LogDebug(ex);
}
- return null;
+ throw new HttpRequestException();
}
- return null;
+ throw new HttpRequestException();
}
internal static async Task DownloadFile(string url, string destinationPath, string referrer, IProgress<double> progress, CancellationToken ct)
@@ -125,13 +168,18 @@ namespace GTweak.Core.Services
long existingBytes = 0;
bool append = false;
- try
+ if (File.Exists(tempPath))
{
- if (File.Exists(tempPath))
- {
- existingBytes = new FileInfo(tempPath).Length;
- }
+ existingBytes = new FileInfo(tempPath).Length;
+ }
+
+ if (existingBytes == 0)
+ {
+ progress?.Report(0);
+ }
+ try
+ {
using HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, url);
if (!string.IsNullOrEmpty(referrer))
@@ -150,6 +198,7 @@ namespace GTweak.Core.Services
{
existingBytes = 0;
append = false;
+ progress?.Report(0);
}
else if (response.StatusCode == HttpStatusCode.PartialContent && existingBytes > 0)
{
diff --git a/.Source/GTweak/Core/ViewModel/Components/DownloadSession.cs b/.Source/GTweak/Core/ViewModel/Components/DownloadSession.cs
index b7ba1682..dc988cad 100644
--- a/.Source/GTweak/Core/ViewModel/Components/DownloadSession.cs
+++ b/.Source/GTweak/Core/ViewModel/Components/DownloadSession.cs
@@ -53,24 +53,49 @@ namespace GTweak.Core.ViewModel.Components
public async Task Start()
{
+ IsDownloading = true;
+ Progress = 0;
+
try
{
string finalUrl = await ToolsetDownloadService.GetResolvedDownloadUrl(_model);
- if (!string.IsNullOrEmpty(finalUrl))
+ if (string.IsNullOrEmpty(finalUrl))
{
- IsDownloading = true;
- _cts = new CancellationTokenSource();
+ IsDownloading = false;
+ return;
+ }
+
+ _cts = new CancellationTokenSource();
+ string destinationFolder = SettingsEngine.DownloadPath ?? Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
+ string destinationPath = Path.Combine(destinationFolder, GetFileNameFromUrl(finalUrl));
- string destinationFolder = SettingsEngine.DownloadPath ?? Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
- string destinationPath = Path.Combine(destinationFolder, GetFileNameFromUrl(finalUrl));
+ if (!File.Exists($"{destinationPath}.download"))
+ {
+ Progress = 0;
+ }
- await ToolsetDownloadService.DownloadFile(finalUrl, destinationPath, _model.SourceUrl, new Progress<double>(v => Progress = v), _cts.Token);
+ await ToolsetDownloadService.DownloadFile(finalUrl, destinationPath, _model.SourceUrl, new Progress<double>(v => Progress = v), _cts.Token);
+ }
+ catch (Exception ex)
+ {
+ await Task.Delay(500);
+
+ switch (ex)
+ {
+ case Exception e when e.Message == "GitHubRateLimit":
+ NotificationManager.Show("warn", "error_git_limit_noty").Perform();
+ break;
+ case HttpRequestException _:
+ NotificationManager.Show("warn", "error_download_noty").Perform();
+ break;
+ case OperationCanceledException _:
+ break;
+ default:
+ ErrorLogging.LogDebug(ex);
+ break;
}
}
- catch (Exception ex) when (ex.Message == "GitHubRateLimit") { NotificationManager.Show("warn", "error_git_limit_noty").Perform(); }
- catch (HttpRequestException) { NotificationManager.Show("warn", "error_download_noty").Perform(); }
- catch (Exception ex) { ErrorLogging.LogDebug(ex); }
finally
{
IsDownloading = false;
diff --git a/.Source/GTweak/Languages/en/Localize.xaml b/.Source/GTweak/Languages/en/Localize.xaml
index 6c6f0b59..37eaeffe 100644
--- a/.Source/GTweak/Languages/en/Localize.xaml
+++ b/.Source/GTweak/Languages/en/Localize.xaml
@@ -554,6 +554,7 @@
<v:String x:Key="btn_cancel">Сancel</v:String>
<v:String x:Key="btn_open_dir">Open folder</v:String>
<v:String x:Key="text_ts">Search by name or author</v:String>
+ <v:String x:Key="connecting_ts">Connecting</v:String>
<!--#endregion-->
</ResourceDictionary>
\ No newline at end of file
diff --git a/.Source/GTweak/Languages/fr/Localize.xaml b/.Source/GTweak/Languages/fr/Localize.xaml
index a0116a0b..c41ebfb3 100644
--- a/.Source/GTweak/Languages/fr/Localize.xaml
+++ b/.Source/GTweak/Languages/fr/Localize.xaml
@@ -554,6 +554,7 @@
<v:String x:Key="btn_cancel">Сancel</v:String>
<v:String x:Key="btn_open_dir">Open folder</v:String>
<v:String x:Key="text_ts">Search by name or author</v:String>
+ <v:String x:Key="connecting_ts">Connecting</v:String>
<!--#endregion-->
</ResourceDictionary>
diff --git a/.Source/GTweak/Languages/hu/Localize.xaml b/.Source/GTweak/Languages/hu/Localize.xaml
index 5a6516e2..57d3a302 100644
--- a/.Source/GTweak/Languages/hu/Localize.xaml
+++ b/.Source/GTweak/Languages/hu/Localize.xaml
@@ -554,6 +554,7 @@
<v:String x:Key="btn_cancel">Сancel</v:String>
<v:String x:Key="btn_open_dir">Open folder</v:String>
<v:String x:Key="text_ts">Search by name or author</v:String>
+ <v:String x:Key="connecting_ts">Connecting</v:String>
<!--#endregion-->
</ResourceDictionary>
diff --git a/.Source/GTweak/Languages/it/Localize.xaml b/.Source/GTweak/Languages/it/Localize.xaml
index 287c464c..109127e7 100644
--- a/.Source/GTweak/Languages/it/Localize.xaml
+++ b/.Source/GTweak/Languages/it/Localize.xaml
@@ -554,6 +554,7 @@
<v:String x:Key="btn_cancel">Сancel</v:String>
<v:String x:Key="btn_open_dir">Open folder</v:String>
<v:String x:Key="text_ts">Search by name or author</v:String>
+ <v:String x:Key="connecting_ts">Connecting</v:String>
<!--#endregion-->
</ResourceDictionary>
diff --git a/.Source/GTweak/Languages/ko/Localize.xaml b/.Source/GTweak/Languages/ko/Localize.xaml
index 547c0c13..d69d2263 100644
--- a/.Source/GTweak/Languages/ko/Localize.xaml
+++ b/.Source/GTweak/Languages/ko/Localize.xaml
@@ -554,6 +554,7 @@
<v:String x:Key="btn_cancel">Сancel</v:String>
<v:String x:Key="btn_open_dir">Open folder</v:String>
<v:String x:Key="text_ts">Search by name or author</v:String>
+ <v:String x:Key="connecting_ts">Connecting</v:String>
<!--#endregion-->
</ResourceDictionary>
diff --git a/.Source/GTweak/Languages/pl/Localize.xaml b/.Source/GTweak/Languages/pl/Localize.xaml
index 1f82162b..8546569b 100644
--- a/.Source/GTweak/Languages/pl/Localize.xaml
+++ b/.Source/GTweak/Languages/pl/Localize.xaml
@@ -554,6 +554,7 @@
<v:String x:Key="btn_cancel">Сancel</v:String>
<v:String x:Key="btn_open_dir">Open folder</v:String>
<v:String x:Key="text_ts">Search by name or author</v:String>
+ <v:String x:Key="connecting_ts">Connecting</v:String>
<!--#endregion-->
</ResourceDictionary>
diff --git a/.Source/GTweak/Languages/pt-BR/Localize.xaml b/.Source/GTweak/Languages/pt-BR/Localize.xaml
index 8d314f8e..4885dc68 100644
--- a/.Source/GTweak/Languages/pt-BR/Localize.xaml
+++ b/.Source/GTweak/Languages/pt-BR/Localize.xaml
@@ -554,6 +554,7 @@
<v:String x:Key="btn_cancel">Сancel</v:String>
<v:String x:Key="btn_open_dir">Open folder</v:String>
<v:String x:Key="text_ts">Search by name or author</v:String>
+ <v:String x:Key="connecting_ts">Connecting</v:String>
<!--#endregion-->
</ResourceDictionary>
diff --git a/.Source/GTweak/Languages/ru/Localize.xaml b/.Source/GTweak/Languages/ru/Localize.xaml
index afed813d..23c77d3d 100644
--- a/.Source/GTweak/Languages/ru/Localize.xaml
+++ b/.Source/GTweak/Languages/ru/Localize.xaml
@@ -554,6 +554,7 @@
<v:String x:Key="btn_cancel">Отмена</v:String>
<v:String x:Key="btn_open_dir">Открыть папку</v:String>
<v:String x:Key="text_ts">Поиск по названию или автору</v:String>
+ <v:String x:Key="connecting_ts">Подключение</v:String>
<!--#endregion-->
</ResourceDictionary>
diff --git a/.Source/GTweak/Languages/uk/Localize.xaml b/.Source/GTweak/Languages/uk/Localize.xaml
index 426ee568..48314361 100644
--- a/.Source/GTweak/Languages/uk/Localize.xaml
+++ b/.Source/GTweak/Languages/uk/Localize.xaml
@@ -554,6 +554,7 @@
<v:String x:Key="btn_cancel">Сancel</v:String>
<v:String x:Key="btn_open_dir">Open folder</v:String>
<v:String x:Key="text_ts">Search by name or author</v:String>
+ <v:String x:Key="connecting_ts">Connecting</v:String>
<!--#endregion-->
</ResourceDictionary>
diff --git a/.Source/GTweak/Languages/zh-CN/Localize.xaml b/.Source/GTweak/Languages/zh-CN/Localize.xaml
index b3595de1..0c608c2f 100644
--- a/.Source/GTweak/Languages/zh-CN/Localize.xaml
+++ b/.Source/GTweak/Languages/zh-CN/Localize.xaml
@@ -554,6 +554,7 @@
<v:String x:Key="btn_cancel">Сancel</v:String>
<v:String x:Key="btn_open_dir">Open folder</v:String>
<v:String x:Key="text_ts">Search by name or author</v:String>
+ <v:String x:Key="connecting_ts">Connecting</v:String>
<!--#endregion-->
</ResourceDictionary>
diff --git a/.Source/GTweak/Languages/zh-TW/Localize.xaml b/.Source/GTweak/Languages/zh-TW/Localize.xaml
index 6d58f8fc..774b5b7d 100644
--- a/.Source/GTweak/Languages/zh-TW/Localize.xaml
+++ b/.Source/GTweak/Languages/zh-TW/Localize.xaml
@@ -554,6 +554,7 @@
<v:String x:Key="btn_cancel">Сancel</v:String>
<v:String x:Key="btn_open_dir">Open folder</v:String>
<v:String x:Key="text_ts">Search by name or author</v:String>
+ <v:String x:Key="connecting_ts">Connecting</v:String>
<!--#endregion-->
</ResourceDictionary>