Files
xCommons/Helpers/XFileHelper.cs
T

420 lines
11 KiB
C#

using System;
using System.Linq;
using xCommons.Models;
using System.Reflection;
using xCommons.Constants;
using xCommons.Extensions;
using xExceptions.Attributes;
using System.Collections.Generic;
namespace xCommons.Helpers
{
/// <summary>
/// File Helper ...
/// </summary>
public static class XFileHelper
{
/// <summary>
/// Cache Data ...
/// </summary>
private static readonly Lazy<IReadOnlyList<XFileTypeInfo>> _cache =
new(Build, isThreadSafe: true);
/// <summary>
/// Retrieve List ...
/// </summary>
/// <returns></returns>
public static IReadOnlyList<XFileTypeInfo> GetAll() => _cache.Value;
/// <summary>
/// Normalize File Extension ...
/// </summary>
/// <param name="extension"></param>
/// <returns></returns>
public static string NormalizeExtension(string extension)
{
//
var result = extension;
//
if (result.IsNullOrEmpty())
{
return result;
}
//
result = result.Trim();
if (result.IsNullOrEmpty())
{
return result;
}
//
if (result == ".")
{
//
result = string.Empty;
return result;
}
//
result = result.StartsWith(".")
? result.ToLowerInvariant()
: "." + result.ToLowerInvariant();
//
return result;
}
/// <summary>
/// Check a File Extension is Supported or not ...
/// </summary>
/// <param name="extension"></param>
/// <returns></returns>
public static bool IsSupported(string extension)
{
//
var info = GetByExtension(extension);
var result = !info.IsNull();
//
return result;
}
/// <summary>
/// Detect File Info based On Extension ...
/// </summary>
/// <param name="extension"></param>
/// <returns></returns>
public static XFileTypeInfo GetByExtension(string extension)
{
//
XFileTypeInfo result = null;
//
// Validate ...
if (extension.IsNullOrEmpty())
{
return result;
}
//
var normalized = NormalizeExtension(extension);
//
// Detect Item ...
result = GetAll()
.FirstOrDefault(x =>
x.Extension.Equals(
normalized,
StringComparison.OrdinalIgnoreCase));
//
return result;
}
/// <summary>
/// Detect File Info based on Mime Type ...
/// </summary>
/// <param name="mimeType"></param>
/// <returns></returns>
public static XFileTypeInfo GetByMimeType(string mimeType)
{
//
XFileTypeInfo result = null;
//
// Validate ...
if (mimeType.IsNullOrEmpty())
{
return result;
}
//
result = GetAll()
.FirstOrDefault(x =>
x.MimeType.Equals(
mimeType.Trim(),
StringComparison.OrdinalIgnoreCase));
//
return result;
}
/// <summary>
/// Detect File Info based on Enum Member Name ...
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static XFileTypeInfo GetByName(string name)
{
//
XFileTypeInfo result = null;
//
// Validate ...
if (name.IsNullOrEmpty())
{
return result;
}
//
result = GetAll()
.FirstOrDefault(x =>
x.Name.Equals(
name.Trim(),
StringComparison.OrdinalIgnoreCase));
//
return result;
}
/// <summary>
/// Detect File Info based on Enum Member Value ...
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static XFileTypeInfo GetByValue(int value)
{
//
var result = GetAll()
.FirstOrDefault(x => x.Value == value);
//
return result;
}
/// <summary>
/// Try Get File Info By Extension ...
/// </summary>
/// <param name="extension"></param>
/// <param name="result"></param>
/// <returns></returns>
public static bool TryGetByExtension(
string extension,
out XFileTypeInfo result
)
{
result = GetByExtension(extension);
return result != null;
}
/// <summary>
/// Try Get File Info By Mime Type ...
/// </summary>
/// <param name="mimeType"></param>
/// <param name="result"></param>
/// <returns></returns>
public static bool TryGetByMimeType(
string mimeType,
out XFileTypeInfo result
)
{
result = GetByMimeType(mimeType);
return result != null;
}
/// <summary>
/// Get Mime Type based on Extension ...
/// </summary>
/// <param name="extension"></param>
/// <returns></returns>
public static string GetMimeType(string extension)
{
//
var result = string.Empty;
var info = GetByExtension(extension);
if (!info.IsNull())
{
result = info.MimeType;
}
//
return result;
}
/// <summary>
/// Get Extension based on Mime Type ...
/// </summary>
/// <param name="mimeType"></param>
/// <returns></returns>
public static string GetExtension(string mimeType)
{
//
var result = string.Empty;
var info = GetByMimeType(mimeType);
if (!info.IsNull())
{
result = info.Extension;
}
//
return result;
}
/// <summary>
/// Get File Category based on Value ...
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static XFileCategory? GetCategory(int value)
{
//
if (value >= 100 && value < 200) return XFileCategory.Document;
if (value >= 200 && value < 300) return XFileCategory.Archive;
if (value >= 300 && value < 400) return XFileCategory.Internal;
if (value >= 400 && value < 500) return XFileCategory.Video;
if (value >= 500 && value < 600) return XFileCategory.Audio;
if (value >= 600 && value < 700) return XFileCategory.Image;
//
return null;
}
/// <summary>
/// Get File Category based on Extension ...
/// </summary>
/// <param name="extension"></param>
/// <returns></returns>
public static XFileCategory? GetCategory(string extension)
{
//
var info = GetByExtension(extension);
var result = !info.IsNull()
? GetCategory(info.Value)
: null;
//
return result;
}
/// <summary>
/// Get all Specified Category File Type Info ...
/// </summary>
/// <param name="category"></param>
/// <returns></returns>
public static IReadOnlyList<XFileTypeInfo> GetByCategory(XFileCategory category)
{
//
var min = (int)category;
var max = min + 100;
//
var result = GetAll()
.Where(x => x.Value >= min && x.Value < max)
.ToList()
.AsReadOnly();
//
return result;
}
/// <summary>
/// Get All Supported Extensions ...
/// </summary>
/// <returns></returns>
public static IReadOnlyList<string> GetExtensions()
{
//
var result = GetAll()
.Select(x => x.Extension)
.ToList()
.AsReadOnly();
//
return result;
}
/// <summary>
/// Get All Supported Mime Types ...
/// </summary>
/// <returns></returns>
public static IReadOnlyList<string> GetMimeTypes()
{
//
var result = GetAll()
.Select(x => x.MimeType)
.Where(m => !string.IsNullOrWhiteSpace(m))
.Distinct(StringComparer.OrdinalIgnoreCase)
.ToList()
.AsReadOnly();
//
return result;
}
//
#region Private ...
/// <summary>
/// Build List ...
/// </summary>
/// <returns></returns>
private static IReadOnlyList<XFileTypeInfo> Build()
{
//
var extensions = ReadEnum<XFileExtensions>();
var mimeTypes = ReadEnum<XFileMimeTypes>();
//
var list = new List<XFileTypeInfo>(extensions.Count);
foreach (var ext in extensions.Values.OrderBy(e => e.Value))
{
//
mimeTypes.TryGetValue(ext.Value, out var mime);
list.Add(new XFileTypeInfo
{
Name = ext.Name,
Value = ext.Value,
Extension = ext.StringValue,
MimeType = mime.StringValue ?? string.Empty
});
}
//
var result = list.AsReadOnly();
return result;
}
/// <summary>
/// Read Enum ...
/// </summary>
/// <typeparam name="TEnum"></typeparam>
/// <returns></returns>
private static Dictionary<int, EnumEntry> ReadEnum<TEnum>()
where TEnum : struct, Enum
{
//
var map = new Dictionary<int, EnumEntry>();
foreach (var field in typeof(TEnum).GetFields(BindingFlags.Public | BindingFlags.Static))
{
//
var attr = field.GetCustomAttribute<StringValueAttribute>();
if (attr == null) continue;
var value = Convert.ToInt32(field.GetValue(null));
//
map[value] = new EnumEntry
{
Value = value,
Name = field.Name,
StringValue = attr.StringValue
};
}
//
return map;
}
/// <summary>
/// Enum Entries for Read ...
/// </summary>
private struct EnumEntry
{
public int Value { get; set; }
public string Name { get; set; }
public string StringValue { get; set; }
}
#endregion
}
}