From 494fc9d7d1f08e05e1ee196bd0746900343b51dd Mon Sep 17 00:00:00 2001 From: ivarlovlie Date: Mon, 16 Jan 2023 00:13:54 +0100 Subject: feat: Fully implemented text/file api, almost finished autodelete service --- src/Tools.cs | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 src/Tools.cs (limited to 'src/Tools.cs') diff --git a/src/Tools.cs b/src/Tools.cs new file mode 100644 index 0000000..8aaedc9 --- /dev/null +++ b/src/Tools.cs @@ -0,0 +1,67 @@ +using System.Text.RegularExpressions; + +namespace BlobBin; + +public static class Tools +{ + public static string GetFilesDirectoryPath(bool createIfNotExists = false) { + var filesDirectoryPath = Path.Combine( + Directory.GetCurrentDirectory(), + "AppData", + "files" + ); + if (createIfNotExists) Directory.CreateDirectory(filesDirectoryPath); + return filesDirectoryPath; + } + + public static TimeSpan ParseHumanTimeSpan(string dateTime) { + var ts = TimeSpan.Zero; + var currentString = ""; + var currentNumber = ""; + foreach (var ch in dateTime + ' ') { + currentString += ch; + if (Regex.IsMatch(currentString, @"^(years(\d|\s)|year(\d|\s)|y(\d|\s))", RegexOptions.IgnoreCase)) { + ts = ts.Add(TimeSpan.FromDays(365 * int.Parse(currentNumber))); + currentString = ""; + currentNumber = ""; + } + + if (Regex.IsMatch(currentString, @"^(weeks(\d|\s)|week(\d|\s)|w(\d|\s))", RegexOptions.IgnoreCase)) { + ts = ts.Add(TimeSpan.FromDays(7 * int.Parse(currentNumber))); + currentString = ""; + currentNumber = ""; + } + + if (Regex.IsMatch(currentString, @"^(days(\d|\s)|day(\d|\s)|d(\d|\s))", RegexOptions.IgnoreCase)) { + ts = ts.Add(TimeSpan.FromDays(int.Parse(currentNumber))); + currentString = ""; + currentNumber = ""; + } + + if (Regex.IsMatch(currentString, @"^(hours(\d|\s)|hour(\d|\s)|h(\d|\s))", RegexOptions.IgnoreCase)) { + ts = ts.Add(TimeSpan.FromHours(int.Parse(currentNumber))); + currentString = ""; + currentNumber = ""; + } + + if (Regex.IsMatch(currentString, @"^(mins(\d|\s)|min(\d|\s)|m(\d|\s))", RegexOptions.IgnoreCase)) { + ts = ts.Add(TimeSpan.FromMinutes(int.Parse(currentNumber))); + currentString = ""; + currentNumber = ""; + } + + if (Regex.IsMatch(currentString, @"^(secs(\d|\s)|sec(\d|\s)|s(\d|\s))", RegexOptions.IgnoreCase)) { + ts = ts.Add(TimeSpan.FromSeconds(int.Parse(currentNumber))); + currentString = ""; + currentNumber = ""; + } + + if (Regex.IsMatch(ch.ToString(), @"\d")) { + currentNumber += ch; + currentString = ""; + } + } + + return ts; + } +} \ No newline at end of file -- cgit v1.3