using Microsoft.AspNetCore.StaticFiles;
using Newtonsoft.Json.Linq;
using System.IO;
using Vit.Core.Util.ConfigurationManager;
namespace Vit.WebHost
{
public class WebHostHelp
{
#region BuildContentTypeProvider
///
/// 静态文件类型映射配置的文件路径。可为相对路径或绝对路径。例如"contentTypeMap.json"。若不指定(或指定的文件不存在)则返回null
///
///
///
public static FileExtensionContentTypeProvider BuildContentTypeProvider(string contentTypeMapFile)
{
if (string.IsNullOrWhiteSpace(contentTypeMapFile))
{
return null;
}
var jsonFile = new JsonFile(contentTypeMapFile);
if (File.Exists(jsonFile.configPath))
{
var provider = new FileExtensionContentTypeProvider();
if (jsonFile.root is JObject jo)
{
var map = provider.Mappings;
foreach (var item in jo)
{
map.Remove(item.Key);
map[item.Key] = item.Value.Value();
}
}
return provider;
}
return null;
}
#endregion
}
}