-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglobals.cs
More file actions
142 lines (139 loc) · 6.18 KB
/
globals.cs
File metadata and controls
142 lines (139 loc) · 6.18 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
global using System;
global using System.Net;
global using System.Linq;
global using System.Threading.Tasks;
global using System.Collections.Generic;
global using Context = System.Net.HttpListenerContext;
using System.IO;
using System.Text;
namespace Webtech3;
public static class Extensions {
extension(FileInfo self) {
public string MimeType {
get {
var ext = self.Extension.TrimStart('.').ToLowerInvariant();
return ext switch {
// Text
"htm" or "html" => "text/html",
"css" => "text/css",
"txt" => "text/plain",
"csv" => "text/csv",
"xml" => "text/xml",
"json" => "application/json",
"js" => "application/javascript",
"rss" => "application/rss+xml",
"atom" => "application/atom+xml",
// Documents
"pdf" => "application/pdf",
"doc" => "application/msword",
"docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"xls" => "application/vnd.ms-excel",
"xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"ppt" => "application/vnd.ms-powerpoint",
"pptx" => "application/vnd.openxmlformats-officedocument.presentationml.presentation",
"odt" => "application/vnd.oasis.opendocument.text",
"ods" => "application/vnd.oasis.opendocument.spreadsheet",
"odp" => "application/vnd.oasis.opendocument.presentation",
// Images
"jpg" or "jpeg" => "image/jpeg",
"png" => "image/png",
"gif" => "image/gif",
"bmp" => "image/bmp",
"webp" => "image/webp",
"ico" => "image/x-icon",
"tiff" or "tif" => "image/tiff",
"svg" => "image/svg+xml",
"heic" => "image/heic",
"heif" => "image/heif",
// Audio
"mp3" => "audio/mpeg",
"wav" => "audio/wav",
"ogg" or "oga" => "audio/ogg",
"flac" => "audio/flac",
"aac" => "audio/aac",
"m4a" => "audio/mp4",
"mid" or "midi" => "audio/midi",
"weba" => "audio/webm",
// Video
"mp4" => "video/mp4",
"webm" => "video/webm",
"avi" => "video/x-msvideo",
"mov" => "video/quicktime",
"wmv" => "video/x-ms-wmv",
"flv" => "video/x-flv",
"mkv" => "video/x-matroska",
"3gp" => "video/3gpp",
"3g2" => "video/3gpp2",
"mpeg" or "mpg" => "video/mpeg",
// Archives
"zip" => "application/zip",
"rar" => "application/x-rar-compressed",
"7z" => "application/x-7z-compressed",
"tar" => "application/x-tar",
"gz" => "application/gzip",
"bz2" => "application/x-bzip2",
"xz" => "application/x-xz",
// Fonts
"woff" => "font/woff",
"woff2" => "font/woff2",
"ttf" => "font/ttf",
"otf" => "font/otf",
"eot" => "application/vnd.ms-fontobject",
// Code/Programming
"c" => "text/x-c",
"cpp" or "cc" or "cxx" => "text/x-c++",
"cs" => "text/x-csharp",
"java" => "text/x-java-source",
"py" => "text/x-python",
"php" => "application/x-httpd-php",
"sh" => "application/x-sh",
"bat" => "application/x-msdos-program",
"pl" => "application/x-perl",
"rb" => "application/x-ruby",
"go" => "text/x-go",
"rs" => "text/x-rust",
// Other
"exe" or "dll" => "application/x-msdownload",
"iso" => "application/x-iso9660-image",
"rtf" => "application/rtf",
"epub" => "application/epub+zip",
"swf" => "application/x-shockwave-flash",
"psd" => "image/vnd.adobe.photoshop",
"ai" or "ps" or "eps" => "application/postscript",
// Default fallbacks
_ when ext.StartsWith("txt.") => "text/plain",
_ when ext.StartsWith("xml.") => "text/xml",
_ => "application/octet-stream"
};
}
}
}
extension<T>(IEnumerable<T> collection) {
public int IndexOf(Func<T, bool> predicate) {
int idx;
for(idx = 0; !predicate(collection.ElementAt(idx)); idx++);
return idx;
}
}
extension(string self) {
internal HTTP.Methods ToHTTPMethod() => self switch {
"OPTIONS" => HTTP.Methods.OPTIONS,
"GET" => HTTP.Methods.GET,
"HEAD" => HTTP.Methods.HEAD,
"POST" => HTTP.Methods.POST,
"PUT" => HTTP.Methods.PUT,
"PATCH" => HTTP.Methods.PATCH,
"DELETE" => HTTP.Methods.DELETE,
"TRACE" => HTTP.Methods.TRACE,
"CONNECT" => HTTP.Methods.CONNECT,
_ => throw new Exception("Incorrect string")
};
/// <summary>
/// Decode string from base64
/// </summary>
/// <returns>Decoded string</returns>
public string DecodeAsBase64() => Encoding.UTF8.GetString(
Convert.FromBase64String(self)
);
}
}