StorageHelper.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 static readonly string[] _byteHumanizedSuffixes = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"];
  15. /// <summary>
  16. /// Tests the available storage capacity on the jellyfin paths with estimated minimum values.
  17. /// </summary>
  18. /// <param name="applicationPaths">The application paths.</param>
  19. /// <param name="logger">Logger.</param>
  20. public static void TestCommonPathsForStorageCapacity(IApplicationPaths applicationPaths, ILogger logger)
  21. {
  22. TestDataDirectorySize(applicationPaths.DataPath, logger, TwoGigabyte);
  23. TestDataDirectorySize(applicationPaths.CachePath, logger, TwoGigabyte);
  24. TestDataDirectorySize(applicationPaths.ProgramDataPath, logger, TwoGigabyte);
  25. }
  26. /// <summary>
  27. /// Gets the free space of a specific directory.
  28. /// </summary>
  29. /// <param name="path">Path to a folder.</param>
  30. /// <returns>The number of bytes available space.</returns>
  31. public static FolderStorageInfo GetFreeSpaceOf(string path)
  32. {
  33. try
  34. {
  35. var driveInfo = new DriveInfo(path);
  36. return new FolderStorageInfo()
  37. {
  38. Path = path,
  39. FreeSpace = driveInfo.AvailableFreeSpace,
  40. UsedSpace = driveInfo.TotalSize - driveInfo.AvailableFreeSpace,
  41. StorageType = driveInfo.DriveType.ToString(),
  42. DeviceId = driveInfo.Name,
  43. };
  44. }
  45. catch
  46. {
  47. return new FolderStorageInfo()
  48. {
  49. Path = path,
  50. FreeSpace = -1,
  51. UsedSpace = -1,
  52. StorageType = null,
  53. DeviceId = null
  54. };
  55. }
  56. }
  57. /// <summary>
  58. /// Gets the underlying drive data from a given path and checks if the available storage capacity matches the threshold.
  59. /// </summary>
  60. /// <param name="path">The path to a folder to evaluate.</param>
  61. /// <param name="logger">The logger.</param>
  62. /// <param name="threshold">The threshold to check for or -1 to just log the data.</param>
  63. /// <exception cref="InvalidOperationException">Thrown when the threshold is not available on the underlying storage.</exception>
  64. private static void TestDataDirectorySize(string path, ILogger logger, long threshold = -1)
  65. {
  66. logger.LogDebug("Check path {TestPath} for storage capacity", path);
  67. Directory.CreateDirectory(path);
  68. var drive = new DriveInfo(path);
  69. if (threshold != -1 && drive.AvailableFreeSpace < threshold)
  70. {
  71. throw new InvalidOperationException($"The path `{path}` has insufficient free space. Available: {HumanizeStorageSize(drive.AvailableFreeSpace)}, Required: {HumanizeStorageSize(threshold)}.");
  72. }
  73. logger.LogInformation(
  74. "Storage path `{TestPath}` ({StorageType}) successfully checked with {FreeSpace} free which is over the minimum of {MinFree}.",
  75. path,
  76. drive.DriveType,
  77. HumanizeStorageSize(drive.AvailableFreeSpace),
  78. HumanizeStorageSize(threshold));
  79. }
  80. /// <summary>
  81. /// Formats a size in bytes into a common human readable form.
  82. /// </summary>
  83. /// <remarks>
  84. /// Taken and slightly modified from https://stackoverflow.com/a/4975942/1786007 .
  85. /// </remarks>
  86. /// <param name="byteCount">The size in bytes.</param>
  87. /// <returns>A human readable approximate representation of the argument.</returns>
  88. public static string HumanizeStorageSize(long byteCount)
  89. {
  90. if (byteCount == 0)
  91. {
  92. return $"0{_byteHumanizedSuffixes[0]}";
  93. }
  94. var bytes = Math.Abs(byteCount);
  95. var place = Convert.ToInt32(Math.Floor(Math.Log(bytes, 1024)));
  96. var num = Math.Round(bytes / Math.Pow(1024, place), 1);
  97. return (Math.Sign(byteCount) * num).ToString(CultureInfo.InvariantCulture) + _byteHumanizedSuffixes[place];
  98. }
  99. }