Johan Björnfot
Dec 16, 2013
  8503
(2 votes)

Blob property

To be able to support media as content (IContentMedia) we introduced a new property type Blob that makes it possible to store binary data related to a content instance. The most obvious usage is through IContentMedia.BinaryData which stores the binary data for a media item.

 

ImageDescriptor attribute

Another usage of blobs for media is the Thumbnail property for IContentMedia where a thumbnail format of the media is stored. For images the thumbnail is auto generated due to the usage of ImageDescriptor attribute. The base class ImageData looks like:

    public class ImageData : MediaData, IContentImage
    {
        /// <summary>
        /// Gets or sets the generated thumbnail for this media.
        /// </summary>
        [ImageDescriptor(Width = 48, Height = 48)]
        public override Framework.Blobs.Blob Thumbnail
        {
            get {return base.Thumbnail;}
            set {base.Thumbnail = value;}
        }
    }

Here we can see that the ImageDescriptor attribute specifies that it should generate a thumbnail in size 48*48. You can override the property and specify the size as you wish.

You can also have additional Blobs properties like LargeThumbnail, MediumThumbnail etc. with ImageDescriptor attribute on your media type. They will then be autogenerated like Thumbnail.

Lazy loading

The actual Blob is stored by the configured BlobProvider so in the content database only the URI to the blob is stored. The actual binary data for the blob will not be loaded before OpenRead method is called on the blob instance.

 

Blob routing

There is a partial route registered that makes it possible to route directly to a blob property on a content instance. The URL pattern is <content URL>/BlobPropertyName. So for example if there is an image with url http://mysite/globalassets/myimage.png then I can route to the thumbnail as http://mysite/globalassets/myimage.png/thumbnail. You can try this by adding “/thumbnail” to any image URL in a CMS 7.5 site.

Export-Import support

Blob properties are included/handled in export-import meaning the Blobs referenced from Blob properties will automatically be included in the export package. This means e.g. that it is possible to transfer content (including media/blobs) from one site with a specific blob provider (e.g. FileBlobProvider) to a site with another blob provider (e.g. blob provider for Amazon or Azure).

PDF version of page

The Blob property is not restricted to be used by only media. It is possible to add a Blob property to any IContent instance. In the example (I used Alloy templates) below I have added a property of type Blob to SitePageData as:

public virtual Blob PDF { get; set; }

I then added an eventhandler for IContentEvents.PublishedContent where I generate a PDF for the page and stores the PDF in the blob property. I can then view the PDF version of the page by appending “/pdf” to the URL for the page. The code for the example is below.

Note: In the example a package called Pechkin is used to generate the PDF but there are many different PDF generators to choose from.

    [ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
    public class PDFCreatorModule : IInitializableModule
    {
        public void Initialize(InitializationEngine context)
        {
            var globalConfig = new GlobalConfig();
            globalConfig.SetMargins(new Margins(300, 100, 150, 100))
                .SetDocumentTitle("Test document")
                .SetPaperSize(PaperKind.A3Rotated);

            var contentEvents = context.Locate.Advanced.GetInstance<IContentEvents>();

            contentEvents.PublishingContent += (sender, args) =>
                {
                    var page = args.Content as SitePageData;
                    if (page != null && !page.ProcessedPDF)
                    {
                        args.Items["ProcessPDF"] = true;
                    }
                };
            contentEvents.PublishedContent += (sender, args) =>
            {
                var page = args.Content as SitePageData;
                if (page != null && args.Items["ProcessPDF"] != null)
                {
                    context.Locate.Advanced.GetInstance<PDFCreator>()
                        .CreatePDF(page, globalConfig, new WebClient());
                }
            };
        }

        public void Preload(string[] parameters)
        {}

        public void Uninitialize(InitializationEngine context)
        {}
    }

    public class PDFCreator
    {
        private IContentRepository _contentRepository;
        private BlobFactory _blobFactory;
        private UrlResolver _urlResolver;

        public PDFCreator(IContentRepository contentRepository, BlobFactory blobFactory, UrlResolver urlResolver)
        {
            _contentRepository = contentRepository;
            _blobFactory = blobFactory;
            _urlResolver = urlResolver;
        }

        public virtual void CreatePDF(SitePageData page, GlobalConfig gc, WebClient webClient)
        {
            page = page.CreateWritableClone() as SitePageData;
            page.PDF = _blobFactory.CreateBlob(Blob.GetContainerIdentifier(page.ContentGuid), ".pdf");

            var pageUrl = UrlResolver.Current.GetUrl(page.ContentLink, page.Language.Name);
            var absolutePageUrl = UriSupport.CreateAbsoluteUri(pageUrl);
            var htmlText = webClient.DownloadString(absolutePageUrl);
            var htmlWithAbsoluteUris = MakeUrisAbsoulte(htmlText, SiteDefinition.Current.SiteUrl);

            var pechin = new SynchronizedPechkin(gc);

            var oc = new ObjectConfig();
            oc.SetCreateExternalLinks(false)
              .SetFallbackEncoding(Encoding.Unicode)
              .SetLoadImages(true);

            using (var writeStream = page.PDF.OpenWrite())
            {
                var convertedData = pechin.Convert(oc, htmlWithAbsoluteUris);
                writeStream.Write(convertedData, 0, convertedData.Length);
            }

            page.ProcessedPDF = true;
            _contentRepository.Save(page, 
                SaveAction.Publish | SaveAction.ForceCurrentVersion | SaveAction.SkipValidation,
                                    AccessLevel.NoAccess);
        }

        const string pattern = @"(?<name>src|href)=""(?<value>/[^""]*)""";
        private string MakeUrisAbsoulte(string html, Uri baseUri)
        {
            var matchEvaluator = new MatchEvaluator(
                match =>
                    {
                        var uri = new Uri(match.Groups["value"].Value, UriKind.RelativeOrAbsolute);
                        Uri absoluteUri;
                        if (!uri.IsAbsoluteUri && Uri.TryCreate(baseUri, uri, out absoluteUri))
                        {
                            var name = match.Groups["name"].Value;
                            return string.Format("{0}=\"{1}\"", name, absoluteUri.AbsoluteUri);
                        }

                        return null;
                });
            return Regex.Replace(html, pattern, matchEvaluator);
        }
    }
Dec 16, 2013

Comments

Dec 18, 2013 01:54 AM

Really nice post Johan! Now I'm looking forward to playing with blob storage myself.

Petter Sørby
Petter Sørby Mar 6, 2014 02:49 PM

Thanks Johan. Very nice both feature and post.

Is it possible to get the URL in a strongly typed manner.

Ex something like:
@Html.PropertyFor(i => i.Thumbnail) to return

Please login to comment.
Latest blogs
Opti ID overview

Opti ID allows you to log in once and switch between Optimizely products using Okta, Entra ID, or a local account. You can also manage all your use...

K Khan | Jul 26, 2024

Getting Started with Optimizely SaaS using Next.js Starter App - Extend a component - Part 3

This is the final part of our Optimizely SaaS CMS proof-of-concept (POC) blog series. In this post, we'll dive into extending a component within th...

Raghavendra Murthy | Jul 23, 2024 | Syndicated blog

Optimizely Graph – Faceting with Geta Categories

Overview As Optimizely Graph (and Content Cloud SaaS) makes its global debut, it is known that there are going to be some bugs and quirks. One of t...

Eric Markson | Jul 22, 2024 | Syndicated blog

Integration Bynder (DAM) with Optimizely

Bynder is a comprehensive digital asset management (DAM) platform that enables businesses to efficiently manage, store, organize, and share their...

Sanjay Kumar | Jul 22, 2024

Frontend Hosting for SaaS CMS Solutions

Introduction Now that CMS SaaS Core has gone into general availability, it is a good time to start discussing where to host the head. SaaS Core is...

Minesh Shah (Netcel) | Jul 20, 2024

Optimizely London Dev Meetup 11th July 2024

On 11th July 2024 in London Niteco and Netcel along with Optimizely ran the London Developer meetup. There was an great agenda of talks that we put...

Scott Reed | Jul 19, 2024