forked from gordon-matt/elFinder.NetCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMimeHelper.cs
More file actions
64 lines (54 loc) · 1.83 KB
/
Copy pathMimeHelper.cs
File metadata and controls
64 lines (54 loc) · 1.83 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
namespace elFinder.NetCore.Helpers
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
public static class MimeHelper
{
private static Dictionary<string, string> mimeTypes;
static MimeHelper()
{
mimeTypes = new Dictionary<string, string>();
var assembly = typeof(MimeHelper).GetTypeInfo().Assembly;
using (var stream = assembly.GetManifestResourceStream("elFinder.NetCore.MimeTypes.txt"))
using (var reader = new StreamReader(stream))
{
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
if (string.IsNullOrEmpty(line) || line.StartsWith("#"))
{
continue;
}
var parts = line.Trim().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length > 1)
{
var mime = parts[0];
for (var i = 1; i < parts.Length; i++)
{
var ext = parts[i].ToLower();
if (!mimeTypes.ContainsKey(ext))
{
mimeTypes.Add(ext, mime);
}
}
}
}
}
}
public static string GetMimeType(string extension)
{
string ext = extension.ToLower();
if (ext.StartsWith("."))
{
ext = ext.Substring(1);
}
if (mimeTypes.ContainsKey(ext))
{
return mimeTypes[ext];
}
return "unknown";
}
}
}