This repository has been archived by the owner on Mar 30, 2023. It is now read-only.
forked from zone117x/MimeMapping
-
Notifications
You must be signed in to change notification settings - Fork 1
/
KnownMimeTypes.tt
198 lines (178 loc) · 6.27 KB
/
KnownMimeTypes.tt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Net.Http" #>
<#@ assembly name="Newtonsoft.Json" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Net.Http" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Text.RegularExpressions" #>
<#@ import namespace="Newtonsoft.Json" #>
<#@ import namespace="Newtonsoft.Json.Linq" #>
<#@ output extension="cs" #>
using System;
namespace MimeMapping
{
<#
const string MIMEDB_URL = "https://raw.githubusercontent.com/jshttp/mime-db/v1.52.0/db.json";
#>
///<summary>
/// MIME type constants. Last updated on <#= DateTime.UtcNow.ToString("s") + "Z" #>.
/// Generated from the <a href="<#= MIMEDB_URL #>">mime-db</a> source
///</summary>
public static class KnownMimeTypes
{
<#
Regex rgx = new Regex("[^a-zA-Z0-9]");
string GetMimeFieldName(string mimeKey)
{
var result = rgx.Replace(mimeKey[0].ToString().ToUpperInvariant() + mimeKey.Substring(1), "");
return char.IsDigit(result[0]) ? ("_" + result) : result;
}
string GetPageContent(string url)
{
using (var client = new HttpClient())
{
return client.GetStringAsync(url).GetAwaiter().GetResult();
}
}
IEnumerable<string[]> GetMimeTypesFromJson(string url, Func<JObject, IEnumerable<string[]>> processObject)
{
var content = GetPageContent(url);
if (string.IsNullOrEmpty(content)) return Enumerable.Empty<string[]>();
var resource = JObject.Parse(content);
return processObject(resource);
}
var entries = new List<string[]>();
entries.AddRange(GetMimeTypesFromJson(MIMEDB_URL, (resource) => {
return from mimeTypes in resource.Children()
let mimeType = ((JProperty) mimeTypes).Name
from mimeTypeProperties in mimeTypes.Children()
from mimeTypeProperty in mimeTypeProperties.Children()
from mimeTypeValue in mimeTypeProperty.Values()
where (mimeTypeProperty as JProperty)?.Name == "extensions"
select new[]
{
mimeType,
mimeTypeValue.Value<string>()
};
}));
// build dictionary from entries, outputing found duplicates
var _dict = new Dictionary<string, string>();
var _reverseDict = new Dictionary<string, List<string>>();
foreach (var parts in entries)
{
for (var i = 1; i < parts.Length; i++)
{
string existing;
if (_dict.TryGetValue(parts[i], out existing))
{
if (existing != parts[0])
{
#>
// Dupe for <#= parts[i] #>: using <#= existing #> vs <#= parts[0] #>
<#
}
}
else
{
_dict[parts[i]] = parts[0];
if (!_reverseDict.TryGetValue(parts[0], out var keyList))
{
keyList = new List<string>();
_reverseDict.Add(parts[0], keyList);
}
keyList.Add(parts[i]);
}
}
}
#>
// Generated <#= _reverseDict.Count #> unique mime type values
// Generated <#= _dict.Count #> type key pairs
<#
// Output constants for each type
foreach(var kp in _dict)
{
#>
///<summary><#= kp.Key #></summary>
public const string <#= GetMimeFieldName(kp.Key) #> = "<#= kp.Value #>";
<#
}
#>
///<summary>File extensions</summary>
public static class FileExtensions
{
<#
// List constant field names
foreach (var kp in _dict)
{
#>
///<summary><#= kp.Key #></summary>
public const string <#= !Char.IsLetter(kp.Key[0]) ? "_" : string.Empty #><#= char.ToUpper(kp.Key[0]) + kp.Key.Substring(1).Replace('-', '_') #> = "<#= kp.Key #>";
<#
}
#>
}
// List of all available extensions, used to build the dictionary
internal static readonly Lazy<string[]> ALL_EXTS = new Lazy<string[]>(() => new [] {
<#
// List constant field names
foreach (var kp in _dict)
{
#>
FileExtensions.<#= !Char.IsLetter(kp.Key[0]) ? "_" : string.Empty #><#= char.ToUpper(kp.Key[0]) + kp.Key.Substring(1).Replace('-', '_') #>,
<#
}
#>
});
// Switch-case instead of dictionary since it does the hashing at compile time rather than run time
internal static string LookupType(string type)
{
switch (type)
{
<#
// Output the actual literal C# dictionary
foreach (var kp in _reverseDict)
{
foreach (var mimeKey in kp.Value)
{
#>
case FileExtensions.<#= !Char.IsLetter(mimeKey[0]) ? "_" : string.Empty #><#= char.ToUpper(mimeKey[0]) + mimeKey.Substring(1).Replace('-', '_') #>:
<#
}
#>
return <#= GetMimeFieldName(kp.Value[0]) #>;
<#
}
#>
default:
return null;
}
}
// Switch-case instead of dictionary since it does the hashing at compile time rather than run time
internal static string[] LookupMimeType(string mimeType)
{
switch (mimeType)
{
<#
// Output the actual literal C# dictionary
foreach (var kp in _reverseDict)
{
var first = true;
foreach (var mimeKey in kp.Value)
{
#>
<#= first ? "" : "//" #>case <#= GetMimeFieldName(mimeKey) #>:
<#
first = false;
}
#> return new[] {<#= string.Join(", ", kp.Value.Select(x => $"FileExtensions.{(!Char.IsLetter(x[0]) ? "_" : string.Empty)}{Char.ToUpper(x[0])}{x.Substring(1).Replace('-', '_')}")) #>};
<#
}
#>
default:
return null;
}
}
}
}