< Summary - Jellyfin

Information
Class: Jellyfin.Api.Helpers.FileStreamResponseHelpers
Assembly: Jellyfin.Api
File(s): /srv/git/jellyfin/Jellyfin.Api/Helpers/FileStreamResponseHelpers.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 1
Coverable lines: 1
Total lines: 119
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
GetStaticFileResult(...)100%210%

File(s)

/srv/git/jellyfin/Jellyfin.Api/Helpers/FileStreamResponseHelpers.cs

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