diff --git a/src/ImageProcessor.Web/Caching/DiskCache.cs b/src/ImageProcessor.Web/Caching/DiskCache.cs
index ed4df4adc5..fc684f0ca2 100644
--- a/src/ImageProcessor.Web/Caching/DiskCache.cs
+++ b/src/ImageProcessor.Web/Caching/DiskCache.cs
@@ -84,7 +84,7 @@ namespace ImageProcessor.Web.Caching
if (!directoryInfo.Exists)
{
// Create the directory.
- Directory.CreateDirectory(cachedDirectory);
+ directoryInfo.Create();
}
}
}
@@ -120,6 +120,7 @@ namespace ImageProcessor.Web.Caching
string applicationPath = request.PhysicalApplicationPath;
string virtualDir = request.ApplicationPath;
virtualDir = virtualDir == "/" ? virtualDir : (virtualDir + "/");
+
if (applicationPath != null)
{
return absolutePath.Replace(applicationPath, virtualDir).Replace(@"\", "/");
@@ -133,19 +134,43 @@ namespace ImageProcessor.Web.Caching
///
/// The original image path.
/// The cached image path.
+ /// Whether the file is a remote request.
///
/// True if the the original file has been updated; otherwise, false.
///
- internal static bool IsUpdatedFile(string imagePath, string cachedImagePath)
+ internal static bool IsUpdatedFile(string imagePath, string cachedImagePath, bool isRemote)
{
string key = Path.GetFileNameWithoutExtension(cachedImagePath);
- bool isUpdated = false;
+ CachedImage cachedImage;
- if (File.Exists(imagePath))
+ if (isRemote)
{
- FileInfo imageFileInfo = new FileInfo(imagePath);
- CachedImage cachedImage;
+ if (PersistantDictionary.Instance.TryGetValue(key, out cachedImage))
+ {
+ // Can't check the last write time so Check to see if the cached image is set to expire
+ // or if the max age is different.
+ if (cachedImage.ExpiresUtc < DateTime.UtcNow.AddDays(-MaxFileCachedDuration)
+ || cachedImage.MaxAge != MaxFileCachedDuration)
+ {
+ if (PersistantDictionary.Instance.TryRemove(key, out cachedImage))
+ {
+ // We can jump out here.
+ return true;
+ }
+ }
+ return false;
+ }
+ else
+ {
+ // Nothing in the cache so we should return true.
+ return true;
+ }
+ }
+
+ FileInfo imageFileInfo = new FileInfo(imagePath);
+ if (imageFileInfo.Exists)
+ {
if (PersistantDictionary.Instance.TryGetValue(key, out cachedImage))
{
// Check to see if the last write time is different of whether the
@@ -156,13 +181,18 @@ namespace ImageProcessor.Web.Caching
{
if (PersistantDictionary.Instance.TryRemove(key, out cachedImage))
{
- isUpdated = true;
+ return true;
}
}
}
+ else
+ {
+ // Nothing in the cache so we should return true.
+ return true;
+ }
}
- return isUpdated;
+ return false;
}
///
@@ -174,15 +204,29 @@ namespace ImageProcessor.Web.Caching
///
/// The cached image path.
///
+ /// Whether the file is remote.
///
/// The set to the last write time of the file.
///
- internal static DateTime SetCachedLastWriteTime(string imagePath, string cachedImagePath)
+ internal static DateTime SetCachedLastWriteTime(string imagePath, string cachedImagePath, bool isRemote)
{
- if (File.Exists(imagePath) && File.Exists(cachedImagePath))
+ FileInfo cachedFileInfo = new FileInfo(cachedImagePath);
+
+ if (isRemote)
+ {
+ if (cachedFileInfo.Exists)
+ {
+ return cachedFileInfo.LastWriteTimeUtc;
+ }
+ }
+
+ FileInfo imageFileInfo = new FileInfo(imagePath);
+
+ if (imageFileInfo.Exists && cachedFileInfo.Exists)
{
- DateTime dateTime = File.GetLastWriteTimeUtc(imagePath);
- File.SetLastWriteTimeUtc(cachedImagePath, dateTime);
+ DateTime dateTime = imageFileInfo.LastWriteTimeUtc;
+ cachedFileInfo.LastWriteTimeUtc = dateTime;
+
return dateTime;
}
diff --git a/src/ImageProcessor.Web/HttpModules/ImageProcessingModule.cs b/src/ImageProcessor.Web/HttpModules/ImageProcessingModule.cs
index 4e31a0d6b9..ba1e6a1c3d 100644
--- a/src/ImageProcessor.Web/HttpModules/ImageProcessingModule.cs
+++ b/src/ImageProcessor.Web/HttpModules/ImageProcessingModule.cs
@@ -99,18 +99,18 @@ namespace ImageProcessor.Web.HttpModules
queryString = HttpUtility.UrlDecode(context.Request.QueryString.ToString());
}
+ // Only process requests that pass our sanitizing filter.
if (ImageUtils.IsValidImageExtension(path) && !string.IsNullOrWhiteSpace(queryString))
{
- string fullPath = string.Format("{0}?{1}", path, queryString);
- string imageName = Path.GetFileName(path);
- string cachedPath = DiskCache.GetCachePath(fullPath, imageName);
-
- if (path != null && this.FileExists(path, isRemote))
+ if (this.FileExists(path, isRemote))
{
- bool exists = File.Exists(cachedPath);
- bool updated = DiskCache.IsUpdatedFile(path, cachedPath);
+ string fullPath = string.Format("{0}?{1}", path, queryString);
+ string imageName = Path.GetFileName(path);
+ string cachedPath = DiskCache.GetCachePath(fullPath, imageName);
+ bool isUpdated = DiskCache.IsUpdatedFile(path, cachedPath, isRemote);
- if ((exists == false) || (!isRemote && updated))
+ // Only process if the file has been updated.
+ if (isUpdated)
{
// Process the image.
using (ImageFactory imageFactory = new ImageFactory())
@@ -139,7 +139,7 @@ namespace ImageProcessor.Web.HttpModules
.AutoProcess().Save(cachedPath);
// Ensure that the LastWriteTime property of the source and cached file match.
- DateTime dateTime = DiskCache.SetCachedLastWriteTime(path, cachedPath);
+ DateTime dateTime = DiskCache.SetCachedLastWriteTime(path, cachedPath, isRemote);
// Add to the cache.
DiskCache.AddImageToCache(cachedPath, dateTime);
@@ -158,7 +158,7 @@ namespace ImageProcessor.Web.HttpModules
imageFactory.Load(fullPath).AutoProcess().Save(cachedPath);
// Ensure that the LastWriteTime property of the source and cached file match.
- DateTime dateTime = DiskCache.SetCachedLastWriteTime(path, cachedPath);
+ DateTime dateTime = DiskCache.SetCachedLastWriteTime(path, cachedPath, isRemote);
// Add to the cache.
DiskCache.AddImageToCache(cachedPath, dateTime);
@@ -201,12 +201,18 @@ namespace ImageProcessor.Web.HttpModules
/// returns a value indicating whether a file exists.
///
/// The path to the file to check.
- /// Whether the file is remote.
+ /// Whether the file is remote.
/// True if the file exists, otherwise false.
/// If the file is remote the method will always return true.
- private bool FileExists(string path, bool remote)
+ private bool FileExists(string path, bool isRemote)
{
- return remote || File.Exists(path);
+ if (isRemote)
+ {
+ return true;
+ }
+
+ FileInfo fileInfo = new FileInfo(path);
+ return fileInfo.Exists;
}
///