asp.net mvc - 如何实现 IVirtualFileWithModifiedDate, ImageResize

本文关键字:实现 IVirtualFileWithModifiedDate ImageResize mvc net 何实现 asp | 更新日期: 2023-09-27 17:55:20

我写了以下 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

你能指导我吗?

asp.net mvc - 如何实现 IVirtualFileWithModifiedDate, ImageResize

当基础文件更改时,您必须为 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