< Summary - Jellyfin

Information
Class: Jellyfin.Server.Implementations.StorageHelpers.StorageHelper
Assembly: Jellyfin.Server.Implementations
File(s): /srv/git/jellyfin/Jellyfin.Server.Implementations/StorageHelpers/StorageHelper.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 42
Coverable lines: 42
Total lines: 108
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 6
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 8/14/2025 - 12:11:05 AM Line coverage: 0% (0/44) Branch coverage: 0% (0/6) Total lines: 11111/18/2025 - 12:11:25 AM Line coverage: 0% (0/42) Branch coverage: 0% (0/6) Total lines: 108 8/14/2025 - 12:11:05 AM Line coverage: 0% (0/44) Branch coverage: 0% (0/6) Total lines: 11111/18/2025 - 12:11:25 AM Line coverage: 0% (0/42) Branch coverage: 0% (0/6) Total lines: 108

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%210%
TestCommonPathsForStorageCapacity(...)100%210%
GetFreeSpaceOf(...)100%210%
TestDataDirectorySize(...)0%2040%
HumanizeStorageSize(...)0%620%

File(s)

/srv/git/jellyfin/Jellyfin.Server.Implementations/StorageHelpers/StorageHelper.cs

#LineLine coverage
 1using System;
 2using System.Globalization;
 3using System.IO;
 4using MediaBrowser.Common.Configuration;
 5using MediaBrowser.Model.System;
 6using Microsoft.Extensions.Logging;
 7
 8namespace Jellyfin.Server.Implementations.StorageHelpers;
 9
 10/// <summary>
 11/// Contains methods to help with checking for storage and returning storage data for jellyfin folders.
 12/// </summary>
 13public static class StorageHelper
 14{
 15    private const long TwoGigabyte = 2_147_483_647L;
 016    private static readonly string[] _byteHumanizedSuffixes = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"];
 17
 18    /// <summary>
 19    /// Tests the available storage capacity on the jellyfin paths with estimated minimum values.
 20    /// </summary>
 21    /// <param name="applicationPaths">The application paths.</param>
 22    /// <param name="logger">Logger.</param>
 23    public static void TestCommonPathsForStorageCapacity(IApplicationPaths applicationPaths, ILogger logger)
 24    {
 025        TestDataDirectorySize(applicationPaths.DataPath, logger, TwoGigabyte);
 026        TestDataDirectorySize(applicationPaths.CachePath, logger, TwoGigabyte);
 027        TestDataDirectorySize(applicationPaths.ProgramDataPath, logger, TwoGigabyte);
 028    }
 29
 30    /// <summary>
 31    /// Gets the free space of a specific directory.
 32    /// </summary>
 33    /// <param name="path">Path to a folder.</param>
 34    /// <returns>The number of bytes available space.</returns>
 35    public static FolderStorageInfo GetFreeSpaceOf(string path)
 36    {
 37        try
 38        {
 039            var driveInfo = new DriveInfo(path);
 040            return new FolderStorageInfo()
 041            {
 042                Path = path,
 043                FreeSpace = driveInfo.AvailableFreeSpace,
 044                UsedSpace = driveInfo.TotalSize - driveInfo.AvailableFreeSpace,
 045                StorageType = driveInfo.DriveType.ToString(),
 046                DeviceId = driveInfo.Name,
 047            };
 48        }
 049        catch
 50        {
 051            return new FolderStorageInfo()
 052            {
 053                Path = path,
 054                FreeSpace = -1,
 055                UsedSpace = -1,
 056                StorageType = null,
 057                DeviceId = null
 058            };
 59        }
 060    }
 61
 62    /// <summary>
 63    /// Gets the underlying drive data from a given path and checks if the available storage capacity matches the thresh
 64    /// </summary>
 65    /// <param name="path">The path to a folder to evaluate.</param>
 66    /// <param name="logger">The logger.</param>
 67    /// <param name="threshold">The threshold to check for or -1 to just log the data.</param>
 68    /// <exception cref="InvalidOperationException">Thrown when the threshold is not available on the underlying storage
 69    private static void TestDataDirectorySize(string path, ILogger logger, long threshold = -1)
 70    {
 071        logger.LogDebug("Check path {TestPath} for storage capacity", path);
 072        Directory.CreateDirectory(path);
 73
 074        var drive = new DriveInfo(path);
 075        if (threshold != -1 && drive.AvailableFreeSpace < threshold)
 76        {
 077            throw new InvalidOperationException($"The path `{path}` has insufficient free space. Available: {HumanizeSto
 78        }
 79
 080        logger.LogInformation(
 081            "Storage path `{TestPath}` ({StorageType}) successfully checked with {FreeSpace} free which is over the mini
 082            path,
 083            drive.DriveType,
 084            HumanizeStorageSize(drive.AvailableFreeSpace),
 085            HumanizeStorageSize(threshold));
 086    }
 87
 88    /// <summary>
 89    /// Formats a size in bytes into a common human readable form.
 90    /// </summary>
 91    /// <remarks>
 92    /// Taken and slightly modified from https://stackoverflow.com/a/4975942/1786007 .
 93    /// </remarks>
 94    /// <param name="byteCount">The size in bytes.</param>
 95    /// <returns>A human readable approximate representation of the argument.</returns>
 96    public static string HumanizeStorageSize(long byteCount)
 97    {
 098        if (byteCount == 0)
 99        {
 0100            return $"0{_byteHumanizedSuffixes[0]}";
 101        }
 102
 0103        var bytes = Math.Abs(byteCount);
 0104        var place = Convert.ToInt32(Math.Floor(Math.Log(bytes, 1024)));
 0105        var num = Math.Round(bytes / Math.Pow(1024, place), 1);
 0106        return (Math.Sign(byteCount) * num).ToString(CultureInfo.InvariantCulture) + _byteHumanizedSuffixes[place];
 107    }
 108}