FileStreamResponseHelpers.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. /// <returns>A <see cref="Task{ActionResult}"/> containing the API response.</returns>
  27. public static async Task<ActionResult> GetStaticRemoteStreamResult(
  28. StreamState state,
  29. bool isHeadRequest,
  30. HttpClient httpClient,
  31. HttpContext httpContext)
  32. {
  33. if (state.RemoteHttpHeaders.TryGetValue(HeaderNames.UserAgent, out var useragent))
  34. {
  35. httpClient.DefaultRequestHeaders.Add(HeaderNames.UserAgent, useragent);
  36. }
  37. // Can't dispose the response as it's required up the call chain.
  38. var response = await httpClient.GetAsync(state.MediaPath).ConfigureAwait(false);
  39. var contentType = response.Content.Headers.ContentType.ToString();
  40. httpContext.Response.Headers[HeaderNames.AcceptRanges] = "none";
  41. if (isHeadRequest)
  42. {
  43. return new FileContentResult(Array.Empty<byte>(), contentType);
  44. }
  45. return new FileStreamResult(await response.Content.ReadAsStreamAsync().ConfigureAwait(false), contentType);
  46. }
  47. /// <summary>
  48. /// Returns a static file from the server.
  49. /// </summary>
  50. /// <param name="path">The path to the file.</param>
  51. /// <param name="contentType">The content type of the file.</param>
  52. /// <param name="isHeadRequest">Whether the current request is a HTTP HEAD request so only the headers get returned.</param>
  53. /// <param name="httpContext">The current http context.</param>
  54. /// <returns>An <see cref="ActionResult"/> the file.</returns>
  55. public static ActionResult GetStaticFileResult(
  56. string path,
  57. string contentType,
  58. bool isHeadRequest,
  59. HttpContext httpContext)
  60. {
  61. httpContext.Response.ContentType = contentType;
  62. // if the request is a head request, return a NoContent result with the same headers as it would with a GET request
  63. if (isHeadRequest)
  64. {
  65. return new NoContentResult();
  66. }
  67. return new PhysicalFileResult(path, contentType) { EnableRangeProcessing = true };
  68. }
  69. /// <summary>
  70. /// Returns a transcoded file from the server.
  71. /// </summary>
  72. /// <param name="state">The current <see cref="StreamState"/>.</param>
  73. /// <param name="isHeadRequest">Whether the current request is a HTTP HEAD request so only the headers get returned.</param>
  74. /// <param name="httpContext">The current http context.</param>
  75. /// <param name="transcodingJobHelper">The <see cref="TranscodingJobHelper"/> singleton.</param>
  76. /// <param name="ffmpegCommandLineArguments">The command line arguments to start ffmpeg.</param>
  77. /// <param name="transcodingJobType">The <see cref="TranscodingJobType"/>.</param>
  78. /// <param name="cancellationTokenSource">The <see cref="CancellationTokenSource"/>.</param>
  79. /// <returns>A <see cref="Task{ActionResult}"/> containing the transcoded file.</returns>
  80. public static async Task<ActionResult> GetTranscodedFile(
  81. StreamState state,
  82. bool isHeadRequest,
  83. HttpContext httpContext,
  84. TranscodingJobHelper transcodingJobHelper,
  85. string ffmpegCommandLineArguments,
  86. TranscodingJobType transcodingJobType,
  87. CancellationTokenSource cancellationTokenSource)
  88. {
  89. // Use the command line args with a dummy playlist path
  90. var outputPath = state.OutputFilePath;
  91. httpContext.Response.Headers[HeaderNames.AcceptRanges] = "none";
  92. var contentType = state.GetMimeType(outputPath);
  93. // Headers only
  94. if (isHeadRequest)
  95. {
  96. return new FileContentResult(Array.Empty<byte>(), contentType);
  97. }
  98. var transcodingLock = transcodingJobHelper.GetTranscodingLock(outputPath);
  99. await transcodingLock.WaitAsync(cancellationTokenSource.Token).ConfigureAwait(false);
  100. try
  101. {
  102. TranscodingJobDto? job;
  103. if (!File.Exists(outputPath))
  104. {
  105. job = await transcodingJobHelper.StartFfMpeg(state, outputPath, ffmpegCommandLineArguments, httpContext.Request, transcodingJobType, cancellationTokenSource).ConfigureAwait(false);
  106. }
  107. else
  108. {
  109. job = transcodingJobHelper.OnTranscodeBeginRequest(outputPath, TranscodingJobType.Progressive);
  110. state.Dispose();
  111. }
  112. var stream = new ProgressiveFileStream(outputPath, job, transcodingJobHelper);
  113. return new FileStreamResult(stream, contentType);
  114. }
  115. finally
  116. {
  117. transcodingLock.Release();
  118. }
  119. }
  120. }
  121. }