|
|
|
@ -22,6 +22,7 @@ namespace ImageProcessor.Web.Caching |
|
|
|
|
|
|
|
using ImageProcessor.Common.Extensions; |
|
|
|
using ImageProcessor.Web.Configuration; |
|
|
|
using ImageProcessor.Web.Extensions; |
|
|
|
using ImageProcessor.Web.Helpers; |
|
|
|
#endregion
|
|
|
|
|
|
|
|
@ -212,37 +213,44 @@ namespace ImageProcessor.Web.Caching |
|
|
|
/// </param>
|
|
|
|
private void TrimCachedFolders(string path) |
|
|
|
{ |
|
|
|
// ReSharper disable once AssignNullToNotNullAttribute
|
|
|
|
DirectoryInfo directoryInfo = new DirectoryInfo(Path.GetDirectoryName(path)); |
|
|
|
DirectoryInfo parentDirectoryInfo = directoryInfo.Parent; |
|
|
|
string directory = Path.GetDirectoryName(path); |
|
|
|
|
|
|
|
// ReSharper disable once PossibleNullReferenceException
|
|
|
|
foreach (DirectoryInfo enumerateDirectory in parentDirectoryInfo.EnumerateDirectories()) |
|
|
|
if (directory != null) |
|
|
|
{ |
|
|
|
IEnumerable<FileInfo> files = enumerateDirectory.EnumerateFiles().OrderBy(f => f.CreationTimeUtc); |
|
|
|
int count = files.Count(); |
|
|
|
DirectoryInfo directoryInfo = new DirectoryInfo(directory); |
|
|
|
DirectoryInfo parentDirectoryInfo = directoryInfo.Parent; |
|
|
|
|
|
|
|
foreach (FileInfo fileInfo in files) |
|
|
|
if (parentDirectoryInfo != null) |
|
|
|
{ |
|
|
|
try |
|
|
|
// UNC folders can throw exceptions if the file doesn't exist.
|
|
|
|
foreach (DirectoryInfo enumerateDirectory in parentDirectoryInfo.SafeEnumerateDirectories()) |
|
|
|
{ |
|
|
|
// 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) |
|
|
|
IEnumerable<FileInfo> files = enumerateDirectory.EnumerateFiles().OrderBy(f => f.CreationTimeUtc); |
|
|
|
int count = files.Count(); |
|
|
|
|
|
|
|
foreach (FileInfo fileInfo in files) |
|
|
|
{ |
|
|
|
break; |
|
|
|
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.
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 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.
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|