Browse Source

Refactoring

Former-commit-id: 8ba378bc6b2ba8699a05c4c2ad54d8980d6fb55d
af/merge-core
James South 13 years ago
parent
commit
625d55045b
  1. 68
      src/ImageProcessor.Web/Caching/DiskCache.cs
  2. 32
      src/ImageProcessor.Web/HttpModules/ImageProcessingModule.cs

68
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
/// </summary>
/// <param name="imagePath">The original image path.</param>
/// <param name="cachedImagePath">The cached image path.</param>
/// <param name="isRemote">Whether the file is a remote request.</param>
/// <returns>
/// True if the the original file has been updated; otherwise, false.
/// </returns>
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;
}
/// <summary>
@ -174,15 +204,29 @@ namespace ImageProcessor.Web.Caching
/// <param name="cachedImagePath">
/// The cached image path.
/// </param>
/// <param name="isRemote">Whether the file is remote.</param>
/// <returns>
/// The <see cref="System.DateTime"/> set to the last write time of the file.
/// </returns>
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;
}

32
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.
/// </summary>
/// <param name="path">The path to the file to check.</param>
/// <param name="remote">Whether the file is remote.</param>
/// <param name="isRemote">Whether the file is remote.</param>
/// <returns>True if the file exists, otherwise false.</returns>
/// <remarks>If the file is remote the method will always return true.</remarks>
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;
}
/// <summary>

Loading…
Cancel
Save