+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Can I develop my own StreamingProvider?

Can I develop my own StreamingProvider?

5 posts, 1 answered
  1. buck
    buck avatar
    3 posts
    Registered:
    16 Nov 2006
    04 Apr 2010
    Link to this post
    Hi! I unsuccessfully tried to develop my own StreamingFormatter. I created class implementing IStreamingContentProvider interface. I override AvailableStreamingProviders property in a descendant of Telerik.Libraries.Data.DefaultProvider. I changed type of provider for Libraries in configuration file.
    And finnaly I added streamingIsEnabled="True" and streamingProviderName in <libraries> section.
    I tryed to upload a document and it was successful, GetUploadStream method was called. But GetDownloadStream and Delete methods were not called when I tryed to download and delete the document. Downloading resulted in error page, deletion didn't delete the document content, but removed it from a library.
    What did I do wrong?
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    05 Apr 2010
    Link to this post
    Hello buck,

    1. What are the errors you are getting?
    2. Is there any reason for overriding the provider?
    3. Can you share some code or more detailed information, because from your post we are not able to let you know what the problem is, since you are doing some custom logic.

    By default the DeleteContent method of the FileSystemProvider cannot be overridden, because the method is public only. This means that you should have a custom provider with written methods for uploading and deleting. You should also override Telerik.Libraries.Data.DefaultProvider where you initialize your own FileSystem provider by overriding AvailableStreamingProviders.

    Regards,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  3. buck
    buck avatar
    3 posts
    Registered:
    16 Nov 2006
    05 Apr 2010
    Link to this post
    Well, that's the code snippets. This code successfully uploads documents throw my method GetUploadStream(). But GetDownloadStream and Delete methods are called never. I can walk around problem with deletion by overriding DeleteContent method in ContentProvider. But I can't think out how to do it with downloading.

    public sealed class ContentProvider : DefaultProvider
    {
        public override IDictionary<string, IStreamingContentProvider> AvailableStreamingProviders
        {
            get
            {
                if (_availableStreamingProviders == null)
                {
                    _availableStreamingProviders = base.AvailableStreamingProviders;
                    _availableStreamingProviders["MyStorage"] = new StreamingContentProvider();
                }
     
                return _availableStreamingProviders;
            }
        }
     
        private IDictionary<string, IStreamingContentProvider> _availableStreamingProviders;
    }

    public sealed class StreamingContentProvider : IStreamingContentProvider
    {
        public DownloadStream GetDownloadStream(IStreamableContent content)
        {
            using (var proxy = new DocumentStorageClient())
                return new MyDownloadStream(proxy.Get(content.ID));
        }
     
        public UploadStream GetUploadStream(IStreamableContent content)
        {
            return new MyUploadStream(content.ID);
        }
     
        public void DeleteContent(IStreamableContent content)
        {
            using (var proxy = new DocumentStorageClient())
                proxy.Delete(content.ID);
        }
    }

    And my changes in the configuration file:

    <libraries defaultGenericProvider="Libraries"
               streamingIsEnabled="True" streamingProviderName="MyStorage">

    <add name="Libraries" .... type="Storage.ContentProvider, Storage" />

  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    05 Apr 2010
    Link to this post
    Hello buck,

    File download is processed by the ContentHttpHandler - ProcessStreamingContentItem method. You have to override it because there the base method - GetDownloadStream is called. You need to call your custom method instead of the base.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Telerik.Cms.Engine;
    using Telerik.Cms.Engine.Streaming;
    using Telerik.Cms.Web;
    using Telerik.Utilities;
     
    /// <summary>
    /// Summary description for ContentHttpHandlerCustom
    /// </summary>
    public class ContentHttpHandlerCustom : ContentHttpHandler
    {
        public ContentHttpHandlerCustom()
        {
        }
     
        protected override object ProcessStreamingContentItem(Telerik.Cms.Engine.Streaming.IStreamableContent content, HttpRequest request, Telerik.Cms.Engine.Streaming.IStreamingContentProvider provider)
        {
           // return base.ProcessStreamingContentItem(content, request, provider);
             object data = content.Content;
     
                if (!String.IsNullOrEmpty(request.QueryString["width"]) &&
                    !String.IsNullOrEmpty(request.QueryString["height"]))
                {
                    try
                    {
     
                        // HERE CALL YOUR CUSTOM METHOD AND PROVIDER
                        using (var reader = provider.GetDownloadStreamCustom(content))
                        {
                            var buffer = StreamHelper.ReadToEnd(reader, false);
                            data = this.ResizeImage(buffer, content.MimeType, request);
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Exception(ex);
                    }
                }
                else
                {
                    // if this is a normal streaming request, do the writing
                    data = new CustomResponseDataWriter(new object[] { content, provider }, new ResponseWriteDelegate(this.StreamingResponseWriter));
                }
     
                return data;
            }
        // implement StreamingResponseWriter
        private void StreamingResponseWriter(HttpResponse response, object data)
        {
            // HERE CALL YOUR CUSTOM METHOD AND PROVIDER
            using (var stream = provider.GetDownloadStreamCustom(content))
            {
               
                response.AddHeader("Content-Length", stream.Length.ToString());
                //
                StreamHelper.CopyStream(stream, response.OutputStream, false);
            }
        }
    }

    Then you need to replace the default ContentHttpHandler with the newly created in your web.config.

    Greetings,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
    Answered
  5. buck
    buck avatar
    3 posts
    Registered:
    16 Nov 2006
    05 Apr 2010
    Link to this post
    I had to override ProcessContentItem method instead of ProcessStreamingContentItem. It seems like HttpHandle ignores IsStreamingEnabled flag. It may be a reason why GetDownloadStream of StreamingContentProvider isn't invoked when trying to download a document.
    Anyway, now it works. Thanks.
Register for webinar
5 posts, 1 answered