-
Notifications
You must be signed in to change notification settings - Fork 4
/
Utility.cs
173 lines (169 loc) · 6.09 KB
/
Utility.cs
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
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace TranscodeProcessor
{
public static class Utility
{
private static FastSerialize.Serializer serializer = new FastSerialize.Serializer(typeof(FastSerialize.JsonSerializerGeneric));
public static string GetFirstValue(this NameValueCollection collection, string key)
{
string[] values = collection.GetValues(key);
if (values == null) return null;
if (values.Length > 0)
{
return values[0];
}
return null;
}
public static void WriteResponse(this HttpListenerResponse response, string output)
{
if (response.OutputStream == null || !response.OutputStream.CanWrite)
{
return;
}
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(output);
// Get a response stream and write the response to it.
response.ContentLength64 = buffer.Length;
System.IO.Stream outputStream = response.OutputStream;
outputStream.Write(buffer, 0, buffer.Length);
// You must close the output stream.
outputStream.Close();
}
public static void WriteResponse(this HttpListenerResponse response, object output)
{
if (response.OutputStream == null || !response.OutputStream.CanWrite)
{
return;
}
var data = serializer.Serialize(output);
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(data);
// Get a response stream and write the response to it.
response.ContentLength64 = buffer.Length;
System.IO.Stream outputStream = response.OutputStream;
outputStream.Write(buffer, 0, buffer.Length);
// You must close the output stream.
outputStream.Close();
}
public static void WriteResponse(this HttpListenerResponse response, int statusCode, string output)
{
if (response.OutputStream == null || !response.OutputStream.CanWrite)
{
return;
}
response.StatusCode = statusCode;
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(output);
// Get a response stream and write the response to it.
response.ContentLength64 = buffer.Length;
System.IO.Stream outputStream = response.OutputStream;
outputStream.Write(buffer, 0, buffer.Length);
// You must close the output stream.
outputStream.Close();
}
internal static void CopyStream(Stream input, Stream output)
{
byte[] buffer = new byte[32768];
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, read);
}
}
internal static string GetMime(string fileType)
{
if (fileType[0] == '.')
fileType = fileType.Substring(1);
switch (fileType.ToLower())
{
case "txt":
return "text/plain";
case "html":
return "text/html";
case "htm":
return "text/html";
case "css":
return "text/css";
case "gif":
return "image/gif";
case "jpeg":
case "jpg":
return "image/jpeg";
case "js":
return "application/x-javascript";
case "png":
return "image/png";
case "mp3":
return "audio/mpeg";
case "mp4":
return "video/mp4";
case "mov":
return "video/quicktime";
case "mpeg":
case "mpg":
return "video/mpeg";
case "3gp":
case "3gpp":
return "video/3gpp";
case "flv":
return "video/x-flv";
case "ts":
case "avi":
return "video/x-msvideo";
case "wmv":
return "video/x-ms-wmv";
case "m4v":
return "video/mp4";
case "webm":
return "video/webm";
case "ogg":
return "audio/ogg";
case "ogv":
return "video/ogg";
}
return "application/octet-stream";
}
public static void WriteResponse(this HttpListenerResponse response, int statusCode, object output)
{
if (response.OutputStream == null || !response.OutputStream.CanWrite)
{
return;
}
response.StatusCode = statusCode;
var data = serializer.Serialize(output);
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(data);
// Get a response stream and write the response to it.
response.ContentLength64 = buffer.Length;
System.IO.Stream outputStream = response.OutputStream;
outputStream.Write(buffer, 0, buffer.Length);
// You must close the output stream.
outputStream.Close();
}
// Todo: Handle permission errors?
public static void DirectoryCreate(string path)
{
if (!Directory.Exists(path))
{
try
{
Directory.CreateDirectory(path);
}
catch (Exception ex)
{
}
}
}
// Todo: Handle permission errors?
public static void FileDelete(string path)
{
if (File.Exists(path))
{
File.Delete(path);
}
}
}
}