Skip to content

Commit

Permalink
Merge pull request #127 from stefannikolei/feature/cacheRoot
Browse files Browse the repository at this point in the history
Add an option to enable setting a different CacheRoot
  • Loading branch information
JimBobSquarePants committed Nov 12, 2020
2 parents 2c5961e + af70a40 commit afff0fe
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/ImageSharp.Web/Caching/PhysicalFileSystemCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public PhysicalFileSystemCache(

// Allow configuration of the cache without having to register everything.
this.cacheOptions = cacheOptions != null ? cacheOptions.Value : new PhysicalFileSystemCacheOptions();
this.cacheRootPath = Path.Combine(environment.WebRootPath, this.cacheOptions.CacheFolder);
this.cacheRootPath = GetCacheRoot(this.cacheOptions, environment.WebRootPath, environment.ContentRootPath);
if (!Directory.Exists(this.cacheRootPath))
{
Directory.CreateDirectory(this.cacheRootPath);
Expand All @@ -84,6 +84,24 @@ public PhysicalFileSystemCache(
this.formatUtilities = formatUtilities;
}

/// <summary>
/// Determine the cache root path
/// </summary>
/// <param name="cacheOptions">the cache options.</param>
/// <param name="webRootPath">the webRootPath.</param>
/// <param name="contentRootPath">the contentRootPath.</param>
/// <returns>root path.</returns>
internal static string GetCacheRoot(PhysicalFileSystemCacheOptions cacheOptions, string webRootPath, string contentRootPath)
{
var cacheRoot = string.IsNullOrEmpty(cacheOptions.CacheRoot)
? webRootPath
: cacheOptions.CacheRoot;

return Path.IsPathFullyQualified(cacheRoot)
? Path.Combine(cacheRoot, cacheOptions.CacheFolder)
: Path.GetFullPath(Path.Combine(cacheRoot, cacheOptions.CacheFolder), contentRootPath);
}

/// <inheritdoc/>
public Task<IImageCacheResolver> GetAsync(string key)
{
Expand Down
5 changes: 5 additions & 0 deletions src/ImageSharp.Web/Caching/PhysicalFileSystemCacheOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,10 @@ public class PhysicalFileSystemCacheOptions
/// Gets or sets the cache folder name.
/// </summary>
public string CacheFolder { get; set; } = "is-cache";

/// <summary>
/// Gets or sets the cache root folder.
/// </summary>
public string CacheRoot { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.

using System.IO;
using SixLabors.ImageSharp.Web.Caching;
using Xunit;

Expand All @@ -19,5 +20,30 @@ public void FilePathMatchesReference()

Assert.Equal(expected, actual);
}

[Theory]
#if Linux
[InlineData("cacheFolder", "/Users/username", null, null, "/Users/username/cacheFolder")]
[InlineData("cacheFolder", null, "/Users/WebRoot", null, "/Users/WebRoot/cacheFolder")]
[InlineData("cacheFolder", "../Temp", null, "/Users/this/a/root", "/Users/this/a/Temp/cacheFolder")]
#elif OSX
[InlineData("cacheFolder", "/Users/username", null, null, "/Users/username/cacheFolder")]
[InlineData("cacheFolder", null, "/Users/WebRoot", null, "/Users/WebRoot/cacheFolder")]
[InlineData("cacheFolder", "../Temp", null, "/Users/this/a/root", "/Users/this/a/Temp/cacheFolder")]
#elif Windows
[InlineData("cacheFolder", "C:/Temp", null, null, "C:/Temp\\cacheFolder")]
[InlineData("cacheFolder", null, "C:/WebRoot", null, "C:/WebRoot\\cacheFolder")]
[InlineData("cacheFolder", "../Temp", null, "C:/this/a/root", "C:\\this\\a\\Temp\\cacheFolder")]
#endif
public void CacheRootFromOptions(string cacheFolder, string cacheRoot, string webRootPath, string contentRootPath, string expected)
{
var cacheOptions = new PhysicalFileSystemCacheOptions();
cacheOptions.CacheFolder = cacheFolder;
cacheOptions.CacheRoot = cacheRoot;

var cacheRootResult = PhysicalFileSystemCache.GetCacheRoot(cacheOptions, webRootPath, contentRootPath);

Assert.Equal(expected, cacheRootResult);
}
}
}
13 changes: 13 additions & 0 deletions tests/ImageSharp.Web.Tests/ImageSharp.Web.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@

<!--Used to show test project to dotnet test-->
<IsTestProject>true</IsTestProject>
<IsWindows Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Windows)))' == 'true'">true</IsWindows>
<IsOSX Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::OSX)))' == 'true'">true</IsOSX>
<IsLinux Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Linux)))' == 'true'">true</IsLinux>
</PropertyGroup>

<PropertyGroup Condition="'$(IsWindows)'=='true'">
<DefineConstants>Windows</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(IsOSX)'=='true'">
<DefineConstants>OSX</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(IsLinux)'=='true'">
<DefineConstants>Linux</DefineConstants>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit afff0fe

Please sign in to comment.