jellyfin/Jellyfin.Api/Helpers/FileStreamResponseHelpers.cs

126 lines
5.0 KiB
C#
Raw Normal View History

2020-09-05 15:03:21 -04:00
using System;
2020-07-12 05:14:38 -04:00
using System.IO;
using System.Net.Http;
2021-02-14 09:11:46 -05:00
using System.Net.Mime;
2020-07-12 05:14:38 -04:00
using System.Threading;
using System.Threading.Tasks;
2023-10-31 13:26:37 -04:00
using Jellyfin.Api.Extensions;
2020-07-12 05:14:38 -04:00
using MediaBrowser.Controller.MediaEncoding;
2023-10-31 13:26:37 -04:00
using MediaBrowser.Controller.Streaming;
2020-07-12 05:14:38 -04:00
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Net.Http.Headers;
2023-01-31 06:18:10 -05:00
namespace Jellyfin.Api.Helpers;
/// <summary>
/// The stream response helpers.
/// </summary>
public static class FileStreamResponseHelpers
2020-07-12 05:14:38 -04:00
{
/// <summary>
2023-01-31 06:18:10 -05:00
/// Returns a static file from a remote source.
2020-07-12 05:14:38 -04:00
/// </summary>
2023-01-31 06:18:10 -05:00
/// <param name="state">The current <see cref="StreamState"/>.</param>
/// <param name="httpClient">The <see cref="HttpClient"/> making the remote request.</param>
/// <param name="httpContext">The current http context.</param>
/// <param name="cancellationToken">A cancellation token that can be used to cancel the operation.</param>
/// <returns>A <see cref="Task{ActionResult}"/> containing the API response.</returns>
public static async Task<ActionResult> GetStaticRemoteStreamResult(
StreamState state,
HttpClient httpClient,
HttpContext httpContext,
CancellationToken cancellationToken = default)
2020-07-12 05:14:38 -04:00
{
2023-01-31 06:18:10 -05:00
if (state.RemoteHttpHeaders.TryGetValue(HeaderNames.UserAgent, out var useragent))
2020-07-12 05:14:38 -04:00
{
2023-01-31 06:18:10 -05:00
httpClient.DefaultRequestHeaders.Add(HeaderNames.UserAgent, useragent);
}
2020-07-12 05:14:38 -04:00
2023-01-31 06:18:10 -05:00
// Can't dispose the response as it's required up the call chain.
var response = await httpClient.GetAsync(new Uri(state.MediaPath), cancellationToken).ConfigureAwait(false);
var contentType = response.Content.Headers.ContentType?.ToString() ?? MediaTypeNames.Text.Plain;
2020-07-12 05:14:38 -04:00
2023-01-31 06:18:10 -05:00
httpContext.Response.Headers[HeaderNames.AcceptRanges] = "none";
2020-07-12 05:14:38 -04:00
2023-01-31 06:18:10 -05:00
return new FileStreamResult(await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false), contentType);
}
2020-07-12 05:14:38 -04:00
2023-01-31 06:18:10 -05:00
/// <summary>
/// Returns a static file from the server.
/// </summary>
/// <param name="path">The path to the file.</param>
/// <param name="contentType">The content type of the file.</param>
/// <returns>An <see cref="ActionResult"/> the file.</returns>
public static ActionResult GetStaticFileResult(
string path,
string contentType)
{
return new PhysicalFileResult(path, contentType) { EnableRangeProcessing = true };
}
2020-07-12 05:14:38 -04:00
2023-01-31 06:18:10 -05:00
/// <summary>
/// Returns a transcoded file from the server.
/// </summary>
/// <param name="state">The current <see cref="StreamState"/>.</param>
/// <param name="isHeadRequest">Whether the current request is a HTTP HEAD request so only the headers get returned.</param>
/// <param name="httpContext">The current http context.</param>
2023-10-31 13:26:37 -04:00
/// <param name="transcodeManager">The <see cref="ITranscodeManager"/> singleton.</param>
2023-01-31 06:18:10 -05:00
/// <param name="ffmpegCommandLineArguments">The command line arguments to start ffmpeg.</param>
/// <param name="transcodingJobType">The <see cref="TranscodingJobType"/>.</param>
/// <param name="cancellationTokenSource">The <see cref="CancellationTokenSource"/>.</param>
/// <returns>A <see cref="Task{ActionResult}"/> containing the transcoded file.</returns>
public static async Task<ActionResult> GetTranscodedFile(
StreamState state,
bool isHeadRequest,
HttpContext httpContext,
2023-10-31 13:26:37 -04:00
ITranscodeManager transcodeManager,
2023-01-31 06:18:10 -05:00
string ffmpegCommandLineArguments,
TranscodingJobType transcodingJobType,
CancellationTokenSource cancellationTokenSource)
{
// Use the command line args with a dummy playlist path
var outputPath = state.OutputFilePath;
2020-07-12 05:14:38 -04:00
2023-01-31 06:18:10 -05:00
httpContext.Response.Headers[HeaderNames.AcceptRanges] = "none";
2020-07-12 05:14:38 -04:00
2023-01-31 06:18:10 -05:00
var contentType = state.GetMimeType(outputPath);
2020-07-12 05:14:38 -04:00
2023-01-31 06:18:10 -05:00
// Headers only
if (isHeadRequest)
{
httpContext.Response.Headers[HeaderNames.ContentType] = contentType;
return new OkResult();
}
2020-07-12 05:14:38 -04:00
2023-10-31 13:26:37 -04:00
var transcodingLock = transcodeManager.GetTranscodingLock(outputPath);
2023-01-31 06:18:10 -05:00
await transcodingLock.WaitAsync(cancellationTokenSource.Token).ConfigureAwait(false);
try
{
2023-10-31 11:31:09 -04:00
TranscodingJob? job;
2023-01-31 06:18:10 -05:00
if (!File.Exists(outputPath))
2020-07-12 05:14:38 -04:00
{
2023-10-31 13:26:37 -04:00
job = await transcodeManager.StartFfMpeg(
state,
outputPath,
ffmpegCommandLineArguments,
httpContext.User.GetUserId(),
transcodingJobType,
cancellationTokenSource).ConfigureAwait(false);
2020-07-12 05:14:38 -04:00
}
2023-01-31 06:18:10 -05:00
else
2020-07-12 05:14:38 -04:00
{
2023-10-31 13:26:37 -04:00
job = transcodeManager.OnTranscodeBeginRequest(outputPath, TranscodingJobType.Progressive);
2023-01-31 06:18:10 -05:00
state.Dispose();
2020-07-12 05:14:38 -04:00
}
2023-01-31 06:18:10 -05:00
2023-10-31 13:26:37 -04:00
var stream = new ProgressiveFileStream(outputPath, job, transcodeManager);
2023-01-31 06:18:10 -05:00
return new FileStreamResult(stream, contentType);
}
finally
{
transcodingLock.Release();
2020-07-12 05:14:38 -04:00
}
}
}