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,
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.