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
{
///
/// File Helper ...
///
public static class XFileHelper
{
///
/// Cache Data ...
///
private static readonly Lazy> _cache =
new(Build, isThreadSafe: true);
///
/// Retrieve List ...
///
///
public static IReadOnlyList GetAll() => _cache.Value;
///
/// Normalize File Extension ...
///
///
///
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;
}
///
/// Check a File Extension is Supported or not ...
///
///
///
public static bool IsSupported(string extension)
{
//
var info = GetByExtension(extension);
var result = !info.IsNull();
//
return result;
}
///
/// Detect File Info based On Extension ...
///
///
///
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;
}
///
/// Detect File Info based on Mime Type ...
///
///
///
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;
}
///
/// Detect File Info based on Enum Member Name ...
///
///
///
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;
}
///
/// Detect File Info based on Enum Member Value ...
///
///
///
public static XFileTypeInfo GetByValue(int value)
{
//
var result = GetAll()
.FirstOrDefault(x => x.Value == value);
//
return result;
}
///
/// Try Get File Info By Extension ...
///
///
///
///
public static bool TryGetByExtension(
string extension,
out XFileTypeInfo result
)
{
result = GetByExtension(extension);
return result != null;
}
///
/// Try Get File Info By Mime Type ...
///
///
///
///
public static bool TryGetByMimeType(
string mimeType,
out XFileTypeInfo result
)
{
result = GetByMimeType(mimeType);
return result != null;
}
///
/// Get Mime Type based on Extension ...
///
///
///
public static string GetMimeType(string extension)
{
//
var result = string.Empty;
var info = GetByExtension(extension);
if (!info.IsNull())
{
result = info.MimeType;
}
//
return result;
}
///
/// Get Extension based on Mime Type ...
///
///
///
public static string GetExtension(string mimeType)
{
//
var result = string.Empty;
var info = GetByMimeType(mimeType);
if (!info.IsNull())
{
result = info.Extension;
}
//
return result;
}
///
/// Get File Category based on Value ...
///
///
///
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;
}
///
/// Get File Category based on Extension ...
///
///
///
public static XFileCategory? GetCategory(string extension)
{
//
var info = GetByExtension(extension);
var result = !info.IsNull()
? GetCategory(info.Value)
: null;
//
return result;
}
///
/// Get all Specified Category File Type Info ...
///
///
///
public static IReadOnlyList 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;
}
///
/// Get All Supported Extensions ...
///
///
public static IReadOnlyList GetExtensions()
{
//
var result = GetAll()
.Select(x => x.Extension)
.ToList()
.AsReadOnly();
//
return result;
}
///
/// Get All Supported Mime Types ...
///
///
public static IReadOnlyList GetMimeTypes()
{
//
var result = GetAll()
.Select(x => x.MimeType)
.Where(m => !string.IsNullOrWhiteSpace(m))
.Distinct(StringComparer.OrdinalIgnoreCase)
.ToList()
.AsReadOnly();
//
return result;
}
//
#region Private ...
///
/// Build List ...
///
///
private static IReadOnlyList Build()
{
//
var extensions = ReadEnum();
var mimeTypes = ReadEnum();
//
var list = new List(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;
}
///
/// Read Enum ...
///
///
///
private static Dictionary ReadEnum()
where TEnum : struct, Enum
{
//
var map = new Dictionary();
foreach (var field in typeof(TEnum).GetFields(BindingFlags.Public | BindingFlags.Static))
{
//
var attr = field.GetCustomAttribute();
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;
}
///
/// Enum Entries for Read ...
///
private struct EnumEntry
{
public int Value { get; set; }
public string Name { get; set; }
public string StringValue { get; set; }
}
#endregion
}
}