FileStreamResponseHelpers.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System;
  2. using System.IO;
  3. using System.Net.Http;
  4. using System.Net.Mime;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using Jellyfin.Api.Extensions;
  8. using MediaBrowser.Controller.MediaEncoding;
  9. using MediaBrowser.Controller.Streaming;
  10. using Microsoft.AspNetCore.Http;
  11. using Microsoft.AspNetCore.Mvc;
  12. using Microsoft.Net.Http.Headers;
  13. namespace Jellyfin.Api.Helpers;
  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="httpClient">The <see cref="HttpClient"/> making the remote request.</param>
  24. /// <param name="httpContext">The current http context.</param>
  25. /// <param name="cancellationToken">A cancellation token that can be used to cancel the operation.</param>
  26. /// <returns>A <see cref="Task{ActionResult}"/> containing the API response.</returns>
  27. public static async Task<ActionResult> GetStaticRemoteStreamResult(
  28. StreamState state,
  29. HttpClient httpClient,
  30. HttpContext httpContext,
  31. CancellationToken cancellationToken = default)
  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(new Uri(state.MediaPath), HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
  39. var contentType = response.Content.Headers.ContentType?.ToString() ?? MediaTypeNames.Text.Plain;
  40. httpContext.Response.Headers[HeaderNames.AcceptRanges] = "none";
  41. return new FileStreamResult(await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false), contentType);
  42. }
  43. /// <summary>
  44. /// Returns a static file from the server.
  45. /// </summary>
  46. /// <param name="path">The path to the file.</param>
  47. /// <param name="contentType">The content type of the file.</param>
  48. /// <returns>An <see cref="ActionResult"/> the file.</returns>
  49. public static ActionResult GetStaticFileResult(
  50. string path,
  51. string contentType)
  52. {
  53. return new PhysicalFileResult(path, contentType) { EnableRangeProcessing = true };
  54. }
  55. /// <summary>
  56. /// Returns a transcoded file from the server.
  57. /// </summary>
  58. /// <param name="state">The current <see cref="StreamState"/>.</param>
  59. /// <param name="isHeadRequest">Whether the current request is a HTTP HEAD request so only the headers get returned.</param>
  60. /// <param name="httpContext">The current http context.</param>
  61. /// <param name="transcodeManager">The <see cref="ITranscodeManager"/> singleton.</param>
  62. /// <param name="ffmpegCommandLineArguments">The command line arguments to start ffmpeg.</param>
  63. /// <param name="transcodingJobType">The <see cref="TranscodingJobType"/>.</param>
  64. /// <param name="cancellationTokenSource">The <see cref="CancellationTokenSource"/>.</param>
  65. /// <returns>A <see cref="Task{ActionResult}"/> containing the transcoded file.</returns>
  66. public static async Task<ActionResult> GetTranscodedFile(
  67. StreamState state,
  68. bool isHeadRequest,
  69. HttpContext httpContext,
  70. ITranscodeManager transcodeManager,
  71. string ffmpegCommandLineArguments,
  72. TranscodingJobType transcodingJobType,
  73. CancellationTokenSource cancellationTokenSource)
  74. {
  75. // Use the command line args with a dummy playlist path
  76. var outputPath = state.OutputFilePath;
  77. httpContext.Response.Headers[HeaderNames.AcceptRanges] = "none";
  78. var contentType = state.GetMimeType(outputPath);
  79. // Headers only
  80. if (isHeadRequest)
  81. {
  82. httpContext.Response.Headers[HeaderNames.ContentType] = contentType;
  83. return new OkResult();
  84. }
  85. using (await transcodeManager.LockAsync(outputPath, cancellationTokenSource.Token).ConfigureAwait(false))
  86. {
  87. TranscodingJob? job;
  88. if (!File.Exists(outputPath))
  89. {
  90. job = await transcodeManager.StartFfMpeg(
  91. state,
  92. outputPath,
  93. ffmpegCommandLineArguments,
  94. httpContext.User.GetUserId(),
  95. transcodingJobType,
  96. cancellationTokenSource).ConfigureAwait(false);
  97. }
  98. else
  99. {
  100. job = transcodeManager.OnTranscodeBeginRequest(outputPath, TranscodingJobType.Progressive);
  101. state.Dispose();
  102. }
  103. var stream = new ProgressiveFileStream(outputPath, job, transcodeManager);
  104. return new FileStreamResult(stream, contentType);
  105. }
  106. }
  107. }