decePubClient/Helpers/SUtility.cs
Eugenio Chiodo ba4a4b14c8 Saving
2022-02-14 01:51:37 +01:00

106 lines
2.3 KiB
C#

using collAnon.Shared;
using System;
using System.IO;
using System.Net.Mail;
using System.Text.Json;
namespace collAnon.Client.Helpers
{
public static class SUtility
{
public static T IfTrueThen<T>(bool conditionIsTrue, T ifTrue, T ifFalse = default)
{
if (conditionIsTrue)
return ifTrue;
else
return ifFalse;
}
public static bool TimeBetween(this DateTime datetime, TimeSpan start, TimeSpan end)
{
// convert datetime to a TimeSpan
var now = datetime.TimeOfDay;
// see if start comes before end
if (start < end)
return start <= now && now <= end;
// start is after end, so do the inverse comparison
return !(end < now && now < start);
}
public static bool CacheHasExpired(long? lastTimeCacheTimeTicks)
{
if (!lastTimeCacheTimeTicks.HasValue) return true;
return (DateTime.Now.Ticks - lastTimeCacheTimeTicks.Value) > VConstants.CacheExpirationPeriod.Ticks;
}
public static string GetFileIcon(string fileName)
{
switch (Path.GetExtension(fileName))
{
case ".odp":
case ".pptx":
return "file-powerpoint";
case ".ods":
case ".xlsx":
return "file-excel";
case ".odt":
case ".docx":
return "file-word";
case ".pdf":
return "file-pdf";
case ".jpg":
case ".jpeg":
case ".png":
return "file-image";
default:
return "file";
}
}
public static string GetMissingMimeType(string fileName)
{
switch (Path.GetExtension(fileName))
{
case ".docx":
return "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
case ".xlsx":
return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
default:
return string.Empty;
}
}
public static bool IsValidEmail(string email)
{
try
{
var addr = new MailAddress(email);
return addr.Address == email;
}
catch
{
return false;
}
}
public static string GetQrCodeBase64(string base64String) => $"data:image/png;base64,{base64String}";
public static int GetRand()
{
var random = new Random(DateTime.Now.Millisecond);
return random.Next(0, random.Next(10, 1000));
}
public static T Deserialize<T>(string value)
{
return JsonSerializer.Deserialize<T>(value, new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
}
}
}