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.

Regenerate media (from v5.4)

The easiest way to regenerate a conversion is to mark a mediaContentReference for regeneration by setting the RegenerateMedia column to 1. The RegenerateMedia job will run every 10 minutes and process all marked media.

Regenerate media example

The example below will mark all pdf files for regeneration. This can be used if you have pdf files in your system that not uses the pdf converter but have installed it afterwards.

DECLARE @converterType NVARCHAR(255)
DECLARE @originalContentType NVARCHAR(255)
SET @converterType = N'ImageVault.Core.PdfEpsPlugin.PdfEpsConverter,ImageVault.Core.PdfEpsPlugin'
SET @originalContentType=N'application/pdf'

DECLARE @originalFormatId int
DECLARE @converterTypeId int
SET @converterTypeId=0
SET @originalFormatId=0

--Find correct converter
SELECT @converterTypeId=MediaConverterTypeId 
    FROM MediaConverterTypes WHERE Type=@converterType

IF @converterTypeId=0
    RAISERROR('Converter type %s is not installed. Cannot continue',11,0,@converterType)
RAISERROR('Converter type %s has id %d.',0,0,@converterType,@converterTypeId)

--Find id of original formats
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'

IF @originalFormatId=0
    RAISERROR('Cannot find OriginalFormat. Cannot continue',11,0)
RAISERROR('OriginalFormat has id %d.',0,0,@originalFormatId)

--Mark all references that not already uses the correct converter to be regenerated
--Marking the original version for regenerate will regenerate its media information which then will be correct for the original representation
UPDATE MediaContentReferences SET RegenerateMedia=1 
    WHERE VersionId IN(
        SELECT VersionId from MediaContentReferences where MediaConversionId=@originalFormatId and ContentType=@originalContentType
    AND (MediaConverterTypeId<>@converterTypeId or MediaConverterTypeId is null)

RAISERROR('Marked %d items for Regeneration',0,0,@@ROWCOUNT)


If you would like to utilize the SvgConverter included in v5.7, just change the variables above on row 3-4 to the following data

SET @converterType = N'ImageVault.Core.SvgPlugin.SvgConverter,ImageVault.Core.SvgPlugin'
SET @originalContentType=N'image/svg+xml'

Manual method (prior to v5.4)

If you have an older version, you can manually remove the converted media and trigger regeneration by accessing the media url.

Manual method 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