using System; using System.IO; using System.IO.Compression; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; using Newtonsoft.Json; namespace GB5Shared.GB5CommonFunction { public class GB5CommonFunction : IGB5CommonFunction { // For string input public async Task GZipCompress(string uncompressedString) { byte[] inputBytes = Encoding.UTF8.GetBytes(uncompressedString); using (var outputStream = new MemoryStream()) { using (var gzipStream = new GZipStream(outputStream, CompressionMode.Compress, leaveOpen: true)) { await gzipStream.WriteAsync(inputBytes, 0, inputBytes.Length); } return Convert.ToBase64String(outputStream.ToArray()); } } // For generic object input (auto-serialize to JSON) public async Task GZipCompress(T inputObject) { string json = JsonConvert.SerializeObject(inputObject); return await GZipCompress(json); // reuse string version } // Decompression public async Task Decompress(string compressedBase64) { byte[] compressedBytes = Convert.FromBase64String(compressedBase64); using (var inputStream = new MemoryStream(compressedBytes)) using (var gzipStream = new GZipStream(inputStream, CompressionMode.Decompress)) using (var outputStream = new MemoryStream()) { await gzipStream.CopyToAsync(outputStream); return Encoding.UTF8.GetString(outputStream.ToArray()); } } // Decompression + deserialize to object public async Task GZipDecompress(string compressedBase64) { string json = await Decompress(compressedBase64); return JsonConvert.DeserializeObject(json)!; } public async Task GZipDecompress(string compressedBase64) { string json = await Decompress(compressedBase64); return json; } public async Task QueryReplaceForTotal(string Query) { try { string[] separator = new string[1] { "group by" }; string[] array = Query.Split(separator, StringSplitOptions.None); if (array.Length == 2) { Query = array[0]; } else { separator = new string[1] { "order by" }; array = Query.Split(separator, StringSplitOptions.None); if (array.Length == 2) { Query = array[0]; } } array = Query.Split(Environment.NewLine.ToCharArray()); bool flag = true; string text = ""; if (array.Length == 1) { separator = new string[1] { " from " }; array = Query.Split(separator, StringSplitOptions.None); if (array.Length == 2) { return "Select Count(*) From " + array[1]; } } for (int i = 0; i < array.Length; i++) { if (i == 0) { text = "Select Count(*) From "; } else if (array[i] != "from" && !flag) { text = text + " " + array[i]; } else if (array[i].ToString().Trim() == "from") { flag = false; } } return text; } catch (Exception) { throw ; } } public async Task CountArrayAsync(IEnumerable array) { try { return await Task.Run(() => array?.Count() ?? 0).ConfigureAwait(false); } catch (Exception) { return 0; } } public async Task CountConditionalAsync(IEnumerable collection, Func predicate) { try { if (collection == null) { return 0; } if (predicate == null) { throw new ArgumentNullException(nameof(predicate)); } return await Task.Run(() => collection.Count(predicate)).ConfigureAwait(false); } catch (ArgumentNullException) { throw; } catch (Exception ) { return 0; } } public async Task FromEpoch(long unixTime) { return new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc) .AddSeconds(unixTime); } public object? GetPropertyValue(object obj, string propertyName) { var prop = obj.GetType().GetProperty(propertyName); return prop?.GetValue(obj); } } }