< 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: 43
Coverable lines: 43
Total lines: 109
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

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;
 16    private const long FiveHundredAndTwelveMegaByte = 536_870_911L;
 017    private static readonly string[] _byteHumanizedSuffixes = ["B", "KB", "MB", "GB", "TB", "PB", "EB"];
 18
 19    /// <summary>
 20    /// Tests the available storage capacity on the jellyfin paths with estimated minimum values.
 21    /// </summary>
 22    /// <param name="applicationPaths">The application paths.</param>
 23    /// <param name="logger">Logger.</param>
 24    public static void TestCommonPathsForStorageCapacity(IApplicationPaths applicationPaths, ILogger logger)
 25    {
 026        TestDataDirectorySize(applicationPaths.DataPath, logger, TwoGigabyte);
 027        TestDataDirectorySize(applicationPaths.LogDirectoryPath, logger, FiveHundredAndTwelveMegaByte);
 028        TestDataDirectorySize(applicationPaths.CachePath, logger, TwoGigabyte);
 029        TestDataDirectorySize(applicationPaths.ProgramDataPath, logger, TwoGigabyte);
 030        TestDataDirectorySize(applicationPaths.TempDirectory, logger, TwoGigabyte);
 031    }
 32
 33    /// <summary>
 34    /// Gets the free space of a specific directory.
 35    /// </summary>
 36    /// <param name="path">Path to a folder.</param>
 37    /// <returns>The number of bytes available space.</returns>
 38    public static FolderStorageInfo GetFreeSpaceOf(string path)
 39    {
 40        try
 41        {
 042            var driveInfo = new DriveInfo(path);
 043            return new FolderStorageInfo()
 044            {
 045                Path = path,
 046                FreeSpace = driveInfo.AvailableFreeSpace,
 047                UsedSpace = driveInfo.TotalSize - driveInfo.AvailableFreeSpace,
 048                StorageType = driveInfo.DriveType.ToString(),
 049                DeviceId = driveInfo.Name,
 050            };
 51        }
 052        catch
 53        {
 054            return new FolderStorageInfo()
 055            {
 056                Path = path,
 057                FreeSpace = -1,
 058                UsedSpace = -1,
 059                StorageType = null,
 060                DeviceId = null
 061            };
 62        }
 063    }
 64
 65    /// <summary>
 66    /// Gets the underlying drive data from a given path and checks if the available storage capacity matches the thresh
 67    /// </summary>
 68    /// <param name="path">The path to a folder to evaluate.</param>
 69    /// <param name="logger">The logger.</param>
 70    /// <param name="threshold">The threshold to check for or -1 to just log the data.</param>
 71    /// <exception cref="InvalidOperationException">Thrown when the threshold is not available on the underlying storage
 72    private static void TestDataDirectorySize(string path, ILogger logger, long threshold = -1)
 73    {
 074        logger.LogDebug("Check path {TestPath} for storage capacity", path);
 075        var drive = new DriveInfo(path);
 076        if (threshold != -1 && drive.AvailableFreeSpace < threshold)
 77        {
 078            throw new InvalidOperationException($"The path `{path}` has insufficient free space. Required: at least {Hum
 79        }
 80
 081        logger.LogInformation(
 082            "Storage path `{TestPath}` ({StorageType}) successfully checked with {FreeSpace} free which is over the mini
 083            path,
 084            drive.DriveType,
 085            HumanizeStorageSize(drive.AvailableFreeSpace),
 086            HumanizeStorageSize(threshold));
 087    }
 88
 89    /// <summary>
 90    /// Formats a size in bytes into a common human readable form.
 91    /// </summary>
 92    /// <remarks>
 93    /// Taken and slightly modified from https://stackoverflow.com/a/4975942/1786007 .
 94    /// </remarks>
 95    /// <param name="byteCount">The size in bytes.</param>
 96    /// <returns>A human readable approximate representation of the argument.</returns>
 97    public static string HumanizeStorageSize(long byteCount)
 98    {
 099        if (byteCount == 0)
 100        {
 0101            return $"0{_byteHumanizedSuffixes[0]}";
 102        }
 103
 0104        var bytes = Math.Abs(byteCount);
 0105        var place = Convert.ToInt32(Math.Floor(Math.Log(bytes, 1024)));
 0106        var num = Math.Round(bytes / Math.Pow(1024, place), 1);
 0107        return (Math.Sign(byteCount) * num).ToString(CultureInfo.InvariantCulture) + _byteHumanizedSuffixes[place];
 108    }
 109}