StorageHelper.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System;
  2. using System.Globalization;
  3. using System.IO;
  4. using MediaBrowser.Common.Configuration;
  5. using MediaBrowser.Model.System;
  6. using Microsoft.Extensions.Logging;
  7. namespace Jellyfin.Server.Implementations.StorageHelpers;
  8. /// <summary>
  9. /// Contains methods to help with checking for storage and returning storage data for jellyfin folders.
  10. /// </summary>
  11. public static class StorageHelper
  12. {
  13. private const long TwoGigabyte = 2_147_483_647L;
  14. private const long FiveHundredAndTwelveMegaByte = 536_870_911L;
  15. private static readonly string[] _byteHumanizedSuffixes = ["B", "KB", "MB", "GB", "TB", "PB", "EB"];
  16. /// <summary>
  17. /// Tests the available storage capacity on the jellyfin paths with estimated minimum values.
  18. /// </summary>
  19. /// <param name="applicationPaths">The application paths.</param>
  20. /// <param name="logger">Logger.</param>
  21. public static void TestCommonPathsForStorageCapacity(IApplicationPaths applicationPaths, ILogger logger)
  22. {
  23. TestDataDirectorySize(applicationPaths.DataPath, logger, TwoGigabyte);
  24. TestDataDirectorySize(applicationPaths.LogDirectoryPath, logger, FiveHundredAndTwelveMegaByte);
  25. TestDataDirectorySize(applicationPaths.CachePath, logger, TwoGigabyte);
  26. TestDataDirectorySize(applicationPaths.ProgramDataPath, logger, TwoGigabyte);
  27. TestDataDirectorySize(applicationPaths.TempDirectory, logger, TwoGigabyte);
  28. }
  29. /// <summary>
  30. /// Gets the free space of a specific directory.
  31. /// </summary>
  32. /// <param name="path">Path to a folder.</param>
  33. /// <returns>The number of bytes available space.</returns>
  34. public static FolderStorageInfo GetFreeSpaceOf(string path)
  35. {
  36. try
  37. {
  38. var driveInfo = new DriveInfo(path);
  39. return new FolderStorageInfo()
  40. {
  41. Path = path,
  42. FreeSpace = driveInfo.AvailableFreeSpace,
  43. UsedSpace = driveInfo.TotalSize - driveInfo.AvailableFreeSpace,
  44. StorageType = driveInfo.DriveType.ToString(),
  45. DeviceId = driveInfo.Name,
  46. };
  47. }
  48. catch
  49. {
  50. return new FolderStorageInfo()
  51. {
  52. Path = path,
  53. FreeSpace = -1,
  54. UsedSpace = -1,
  55. StorageType = null,
  56. DeviceId = null
  57. };
  58. }
  59. }
  60. /// <summary>
  61. /// Gets the underlying drive data from a given path and checks if the available storage capacity matches the threshold.
  62. /// </summary>
  63. /// <param name="path">The path to a folder to evaluate.</param>
  64. /// <param name="logger">The logger.</param>
  65. /// <param name="threshold">The threshold to check for or -1 to just log the data.</param>
  66. /// <exception cref="InvalidOperationException">Thrown when the threshold is not available on the underlying storage.</exception>
  67. private static void TestDataDirectorySize(string path, ILogger logger, long threshold = -1)
  68. {
  69. logger.LogDebug("Check path {TestPath} for storage capacity", path);
  70. Directory.CreateDirectory(path);
  71. var drive = new DriveInfo(path);
  72. if (threshold != -1 && drive.AvailableFreeSpace < threshold)
  73. {
  74. throw new InvalidOperationException($"The path `{path}` has insufficient free space. Required: at least {HumanizeStorageSize(threshold)}.");
  75. }
  76. logger.LogInformation(
  77. "Storage path `{TestPath}` ({StorageType}) successfully checked with {FreeSpace} free which is over the minimum of {MinFree}.",
  78. path,
  79. drive.DriveType,
  80. HumanizeStorageSize(drive.AvailableFreeSpace),
  81. HumanizeStorageSize(threshold));
  82. }
  83. /// <summary>
  84. /// Formats a size in bytes into a common human readable form.
  85. /// </summary>
  86. /// <remarks>
  87. /// Taken and slightly modified from https://stackoverflow.com/a/4975942/1786007 .
  88. /// </remarks>
  89. /// <param name="byteCount">The size in bytes.</param>
  90. /// <returns>A human readable approximate representation of the argument.</returns>
  91. public static string HumanizeStorageSize(long byteCount)
  92. {
  93. if (byteCount == 0)
  94. {
  95. return $"0{_byteHumanizedSuffixes[0]}";
  96. }
  97. var bytes = Math.Abs(byteCount);
  98. var place = Convert.ToInt32(Math.Floor(Math.Log(bytes, 1024)));
  99. var num = Math.Round(bytes / Math.Pow(1024, place), 1);
  100. return (Math.Sign(byteCount) * num).ToString(CultureInfo.InvariantCulture) + _byteHumanizedSuffixes[place];
  101. }
  102. }