| | 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 | |
|
| | 8 | | namespace 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> |
| | 13 | | public static class StorageHelper |
| | 14 | | { |
| | 15 | | private const long TwoGigabyte = 2_147_483_647L; |
| | 16 | | private const long FiveHundredAndTwelveMegaByte = 536_870_911L; |
| 0 | 17 | | 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 | | { |
| 0 | 26 | | TestDataDirectorySize(applicationPaths.DataPath, logger, TwoGigabyte); |
| 0 | 27 | | TestDataDirectorySize(applicationPaths.LogDirectoryPath, logger, FiveHundredAndTwelveMegaByte); |
| 0 | 28 | | TestDataDirectorySize(applicationPaths.CachePath, logger, TwoGigabyte); |
| 0 | 29 | | TestDataDirectorySize(applicationPaths.ProgramDataPath, logger, TwoGigabyte); |
| 0 | 30 | | TestDataDirectorySize(applicationPaths.TempDirectory, logger, TwoGigabyte); |
| 0 | 31 | | } |
| | 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 | | { |
| 0 | 42 | | var driveInfo = new DriveInfo(path); |
| 0 | 43 | | return new FolderStorageInfo() |
| 0 | 44 | | { |
| 0 | 45 | | Path = path, |
| 0 | 46 | | FreeSpace = driveInfo.AvailableFreeSpace, |
| 0 | 47 | | UsedSpace = driveInfo.TotalSize - driveInfo.AvailableFreeSpace, |
| 0 | 48 | | StorageType = driveInfo.DriveType.ToString(), |
| 0 | 49 | | DeviceId = driveInfo.Name, |
| 0 | 50 | | }; |
| | 51 | | } |
| 0 | 52 | | catch |
| | 53 | | { |
| 0 | 54 | | return new FolderStorageInfo() |
| 0 | 55 | | { |
| 0 | 56 | | Path = path, |
| 0 | 57 | | FreeSpace = -1, |
| 0 | 58 | | UsedSpace = -1, |
| 0 | 59 | | StorageType = null, |
| 0 | 60 | | DeviceId = null |
| 0 | 61 | | }; |
| | 62 | | } |
| 0 | 63 | | } |
| | 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 | | { |
| 0 | 74 | | logger.LogDebug("Check path {TestPath} for storage capacity", path); |
| 0 | 75 | | var drive = new DriveInfo(path); |
| 0 | 76 | | if (threshold != -1 && drive.AvailableFreeSpace < threshold) |
| | 77 | | { |
| 0 | 78 | | throw new InvalidOperationException($"The path `{path}` has insufficient free space. Required: at least {Hum |
| | 79 | | } |
| | 80 | |
|
| 0 | 81 | | logger.LogInformation( |
| 0 | 82 | | "Storage path `{TestPath}` ({StorageType}) successfully checked with {FreeSpace} free which is over the mini |
| 0 | 83 | | path, |
| 0 | 84 | | drive.DriveType, |
| 0 | 85 | | HumanizeStorageSize(drive.AvailableFreeSpace), |
| 0 | 86 | | HumanizeStorageSize(threshold)); |
| 0 | 87 | | } |
| | 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 | | { |
| 0 | 99 | | if (byteCount == 0) |
| | 100 | | { |
| 0 | 101 | | return $"0{_byteHumanizedSuffixes[0]}"; |
| | 102 | | } |
| | 103 | |
|
| 0 | 104 | | var bytes = Math.Abs(byteCount); |
| 0 | 105 | | var place = Convert.ToInt32(Math.Floor(Math.Log(bytes, 1024))); |
| 0 | 106 | | var num = Math.Round(bytes / Math.Pow(1024, place), 1); |
| 0 | 107 | | return (Math.Sign(byteCount) * num).ToString(CultureInfo.InvariantCulture) + _byteHumanizedSuffixes[place]; |
| | 108 | | } |
| | 109 | | } |