FileStreamResponseHelpers.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System;
  2. using System.IO;
  3. using System.Net.Http;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using Jellyfin.Api.Models.PlaybackDtos;
  7. using Jellyfin.Api.Models.StreamingDtos;
  8. using MediaBrowser.Controller.MediaEncoding;
  9. using Microsoft.AspNetCore.Http;
  10. using Microsoft.AspNetCore.Mvc;
  11. using Microsoft.Net.Http.Headers;
  12. namespace Jellyfin.Api.Helpers
  13. {
  14. /// <summary>
  15. /// The stream response helpers.
  16. /// </summary>
  17. public static class FileStreamResponseHelpers
  18. {
  19. /// <summary>
  20. /// Returns a static file from a remote source.
  21. /// </summary>
  22. /// <param name="state">The current <see cref="StreamState"/>.</param>
  23. /// <param name="isHeadRequest">Whether the current request is a HTTP HEAD request so only the headers get returned.</param>
  24. /// <param name="httpClient">The <see cref="HttpClient"/> making the remote request.</param>
  25. /// <param name="httpContext">The current http context.</param>
  26. /// <param name="cancellationToken">A cancellation token that can be used to cancel the operation.</param>
  27. /// <returns>A <see cref="Task{ActionResult}"/> containing the API response.</returns>
  28. public static async Task<ActionResult> GetStaticRemoteStreamResult(
  29. StreamState state,
  30. bool isHeadRequest,
  31. HttpClient httpClient,
  32. HttpContext httpContext,
  33. CancellationToken cancellationToken = default)
  34. {
  35. if (state.RemoteHttpHeaders.TryGetValue(HeaderNames.UserAgent, out var useragent))
  36. {
  37. httpClient.DefaultRequestHeaders.Add(HeaderNames.UserAgent, useragent);
  38. }
  39. // Can't dispose the response as it's required up the call chain.
  40. var response = await httpClient.GetAsync(new Uri(state.MediaPath), cancellationToken).ConfigureAwait(false);
  41. var contentType = response.Content.Headers.ContentType?.ToString();
  42. httpContext.Response.Headers[HeaderNames.AcceptRanges] = "none";
  43. if (isHeadRequest)
  44. {
  45. httpContext.Response.Headers[HeaderNames.ContentType] = contentType;
  46. return new OkResult();
  47. }
  48. return new FileStreamResult(await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false), contentType);
  49. }
  50. /// <summary>
  51. /// Returns a static file from the server.
  52. /// </summary>
  53. /// <param name="path">The path to the file.</param>
  54. /// <param name="contentType">The content type of the file.</param>
  55. /// <param name="isHeadRequest">Whether the current request is a HTTP HEAD request so only the headers get returned.</param>
  56. /// <param name="httpContext">The current http context.</param>
  57. /// <returns>An <see cref="ActionResult"/> the file.</returns>
  58. public static ActionResult GetStaticFileResult(
  59. string path,
  60. string contentType,
  61. bool isHeadRequest,
  62. HttpContext httpContext)
  63. {
  64. httpContext.Response.ContentType = contentType;
  65. // if the request is a head request, return an OkResult (200) with the same headers as it would with a GET request
  66. if (isHeadRequest)
  67. {
  68. return new OkResult();
  69. }
  70. return new PhysicalFileResult(path, contentType) { EnableRangeProcessing = true };
  71. }
  72. /// <summary>
  73. /// Returns a transcoded file from the server.
  74. /// </summary>
  75. /// <param name="state">The current <see cref="StreamState"/>.</param>
  76. /// <param name="isHeadRequest">Whether the current request is a HTTP HEAD request so only the headers get returned.</param>
  77. /// <param name="httpContext">The current http context.</param>
  78. /// <param name="transcodingJobHelper">The <see cref="TranscodingJobHelper"/> singleton.</param>
  79. /// <param name="ffmpegCommandLineArguments">The command line arguments to start ffmpeg.</param>
  80. /// <param name="transcodingJobType">The <see cref="TranscodingJobType"/>.</param>
  81. /// <param name="cancellationTokenSource">The <see cref="CancellationTokenSource"/>.</param>
  82. /// <returns>A <see cref="Task{ActionResult}"/> containing the transcoded file.</returns>
  83. public static async Task<ActionResult> GetTranscodedFile(
  84. StreamState state,
  85. bool isHeadRequest,
  86. HttpContext httpContext,
  87. TranscodingJobHelper transcodingJobHelper,
  88. string ffmpegCommandLineArguments,
  89. TranscodingJobType transcodingJobType,
  90. CancellationTokenSource cancellationTokenSource)
  91. {
  92. // Use the command line args with a dummy playlist path
  93. var outputPath = state.OutputFilePath;
  94. httpContext.Response.Headers[HeaderNames.AcceptRanges] = "none";
  95. var contentType = state.GetMimeType(outputPath);
  96. // Headers only
  97. if (isHeadRequest)
  98. {
  99. httpContext.Response.Headers[HeaderNames.ContentType] = contentType;
  100. return new OkResult();
  101. }
  102. var transcodingLock = transcodingJobHelper.GetTranscodingLock(outputPath);
  103. await transcodingLock.WaitAsync(cancellationTokenSource.Token).ConfigureAwait(false);
  104. try
  105. {
  106. TranscodingJobDto? job;
  107. if (!File.Exists(outputPath))
  108. {
  109. job = await transcodingJobHelper.StartFfMpeg(state, outputPath, ffmpegCommandLineArguments, httpContext.Request, transcodingJobType, cancellationTokenSource).ConfigureAwait(false);
  110. }
  111. else
  112. {
  113. job = transcodingJobHelper.OnTranscodeBeginRequest(outputPath, TranscodingJobType.Progressive);
  114. state.Dispose();
  115. }
  116. var stream = new ProgressiveFileStream(outputPath, job, transcodingJobHelper);
  117. return new FileStreamResult(stream, contentType);
  118. }
  119. finally
  120. {
  121. transcodingLock.Release();
  122. }
  123. }
  124. }
  125. }