-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpMimeTypes.cs
More file actions
44 lines (37 loc) · 1.12 KB
/
HttpMimeTypes.cs
File metadata and controls
44 lines (37 loc) · 1.12 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
using System;
using System.Collections.Generic;
using System.IO;
namespace SimpleHttp
{
public static class HttpMimeTypes
{
static readonly Dictionary<string, string> mimeTypes;
static HttpMimeTypes()
{
mimeTypes = new Dictionary<string, string>();
using (var reader = new StreamReader(new MemoryStream(Properties.Resources.MimeTypes, false)))
{
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
if (String.IsNullOrEmpty(line) || line.StartsWith("#"))
continue;
var lineParts = line.Split(new[] { '\t' }, StringSplitOptions.RemoveEmptyEntries);
var mimeType = lineParts[0];
var extensions = lineParts[1].Split(' ');
foreach (var extension in extensions)
{
if (!mimeTypes.ContainsKey(extension))
mimeTypes.Add(extension, mimeType);
}
}
}
}
public static string GetByExtension(string extension)
{
extension = extension.TrimStart('.');
return mimeTypes.ContainsKey(extension) ? mimeTypes[extension] : "application/octet-stream";
}
public static string GetByPath(string path) => GetByExtension(Path.GetExtension(path));
}
}