You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jellyfin/Jellyfin.Api/Helpers/FileStreamResponseHelpers.cs

126 lines
5.0 KiB

using System;
4 years ago
using System.IO;
using System.Net.Http;
4 years ago
using System.Net.Mime;
4 years ago
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Api.Extensions;
4 years ago
using MediaBrowser.Controller.MediaEncoding;
using MediaBrowser.Controller.Streaming;
4 years ago
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Net.Http.Headers;
namespace Jellyfin.Api.Helpers;
/// <summary>
/// The stream response helpers.
/// </summary>
public static class FileStreamResponseHelpers
4 years ago
{
/// <summary>
/// Returns a static file from a remote source.
4 years ago
/// </summary>
/// <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)
4 years ago
{
if (state.RemoteHttpHeaders.TryGetValue(HeaderNames.UserAgent, out var useragent))
4 years ago
{
httpClient.DefaultRequestHeaders.Add(HeaderNames.UserAgent, useragent);
}
4 years ago
// 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;
4 years ago
httpContext.Response.Headers[HeaderNames.AcceptRanges] = "none";
4 years ago
return new FileStreamResult(await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false), contentType);
}
4 years ago
/// <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 };
}
4 years ago
/// <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>
/// <param name="transcodeManager">The <see cref="ITranscodeManager"/> singleton.</param>
/// <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,
ITranscodeManager transcodeManager,
string ffmpegCommandLineArguments,
TranscodingJobType transcodingJobType,
CancellationTokenSource cancellationTokenSource)
{
// Use the command line args with a dummy playlist path
var outputPath = state.OutputFilePath;
4 years ago
httpContext.Response.Headers[HeaderNames.AcceptRanges] = "none";
4 years ago
var contentType = state.GetMimeType(outputPath);
4 years ago
// Headers only
if (isHeadRequest)
{
httpContext.Response.Headers[HeaderNames.ContentType] = contentType;
return new OkResult();
}
4 years ago
var transcodingLock = transcodeManager.GetTranscodingLock(outputPath);
await transcodingLock.WaitAsync(cancellationTokenSource.Token).ConfigureAwait(false);
try
{
TranscodingJob? job;
if (!File.Exists(outputPath))
4 years ago
{
job = await transcodeManager.StartFfMpeg(
state,
outputPath,
ffmpegCommandLineArguments,
httpContext.User.GetUserId(),
transcodingJobType,
cancellationTokenSource).ConfigureAwait(false);
4 years ago
}
else
4 years ago
{
job = transcodeManager.OnTranscodeBeginRequest(outputPath, TranscodingJobType.Progressive);
state.Dispose();
4 years ago
}
var stream = new ProgressiveFileStream(outputPath, job, transcodeManager);
return new FileStreamResult(stream, contentType);
}
finally
{
transcodingLock.Release();
4 years ago
}
}
}