From b2d6ada99df64621d079ce42d21de503aa95c7ec Mon Sep 17 00:00:00 2001 From: Steve Desmond Date: Thu, 28 Oct 2021 16:21:20 -0400 Subject: [PATCH] Fix Statiq image caching after the bug was fixed --- SrcSet.Core/SrcSet.Core.csproj | 2 +- .../CreateReponsiveImagesTests.cs | 10 +++++----- SrcSet.Statiq/CreateResponsiveImages.cs | 19 ++++++++++++++----- SrcSet.Statiq/NormalizedPathExtensions.cs | 4 ++-- SrcSet.Statiq/ResponsiveImages.cs | 2 +- SrcSet.Statiq/SrcSet.Statiq.csproj | 2 +- SrcSet/SrcSet.csproj | 2 +- 7 files changed, 25 insertions(+), 16 deletions(-) diff --git a/SrcSet.Core/SrcSet.Core.csproj b/SrcSet.Core/SrcSet.Core.csproj index 9704ece..3e0d8d0 100644 --- a/SrcSet.Core/SrcSet.Core.csproj +++ b/SrcSet.Core/SrcSet.Core.csproj @@ -1,7 +1,7 @@ net5.0 - 3.2.1 + 3.2.2 SrcSet.Core A library to create sets of responsive images for the web ecoAPM LLC diff --git a/SrcSet.Statiq.Tests/CreateReponsiveImagesTests.cs b/SrcSet.Statiq.Tests/CreateReponsiveImagesTests.cs index 6b03edc..b3c5b26 100644 --- a/SrcSet.Statiq.Tests/CreateReponsiveImagesTests.cs +++ b/SrcSet.Statiq.Tests/CreateReponsiveImagesTests.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.IO; using System.Linq; using System.Threading.Tasks; @@ -47,9 +47,9 @@ public async Task ImageIsNotLoadedIfAllSizesExist() var doc = new TestDocument(new NormalizedPath("input/test.png")); var context = new TestExecutionContext(); context.SetInputs(doc); - context.FileSystem.GetCacheFile("input/test-0001.png").OpenWrite(); - context.FileSystem.GetCacheFile("input/test-0002.png").OpenWrite(); - context.FileSystem.GetCacheFile("input/test-0003.png").OpenWrite(); + context.FileSystem.GetOutputFile("input/test-0001.png").OpenWrite(); + context.FileSystem.GetOutputFile("input/test-0002.png").OpenWrite(); + context.FileSystem.GetOutputFile("input/test-0003.png").OpenWrite(); var module = new CreateResponsiveImages(_ => throw new Exception(), new ushort[] { 1, 2, 3 }); @@ -67,7 +67,7 @@ public async Task ImageIsOnlyResizedForNewSizes() var doc = new TestDocument(new NormalizedPath("input/test.png")); var context = new TestExecutionContext(); context.SetInputs(doc); - context.FileSystem.GetCacheFile("input/test-0002.png").OpenWrite(); + context.FileSystem.GetOutputFile("input/test-0002.png").OpenWrite(); Image image = new Image(4, 4); diff --git a/SrcSet.Statiq/CreateResponsiveImages.cs b/SrcSet.Statiq/CreateResponsiveImages.cs index abb93ef..89bfeaa 100644 --- a/SrcSet.Statiq/CreateResponsiveImages.cs +++ b/SrcSet.Statiq/CreateResponsiveImages.cs @@ -26,12 +26,13 @@ protected override async Task> ExecuteInputAsync(IDocumen { var inputStream = input.GetContentStream(); var destinations = _widths.Select(w => input.Source.GetDestination(w)).ToList(); - var cached = destinations.Select(d => context.FileSystem.GetCacheFile(d)).Where(f => f.Exists).ToList(); + var cached = destinations.Select(d => context.FileSystem.GetOutputFile(d)).Where(f => f.Exists).ToList(); var alreadyDone = cached.Count == destinations.Count; if (alreadyDone) { context.Log(LogLevel.Debug, $"Skipping {input.Source} since all sizes already exist"); - return cached.Select(f => f.ToDocument()); + var docs = cached.Select(f => CachedDocument(input.Source, f.Path, f, context)); + return await Task.WhenAll(docs); } var image = await _loadImage(inputStream); @@ -42,14 +43,14 @@ protected override async Task> ExecuteInputAsync(IDocumen private static async Task GetResizedDocument(NormalizedPath source, Image image, ushort width, IExecutionContext context) { var destination = source.GetDestination(width); - var cached = context.FileSystem.GetCacheFile(destination); + var cached = context.FileSystem.GetOutputFile(destination); if (cached.Exists) { context.Log(LogLevel.Debug, $"Skipping {destination} since it already exists"); - return cached.ToDocument(); + return await CachedDocument(source, destination, cached, context); } - var encoder = image.DetectEncoder(destination); + var encoder = image.DetectEncoder(destination.ToString()); var output = new MemoryStream(); context.Log(LogLevel.Debug, $"Resizing {source} to {destination}..."); @@ -60,5 +61,13 @@ private static async Task GetResizedDocument(NormalizedPath source, I var content = context.GetContentProvider(output); return context.CreateDocument(source, destination, content); } + + private static async Task CachedDocument(NormalizedPath source, NormalizedPath destination, IFile cached, IExecutionContext context) + { + var stream = new MemoryStream(); + await cached.OpenRead().CopyToAsync(stream); + var content = context.GetContentProvider(stream); + return context.CreateDocument(source, context.FileSystem.GetRelativeOutputPath(destination), content); + } } } \ No newline at end of file diff --git a/SrcSet.Statiq/NormalizedPathExtensions.cs b/SrcSet.Statiq/NormalizedPathExtensions.cs index d58368c..d396b8f 100644 --- a/SrcSet.Statiq/NormalizedPathExtensions.cs +++ b/SrcSet.Statiq/NormalizedPathExtensions.cs @@ -9,11 +9,11 @@ public static class NormalizedPathExtensions public static bool IsImage(this NormalizedPath path) => SrcSetManager.ValidExtensions.Contains(path.Extension); - public static string GetDestination(this NormalizedPath source, ushort width) + public static NormalizedPath GetDestination(this NormalizedPath source, ushort width) { var input = source.GetRelativeInputPath(); var filename = FileHelpers.GetFilename(source.FileName.ToString(), width); - return input.ChangeFileName(filename).ToString(); + return input.ChangeFileName(filename); } } } \ No newline at end of file diff --git a/SrcSet.Statiq/ResponsiveImages.cs b/SrcSet.Statiq/ResponsiveImages.cs index 077b2bd..ac4daba 100644 --- a/SrcSet.Statiq/ResponsiveImages.cs +++ b/SrcSet.Statiq/ResponsiveImages.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.IO; using System.Linq; diff --git a/SrcSet.Statiq/SrcSet.Statiq.csproj b/SrcSet.Statiq/SrcSet.Statiq.csproj index 0dec36d..2b09b63 100644 --- a/SrcSet.Statiq/SrcSet.Statiq.csproj +++ b/SrcSet.Statiq/SrcSet.Statiq.csproj @@ -1,7 +1,7 @@ net5.0 - 3.2.1 + 3.2.2 SrcSet.Statiq Automate creating sets of responsive images for your Statiq site ecoAPM LLC diff --git a/SrcSet/SrcSet.csproj b/SrcSet/SrcSet.csproj index bcb3793..00fe3f1 100644 --- a/SrcSet/SrcSet.csproj +++ b/SrcSet/SrcSet.csproj @@ -2,7 +2,7 @@ net5.0 - 3.2.1 + 3.2.2 SrcSet A CLI to create sets of responsive images for the web ecoAPM LLC