如何实现IVirtualFileWithModifiedDate,ImageResizer C#?

时间:2014-03-30 21:41:02

标签: c# asp.net-mvc imageresizer

我编写了以下ImageResizer插件,但是当我更改原始图像时,除非删除imagecache文件夹,否则缓存图像不会改变。

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 个答案:

答案 0 :(得分: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