2014-03-30 35 views
0

我已經寫了下面的ImageResizer插件,但是當我更改原始圖像時,緩存圖像不會改變,除非刪除imagecache文件夾。如何實現IVirtualFileWithModifiedDate,ImageResizer C#?

public class MyImageResizerPlugin : IPlugin, IQuerystringPlugin, IVirtualImageProvider 
{ 
    public bool FileExists(string virtualPath, NameValueCollection queryString) 
    { 
     string fileController = SmartizUrlHelpers.GetUrlHelperInstance().Action("Get", "File"); 
     return !string.IsNullOrWhiteSpace(fileController) && (virtualPath.StartsWith(fileController, StringComparison.OrdinalIgnoreCase)); 
    } 

    public IVirtualFile GetFile(string virtualPath, NameValueCollection queryString) 
    { 
     return new GetImageFromVirtualFile(virtualPath, queryString); 
    } 

    public IEnumerable<string> GetSupportedQuerystringKeys() 
    { 
     return new[] { "maxwidth", "maxheight", "img" }; 
    } 

    public IPlugin Install(Config c) 
    { 
     c.Plugins.add_plugin(this); 
     return this; 
    } 

    public bool Uninstall(Config c) 
    { 
     c.Plugins.remove_plugin(this); 
     return true; 
    } 

    public class GetImageFromVirtualFile : IVirtualFileWithModifiedDate 
    { 
     public GetImageFromVirtualFile(string virtualPath, NameValueCollection query) 
     { 
      uint maxwidth, maxheight; 

      uint.TryParse(query["maxwidth"], out maxwidth); 
      uint.TryParse(query["maxheight"], out maxheight); 

      ResizeSettings resizeSettings = new ResizeSettings(); 

      if (maxwidth > 0) resizeSettings.MaxWidth = (int)maxwidth; 
      if (maxheight > 0) resizeSettings.MaxHeight = (int)maxheight; 

      this.ResizeSettings = resizeSettings; 
      VirtualPath = virtualPath; 
     } 

     public string VirtualPath { get; private set; } 

     protected ResizeSettings ResizeSettings; 

     public Stream Open() 
     { 
      long id; 
      string idString = Regex.Replace(VirtualPath, @"\D", ""); 
      if (!long.TryParse(idString, out id)) return null; 
      Attachment attachment = new SmartService().GetAttachmentById(id); 
      if (attachment == null) return null; 
      MemoryStream memoryStream = new MemoryStream(); 
      string absoluteFilePath = UploadedPaths.GetAbsolutePath(attachment.Path); 
      if (!File.Exists(absoluteFilePath)) throw new HttpException(404, "404 error"); 
      using (FileStream file = new FileStream(absoluteFilePath, FileMode.Open, FileAccess.Read)) 
      { 
       byte[] bytes = new byte[file.Length]; 
       file.Read(bytes, 0, (int)file.Length); 
       memoryStream.Write(bytes, 0, (int)file.Length); 

      } 
      memoryStream.Seek(0, SeekOrigin.Begin); 
      return memoryStream; 
     } 

     public DateTime ModifiedDateUTC { get; private set; } 
    } 
} 

我想我不能正確執行IVirtualFileWithModifiedDateModifiedDateUTC

你能指導我嗎?

回答

1

當底層文件發生更改時,您必須爲ModifiedDateUTC提供不同的值。簡單地定義屬性並不能解決任何問題。

由於我們的產品是開源的,您可以查看一些包含的插件以及它們如何實現此行爲。請記住,增加的延遲是檢查每個請求上的修改日期的常見副作用,所以通常最好將blob設置爲不可變(如果需要更新URL,則使用重寫),或者在圖像內容更改時更改URL (這也確保了CDN和瀏覽器可以處理它)。

請記住這些插件比通常所需的更復雜,因爲它們實現了簡單的IVirtualFileWithModified日期接口以及ASP.NET中包含的凌亂的VirtualPathProvider系統。

https://github.com/imazen/resizer/blob/master/Plugins/S3Reader2/S3File.cs

https://github.com/imazen/resizer/blob/master/Plugins/SqlReader/SqlReader.cs

+0

感謝,但我應該在哪裏設置ModifiedDateUTC,在開放式或每當我修改的文件(出來插件類的) –

+0

不能幫我? ! –

+0

@穆罕默德你曾經得到過這個答案嗎? – Slee