Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
/// <summary>
/// Defines the <see cref="MediaManifestProxyController" />.
/// </summary>
[Authorize]
[Route("api/[controller]")]
[ApiController]
public class MediaManifestProxyController : ControllerBase
Expand All @@ -37,6 +36,7 @@ public MediaManifestProxyController(ILogger<MediaManifestProxyController> logger
/// <param name="playBackUrl">The playBackUrl.</param>
/// <param name="token">The token.</param>
/// <returns>The MediaManifestProxy string.</returns>
[Authorize]
public string Get(string playBackUrl, string token)
{
this.logger.LogDebug($"playBackUrl={playBackUrl} token={token}");
Expand Down Expand Up @@ -103,6 +103,80 @@ public string Get(string playBackUrl, string token)
return null;
}

/// <summary>
/// The LandingPageGet.
/// </summary>
/// <param name="playBackUrl">The playBackUrl.</param>
/// <param name="token">The token.</param>
/// <returns>The MediaManifestProxy string.</returns>
[HttpGet]
[Route("LandingPageGet")]
public string LandingPageGet(string playBackUrl, string token)
{
this.logger.LogDebug($"playBackUrl={playBackUrl} token={token}");
var httpRequest = (HttpWebRequest)WebRequest.Create(new Uri(playBackUrl));
httpRequest.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
httpRequest.Timeout = 30000;

var httpResponse = httpRequest.GetResponse();

try
{
this.logger.LogDebug($"Calling httpResponse.GetResponseStream(): playBackUrl={playBackUrl} ");
var stream = httpResponse.GetResponseStream();
if (stream != null)
{
using (var reader = new StreamReader(stream))
{
const string qualityLevelRegex = @"(|)([^""\s]+\.m3u8\(encryption=cbc\))";
const string fragmentsRegex = @"(Fragments\([\w\d=-]+,[\w\d=-]+\))";
const string urlRegex = @"(https?:\/\/[\da-z\.-]+\.[a-z\.]{2,6}[\/\w \.-]*\?[^,\s""]*)";

var baseUrl = playBackUrl.Substring(0, playBackUrl.IndexOf(".ism", System.StringComparison.OrdinalIgnoreCase)) + ".ism";
this.logger.LogDebug($"baseUrl={baseUrl}");

var content = reader.ReadToEnd();

content = ReplaceUrisWithProxy(content, baseUrl);
var newContent = Regex.Replace(content, urlRegex, match =>
{
string baseUrlWithQuery = match.Groups[1].Value; // URL including the query string

// Append the token correctly without modifying surrounding characters
string newUrl = baseUrlWithQuery.Contains("?") ?
$"{baseUrlWithQuery}&token={token}" :
$"{baseUrlWithQuery}?token={token}";

return newUrl;
});

this.logger.LogDebug($"newContent={newContent}");

var match = Regex.Match(playBackUrl, qualityLevelRegex);
if (match.Success)
{
this.logger.LogDebug($"match.Success");
var qualityLevel = match.Groups[0].Value;
newContent = Regex.Replace(newContent, fragmentsRegex, m => string.Format(CultureInfo.InvariantCulture, baseUrl + "/" + qualityLevel + "/" + m.Value));
this.logger.LogDebug($"Updated newContent={newContent}");
}

return newContent;
}
}
}
catch (Exception ex)
{
this.logger.LogError(ex.Message);
}
finally
{
httpResponse.Close();
}

return null;
}

private static string ReplaceUrisWithProxy(string playlistContent, string proxyUrl)
{
// Split the playlist content into lines
Expand Down
13 changes: 11 additions & 2 deletions LearningHub.Nhs.WebUI/Controllers/MediaController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,25 @@ public MediaController(IWebHostEnvironment hostingEnvironment, ILogger<MediaCont
/// <param name="playBackUrl">The playBackUrl.</param>
/// <param name="token">The token.</param>
/// <param name="origin">The orgin node.</param>
/// <param name="isLandingPage">The isLandingPage.</param>
/// <returns>The <see cref="IActionResult"/>.</returns>
[Route("Media/MediaManifest")]
public IActionResult MediaManifest(string playBackUrl, string token, string origin = "*")
public IActionResult MediaManifest(string playBackUrl, string token, string origin = "*", bool isLandingPage = false)
{
try
{
this.Logger.LogDebug($"playBackUrl={playBackUrl} token={token}");
var hostPortion = this.Request.Host;
var manifestProxyUrl = string.Empty;
if (isLandingPage)
{
manifestProxyUrl = string.Format("https://{0}/api/MediaManifestProxy/LandingPageGet", hostPortion);
}
else
{
manifestProxyUrl = string.Format("https://{0}/api/MediaManifestProxy", hostPortion);
}

var manifestProxyUrl = string.Format("https://{0}/api/MediaManifestProxy", hostPortion);
this.Logger.LogDebug($"manifestProxyUrl={manifestProxyUrl}");

var modifiedTopLeveLManifest = this.azureMediaService.GetTopLevelManifestForToken(manifestProxyUrl, playBackUrl, token);
Expand Down
2 changes: 1 addition & 1 deletion LearningHub.Nhs.WebUI/Views/Home/_CmsVideo.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@

if (checkIfIphone()) {
var token = model.videoAsset.azureMediaAsset.authenticationToken;
url = '@requestURL' + "Media/MediaManifest?playBackUrl=" + url + "&token=" + token + "&origin=" + '@requestURL';
url = '@requestURL' + "Media/MediaManifest?playBackUrl=" + url + "&token=" + token + "&origin=" + '@requestURL' + "&isLandingPage=" + true;
}
var subtitleTrack = null;
if (model.videoAsset.azureMediaAsset && model.videoAsset.closedCaptionsFile) {
Expand Down
Loading