|
|
|
@ -16,7 +16,6 @@ namespace ImageProcessor.Web.Caching |
|
|
|
using System.Globalization; |
|
|
|
using System.IO; |
|
|
|
using System.Linq; |
|
|
|
using System.Threading.Tasks; |
|
|
|
using System.Web.Hosting; |
|
|
|
|
|
|
|
using ImageProcessor.Web.Configuration; |
|
|
|
@ -130,14 +129,65 @@ namespace ImageProcessor.Web.Caching |
|
|
|
} |
|
|
|
|
|
|
|
#region Methods
|
|
|
|
#region Internal
|
|
|
|
#region Public
|
|
|
|
/// <summary>
|
|
|
|
/// Trims a cached folder ensuring that it does not exceed the maximum file count.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="path">
|
|
|
|
/// The path to the folder.
|
|
|
|
/// </param>
|
|
|
|
public static void TrimCachedFolders(string path) |
|
|
|
{ |
|
|
|
string directory = Path.GetDirectoryName(path); |
|
|
|
|
|
|
|
if (directory != null) |
|
|
|
{ |
|
|
|
DirectoryInfo directoryInfo = new DirectoryInfo(directory); |
|
|
|
DirectoryInfo parentDirectoryInfo = directoryInfo.Parent; |
|
|
|
|
|
|
|
if (parentDirectoryInfo != null) |
|
|
|
{ |
|
|
|
// UNC folders can throw exceptions if the file doesn't exist.
|
|
|
|
foreach (DirectoryInfo enumerateDirectory in parentDirectoryInfo.SafeEnumerateDirectories()) |
|
|
|
{ |
|
|
|
IEnumerable<FileInfo> files = enumerateDirectory.EnumerateFiles().OrderBy(f => f.CreationTimeUtc); |
|
|
|
int count = files.Count(); |
|
|
|
|
|
|
|
foreach (FileInfo fileInfo in files) |
|
|
|
{ |
|
|
|
try |
|
|
|
{ |
|
|
|
// If the group count is equal to the max count minus 1 then we know we
|
|
|
|
// have reduced the number of items below the maximum allowed.
|
|
|
|
// We'll cleanup any orphaned expired files though.
|
|
|
|
if (!IsExpired(fileInfo.CreationTimeUtc) && count <= MaxFilesCount - 1) |
|
|
|
{ |
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
// Remove from the cache and delete each CachedImage.
|
|
|
|
CacheIndexer.Remove(fileInfo.Name); |
|
|
|
fileInfo.Delete(); |
|
|
|
count -= 1; |
|
|
|
} |
|
|
|
// ReSharper disable once EmptyGeneralCatchClause
|
|
|
|
catch |
|
|
|
{ |
|
|
|
// Do nothing; skip to the next file.
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Adds an image to the cache.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="cachedPath">
|
|
|
|
/// The path to the cached image.
|
|
|
|
/// </param>
|
|
|
|
internal void AddImageToCache(string cachedPath) |
|
|
|
public void AddImageToCache(string cachedPath) |
|
|
|
{ |
|
|
|
string key = Path.GetFileNameWithoutExtension(cachedPath); |
|
|
|
CachedImage cachedImage = new CachedImage |
|
|
|
@ -159,7 +209,7 @@ namespace ImageProcessor.Web.Caching |
|
|
|
/// <returns>
|
|
|
|
/// True if the the original file is new or has been updated; otherwise, false.
|
|
|
|
/// </returns>
|
|
|
|
internal bool IsNewOrUpdatedFile(string cachedPath) |
|
|
|
public bool IsNewOrUpdatedFile(string cachedPath) |
|
|
|
{ |
|
|
|
bool isUpdated = false; |
|
|
|
CachedImage cachedImage = CacheIndexer.GetValue(cachedPath); |
|
|
|
@ -172,7 +222,7 @@ namespace ImageProcessor.Web.Caching |
|
|
|
else |
|
|
|
{ |
|
|
|
// Check to see if the cached image is set to expire.
|
|
|
|
if (this.IsExpired(cachedImage.CreationTimeUtc)) |
|
|
|
if (IsExpired(cachedImage.CreationTimeUtc)) |
|
|
|
{ |
|
|
|
CacheIndexer.Remove(cachedPath); |
|
|
|
isUpdated = true; |
|
|
|
@ -181,72 +231,22 @@ namespace ImageProcessor.Web.Caching |
|
|
|
|
|
|
|
return isUpdated; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Trims a cached folder ensuring that it does not exceed the maximum file count.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="path">
|
|
|
|
/// The path to the folder.
|
|
|
|
/// </param>
|
|
|
|
/// <returns>
|
|
|
|
/// The <see cref="T:System.Threading.Tasks.Task"/>.
|
|
|
|
/// </returns>
|
|
|
|
internal async Task TrimCachedFolderAsync(string path) |
|
|
|
{ |
|
|
|
await Task.Run(() => this.TrimCachedFolders(path)); |
|
|
|
} |
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Private
|
|
|
|
/// <summary>
|
|
|
|
/// Trims a cached folder ensuring that it does not exceed the maximum file count.
|
|
|
|
/// Gets a value indicating whether the given images creation date is out with
|
|
|
|
/// the prescribed limit.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="path">
|
|
|
|
/// The path to the folder.
|
|
|
|
/// <param name="creationDate">
|
|
|
|
/// The creation date.
|
|
|
|
/// </param>
|
|
|
|
private void TrimCachedFolders(string path) |
|
|
|
/// <returns>
|
|
|
|
/// The true if the date is out with the limit, otherwise; false.
|
|
|
|
/// </returns>
|
|
|
|
private static bool IsExpired(DateTime creationDate) |
|
|
|
{ |
|
|
|
string directory = Path.GetDirectoryName(path); |
|
|
|
|
|
|
|
if (directory != null) |
|
|
|
{ |
|
|
|
DirectoryInfo directoryInfo = new DirectoryInfo(directory); |
|
|
|
DirectoryInfo parentDirectoryInfo = directoryInfo.Parent; |
|
|
|
|
|
|
|
if (parentDirectoryInfo != null) |
|
|
|
{ |
|
|
|
// UNC folders can throw exceptions if the file doesn't exist.
|
|
|
|
foreach (DirectoryInfo enumerateDirectory in parentDirectoryInfo.SafeEnumerateDirectories()) |
|
|
|
{ |
|
|
|
IEnumerable<FileInfo> files = enumerateDirectory.EnumerateFiles().OrderBy(f => f.CreationTimeUtc); |
|
|
|
int count = files.Count(); |
|
|
|
|
|
|
|
foreach (FileInfo fileInfo in files) |
|
|
|
{ |
|
|
|
try |
|
|
|
{ |
|
|
|
// If the group count is equal to the max count minus 1 then we know we
|
|
|
|
// have reduced the number of items below the maximum allowed.
|
|
|
|
// We'll cleanup any orphaned expired files though.
|
|
|
|
if (!this.IsExpired(fileInfo.CreationTimeUtc) && count <= MaxFilesCount - 1) |
|
|
|
{ |
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
// Remove from the cache and delete each CachedImage.
|
|
|
|
CacheIndexer.Remove(fileInfo.Name); |
|
|
|
fileInfo.Delete(); |
|
|
|
count -= 1; |
|
|
|
} |
|
|
|
// ReSharper disable once EmptyGeneralCatchClause
|
|
|
|
catch |
|
|
|
{ |
|
|
|
// Do nothing; skip to the next file.
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return creationDate.AddDays(MaxFileCachedDuration) < DateTime.UtcNow.AddDays(-MaxFileCachedDuration); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -305,21 +305,6 @@ namespace ImageProcessor.Web.Caching |
|
|
|
this.virtualCachedPath = Path.Combine(VirtualCachePath, virtualPathFromKey, cachedFileName).Replace(@"\", "/"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets a value indicating whether the given images creation date is out with
|
|
|
|
/// the prescribed limit.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="creationDate">
|
|
|
|
/// The creation date.
|
|
|
|
/// </param>
|
|
|
|
/// <returns>
|
|
|
|
/// The true if the date is out with the limit, otherwise; false.
|
|
|
|
/// </returns>
|
|
|
|
private bool IsExpired(DateTime creationDate) |
|
|
|
{ |
|
|
|
return creationDate.AddDays(MaxFileCachedDuration) < DateTime.UtcNow.AddDays(-MaxFileCachedDuration); |
|
|
|
} |
|
|
|
#endregion
|
|
|
|
#endregion
|
|
|
|
} |
|
|
|
|