using Microsoft.AspNetCore.StaticFiles; namespace BotSharp.Abstraction.Files.Utilities; public static class FileUtility { /// /// Get file bytes and content type from data, e.g., "data:image/png;base64,aaaaaaaaa" /// /// /// public static (string, byte[]) GetFileInfoFromData(string data) { if (string.IsNullOrEmpty(data)) { return (string.Empty, new byte[0]); } var typeStartIdx = data.IndexOf(':'); var typeEndIdx = data.IndexOf(';'); var contentType = data.Substring(typeStartIdx + 1, typeEndIdx - typeStartIdx - 1); var base64startIdx = data.IndexOf(','); var base64Str = data.Substring(base64startIdx + 1); return (contentType, Convert.FromBase64String(base64Str)); } public static string GetFileContentType(string filePath) { string contentType; var provider = new FileExtensionContentTypeProvider(); if (!provider.TryGetContentType(filePath, out contentType)) { contentType = string.Empty; } return contentType; } }