You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.6 KiB

using NPOI.SS.UserModel;
using System;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
namespace SiteLibrary
{
public static class ObjectExtensions
{
public static string[] SplitBySpace(this string input)
{
return input.Split(' ').Where(o => o != null).Select(o => o.Trim()).Where(o => !string.IsNullOrEmpty(o)).ToArray();
}
public static string ToText(this ICell cell)
{
return (cell?.ToString() ?? "").Trim();
}
public static int? ToInt(this ICell cell)
{
if(int.TryParse(cell.ToText(), out int result))
{
return result;
}
return null;
}
public static string Md5(this string file)
{
using var md5 = MD5.Create();
using var stream = File.OpenRead(file);
var hash = md5.ComputeHash(stream);
stream.Dispose();
return BitConverter.ToString(hash).Replace("-", "").ToLower();
}
public static string ExtToLower(this string file)
{
if (!string.IsNullOrEmpty(file))
{
var ext = Path.GetExtension(file);
var extLower = ext.ToLower();
if (ext != extLower)
{
return Path.Combine(Path.GetDirectoryName(file), Path.GetFileNameWithoutExtension(file) + extLower).Replace("\\", "/");
}
}
return file;
}
}
}