Versions

From ImageVault 5.1, versioning of media items are supported. Each MediaItem now has a CurrentVersion property that indicates the current version id of the media item. If you upload a new version of a media item, the new version will replace all existing usages of the previous version. Old published url:s will be redirected to the new version. New versions will result in new urls.

Note: Even if old url:s are redirected to the new resource using a HTTP 301 response, they can still be cached in the client browser since media resources has an expiration for 1 year as default.

Get versions

Normally when you search for media using the IMediaService.Find method, you will always get the current version of the media. You can specify the VersionFilter.All to get all versions for the media item.

Example

var query = new ImageVault.Common.Data.Query.MediaItemQuery
{
    Filter =
    {
        //get media item by it's id
        Id = {27236},
        //get all versions
        VersionFilter = ImageVault.Common.Data.Query.VersionFilter.All
    },
    Populate =
    {
        MediaFormats =
        {
            //get a thumbnail for each version
            new ImageVault.Common.Data.ThumbnailFormat
            {
                Effects = {new ImageVault.Common.Data.Effects.ResizeEffect(100, 100)}
            }
        },
        //populate the MediaVersion property
        MediaVersion = true,
    }
};
var mediaService = client.CreateChannel<ImageVault.Common.Services.IMediaService>();
var mediaItemVersions = mediaService.Find(query).ToList();
foreach (var mediaItemVersion in mediaItemVersions.OrderByDescending(m => m.MediaVersion.Created))
{
    Console.Write("Id: " + mediaItemVersion.Id + "\t");
    Console.Write("CurrentVersionId: " + mediaItemVersion.CurrentVersionId + "\t");
    Console.Write("VersionId: " + mediaItemVersion.VersionId + "\t");
    Console.Write("Name: " + mediaItemVersion.MediaVersion.Name + "\t");
    Console.Write("Created: " + mediaItemVersion.MediaVersion.Created + "\t");
    Console.Write("Created By: " + mediaItemVersion.MediaVersion.CreatedBy + "\t");
    Console.Write("Comment: " + mediaItemVersion.MediaVersion.Comment + "\t");
    Console.WriteLine("Thumbnail: " + mediaItemVersion.MediaConversions[0].Url);

}

The example could produce the output below. MediaItem 27236 has 5 versions where version 28003 is the current one.

    Id: 27236   CurrentVersionId: 28003 VersionId: 28005    Name: v5.jpg    Created: 2016-10-12 15:31:24    Created By: Maria   Comment: new photo  Thumbnail: /media/b4nj32ddizduokqyyr4k/v5.jpg
    Id: 27236   CurrentVersionId: 28003 VersionId: 28004    Name: v4.jpg    Created: 2016-10-08 14:53:52    Created By: John    Comment: removed grades Thumbnail: /media/40qy270pfnxrewso28ej/v4.jpg
    Id: 27236   CurrentVersionId: 28003 VersionId: 28003    Name: v3.jpg    Created: 2016-10-04 14:53:44    Created By: admin   Comment: new layout Thumbnail: /media/pffxh464taqr3xcfh2fg/v3.jpg
    Id: 27236   CurrentVersionId: 28003 VersionId: 28002    Name: v2.jpg    Created: 2016-10-03 14:53:22    Created By: Louise  Comment: changed logo   Thumbnail: /media/t8vme7y7jjj60k4lmtqk/v2.jpg
    Id: 27236   CurrentVersionId: 28003 VersionId: 28001    Name: v1.jpg    Created: 2016-10-01 14:53:09    Created By: admin   Comment: first version  Thumbnail: /media/d3ejdsyz8c4rzaz9bv67/v1.jpg

Note: The id from the media item and the media version id has nothing in common.

You can also specify a specific media version id to retrieve. In this case the VersionFilter is ignored.

var query = new ImageVault.Common.Data.Query.MediaItemQuery
{
    Filter =
    {
        //get a specific media version
        VersionId = {28002},
    },
    Populate =
    {
        MediaFormats =
        {
            //get a thumbnail for each version
            new ImageVault.Common.Data.ThumbnailFormat
            {
                Effects = {new ImageVault.Common.Data.Effects.ResizeEffect(100, 100)}
            }
        },
        //populate the MediaVersion property
        MediaVersion = true,
    }
};
var mediaService = client.CreateChannel<ImageVault.Common.Services.IMediaService>();
var mediaItemVersion = mediaService.Find(query).Single();

Console.Write("Id: " + mediaItemVersion.Id + "\t");
Console.Write("CurrentVersionId: " + mediaItemVersion.CurrentVersionId + "\t");
Console.Write("VersionId: " + mediaItemVersion.VersionId + "\t");
Console.Write("Name: " + mediaItemVersion.MediaVersion.Name + "\t");
Console.Write("Created: " + mediaItemVersion.MediaVersion.Created + "\t");
Console.Write("Created By: " + mediaItemVersion.MediaVersion.CreatedBy + "\t");
Console.Write("Comment: " + mediaItemVersion.MediaVersion.Comment + "\t");
Console.WriteLine("Thumbnail: " + mediaItemVersion.MediaConversions[0].Url);

Upload a new version

To upload a new version for a media item, you upload the file using the IUploadService.UploadFileContent method and to store it as a new version, call the IMediaContentService.StoreContentAsNewVersion and pass the media item id for the media item that you would like to add the file as a new version for.

var mediaItemId = 27236;
var file = new FileInfo(@"c:\myNewversion.jpg");
var uploadService = client.CreateChannel<ImageVault.Common.Services.IUploadService>();
string uploadId;
using (var fileStream = file.OpenRead())
{
    uploadId = uploadService.UploadFileContent(fileStream, null);
}

var mediaContentService = client.CreateChannel<ImageVault.Common.Services.IMediaContentService>();
var mediaVersion = mediaContentService.StoreContentAsNewVersion(uploadId, file.Name, null, mediaItemId);
Console.WriteLine("Uploaded file and stored it as a new version for media item " +
    mediaItemId + " and it was assigned media version id " + mediaVersion.Id);

Note: This will only upload a new version, not make it the current version of the media item.

Set current

You can set the current version of a media Item by calling the IMediaVersionService.SetCurrent method.

var mediaVersionService = client.CreateChannel<ImageVault.Common.Services.IMediaVersionService>();
mediaVersionService.SetCurrent(28004);

Save comment

Each version has a comment that can be modified using the IMediaVersionService.SaveComment method.

var mediaVersionService = client.CreateChannel<ImageVault.Common.Services.IMediaVersionService>();
mediaVersionService.SaveComment(28004, "This is a great version!!");

Delete version

To delete a version, call the IMediaVersionService.Delete method.

var mediaVersionService = client.CreateChannel<ImageVault.Common.Services.IMediaVersionService>();
mediaVersionService.Delete(28003);
comments powered by Disqus
+46 (0)480 - 31 47 95
info@imagevault.se
Swedish website