Skip to content

Commit afff0fe

Browse files
Merge pull request #127 from stefannikolei/feature/cacheRoot
Add an option to enable setting a different CacheRoot
2 parents 2c5961e + af70a40 commit afff0fe

File tree

4 files changed

+63
-1
lines changed

4 files changed

+63
-1
lines changed

src/ImageSharp.Web/Caching/PhysicalFileSystemCache.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public PhysicalFileSystemCache(
7272

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

87+
/// <summary>
88+
/// Determine the cache root path
89+
/// </summary>
90+
/// <param name="cacheOptions">the cache options.</param>
91+
/// <param name="webRootPath">the webRootPath.</param>
92+
/// <param name="contentRootPath">the contentRootPath.</param>
93+
/// <returns>root path.</returns>
94+
internal static string GetCacheRoot(PhysicalFileSystemCacheOptions cacheOptions, string webRootPath, string contentRootPath)
95+
{
96+
var cacheRoot = string.IsNullOrEmpty(cacheOptions.CacheRoot)
97+
? webRootPath
98+
: cacheOptions.CacheRoot;
99+
100+
return Path.IsPathFullyQualified(cacheRoot)
101+
? Path.Combine(cacheRoot, cacheOptions.CacheFolder)
102+
: Path.GetFullPath(Path.Combine(cacheRoot, cacheOptions.CacheFolder), contentRootPath);
103+
}
104+
87105
/// <inheritdoc/>
88106
public Task<IImageCacheResolver> GetAsync(string key)
89107
{

src/ImageSharp.Web/Caching/PhysicalFileSystemCacheOptions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,10 @@ public class PhysicalFileSystemCacheOptions
1212
/// Gets or sets the cache folder name.
1313
/// </summary>
1414
public string CacheFolder { get; set; } = "is-cache";
15+
16+
/// <summary>
17+
/// Gets or sets the cache root folder.
18+
/// </summary>
19+
public string CacheRoot { get; set; }
1520
}
1621
}

tests/ImageSharp.Web.Tests/Caching/PhysicialFileSystemCacheTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Apache License, Version 2.0.
33

4+
using System.IO;
45
using SixLabors.ImageSharp.Web.Caching;
56
using Xunit;
67

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

2021
Assert.Equal(expected, actual);
2122
}
23+
24+
[Theory]
25+
#if Linux
26+
[InlineData("cacheFolder", "/Users/username", null, null, "/Users/username/cacheFolder")]
27+
[InlineData("cacheFolder", null, "/Users/WebRoot", null, "/Users/WebRoot/cacheFolder")]
28+
[InlineData("cacheFolder", "../Temp", null, "/Users/this/a/root", "/Users/this/a/Temp/cacheFolder")]
29+
#elif OSX
30+
[InlineData("cacheFolder", "/Users/username", null, null, "/Users/username/cacheFolder")]
31+
[InlineData("cacheFolder", null, "/Users/WebRoot", null, "/Users/WebRoot/cacheFolder")]
32+
[InlineData("cacheFolder", "../Temp", null, "/Users/this/a/root", "/Users/this/a/Temp/cacheFolder")]
33+
#elif Windows
34+
[InlineData("cacheFolder", "C:/Temp", null, null, "C:/Temp\\cacheFolder")]
35+
[InlineData("cacheFolder", null, "C:/WebRoot", null, "C:/WebRoot\\cacheFolder")]
36+
[InlineData("cacheFolder", "../Temp", null, "C:/this/a/root", "C:\\this\\a\\Temp\\cacheFolder")]
37+
#endif
38+
public void CacheRootFromOptions(string cacheFolder, string cacheRoot, string webRootPath, string contentRootPath, string expected)
39+
{
40+
var cacheOptions = new PhysicalFileSystemCacheOptions();
41+
cacheOptions.CacheFolder = cacheFolder;
42+
cacheOptions.CacheRoot = cacheRoot;
43+
44+
var cacheRootResult = PhysicalFileSystemCache.GetCacheRoot(cacheOptions, webRootPath, contentRootPath);
45+
46+
Assert.Equal(expected, cacheRootResult);
47+
}
2248
}
2349
}

tests/ImageSharp.Web.Tests/ImageSharp.Web.Tests.csproj

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@
88

99
<!--Used to show test project to dotnet test-->
1010
<IsTestProject>true</IsTestProject>
11+
<IsWindows Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Windows)))' == 'true'">true</IsWindows>
12+
<IsOSX Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::OSX)))' == 'true'">true</IsOSX>
13+
<IsLinux Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Linux)))' == 'true'">true</IsLinux>
14+
</PropertyGroup>
15+
16+
<PropertyGroup Condition="'$(IsWindows)'=='true'">
17+
<DefineConstants>Windows</DefineConstants>
18+
</PropertyGroup>
19+
<PropertyGroup Condition="'$(IsOSX)'=='true'">
20+
<DefineConstants>OSX</DefineConstants>
21+
</PropertyGroup>
22+
<PropertyGroup Condition="'$(IsLinux)'=='true'">
23+
<DefineConstants>Linux</DefineConstants>
1124
</PropertyGroup>
1225

1326
<ItemGroup>

0 commit comments

Comments
 (0)