0

Media item are getting stored on azure, for this I have overridden Media library classes. Now what happen when I am selecting any media in Media Library to see, enter image description here

It gets automatically downloaded. It is happening for every media type item like pdf,zip, exe, xls except images.

For code : Overridden Class: Sitecore.Resources.Media.MediaRequestHandler

protected override bool DoProcessRequest(HttpContext context)
            {
                Assert.ArgumentNotNull((object)context, "context");
                MediaRequest request = MediaManager.ParseMediaRequest(context.Request);
                if (request == null)
                    return false;
                Sitecore.Resources.Media.Media media = null;
               // var mediaPath = request?.MediaUri?.MediaPath.Substring(24);
                string mediaPath = string.Empty;
                bool mediaById = false;
                if (request?.MediaUri != null && !string.IsNullOrEmpty(request?.MediaUri?.MediaPath.Substring(24)))
                {
                    try
                    {
                        var mediaGuid = Guid.Parse(request?.MediaUri?.MediaPath.Substring(24).Replace(" ", "-"));
                        mediaPath = request?.MediaUri?.MediaPath.Substring(24).Replace(" ", "-");
                        mediaById = true;
                    }
                    catch (Exception)
                    {
                        //log nothing
                        mediaById = false;
                    }
                }
                if (!mediaById)
                {
                    media = MediaManager.GetMedia(request.MediaUri);
                }
                else
                {
                    var mediaItem1 = Sitecore.Context.Database.GetItem(mediaPath);
                    if (mediaItem1 != null)
                    {
                        Sitecore.Data.Items.MediaItem mediaItem = new Sitecore.Data.Items.MediaItem(mediaItem1);
                        if (mediaItem != null)
                            media = MediaManager.GetMedia(mediaItem);
                    }
                }
    
                if (!IsCdnMedia(media))
                    return base.DoProcessRequest(context);
    
    if (request.Options.Thumbnail && (media.MimeType != "image/jpeg" && media.MimeType != "image/png"))
                {
                    //request.Options.UseMediaCache = false;
                    return base.DoProcessRequest(context, request, media);
                     
                }
    
                return this.DoProcessRequest(context, request, media);
            }
    
           
           
            private bool IsCdnMedia(Sitecore.Resources.Media.Media media)
            {
                return (media != null && media.MediaData.MediaItem.FileBased);
            }
    
    
            
            protected override bool DoProcessRequest(HttpContext context, MediaRequest request, Sitecore.Resources.Media.Media media)
            {
                Assert.ArgumentNotNull((object)context, nameof(context));
                Assert.ArgumentNotNull((object)request, nameof(request));
                Assert.ArgumentNotNull((object)media, nameof(media));
                if (this.Modified(context, media, request.Options) == Sitecore.Tristate.False)
                {
                    this.RaiseEvent("media:request", request);
                    this.SendMediaHeaders(media, context);
                    context.Response.StatusCode = 304;
                    return true;
                }
                this.ProcessImageDimensions(request, media);
                MediaStream mediaStream = null;
                if (!IsCdnMedia(media))
                {
                    mediaStream = this.GetMediaStream(media, request);
                }
                else
                {
                    CloudStorageProvider cloudStorageProvider = new CloudStorageProvider();
                    var stream = cloudStorageProvider.GetStream(media.MediaData.MediaItem);
                    mediaStream = new MediaStream(stream, media.MediaData.MediaItem.Extension, media.MediaData.MediaItem);
                }
                if (mediaStream == null)
                    return false;
                this.RaiseEvent("media:request", request);
                if (Settings.Media.EnableRangeRetrievalRequest && Settings.Media.CachingEnabled)
                {
                    using (mediaStream)
                    {
                        this.SendMediaHeaders(media, context);
                        new RangeRetrievalResponse(RangeRetrievalRequest.BuildRequest(context, media), mediaStream).ExecuteRequest(context);
                        return true;
                    }
                }
                else
                {
                    this.SendMediaHeaders(media, context);
                    this.SendStreamHeaders(mediaStream, context);
                    using (mediaStream)
                    {
                        context.Response.AddHeader("Content-Length", mediaStream.Stream.Length.ToString());
                        WebUtil.TransmitStream(mediaStream.Stream, context.Response, Settings.Media.StreamBufferSize);
                    }
                    return true;
                }
            }

Does anyone faced such issue? or any suggestion.

Note: Sitecore is upgraded from 8.1 to 9.2.

7
  • can you check in showconfig. Srach for mediaType name="PDF file" and check forceDownload like <forceDownload>true</forceDownload>, tru to make it false if it set to true Commented Sep 9, 2020 at 13:46
  • but that just work for pdf, other media type still gets downloaded
    – OyeHarish
    Commented Sep 9, 2020 at 14:12
  • what are you expecting to happen? That sounds like perfectly normal behaviour
    – Richard Seal
    Commented Sep 9, 2020 at 16:23
  • In sitecore> Media Library when I am selecting any item it automatically downloads, that is not normal. it should not be downloaded untill clicked upon Download in Media.
    – OyeHarish
    Commented Sep 9, 2020 at 16:40
  • Please clarify your question with these details. It is not clear in your post that you are trying to select the items in the media library.
    – Richard Seal
    Commented Sep 9, 2020 at 16:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.