From 75a9525a5e6f02b8a6083b9c1c30fc731813ba8d Mon Sep 17 00:00:00 2001 From: Ronald Barendse Date: Mon, 29 Jun 2020 11:01:13 +0200 Subject: [PATCH 1/4] Remove RecursivelyDeleteEmptyDirectories and fix DirectoryNotFoundException --- src/ImageProcessor.Web/Caching/DiskCache.cs | 47 ++++----------------- 1 file changed, 8 insertions(+), 39 deletions(-) diff --git a/src/ImageProcessor.Web/Caching/DiskCache.cs b/src/ImageProcessor.Web/Caching/DiskCache.cs index fb201ace9..1b8e981b5 100644 --- a/src/ImageProcessor.Web/Caching/DiskCache.cs +++ b/src/ImageProcessor.Web/Caching/DiskCache.cs @@ -255,15 +255,19 @@ public override Task TrimCacheAsync() ImageProcessorBootstrapper.Instance.Logger.Log($"Unable to clean cached file: {fileInfo.FullName}, {ex.Message}"); } } + + // If the directory is empty, delete it to remove the FCN + if (Directory.GetFiles(directory, "*", SearchOption.TopDirectoryOnly).Length == 0 + && Directory.GetDirectories(directory, "*", SearchOption.TopDirectoryOnly).Length == 0) + { + Directory.Delete(directory); + } } catch (Exception ex) { // Log it but skip to the next directory ImageProcessorBootstrapper.Instance.Logger.Log($"Unable to clean cached directory: {directory}, {ex.Message}"); } - - // If the directory is empty of files delete it to remove the FCN - this.RecursivelyDeleteEmptyDirectories(directory, validatedAbsoluteCachePath, token); } } @@ -554,42 +558,6 @@ private static IEnumerable SafeEnumerateDirectories(string directoryPath return directories; } - /// - /// Recursively delete the directories in the folder. - /// - /// The current directory. - /// The root path. - /// The cancellation token. - private void RecursivelyDeleteEmptyDirectories(string directory, string root, CancellationToken token) - { - if (token.IsCancellationRequested) - { - return; - } - - try - { - if (directory == root) - { - return; - } - - // If the directory is empty of files delete it to remove the FCN. - if (Directory.GetFiles(directory, "*", SearchOption.TopDirectoryOnly).Length == 0 - && Directory.GetDirectories(directory, "*", SearchOption.TopDirectoryOnly).Length == 0) - { - Directory.Delete(directory); - } - - this.RecursivelyDeleteEmptyDirectories(Directory.GetParent(directory).FullName, root, token); - } - catch (Exception ex) - { - // Log it but skip to the next directory. - ImageProcessorBootstrapper.Instance.Logger.Log($"Unable to clean cached directory: {directory}, {ex.Message}"); - } - } - /// /// Sets the ETag Header /// @@ -621,6 +589,7 @@ private string GetETag() return "\"" + hexFileTime + "\""; } + return null; } } From 197bc2502849c032965fa564d5ba1e264d1c03fe Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Mon, 29 Jun 2020 17:38:36 +0100 Subject: [PATCH 2/4] Update src/ImageProcessor.Web/Caching/DiskCache.cs Co-authored-by: Ronald Barendse --- src/ImageProcessor.Web/Caching/DiskCache.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ImageProcessor.Web/Caching/DiskCache.cs b/src/ImageProcessor.Web/Caching/DiskCache.cs index 1b8e981b5..e9d390377 100644 --- a/src/ImageProcessor.Web/Caching/DiskCache.cs +++ b/src/ImageProcessor.Web/Caching/DiskCache.cs @@ -257,7 +257,7 @@ public override Task TrimCacheAsync() } // If the directory is empty, delete it to remove the FCN - if (Directory.GetFiles(directory, "*", SearchOption.TopDirectoryOnly).Length == 0 + if (count == 0 && Directory.GetDirectories(directory, "*", SearchOption.TopDirectoryOnly).Length == 0) { Directory.Delete(directory); @@ -593,4 +593,4 @@ private string GetETag() return null; } } -} \ No newline at end of file +} From d6e748f33242d2ee46da181dea523bb5e898d028 Mon Sep 17 00:00:00 2001 From: Ronald Barendse Date: Tue, 30 Jun 2020 16:48:04 +0200 Subject: [PATCH 3/4] Bump version ImageProcessor.Web 4.12.1 --- build/build.ps1 | 2 +- .../Properties/AssemblyInfo.cs | 6 +++--- src/ImageProcessor.Web/Properties/AssemblyInfo.cs | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/build/build.ps1 b/build/build.ps1 index 14e886689..9d83b81d7 100644 --- a/build/build.ps1 +++ b/build/build.ps1 @@ -35,7 +35,7 @@ $imageProcessorPluginsWebP = @{ $imageprocessorWeb = @{ name = "ImageProcessor.Web" - version = "4.12.0" + version = "4.12.1" folder = Join-Path $buildPath "src\ImageProcessor.Web" output = Join-Path $binPath "ImageProcessor.Web\lib\net452" csproj = "ImageProcessor.Web.csproj" diff --git a/src/ImageProcessor.Web.Plugins.AmazonS3Cache/Properties/AssemblyInfo.cs b/src/ImageProcessor.Web.Plugins.AmazonS3Cache/Properties/AssemblyInfo.cs index 327867e5e..7636589cf 100644 --- a/src/ImageProcessor.Web.Plugins.AmazonS3Cache/Properties/AssemblyInfo.cs +++ b/src/ImageProcessor.Web.Plugins.AmazonS3Cache/Properties/AssemblyInfo.cs @@ -39,6 +39,6 @@ // // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: -// [assembly: AssemblyVersion("1.7.0.00000")] -[assembly: AssemblyVersion("1.7.0.00000")] -[assembly: AssemblyFileVersion("1.7.0.00000")] +// [assembly: AssemblyVersion("1.2.0.00000")] +[assembly: AssemblyVersion("1.2.0.00000")] +[assembly: AssemblyFileVersion("1.2.0.00000")] diff --git a/src/ImageProcessor.Web/Properties/AssemblyInfo.cs b/src/ImageProcessor.Web/Properties/AssemblyInfo.cs index 5f3bd5a65..eb9552f67 100644 --- a/src/ImageProcessor.Web/Properties/AssemblyInfo.cs +++ b/src/ImageProcessor.Web/Properties/AssemblyInfo.cs @@ -41,7 +41,7 @@ // // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: -[assembly: AssemblyVersion("4.12.0.00000")] -[assembly: AssemblyFileVersion("4.12.0.00000")] +[assembly: AssemblyVersion("4.12.1.00000")] +[assembly: AssemblyFileVersion("4.12.1.00000")] [assembly: InternalsVisibleTo("ImageProcessor.Web.UnitTests")] From a369ac956489056779e2179162b01639b0559135 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Tue, 21 Jul 2020 14:33:17 +0100 Subject: [PATCH 4/4] Check StorageException response code. Fix #809 --- build/build.ps1 | 2 +- .../AzureBlobCache.cs | 30 ++++++++++++------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/build/build.ps1 b/build/build.ps1 index 9d83b81d7..704e14555 100644 --- a/build/build.ps1 +++ b/build/build.ps1 @@ -49,7 +49,7 @@ $imageprocessorWebConfig = @{ $imageProcessorWebPluginsAzureBlobCache = @{ name = "ImageProcessor.Web.Plugins.AzureBlobCache" - version = "1.7.0" + version = "1.7.1" folder = Join-Path $buildPath "src\ImageProcessor.Web.Plugins.AzureBlobCache" output = Join-Path $binPath "ImageProcessor.Web.Plugins.AzureBlobCache\lib\net452" csproj = "ImageProcessor.Web.Plugins.AzureBlobCache.csproj" diff --git a/src/ImageProcessor.Web.Plugins.AzureBlobCache/AzureBlobCache.cs b/src/ImageProcessor.Web.Plugins.AzureBlobCache/AzureBlobCache.cs index d3c3b6587..a880aaa40 100644 --- a/src/ImageProcessor.Web.Plugins.AzureBlobCache/AzureBlobCache.cs +++ b/src/ImageProcessor.Web.Plugins.AzureBlobCache/AzureBlobCache.cs @@ -16,6 +16,7 @@ namespace ImageProcessor.Web.Plugins.AzureBlobCache using System.IO; using System.Linq; using System.Net; + using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Web; @@ -357,18 +358,25 @@ public override void RewritePath(HttpContext context) catch (StorageException ex) { // A 304 is not a true error, we still need to feed back. - var webException = ex.InnerException as WebException; - if (!(webException is null)) + if (ex.RequestInformation?.HttpStatusCode == (int)HttpStatusCode.NotModified) { - if (webException.Response != null && (((HttpWebResponse)webException.Response).StatusCode == HttpStatusCode.NotModified)) - { - is304 = true; - } - else - { - ImageProcessorBootstrapper.Instance.Logger.Log("Unable to stream cached path: " + this.cachedRewritePath); - return; - } + is304 = true; + } + else if (ex.InnerException is WebException webException + && webException.Response is HttpWebResponse httpWebResponse + && httpWebResponse.StatusCode == HttpStatusCode.NotModified) + { + is304 = true; + } + else + { + var sb = new StringBuilder(); + sb.AppendFormat("Unable to stream cached path: {0}", this.cachedRewritePath); + sb.AppendLine(); + sb.AppendFormat("Exception: {0}", ex.ToString()); + + ImageProcessorBootstrapper.Instance.Logger.Log(sb.ToString()); + return; } }