How to: Force reconversion of converted media

When a media is converted to a specific format, this conversion is stored in the database in one of the registered storages. Whenever the specific format for that media is requested, that conversion is reused and sent to the client.

If you for some reason need to clear these media and force reconversion you can clear those converted states and media from the storage.

Content references

All content are stored in the MediaContentReferences table, or rather the information about a content. What media it originates from and what conversion format it uses and what storage module it has stored the actual content in.

To clear a conversion we need to update the MediaContentReference post and instruct it that it has no content stored. A request for this content (using it's url) will then force a conversion to the specific format and find a suitable storage module that it will store it's data in.

It is also important to remove the old content from the storage module since the storage module might fail if trying to store the same content (using the same MediaContentReferenceId).

Note: When changing an already converted content this will change the content of the url and if the content is already used by clients, this method does not guarantee that the client will use the new conversion since it can be cached on the client and this is nothing that we could affect.

Example: Clear all converted png media

In ImageVault 4.4.12 we introduced a fix that made conversion of png files smoother. If you have previously used png files and already have conversions for those, then we would need to force reconversion of those converted media.

Find the conversions that we need to clear

We first need to identify the conversions that we should clear. This should be all content references to png files that is not original media.

Original MediaFormat

The original MediaFormat is the format that an uploaded media will get. The original format id can be found using the following SQL statement

DECLARE @OriginalFormatId int
SELECT @OriginalFormatId = mf.Id 
    FROM MediaFormats mf 
    INNER JOIN TypeRegistrations tr ON mf.TypeId=tr.Id 
    WHERE tr.Type='ImageVault.Common.Data.OriginalFormat,ImageVault.Common'

Small media storage

The Small media storage is a storage that uses the database for storing content. As default this is used for all media of size less than 1Mb Since this is the case for most thumbnails, we will only clear those conversions that are using the small media storage.

Each storage type is registered in the database and the id for the small media storage can be found using the following SQL statement.

DECLARE @SmallMediaStorageId int
SELECT @SmallMediaStorageId = MediaStorageTypeId 
    FROM MediaStorageTypes 
    WHERE Type='ImageVault.Core.Storage.SmallMediaStorage.SmallMediaStorage,ImageVault.Core.Storage'

Filtering out the content references

To find the content we need to clear, filter out all png files that isn't original and are stored in the small media storage, use the following SQL statement.

SELECT * FROM MediaContentReferences 
    WHERE ContentType='image/png' 
        AND MediaConversionId <> @OriginalFormatId 
        AND MediaStorageTypeId = @SmallMediaStorageId

Removing the conversions

To remove the conversions we need to do the following steps. A good advice is to create a database backup before running any of the following queries since these will affect the database.

Another advice is to use transactions to wrap the queries to make them atomic. (I often use BEGIN TRAN and ROLLBACK TRAN when testing the queries and when running them for real, just replace the ROLLBACK with a COMMIT instead.)

Remove from the small media storage

To delete the content from the small media storage, run the SQL statement below. This will remove all entries from the conversions found above.

DELETE FROM SmallMediaContents 
    WHERE MediaContentReferenceId IN (
        SELECT MediaContentReferenceId 
            FROM MediaContentReferences 
            WHERE ContentType='image/png' 
                AND MediaConversionId <> @OriginalFormatId 
                AND MediaStorageTypeId = @SmallMediaStorageId
    )

Update the content reference table

Then we can use the following statement to clear the storage information from the content reference table.

UPDATE MediaContentReferences 
    SET MediaStorageTypeId=null 
    FROM MediaContentReferences
        WHERE ContentType='image/png' 
            AND MediaConversionId <> @OriginalFormatId 
            AND MediaStorageTypeId = @SmallMediaStorageId

Restart core service

After the database has been changed, the ImageVault core service needs to be restarted for the changes to take effect.

Clear client cache

Since previous requested images will surely be cached in the client (webbrowser) the client cache needs to be cleared for the change to propagate to the end user.

comments powered by Disqus