From ff2ee540e274bb35f78e03f09c97550d318fd905 Mon Sep 17 00:00:00 2001 From: Jeavon Leopold Date: Mon, 24 Mar 2014 19:27:00 +0000 Subject: [PATCH] Fixing the CacheBusting on GetCropUrl as it was totally random which meant the server cache was creating a new cache file for every request. Now changed it to use item updateDate.ToFileTimeUtc as the cache busting value so it will change only when the item is updated in Umbraco. --- .../PropertyEditors/ImageCropperTest.cs | 8 ++++---- .../ImageCropperTemplateExtensions.cs | 18 +++++++++++------- src/Umbraco.Web/Models/ImageCropDataSet.cs | 6 +++--- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/Umbraco.Tests/PropertyEditors/ImageCropperTest.cs b/src/Umbraco.Tests/PropertyEditors/ImageCropperTest.cs index 374a2f4fdc..1a2c0bc92b 100644 --- a/src/Umbraco.Tests/PropertyEditors/ImageCropperTest.cs +++ b/src/Umbraco.Tests/PropertyEditors/ImageCropperTest.cs @@ -16,28 +16,28 @@ namespace Umbraco.Tests.PropertyEditors [Test] public void GetCropUrl_CropAliasTest() { - var urlString = mediaPath.GetCropUrl(imageCropperValue: cropperJson, cropAlias: "Thumb", cacheBuster: false, useCropDimensions: true); + var urlString = mediaPath.GetCropUrl(imageCropperValue: cropperJson, cropAlias: "Thumb", useCropDimensions: true); Assert.AreEqual(mediaPath + "?crop=0.58729977382575338,0.055768992440203169,0,0.32457553600198386&cropmode=percentage&width=100&height=100", urlString); } [Test] public void GetCropUrl_WidthHeightTest() { - var urlString = mediaPath.GetCropUrl(imageCropperValue: cropperJson, width: 200, height: 300, cacheBuster: false); + var urlString = mediaPath.GetCropUrl(imageCropperValue: cropperJson, width: 200, height: 300); Assert.AreEqual(mediaPath + "?center=0.80827067669172936,0.96&mode=crop&width=200&height=300", urlString); } [Test] public void GetCropUrl_FocalPointTest() { - var urlString = mediaPath.GetCropUrl(imageCropperValue: cropperJson, cropAlias: "thumb", preferFocalPoint: true, useCropDimensions: true, cacheBuster: false); + var urlString = mediaPath.GetCropUrl(imageCropperValue: cropperJson, cropAlias: "thumb", preferFocalPoint: true, useCropDimensions: true); Assert.AreEqual(mediaPath + "?center=0.80827067669172936,0.96&mode=crop&width=100&height=100", urlString); } [Test] public void GetCropUrlFurtherOptionsTest() { - var urlString = mediaPath.GetCropUrl(imageCropperValue: cropperJson, width: 200, height: 300, cacheBuster: false, furtherOptions: "&filter=comic&roundedcorners=radius-26|bgcolor-fff"); + var urlString = mediaPath.GetCropUrl(imageCropperValue: cropperJson, width: 200, height: 300, furtherOptions: "&filter=comic&roundedcorners=radius-26|bgcolor-fff"); Assert.AreEqual(mediaPath + "?center=0.80827067669172936,0.96&mode=crop&width=200&height=300&filter=comic&roundedcorners=radius-26|bgcolor-fff", urlString); } diff --git a/src/Umbraco.Web/ImageCropperTemplateExtensions.cs b/src/Umbraco.Web/ImageCropperTemplateExtensions.cs index 15cca2d7bb..4d75b164f4 100644 --- a/src/Umbraco.Web/ImageCropperTemplateExtensions.cs +++ b/src/Umbraco.Web/ImageCropperTemplateExtensions.cs @@ -10,6 +10,8 @@ using Umbraco.Web.PropertyEditors; namespace Umbraco.Web { + using System.Globalization; + /// /// Provides extension methods for getting ImageProcessor Url from the core Image Cropper property editor /// @@ -86,7 +88,7 @@ namespace Umbraco.Web /// Use crop dimensions to have the output image sized according to the predefined crop sizes, this will override the width and height parameters>. /// /// - /// Add a random number at the end of the generated string + /// Add a serialised date of the last edit of the item to ensure client cache refresh when updated /// /// /// The further options. @@ -128,8 +130,10 @@ namespace Umbraco.Web mediaItemUrl = mediaItem.Url; } + var cacheBusterValue = cacheBuster ? mediaItem.UpdateDate.ToFileTimeUtc().ToString(CultureInfo.InvariantCulture) : null; + return mediaItemUrl != null - ? GetCropUrl(mediaItemUrl, width, height, imageCropperValue, cropAlias, quality, imageCropMode, imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBuster, furtherOptions) + ? GetCropUrl(mediaItemUrl, width, height, imageCropperValue, cropAlias, quality, imageCropMode, imageCropAnchor, preferFocalPoint, useCropDimensions, cacheBusterValue, furtherOptions) : string.Empty; } @@ -166,8 +170,8 @@ namespace Umbraco.Web /// /// Use crop dimensions to have the output image sized according to the predefined crop sizes, this will override the width and height parameters>. /// - /// - /// Add a random number at the end of the generated string + /// + /// Add a serialised date of the last edit of the item to ensure client cache refresh when updated /// /// /// The further options. @@ -186,7 +190,7 @@ namespace Umbraco.Web ImageCropAnchor? imageCropAnchor = null, bool preferFocalPoint = false, bool useCropDimensions = false, - bool cacheBuster = true, + string cacheBusterValue = null, string furtherOptions = null) { if (string.IsNullOrEmpty(imageUrl) == false) @@ -198,7 +202,7 @@ namespace Umbraco.Web var cropDataSet = imageCropperValue.SerializeToCropDataSet(); if (cropDataSet != null) { - var cropUrl = cropDataSet.GetCropUrl(cropAlias, false, preferFocalPoint, cacheBuster); + var cropUrl = cropDataSet.GetCropUrl(cropAlias, false, preferFocalPoint, cacheBusterValue); // if crop alias has been specified but not found in the Json we should return null if (string.IsNullOrEmpty(cropAlias) == false && cropUrl == null) @@ -207,7 +211,7 @@ namespace Umbraco.Web } imageResizerUrl.Append(cropDataSet.Src); - imageResizerUrl.Append(cropDataSet.GetCropUrl(cropAlias, useCropDimensions, preferFocalPoint, cacheBuster)); + imageResizerUrl.Append(cropDataSet.GetCropUrl(cropAlias, useCropDimensions, preferFocalPoint, cacheBusterValue)); } } else diff --git a/src/Umbraco.Web/Models/ImageCropDataSet.cs b/src/Umbraco.Web/Models/ImageCropDataSet.cs index 914ce477b7..ebc0cc4251 100644 --- a/src/Umbraco.Web/Models/ImageCropDataSet.cs +++ b/src/Umbraco.Web/Models/ImageCropDataSet.cs @@ -20,7 +20,7 @@ namespace Umbraco.Web.Models public IEnumerable Crops { get; set; } - public string GetCropUrl(string alias, bool useCropDimensions = true, bool useFocalPoint = false, bool cacheBuster = true) + public string GetCropUrl(string alias, bool useCropDimensions = true, bool useFocalPoint = false, string cacheBusterValue = null) { var crop = Crops.GetCrop(alias); @@ -58,9 +58,9 @@ namespace Umbraco.Web.Models sb.Append("&height=").Append(crop.Height); } - if (cacheBuster) + if (cacheBusterValue != null) { - sb.Append("&rnd=").Append(DateTime.Now.Ticks); + sb.Append("&rnd=").Append(cacheBusterValue); } return sb.ToString();