Question

I am trying to use ImageResizer (from http://imageresizing.net) in an ASP.Net MVC 4 website. I created an IVirtualImageProvider to fetch my images on a distant server (actually connected to SQL server with Entity).

All is fine. Something odd is that my provider is called twice for each image request, but visual result is OK.

Now, I want to test DiskCache plugin. So Nuget + config... and... everything seems to behave exactly the same way : my distant server keeps being called twice for each request.

Isn't there something I did not understood ? Using diskcache should prevent normal rendereing of my images and my provider to be called, shouldn't it ?

My config :

<resizer>
    <pipeline fakeExtensions=".ashx" vppUsage="Always" />
    <diskCache dir="~/c_dynimages" autoClean="false" hashModifiedDate="true" enabled="true" subfolders="32" cacheAccessTimeout="15000" />

    <cleanupStrategy startupDelay="00:05" minDelay="00:00:20" maxDelay="00:05" optimalWorkSegmentLength="00:00:04" targetItemsPerFolder="400" maximumItemsPerFolder="1000"
        avoidRemovalIfCreatedWithin="24:00" avoidRemovalIfUsedWithin="4.00:00" prohibitRemovalIfUsedWithin="00:05" prohibitRemovalIfCreatedWithin="00:10" />
    <plugins>
        <add name="MvcRoutingShim" />
        <add name="TwinklazDb" />
        <add name="DiskCache" />
    </plugins>
</resizer>

My plugin :

/// <summary>
/// Actually install plugin into plugin collection of ImageReizer
/// </summary>
/// <param name="c">config</param>
/// <returns>this</returns>
public IPlugin Install(Configuration.Config c)
{
    c.Plugins.add_plugin(this);
    return this;
}

/// <summary>
/// Remove plugin from list
/// </summary>
/// <param name="c">ImageResizer configuration</param>
/// <returns>true</returns>
public bool Uninstall(Configuration.Config c)
{
    c.Plugins.remove_plugin(this);
    return true;
}

#endregion

/// <summary>
/// Returns true if virtualPath contains prefix (dynimages/) 
/// </summary>
/// <param name="virtualPath"></param>
/// <param name="queryString"></param>
/// <returns></returns>
public bool FileExists(string virtualPath, System.Collections.Specialized.NameValueCollection queryString)
{
    bool result = false;
    if (virtualPath.StartsWith(PREFIX))
    {
        long id;
        result = long.TryParse(virtualPath.Substring(PREFIX_LENGTH).Replace(".jpg",""), out id);
    }
    return result;
}

/// <summary>
/// Ask binary service to return a file
/// </summary>
/// <param name="virtualPath">Path as read from http request</param>
/// <param name="queryString">Unused</param>
/// <returns>A dbfile</returns>
public IVirtualFile GetFile(string virtualPath, System.Collections.Specialized.NameValueCollection queryString)
{
    long id;
    if (long.TryParse(virtualPath.Substring(PREFIX_LENGTH).Replace(".jpg", ""), out id))
        return new DbFile(virtualPath, Services.Binary.GetBinary(id));
    return null;
}

And DbFile class:

private class DbFile : IVirtualFile
{
    #region Private members

    /// <summary>
    /// virtual path of the file
    /// </summary>
    private string _virtualPath;

    /// <summary>
    /// File content (blob)
    /// </summary>
    private byte[] _content;

    #endregion
    #region Constructor

    /// <summary>
    /// Builds a DBFile from path and content
    /// </summary>
    /// <param name="path">Path to file (as read from http handler)</param>
    /// <param name="content">Content of the file read from database</param>
    public DbFile(string path, byte[] content)
    {
        _virtualPath = path;
        _content = content;
    }

    #endregion

    /// <summary>
    /// Opens a stream on file content and returns it
    /// </summary>
    /// <returns>A memory stream on content array</returns>
    public System.IO.Stream Open()
    {
        return new System.IO.MemoryStream(_content);
    }

    /// <summary>
    /// Gets virtual path
    /// </summary>
    public string VirtualPath
    {
        get { return _virtualPath; }
    }
}

My request :

http://localhost:36782/dynimages/1.jpg.ashx?width=100&height=50&mode=stretch

Any help is welcome !

Was it helpful?

Solution

Delay all work until .Open() is called

IVirtualImageProvider implementations should delay all work until .Open() is called.

An IVirtualFile instance is needed for DiskCache to derive the cache hash accurately, as some providers also provide a modified date to enable invalidation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top