Skip to content

Commit

Permalink
Fix Statiq image caching after the bug was fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveDesmond-ca committed Oct 28, 2021
1 parent f83647a commit b2d6ada
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 16 deletions.
2 changes: 1 addition & 1 deletion SrcSet.Core/SrcSet.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<Version>3.2.1</Version>
<Version>3.2.2</Version>
<PackageId>SrcSet.Core</PackageId>
<Description>A library to create sets of responsive images for the web</Description>
<Authors>ecoAPM LLC</Authors>
Expand Down
10 changes: 5 additions & 5 deletions SrcSet.Statiq.Tests/CreateReponsiveImagesTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
Expand Down Expand Up @@ -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 });

Expand All @@ -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<Rgba32>(4, 4);

Expand Down
19 changes: 14 additions & 5 deletions SrcSet.Statiq/CreateResponsiveImages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ protected override async Task<IEnumerable<IDocument>> 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);
Expand All @@ -42,14 +43,14 @@ protected override async Task<IEnumerable<IDocument>> ExecuteInputAsync(IDocumen
private static async Task<IDocument> 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}...");
Expand All @@ -60,5 +61,13 @@ private static async Task<IDocument> GetResizedDocument(NormalizedPath source, I
var content = context.GetContentProvider(output);
return context.CreateDocument(source, destination, content);
}

private static async Task<IDocument> 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);
}
}
}
4 changes: 2 additions & 2 deletions SrcSet.Statiq/NormalizedPathExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
2 changes: 1 addition & 1 deletion SrcSet.Statiq/ResponsiveImages.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand Down
2 changes: 1 addition & 1 deletion SrcSet.Statiq/SrcSet.Statiq.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<Version>3.2.1</Version>
<Version>3.2.2</Version>
<PackageId>SrcSet.Statiq</PackageId>
<Description>Automate creating sets of responsive images for your Statiq site</Description>
<Authors>ecoAPM LLC</Authors>
Expand Down
2 changes: 1 addition & 1 deletion SrcSet/SrcSet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<Version>3.2.1</Version>
<Version>3.2.2</Version>
<PackageId>SrcSet</PackageId>
<Description>A CLI to create sets of responsive images for the web</Description>
<Authors>ecoAPM LLC</Authors>
Expand Down

0 comments on commit b2d6ada

Please sign in to comment.