This repository was archived by the owner on Dec 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 616
Expand file tree
/
Copy pathMimeTypes.cs
More file actions
453 lines (386 loc) · 13.2 KB
/
MimeTypes.cs
File metadata and controls
453 lines (386 loc) · 13.2 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
using System;
using System.Collections.Generic;
namespace ServiceStack;
public static class MimeTypes
{
public static Dictionary<string, string> ExtensionMimeTypes = new();
public const string Utf8Suffix = "; charset=utf-8";
public const string Html = "text/html";
public const string HtmlUtf8 = Html + Utf8Suffix;
public const string Css = "text/css";
public const string Xml = "application/xml";
public const string XmlText = "text/xml";
public const string Json = "application/json";
public const string ProblemJson = "application/problem+json";
public const string JsonText = "text/json";
public const string Jsv = "application/jsv";
public const string JsvText = "text/jsv";
public const string Csv = "text/csv";
public const string ProtoBuf = "application/x-protobuf";
public const string JavaScript = "text/javascript";
public const string WebAssembly = "application/wasm";
public const string Jar = "application/java-archive";
public const string Dmg = "application/x-apple-diskimage";
public const string Pkg = "application/x-newton-compatible-pkg";
public const string FormUrlEncoded = "application/x-www-form-urlencoded";
public const string MultiPartFormData = "multipart/form-data";
public const string JsonReport = "text/jsonreport";
public const string Soap11 = "text/xml; charset=utf-8";
public const string Soap12 = "application/soap+xml";
public const string Yaml = "application/yaml";
public const string YamlText = "text/yaml";
public const string PlainText = "text/plain";
public const string MarkdownText = "text/markdown";
public const string MsgPack = "application/x-msgpack";
public const string Wire = "application/x-wire";
public const string Compressed = "application/x-compressed";
public const string NetSerializer = "application/x-netserializer";
public const string Excel = "application/excel";
public const string MsWord = "application/msword";
public const string Cert = "application/x-x509-ca-cert";
public const string ImagePng = "image/png";
public const string ImageGif = "image/gif";
public const string ImageJpg = "image/jpeg";
public const string ImageSvg = "image/svg+xml";
public const string Bson = "application/bson";
public const string Binary = "application/octet-stream";
public const string ServerSentEvents = "text/event-stream";
public static string GetExtension(string mimeType)
{
switch (mimeType)
{
case ProtoBuf:
return ".pbuf";
}
var parts = mimeType.Split('/');
if (parts.Length == 1) return "." + parts[0].LeftPart('+').LeftPart(';');
if (parts.Length == 2) return "." + parts[1].LeftPart('+').LeftPart(';');
throw new NotSupportedException("Unknown mimeType: " + mimeType);
}
//Lower cases and trims left part of content-type prior ';'
public static string GetRealContentType(string contentType)
{
if (contentType == null)
return null;
int start = -1, end = -1;
for(int i=0; i < contentType.Length; i++)
{
if (!char.IsWhiteSpace(contentType[i]))
{
if (contentType[i] == ';')
break;
if (start == -1)
{
start = i;
}
end = i;
}
}
return start != -1
? contentType.Substring(start, end - start + 1).ToLowerInvariant()
: null;
}
/// <summary>
/// Case-insensitive, trimmed compare of two content types from start to ';', i.e. without charset suffix
/// </summary>
public static bool MatchesContentType(string contentType, string matchesContentType)
{
if (contentType == null || matchesContentType == null)
return false;
int start = -1, matchStart = -1, matchEnd = -1;
for (var i=0; i < contentType.Length; i++)
{
if (char.IsWhiteSpace(contentType[i]))
continue;
start = i;
break;
}
for (var i=0; i < matchesContentType.Length; i++)
{
if (char.IsWhiteSpace(matchesContentType[i]))
continue;
if (matchesContentType[i] == ';')
break;
if (matchStart == -1)
matchStart = i;
matchEnd = i;
}
return start != -1 && matchStart != -1 && matchEnd != -1
&& string.Compare(contentType, start,
matchesContentType, matchStart, matchEnd - matchStart + 1,
StringComparison.OrdinalIgnoreCase) == 0;
}
public static Func<string, bool?> IsBinaryFilter { get; set; }
public static bool IsBinary(string contentType)
{
var userFilter = IsBinaryFilter?.Invoke(contentType);
if (userFilter != null)
return userFilter.Value;
var realContentType = GetRealContentType(contentType);
switch (realContentType)
{
case ProtoBuf:
case MsgPack:
case Binary:
case Bson:
case Wire:
case Cert:
case Excel:
case MsWord:
case Compressed:
case WebAssembly:
case Jar:
case Dmg:
case Pkg:
return true;
}
// Text format exceptions to below heuristics
switch (realContentType)
{
case ImageSvg:
return false;
}
var primaryType = realContentType.LeftPart('/');
var secondaryType = realContentType.RightPart('/');
switch (primaryType)
{
case "image":
case "audio":
case "video":
return true;
}
if (secondaryType.StartsWith("pkc")
|| secondaryType.StartsWith("x-pkc")
|| secondaryType.StartsWith("font")
|| secondaryType.StartsWith("vnd.ms-"))
return true;
return false;
}
public static string GetMimeType(string fileNameOrExt)
{
if (string.IsNullOrEmpty(fileNameOrExt))
throw new ArgumentNullException(nameof(fileNameOrExt));
var fileExt = fileNameOrExt.LastRightPart('.').ToLower();
if (ExtensionMimeTypes.TryGetValue(fileExt, out var mimeType))
{
return mimeType;
}
switch (fileExt)
{
case "jpeg":
return "image/jpeg";
case "gif":
return "image/gif";
case "png":
return "image/png";
case "tiff":
return "image/tiff";
case "bmp":
return "image/bmp";
case "webp":
return "image/webp";
case "jpg":
return "image/jpeg";
case "tif":
return "image/tiff";
case "svg":
return ImageSvg;
case "ico":
return "image/x-icon";
case "htm":
case "html":
case "shtml":
return "text/html";
case "js":
return "text/javascript";
case "ts":
return "text/typescript";
case "jsx":
return "text/jsx";
case "csv":
return Csv;
case "css":
return Css;
case "cs":
return "text/x-csharp";
case "fs":
return "text/x-fsharp";
case "vb":
return "text/x-vb";
case "dart":
return "application/dart";
case "go":
return "text/x-go";
case "kt":
case "kts":
return "text/x-kotlin";
case "java":
return "text/x-java";
case "py":
return "text/x-python";
case "groovy":
case "gradle":
return "text/x-groovy";
case "yml":
case "yaml":
return YamlText;
case "sh":
return "text/x-sh";
case "bat":
case "cmd":
return "application/bat";
case "xml":
case "csproj":
case "fsproj":
case "vbproj":
return "text/xml";
case "txt":
case "ps1":
return "text/plain";
case "sgml":
return "text/sgml";
case "mp3":
return "audio/mpeg3";
case "au":
case "snd":
return "audio/basic";
case "aac":
case "ac3":
case "aiff":
case "m4a":
case "m4b":
case "m4p":
case "mid":
case "midi":
case "wav":
return "audio/" + fileExt;
case "qt":
case "mov":
return "video/quicktime";
case "mpg":
return "video/mpeg";
case "ogv":
return "video/ogg";
case "3gpp":
case "avi":
case "dv":
case "divx":
case "ogg":
case "mp4":
case "webm":
return "video/" + fileExt;
case "rtf":
return "application/" + fileExt;
case "xls":
case "xlt":
case "xla":
return Excel;
case "xlsx":
return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
case "xltx":
return "application/vnd.openxmlformats-officedocument.spreadsheetml.template";
case "doc":
case "dot":
return MsWord;
case "docx":
return "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
case "dotx":
return "application/vnd.openxmlformats-officedocument.wordprocessingml.template";
case "ppt":
case "oit":
case "pps":
case "ppa":
return "application/vnd.ms-powerpoint";
case "pptx":
return "application/vnd.openxmlformats-officedocument.presentationml.presentation";
case "potx":
return "application/vnd.openxmlformats-officedocument.presentationml.template";
case "ppsx":
return "application/vnd.openxmlformats-officedocument.presentationml.slideshow";
case "mdb":
return "application/vnd.ms-access";
case "cer":
case "crt":
case "der":
return Cert;
case "p10":
return "application/pkcs10";
case "p12":
return "application/x-pkcs12";
case "p7b":
case "spc":
return "application/x-pkcs7-certificates";
case "p7c":
case "p7m":
return "application/pkcs7-mime";
case "p7r":
return "application/x-pkcs7-certreqresp";
case "p7s":
return "application/pkcs7-signature";
case "sst":
return "application/vnd.ms-pki.certstore";
case "gz":
case "tgz":
case "zip":
case "rar":
case "lzh":
case "z":
return Compressed;
case "eot":
return "application/vnd.ms-fontobject";
case "ttf":
return "application/octet-stream";
case "woff":
return "application/font-woff";
case "woff2":
return "application/font-woff2";
case "jar":
return Jar;
case "aaf":
case "aca":
case "asd":
case "bin":
case "cab":
case "chm":
case "class":
case "cur":
case "db":
case "dat":
case "deploy":
case "dll":
case "dsp":
case "exe":
case "fla":
case "ics":
case "inf":
case "mix":
case "msi":
case "mso":
case "obj":
case "ocx":
case "prm":
case "prx":
case "psd":
case "psp":
case "qxd":
case "sea":
case "snp":
case "so":
case "sqlite":
case "toc":
case "u32":
case "xmp":
case "xsn":
case "xtp":
return Binary;
case "wasm":
return WebAssembly;
case "dmg":
return Dmg;
case "pkg":
return Pkg;
default:
return "application/" + fileExt;
}
}
}