Skip to content

Commit

Permalink
Merge pull request #80 from aschuhardt/develop
Browse files Browse the repository at this point in the history
a handful of fixes for 1.4.7
  • Loading branch information
aschuhardt committed Jan 6, 2024
2 parents 94d3866 + ea5ce32 commit 5570b65
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 28 deletions.
8 changes: 8 additions & 0 deletions Resources/Raw/whats-new.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
margin-top: 0!important;
}
</style>
<h4>Version 1.4.7 - January 6th, 2024</h4>
<p>Happy new year!</p>
<ul>
<li><a class="tracker-id" href="gemini://bbs.geminispace.org/s/Rosy-Crow-Issues/42">#42</a> Inlined image previews will now correctly open the source image when clicked.</li>
<li><a class="tracker-id" href="gemini://bbs.geminispace.org/s/Rosy-Crow-Issues/44">#44</a> Fixed relative link parsing in Opal 1.7.6 and updated Rosy Crow to use that version. This specifically affects links with relative URLs which lack a leading forward-slash.</li>
<li>When prompting the user for input (for 1X responses), empty strings were being discarded. I changed this so that empty query parameters are sent correctly.</li>
<li>I fixed an issue which prevented activating an identity when another identity was already active.</li>
</ul>
<h4>Version 1.4.6 - December 26th, 2023</h4>
<p><a class="tracker-id" href="gemini://bbs.geminispace.org/s/Rosy-Crow-Issues/38">#38</a> Error logs will now be correctly exported when that feature is used, rather than an empty file.</p>
<h4>Version 1.4.5 - December 3rd, 2023</h4>
Expand Down
4 changes: 2 additions & 2 deletions RosyCrow.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@
<PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" />
<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="$(MauiVersion)" />
<PackageReference Include="CommunityToolkit.Maui" Version="6.0.0" />
<PackageReference Include="HtmlAgilityPack" Version="1.11.54" />
<PackageReference Include="HtmlAgilityPack" Version="1.11.57" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="7.0.0" />
<PackageReference Include="MimeTypes" Version="2.4.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Opal" Version="1.7.4" />
<PackageReference Include="Opal" Version="1.7.6" />
<PackageReference Include="Serilog" Version="3.0.1" />
<PackageReference Include="Serilog.Exceptions" Version="8.4.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="7.0.0" />
Expand Down
25 changes: 13 additions & 12 deletions Services/Cache/DiskCacheService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ public DiskCacheService(ILogger<DiskCacheService> logger)
_logger.LogInformation(@"{Count} cache buckets pruned upon initialization", prunedCount);
}

public async Task<bool> TryRead(Uri uri, Stream destination)
public async Task<bool> TryRead(Uri uri, Stream destination, bool isImage)
{
try
{
if (!TryFindCachedByUri(uri, out var path))
if (!TryFindCachedByUri(uri, isImage, out var path))
return false;

_logger.LogDebug(@"Cached resource found at {Path}", path);
Expand All @@ -52,11 +52,11 @@ public async Task<bool> TryRead(Uri uri, Stream destination)
}
}

public async Task Write(Uri uri, Stream contents)
public async Task Write(Uri uri, Stream contents, bool isImage)
{
try
{
var path = GetCurrentPathFromUri(uri);
var path = GetCurrentPathFromUri(uri, isImage);

_logger.LogDebug(@"Cached page file path is {Path}", path);

Expand Down Expand Up @@ -114,23 +114,23 @@ private static string GetRootPath()
return FileSystem.CacheDirectory;
}

private bool TryFindCachedByUri(Uri uri, out string path)
private bool TryFindCachedByUri(Uri uri, bool isImage, out string path)
{
// happy path: resource exists in the current hourly bucket
path = GetCurrentPathFromUri(uri);
path = GetCurrentPathFromUri(uri, isImage);

if (File.Exists(path))
return true;

var key = ComputeUriCachePath(uri);
var key = ComputeUriCachePath(uri, isImage);
foreach (var bucket in Directory.GetDirectories(GetRootPath()))
{
path = Path.Combine(bucket, key);

if (File.Exists(path))
{
// move this file into the current bucket so we find it more quickly next time
var newPath = GetCurrentPathFromUri(uri);
var newPath = GetCurrentPathFromUri(uri, isImage);

var directory = Path.GetDirectoryName(newPath);
if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
Expand All @@ -151,9 +151,9 @@ private bool TryFindCachedByUri(Uri uri, out string path)
return false;
}

private static string GetCurrentPathFromUri(Uri uri)
private static string GetCurrentPathFromUri(Uri uri, bool isImage)
{
return Path.Combine(GetRootPath(), GetHourlyDirectoryName(), ComputeUriCachePath(uri));
return Path.Combine(GetRootPath(), GetHourlyDirectoryName(), ComputeUriCachePath(uri, isImage));
}

private static string ComputeCachePath(string bucket, string key)
Expand All @@ -163,9 +163,10 @@ private static string ComputeCachePath(string bucket, string key)
Convert.ToHexString(MD5.HashData(Encoding.Default.GetBytes(key)))[..12] + ".dat");
}

private static string ComputeUriCachePath(Uri uri)
private static string ComputeUriCachePath(Uri uri, bool isImage)
{
return ComputeCachePath(uri.Host.ToUpperInvariant(), uri.PathAndQuery);
var path = ComputeCachePath(uri.Host.ToUpperInvariant(), uri.PathAndQuery);
return isImage ? Path.Combine("images/", path) : path;
}

private static string GetHourlyDirectoryName()
Expand Down
4 changes: 2 additions & 2 deletions Services/Cache/ICacheService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

public interface ICacheService
{
Task<bool> TryRead(Uri uri, Stream destination);
Task Write(Uri uri, Stream contents);
Task<bool> TryRead(Uri uri, Stream destination, bool isImage);
Task Write(Uri uri, Stream contents, bool isImage);
}
8 changes: 4 additions & 4 deletions Services/Document/DocumentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public async Task<RenderedGemtextDocument> RenderGemtextAsHtml(GemtextResponse g

// cache the page prior to injecting the stylesheet
await using var pageBuffer = new MemoryStream(Encoding.UTF8.GetBytes(document.DocumentNode.OuterHtml));
await _cache.Write(gemtext.Uri, pageBuffer);
await _cache.Write(gemtext.Uri, pageBuffer, false);


return new RenderedGemtextDocument { HtmlContents = document.DocumentNode.OuterHtml, Title = title };
Expand Down Expand Up @@ -246,7 +246,7 @@ private static async Task<MemoryStream> CreateInlinedImagePreview(Stream source,
using var downsized = image.Downsize(256.0f, true);

var output = new MemoryStream();
await downsized.SaveAsync(output);
await downsized.SaveAsync(output, ImageFormat.Jpeg, 0.75f);

return output;
}
Expand All @@ -263,7 +263,7 @@ private async Task<string> TryLoadCachedImage(Uri uri)
{
var image = new MemoryStream();

if (await _cache.TryRead(uri, image))
if (await _cache.TryRead(uri, image, true))
{
_logger.LogDebug(@"Loaded cached image originally from {URI}", uri);
return CreateInlineImageDataUrl(image);
Expand Down Expand Up @@ -308,7 +308,7 @@ private async Task<string> FetchAndCacheInlinedImage(Uri uri)
}

image.Seek(0, SeekOrigin.Begin);
await _cache.Write(uri, image);
await _cache.Write(uri, image, true);

_logger.LogDebug(@"Loaded an inlined image from {URI} after {Attempt} attempt(s)", uri, i + 1);

Expand Down
2 changes: 1 addition & 1 deletion Services/Identity/IdentityService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public async Task Activate(Models.Identity identity)
return;

// the identity has already been loaded
if (ActiveCertificate != null)
if (ActiveCertificate?.Thumbprint == identity.Hash)
return;

_unlockingIdentity = true;
Expand Down
13 changes: 6 additions & 7 deletions Views/BrowserView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ public async Task LoadPage(bool triggeredByRefresh = false, bool useCache = fals
{
var cached = new MemoryStream();

if (await _cache.TryRead(_tab.Location, cached))
if (await _cache.TryRead(_tab.Location, cached, false))
{
_logger.LogInformation(@"Loading a cached copy of the page");

Expand All @@ -336,7 +336,7 @@ public async Task LoadPage(bool triggeredByRefresh = false, bool useCache = fals
}
}

if (!string.IsNullOrWhiteSpace(_tab.Input))
if (_tab.Input != null)
{
_logger.LogInformation(@"User provided input ""{Input}""", _tab.Input);
_tab.Location = new UriBuilder(_tab.Location) { Query = _tab.Input }.Uri;
Expand Down Expand Up @@ -381,8 +381,8 @@ private async Task<ResponseAction> HandleTitanResponse(IGeminiResponse response,
_tab.Input = await _tab.ParentPage.DisplayPromptOnMainThread(Text.BrowserView_LoadPage_Input_Required,
inputRequired.Message);

if (string.IsNullOrEmpty(_tab.Input))
return ResponseAction.Finished; // if no user-input was provided, then we cannot continue
if (_tab.Input == null)
return ResponseAction.Finished; // if no user-input was provided, then we cannot continue (empty string is valid)

return ResponseAction.Retry;
}
Expand Down Expand Up @@ -431,12 +431,11 @@ private async Task<ResponseAction> HandleGeminiResponse(IGeminiResponse response

_tab.Input = await _tab.ParentPage.DisplayPromptOnMainThread(Text.BrowserView_LoadPage_Input_Required,
inputRequired.Message);
;

if (string.IsNullOrEmpty(_tab.Input))
if (_tab.Input == null)
{
_settingsDatabase.LastVisitedUrl = response.Uri.ToString();
return ResponseAction.Finished; // if no user-input was provided, then we cannot continue
return ResponseAction.Finished; // if no user-input was provided, then we cannot continue (empty string is valid)
}

return ResponseAction.Retry;
Expand Down

0 comments on commit 5570b65

Please sign in to comment.