Browse Source

Refactoring

Former-commit-id: 744fcc2257b4299fca986c3f6f9cb7b8f0705111
af/merge-core
James South 13 years ago
parent
commit
4bf6978a2c
  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) if (!directoryInfo.Exists)
{ {
// Create the directory. // Create the directory.
Directory.CreateDirectory(cachedDirectory); directoryInfo.Create();
} }
} }
} }
@ -120,6 +120,7 @@ namespace ImageProcessor.Web.Caching
string applicationPath = request.PhysicalApplicationPath; string applicationPath = request.PhysicalApplicationPath;
string virtualDir = request.ApplicationPath; string virtualDir = request.ApplicationPath;
virtualDir = virtualDir == "/" ? virtualDir : (virtualDir + "/"); virtualDir = virtualDir == "/" ? virtualDir : (virtualDir + "/");
if (applicationPath != null) if (applicationPath != null)
{ {
return absolutePath.Replace(applicationPath, virtualDir).Replace(@"\", "/"); return absolutePath.Replace(applicationPath, virtualDir).Replace(@"\", "/");
@ -133,19 +134,43 @@ namespace ImageProcessor.Web.Caching
/// </summary> /// </summary>
/// <param name="imagePath">The original image path.</param> /// <param name="imagePath">The original image path.</param>
/// <param name="cachedImagePath">The cached image path.</param> /// <param name="cachedImagePath">The cached image path.</param>
/// <param name="isRemote">Whether the file is a remote request.</param>
/// <returns> /// <returns>
/// True if the the original file has been updated; otherwise, false. /// True if the the original file has been updated; otherwise, false.
/// </returns> /// </returns>
internal static bool IsUpdatedFile(string imagePath, string cachedImagePath) internal static bool IsUpdatedFile(string imagePath, string cachedImagePath, bool isRemote)
{ {
string key = Path.GetFileNameWithoutExtension(cachedImagePath); string key = Path.GetFileNameWithoutExtension(cachedImagePath);
bool isUpdated = false; CachedImage cachedImage;
if (File.Exists(imagePath)) if (isRemote)
{ {
FileInfo imageFileInfo = new FileInfo(imagePath); if (PersistantDictionary.Instance.TryGetValue(key, out cachedImage))
CachedImage 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)) if (PersistantDictionary.Instance.TryGetValue(key, out cachedImage))
{ {
// Check to see if the last write time is different of whether the // 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)) 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> /// <summary>
@ -174,15 +204,29 @@ namespace ImageProcessor.Web.Caching
/// <param name="cachedImagePath"> /// <param name="cachedImagePath">
/// The cached image path. /// The cached image path.
/// </param> /// </param>
/// <param name="isRemote">Whether the file is remote.</param>
/// <returns> /// <returns>
/// The <see cref="System.DateTime"/> set to the last write time of the file. /// The <see cref="System.DateTime"/> set to the last write time of the file.
/// </returns> /// </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); DateTime dateTime = imageFileInfo.LastWriteTimeUtc;
File.SetLastWriteTimeUtc(cachedImagePath, dateTime); cachedFileInfo.LastWriteTimeUtc = dateTime;
return 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()); queryString = HttpUtility.UrlDecode(context.Request.QueryString.ToString());
} }
// Only process requests that pass our sanitizing filter.
if (ImageUtils.IsValidImageExtension(path) && !string.IsNullOrWhiteSpace(queryString)) if (ImageUtils.IsValidImageExtension(path) && !string.IsNullOrWhiteSpace(queryString))
{ {
string fullPath = string.Format("{0}?{1}", path, queryString); if (this.FileExists(path, isRemote))
string imageName = Path.GetFileName(path);
string cachedPath = DiskCache.GetCachePath(fullPath, imageName);
if (path != null && this.FileExists(path, isRemote))
{ {
bool exists = File.Exists(cachedPath); string fullPath = string.Format("{0}?{1}", path, queryString);
bool updated = DiskCache.IsUpdatedFile(path, cachedPath); 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. // Process the image.
using (ImageFactory imageFactory = new ImageFactory()) using (ImageFactory imageFactory = new ImageFactory())
@ -139,7 +139,7 @@ namespace ImageProcessor.Web.HttpModules
.AutoProcess().Save(cachedPath); .AutoProcess().Save(cachedPath);
// Ensure that the LastWriteTime property of the source and cached file match. // 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. // Add to the cache.
DiskCache.AddImageToCache(cachedPath, dateTime); DiskCache.AddImageToCache(cachedPath, dateTime);
@ -158,7 +158,7 @@ namespace ImageProcessor.Web.HttpModules
imageFactory.Load(fullPath).AutoProcess().Save(cachedPath); imageFactory.Load(fullPath).AutoProcess().Save(cachedPath);
// Ensure that the LastWriteTime property of the source and cached file match. // 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. // Add to the cache.
DiskCache.AddImageToCache(cachedPath, dateTime); DiskCache.AddImageToCache(cachedPath, dateTime);
@ -201,12 +201,18 @@ namespace ImageProcessor.Web.HttpModules
/// returns a value indicating whether a file exists. /// returns a value indicating whether a file exists.
/// </summary> /// </summary>
/// <param name="path">The path to the file to check.</param> /// <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> /// <returns>True if the file exists, otherwise false.</returns>
/// <remarks>If the file is remote the method will always return true.</remarks> /// <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> /// <summary>

Loading…
Cancel
Save