-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMimeType.cs
More file actions
142 lines (86 loc) · 3.8 KB
/
MimeType.cs
File metadata and controls
142 lines (86 loc) · 3.8 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Gsemac.IO {
public class MimeType :
IMimeType {
// Public members
public string Type { get; private set; }
public string Subtype { get; private set; }
public IDictionary<string, string> Parameters { get; } = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
public MimeType(string mimeType) {
if (string.IsNullOrEmpty(mimeType))
throw new ArgumentNullException(nameof(mimeType));
string[] parts = mimeType.Split(';');
if (!parts.Any())
throw new ArgumentException(Properties.ExceptionMessages.MalformedMimeType, nameof(mimeType));
ParseTypeAndSubtype(parts[0]);
if (parts.Count() > 1)
foreach (string parameter in parts.Skip(1).Where(part => !string.IsNullOrWhiteSpace(part)))
ParseParameter(parameter);
}
public MimeType(string type, string subtype) {
if (string.IsNullOrEmpty(type))
throw new ArgumentNullException(nameof(type));
if (string.IsNullOrEmpty(subtype))
throw new ArgumentNullException(nameof(subtype));
this.Type = type.Trim();
this.Subtype = subtype.Trim();
}
public override bool Equals(object obj) {
if (obj is null)
return false;
return ReferenceEquals(this, obj) ||
(obj is IMimeType mimeType && ToString().Equals(mimeType.ToString(), StringComparison.OrdinalIgnoreCase));
}
public override int GetHashCode() {
return ToString().GetHashCode();
}
public override string ToString() {
return ToString(withParameters: true);
}
public string ToString(bool withParameters) {
StringBuilder sb = new StringBuilder();
sb.Append(Type.ToLowerInvariant());
sb.Append('/');
sb.Append(Subtype.ToLowerInvariant());
if (withParameters && Parameters is object) {
foreach (var parameter in Parameters.Where(pair => !string.IsNullOrWhiteSpace(pair.Key) && !string.IsNullOrWhiteSpace(pair.Value))) {
sb.Append(';');
sb.Append(parameter.Key.Trim());
sb.Append('=');
sb.Append(parameter.Value.Trim());
}
}
return sb.ToString();
}
public static MimeType Parse(string mimeType) {
return new MimeType(mimeType);
}
public static bool TryParse(string mimeType, out MimeType result) {
try {
result = Parse(mimeType);
return true;
}
catch (ArgumentException) {
result = null;
return false;
}
}
// Private members
private void ParseTypeAndSubtype(string typeAndSubtype) {
string[] parts = typeAndSubtype.ToLowerInvariant().Split('/');
if (parts.Count() != 2 || parts.Any(part => string.IsNullOrWhiteSpace(part)))
throw new ArgumentException(Properties.ExceptionMessages.MalformedMimeType, nameof(typeAndSubtype));
this.Type = parts[0].Trim();
this.Subtype = parts[1].Trim();
}
private void ParseParameter(string parameter) {
string[] parts = parameter.Split('=');
if (parts.Count() != 2 || parts.Any(part => string.IsNullOrWhiteSpace(part)))
throw new ArgumentException(Properties.ExceptionMessages.MalformedMimeType, nameof(parameter));
Parameters[parts[0].Trim()] = parts[1].Trim();
}
}
}