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.

This commit is contained in:
Jeavon Leopold
2014-03-24 19:27:00 +00:00
parent b450299d45
commit ff2ee540e2
3 changed files with 18 additions and 14 deletions

View File

@@ -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);
}

View File

@@ -10,6 +10,8 @@ using Umbraco.Web.PropertyEditors;
namespace Umbraco.Web
{
using System.Globalization;
/// <summary>
/// Provides extension methods for getting ImageProcessor Url from the core Image Cropper property editor
/// </summary>
@@ -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>.
/// </param>
/// <param name="cacheBuster">
/// 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
/// </param>
/// <param name="furtherOptions">
/// 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
/// <param name="useCropDimensions">
/// Use crop dimensions to have the output image sized according to the predefined crop sizes, this will override the width and height parameters>.
/// </param>
/// <param name="cacheBuster">
/// Add a random number at the end of the generated string
/// <param name="cacheBusterValue">
/// Add a serialised date of the last edit of the item to ensure client cache refresh when updated
/// </param>
/// <param name="furtherOptions">
/// 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

View File

@@ -20,7 +20,7 @@ namespace Umbraco.Web.Models
public IEnumerable<ImageCropData> 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();