Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(#384) Fix the invalid URI error #388

Merged
merged 1 commit into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions src/Lanceur.Infra/Managers/PackagedAppValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public PackagedAppValidator(IPackagedAppSearchService searchService, IFavIconDow
/// </summary>
/// <param name="alias">The alias to standardise</param>
/// <returns>Standardised alias</returns>
/// <exception cref="ApplicationException">When the <c>alias.FileName</c> is not a valid URI</exception>
public async Task<AliasQueryResult> FixAsync(AliasQueryResult alias)
{
if (alias is null) return default;
Expand All @@ -56,15 +57,22 @@ public async Task<AliasQueryResult> FixAsync(AliasQueryResult alias)

if (response is null)
{
var uri = new Uri(alias.FileName);

if (!await _favIconDownloader.CheckExistsAsync(new($"{uri.Scheme}://{uri.Host}"))) return alias;

var output = Path.Combine(AppPaths.ImageCache, $"{AppPaths.FaviconPrefix}{uri.Host}.png");
await _favIconDownloader.SaveToFileAsync(uri, output);
alias.Thumbnail = output;
alias.Icon = null;
return alias;
try
{
var uri = new Uri(alias.FileName);

if (!await _favIconDownloader.CheckExistsAsync(new($"{uri.Scheme}://{uri.Host}"))) return alias;

var output = Path.Combine(AppPaths.ImageCache, $"{AppPaths.FaviconPrefix}{uri.Host}.png");
await _favIconDownloader.SaveToFileAsync(uri, output);
alias.Thumbnail = output;
alias.Icon = null;
return alias;
}
catch (UriFormatException ex)
{
throw new ApplicationException($"Create an URI from '{alias.FileName}' is impossible.", ex);
}
}

// This is a packaged app
Expand Down
15 changes: 12 additions & 3 deletions src/Lanceur/Views/KeywordsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,18 @@ private async Task<AliasQueryResult> OnSaveOrUpdateAliasAsync(AliasQueryResult a
BusyMessage = "Saving alias...";
using (_busyScope.Open())
{
alias.SetName();
alias = await _packagedAppValidator.FixAsync(alias);
_aliasService.SaveOrUpdate(ref alias);
try
{
alias.SetName();
alias = await _packagedAppValidator.FixAsync(alias);
_aliasService.SaveOrUpdate(ref alias);
}
catch (ApplicationException ex)
{
_log.Warning(ex.Message);
_notify.Warning($"Error when {(created ? "creating" : "updating")} the alias: {ex.Message}");
return alias;
}
}
_notification.Information($"Alias '{alias.Name}' {(created ? "created" : "updated")}.");

Expand Down
Loading