From 00bf436b53e30791c1f709a42c28493e0ba9206d Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Fri, 22 May 2020 18:17:33 +0000 Subject: [PATCH] --- server/AyaNova/util/FileUtil.cs | 63 +++++++++++++++++++++++++++++---- 1 file changed, 57 insertions(+), 6 deletions(-) diff --git a/server/AyaNova/util/FileUtil.cs b/server/AyaNova/util/FileUtil.cs index 1de9d981..356b2c73 100644 --- a/server/AyaNova/util/FileUtil.cs +++ b/server/AyaNova/util/FileUtil.cs @@ -102,7 +102,7 @@ namespace AyaNova.Util public class BackupFileInfo { - public long length { get; set; } + public string length { get; set; } public string Name { get; set; } public DateTime Created { get; set; } @@ -110,11 +110,11 @@ namespace AyaNova.Util public class BackupStatus { - public long AvailableFreeSpace { get; set; } + public string AvailableFreeSpace { get; set; } public List BackupFiles { get; set; } public BackupStatus() { - AvailableFreeSpace = 0; + AvailableFreeSpace = null; BackupFiles = new List(); } } @@ -129,11 +129,11 @@ namespace AyaNova.Util BackupStatus statusReport = new BackupStatus(); try { - statusReport.AvailableFreeSpace = new System.IO.DriveInfo(Path.GetPathRoot(UtilityFilesFolder)).AvailableFreeSpace; + statusReport.AvailableFreeSpace = GetBytesReadable(new System.IO.DriveInfo(Path.GetPathRoot(UtilityFilesFolder)).AvailableFreeSpace); } catch (Exception ex) { - statusReport.AvailableFreeSpace = -1; + statusReport.AvailableFreeSpace = "ERROR"; ILogger log = AyaNova.Util.ApplicationLogging.CreateLogger("FileUtil::BackupStatus"); log.LogError(ex, "FileUtil::BackupStatusReport error getting available space"); } @@ -146,7 +146,7 @@ namespace AyaNova.Util statusReport.BackupFiles.Add(new BackupFileInfo() { Name = Path.GetFileName(file), - length = fi.Length, + length = GetBytesReadable(fi.Length), Created = fi.CreationTimeUtc }); } @@ -529,6 +529,57 @@ namespace AyaNova.Util #region General utilities + //https://stackoverflow.com/a/11124118/8939 + // Returns the human-readable file size for an arbitrary, 64-bit file size + // The default format is "0.### XB", e.g. "4.2 KB" or "1.434 GB" + public static string GetBytesReadable(long i) + { + // Get absolute value + long absolute_i = (i < 0 ? -i : i); + // Determine the suffix and readable value + string suffix; + double readable; + if (absolute_i >= 0x1000000000000000) // Exabyte + { + suffix = "EB"; + readable = (i >> 50); + } + else if (absolute_i >= 0x4000000000000) // Petabyte + { + suffix = "PB"; + readable = (i >> 40); + } + else if (absolute_i >= 0x10000000000) // Terabyte + { + suffix = "TB"; + readable = (i >> 30); + } + else if (absolute_i >= 0x40000000) // Gigabyte + { + suffix = "GB"; + readable = (i >> 20); + } + else if (absolute_i >= 0x100000) // Megabyte + { + suffix = "MB"; + readable = (i >> 10); + } + else if (absolute_i >= 0x400) // Kilobyte + { + suffix = "KB"; + readable = i; + } + else + { + return i.ToString("0 B"); // Byte + } + // Divide by 1024 to get fractional value + readable = (readable / 1024); + // Return formatted number with suffix + return readable.ToString("0.### ") + suffix; + } + + /// /// Attachments / user files folder size info ///