Skip to content

Commit

Permalink
v0.10.3
Browse files Browse the repository at this point in the history
  • Loading branch information
saucecontrol committed Mar 12, 2020
1 parent 779b676 commit f455827
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 18 deletions.
8 changes: 4 additions & 4 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ steps:
- checkout: self
submodules: true

- task: NuGetAuthenticate@0
condition: ne(variables['Build.Reason'], 'PullRequest')

- task: UseDotNet@2
displayName: Ensure 3.1 SDK
inputs:
version: 3.1.x
performMultiLevelLookup: true
includePreviewVersions: true

- script: |
dotnet build src\MagicScaler -c Dist --version-suffix ci$(Build.BuildNumber)
dotnet build src\WebRSize -c Dist --version-suffix ci$(Build.BuildNumber)
displayName: Build

- task: NuGetAuthenticate@0
condition: ne(variables['Build.Reason'], 'PullRequest')

- script: |
dotnet nuget push --api-key AzureArtifacts --source https://pkgs.dev.azure.com/saucecontrol/PhotoSauce/_packaging/photosauce_ci/nuget/v3/index.json out\bin\MagicScaler\Dist\PhotoSauce.MagicScaler.*.nupkg
dotnet nuget push --api-key AzureArtifacts --source https://pkgs.dev.azure.com/saucecontrol/PhotoSauce/_packaging/photosauce_ci/nuget/v3/index.json out\bin\WebRSize\Dist\PhotoSauce.WebRSize.*.nupkg
Expand Down
7 changes: 6 additions & 1 deletion src/MagicScaler/Core/ImageFileInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ public ImageFileInfo(int width, int height)
ContainerType = FileFormat.Unknown;
}

private ImageFileInfo(FileFormat containerType, IReadOnlyList<FrameInfo> frames, long fileSize, DateTime fileDate)
/// <summary>Constructs a new <see cref="ImageFileInfo" /> instance the specified values.</summary>
/// <param name="containerType">The <see cref="FileFormat" /> of the image container.</param>
/// <param name="frames">A list containing one <see cref="FrameInfo" /> per image frame in the container.</param>
/// <param name="fileSize">The size in bytes of the image file.</param>
/// <param name="fileDate">The last modified date of the image file.</param>
public ImageFileInfo(FileFormat containerType, IReadOnlyList<FrameInfo> frames, long fileSize, DateTime fileDate)
{
ContainerType = containerType;
Frames = frames;
Expand Down
7 changes: 4 additions & 3 deletions src/MagicScaler/MagicScaler.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,17 @@
</PropertyGroup>

<ItemGroup Condition="!$(DefineConstants.Contains('BUILTIN_SPAN'))">
<PackageReference Include="System.Buffers" Version="4.5.0" />
<PackageReference Include="System.Buffers" Version="4.4.0" />
<PackageReference Include="System.Memory" Version="4.5.3" />
<PackageReference Include="System.Numerics.Vectors" Version="4.5.0" />
<PackageReference Include="System.Numerics.Vectors" Version="4.4.0" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.5.2" />
</ItemGroup>

<ItemGroup Condition="$(DefineConstants.Contains('SYSTEM_DRAWING')) And !$(DefineConstants.Contains('NETFRAMEWORK'))">
<PackageReference Include="System.Drawing.Common" Version="4.7.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)'!='netcoreapp3.0'">
<ItemGroup Condition="$(DefineConstants.Contains('BUILTIN_SPAN')) And '$(TargetFramework)'!='netcoreapp3.0'">
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.7.0" />
</ItemGroup>

Expand Down
7 changes: 3 additions & 4 deletions src/WebRSize/CacheHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,12 @@ public static Task<T> GetOrAddAsync<T>(string cacheKey, Func<Task<T>> getValue =
return val;
}

public static Task<ImageFileInfo> GetImageInfoAsync(string path)
public static Task<ImageFileInfo> GetImageInfoAsync(VirtualPathProvider vpp, string path)
{
return GetOrAddAsync(string.Concat("wrfi_", path), async () => {
var vpp = HostingEnvironment.VirtualPathProvider;

return GetOrAddAsync(string.Concat("wsvppfi_", path), async () => {
var file = vpp is CachingAsyncVirtualPathProvider vppAsync ? await vppAsync.GetFileAsync(path).ConfigureAwait(false) : vpp.GetFile(path);
var afile = file as AsyncVirtualFile;

using var stream = afile != null ? await afile.OpenAsync().ConfigureAwait(false) : file.Open();
return ImageFileInfo.Load(stream, afile != null ? afile.LastModified : DateTime.MinValue);
}, () => MakeVirtualPathDependency(path));
Expand Down
16 changes: 16 additions & 0 deletions src/WebRSize/CachingAsyncVirtualPathProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using System.Collections;
using System.Threading.Tasks;

using PhotoSauce.MagicScaler;

namespace PhotoSauce.WebRSize
{
/// <summary>A base <see cref="VirtualPathProvider"/> that adds memory caching and async operation.</summary>
Expand Down Expand Up @@ -58,6 +60,9 @@ protected virtual CacheDependency GetCacheDependencyInternal(string virtualPath,
/// <inheritdoc cref="GetDirectory(string)" />
protected virtual Task<VirtualDirectory> GetDirectoryAsyncInternal(string virtualDir) => CacheHelper.CompletedTask<VirtualDirectory>.Default;

/// <inheritdoc cref="GetImageInfoAsync(string)" />
protected virtual Task<ImageFileInfo> GetImageInfoAsyncInternal(string virtualPath) => CacheHelper.GetImageInfoAsync(this, virtualPath);

/// <inheritdoc />
public override bool FileExists(string virtualPath) => IsPathCaptured(virtualPath) ? FileExistsInternal(virtualPath) : Previous.FileExists(virtualPath);

Expand Down Expand Up @@ -114,6 +119,17 @@ public virtual Task<VirtualDirectory> GetDirectoryAsync(string virtualDir)

return AsyncPrevious?.GetDirectoryAsync(virtualDir) ?? Task.FromResult(Previous.GetDirectory(virtualDir));
}

/// <summary>Get the basic metadata associated with an image file at the given <paramref name="virtualPath" />.</summary>
/// <param name="virtualPath">The virtual path of the image file.</param>
/// <returns>The <see cref="ImageFileInfo" />.</returns>
public virtual Task<ImageFileInfo> GetImageInfoAsync(string virtualPath)
{
if (IsPathCaptured(virtualPath))
return CacheHelper.GetOrAddAsync(string.Concat("wsvppfi_", virtualPath), () => GetImageInfoAsyncInternal(virtualPath));

return AsyncPrevious?.GetImageInfoAsync(virtualPath) ?? CacheHelper.GetImageInfoAsync(Previous, virtualPath);
}
}

/// <inheritdoc />
Expand Down
2 changes: 1 addition & 1 deletion src/WebRSize/WebRSize.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<VersionPrefix>0.5.0</VersionPrefix>
<VersionPrefix>0.5.1</VersionPrefix>
<TargetFrameworks>net46;net472</TargetFrameworks>
</PropertyGroup>

Expand Down
6 changes: 6 additions & 0 deletions src/WebRSize/WebRSizeHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ namespace PhotoSauce.WebRSize
/// <summary>An <see cref="IHttpHandler" /> implementation that performs a dynamic image processing operation, returns the result to the client, and queues caching the result.</summary>
public class WebRSizeHandler : HttpTaskAsyncHandler
{
internal static WebRSizeHandler Instance = new WebRSizeHandler();

private struct QueueReleaser : IDisposable { public void Dispose() => sem.Release(); }

private static readonly bool diskCacheEnabled = WebRSizeConfig.Current.DiskCache.Enabled;
private static readonly SemaphoreSlim sem = new SemaphoreSlim(Environment.ProcessorCount, Environment.ProcessorCount);
private static readonly ConcurrentDictionary<string, Task<ArraySegment<byte>>> tdic = new ConcurrentDictionary<string, Task<ArraySegment<byte>>>();
private static readonly Lazy<Type> mpbvfType = new Lazy<Type>(() => Assembly.GetAssembly(typeof(HostingEnvironment)).GetType("System.Web.Hosting.MapPathBasedVirtualFile", true));
Expand All @@ -32,6 +35,9 @@ private static void saveResult(TaskCompletionSource<ArraySegment<byte>> tcs, Mem
oimg.TryGetBuffer(out var bytes);
tcs.SetResult(bytes);

if (!diskCacheEnabled)
return;

HostingEnvironment.QueueBackgroundWorkItem(async __ => { try {
var fi = new FileInfo(cachePath);
if (!fi.Exists)
Expand Down
10 changes: 5 additions & 5 deletions src/WebRSize/WebRSizeModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class WebRSizeModule : IHttpModule
internal const string CachePathKey = "wscachepath";
internal const string NotFoundPath = "/404/notfound.png";

private static readonly WebRSizeHandler handler = new WebRSizeHandler();
private static readonly bool diskCacheEnabled = WebRSizeConfig.Current.DiskCache.Enabled;
private static readonly ImageFolder[] imageFolders = WebRSizeConfig.Current.ImageFolders.OfType<ImageFolder>().ToArray();
private static readonly Lazy<ICacheFileNamingStrategy> namingStrategy = new Lazy<ICacheFileNamingStrategy>(() => (ICacheFileNamingStrategy)Activator.CreateInstance(WebRSizeConfig.Current.DiskCache.NamingStrategy));

Expand All @@ -26,7 +26,7 @@ private async Task mapRequest(object sender, EventArgs e)
var vpp = HostingEnvironment.VirtualPathProvider;

string path = ctx.Request.Path;
bool exists = vpp is CachingAsyncVirtualPathProvider vppAsync ? await vppAsync.FileExistsAsync(path) : vpp.FileExists(path);
bool exists = vpp is CachingAsyncVirtualPathProvider vppAE ? await vppAE.FileExistsAsync(path).ConfigureAwait(false) : vpp.FileExists(path);

var folderConfig = imageFolders.FirstOrDefault(f => ctx.Request.Path.StartsWith(f.Path, StringComparison.OrdinalIgnoreCase));
if (folderConfig is null)
Expand Down Expand Up @@ -63,7 +63,7 @@ private async Task mapRequest(object sender, EventArgs e)
s.SaveFormat = FileFormat.Png;
}

ifi ??= await CacheHelper.GetImageInfoAsync(path);
ifi = vpp is CachingAsyncVirtualPathProvider vppAF ? await vppAF.GetImageInfoAsync(path).ConfigureAwait(false) : await CacheHelper.GetImageInfoAsync(vpp, path).ConfigureAwait(false);
s.NormalizeFrom(ifi);

if (!folderConfig.AllowEnlarge && s.ResizeMode != CropScaleMode.Max)
Expand Down Expand Up @@ -99,7 +99,7 @@ private async Task mapRequest(object sender, EventArgs e)
string cacheVPath = namingStrategy.Value.GetCacheFilePath(path, s);
string cachePath = HostingEnvironment.MapPath(cacheVPath);

if (File.Exists(cachePath))
if (diskCacheEnabled && File.Exists(cachePath))
{
ctx.RewritePath(cacheVPath, null, string.Empty, false);
return;
Expand All @@ -110,7 +110,7 @@ private async Task mapRequest(object sender, EventArgs e)

ctx.Items[ProcessImageSettingsKey] = s;
ctx.Items[CachePathKey] = cachePath;
ctx.RemapHandler(handler);
ctx.RemapHandler(WebRSizeHandler.Instance);
}

/// <inheritdoc />
Expand Down

0 comments on commit f455827

Please sign in to comment.