MimeMapper.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System.Collections.Generic;
  2. using System.Text.RegularExpressions;
  3. namespace Masuit.Tools.AspNetCore.Mime
  4. {
  5. /// <summary>
  6. /// 默认MIME映射器,可以根据文件扩展名获取标准内容类型。
  7. /// </summary>
  8. public class MimeMapper : IMimeMapper
  9. {
  10. /// <summary>
  11. /// 默认Mime - 如果没有找到任何其他映射则作为默认的Mime-Type
  12. /// </summary>
  13. public const string DefaultMime = "application/octet-stream";
  14. /// <summary>
  15. /// 在文件路径中搜索文件扩展名的默认正则表达式
  16. /// </summary>
  17. private readonly Regex _pathExtensionPattern = new Regex("\\.(\\w*)$");
  18. /// <summary>
  19. /// 扩展的Mime类型的默认字典(Content types)
  20. /// </summary>
  21. private static Dictionary<string, string> _items;
  22. /// <summary>
  23. ///
  24. /// </summary>
  25. public MimeMapper() : this(null)
  26. {
  27. }
  28. /// <summary>
  29. ///
  30. /// </summary>
  31. /// <param name="extensions"></param>
  32. public MimeMapper(params MimeMappingItem[] extensions)
  33. {
  34. _items = new Dictionary<string, string>();
  35. foreach (var mapping in DefaultMimeItems.Items)
  36. {
  37. _items.Add(mapping.Extension, mapping.MimeType);
  38. }
  39. Extend(extensions);
  40. }
  41. /// <summary>
  42. /// 扩展mime映射规则的标准列表。扩展的具有更高的优先级 - 如果扩展具有与标准项相同的扩展名,则会覆盖默认的mime
  43. /// </summary>
  44. /// <param name="extensions"></param>
  45. /// <returns></returns>
  46. public IMimeMapper Extend(params MimeMappingItem[] extensions)
  47. {
  48. if (extensions != null)
  49. {
  50. foreach (var mapping in extensions)
  51. {
  52. if (_items.ContainsKey(mapping.Extension))
  53. {
  54. _items[mapping.Extension] = mapping.MimeType;
  55. }
  56. else
  57. {
  58. _items.Add(mapping.Extension, mapping.MimeType);
  59. }
  60. }
  61. }
  62. return this;
  63. }
  64. /// <summary>
  65. /// 返回特定文件扩展名的Content-Type,如果未找到任何对应关系,则返回默认值
  66. /// </summary>
  67. /// <param name="fileExtension"></param>
  68. /// <returns></returns>
  69. public string GetMimeFromExtension(string fileExtension)
  70. {
  71. fileExtension = (fileExtension ?? string.Empty).ToLower();
  72. fileExtension = fileExtension.Trim().StartsWith(".") ? fileExtension.Replace(".", "") : fileExtension;
  73. return _items.ContainsKey(fileExtension) ? _items[fileExtension] : DefaultMime;
  74. }
  75. /// <summary>
  76. /// 根据路径获取MimeType
  77. /// </summary>
  78. /// <param name="path"></param>
  79. /// <returns></returns>
  80. public string GetMimeFromPath(string path)
  81. {
  82. var extension = GetExtension(path);
  83. return GetMimeFromExtension(extension);
  84. }
  85. /// <summary>
  86. /// 获取扩展名
  87. /// </summary>
  88. /// <param name="path"></param>
  89. /// <returns></returns>
  90. protected string GetExtension(string path)
  91. {
  92. var match = _pathExtensionPattern.Match(path ?? string.Empty);
  93. if (match.Groups.Count > 1)
  94. {
  95. return match.Groups[1].Value;
  96. }
  97. return null;
  98. }
  99. }
  100. }