-
Notifications
You must be signed in to change notification settings - Fork 605
/
Copy pathFileDisplayHandler.cs
212 lines (178 loc) · 5.47 KB
/
FileDisplayHandler.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;
using System.IO;
using System.Xml;
using Ude;
using MimeTypes;
namespace Bonobo.Git.Server
{
public static class FileDisplayHandler
{
public const string NoBrush = "nohighlight";
public static bool IsImage(string fileName)
{
return MimeTypeMap.GetMimeType(Path.GetExtension(fileName.ToLower())).Contains("image");
}
public static string GetBrush(string fileName)
{
if (String.IsNullOrWhiteSpace(fileName))
{
throw new ArgumentNullException("fileName");
}
var extension = Path.GetExtension(fileName).ToLower();
switch (extension)
{
case ".vb":
return "vb";
case ".vbs":
return "vbs";
case ".cs":
return "csharp";
case ".as":
return "as3";
case ".sh":
return "bash";
case ".bat":
case ".cmd":
return "dos";
case ".html":
case ".htm":
case ".xhtml":
case ".xslt":
case ".xml":
case ".asp":
case ".aspx":
case ".cshtml":
case ".xaml":
case ".csproj":
case ".config":
case ".wsf":
return "xml";
case ".cf":
return "cf";
case ".h":
case ".c":
case ".cpp":
return "cpp";
case ".css":
return "css";
case ".pas":
return "delphi";
case ".diff":
case ".patch":
return "diff";
case ".erl":
case ".xlr":
case ".hlr":
return "erlang";
case ".groovy":
return "groovy";
case ".js":
case ".jscript":
case ".javascript":
return "js";
case ".json":
return "json";
case ".java":
return "java";
case ".fx":
return "jfx";
case ".pir":
case ".pm":
case ".pl":
return "perl";
case ".php":
return "php";
case ".ps1":
case ".psm1":
return "ps";
case ".py":
return "python";
case ".rb":
return "ruby";
case ".scala":
return "scala";
case ".sql":
return "sql";
case ".ts":
return "typescript";
default:
return NoBrush;
}
}
public static string GetText(byte[] data, Encoding encoding)
{
if (data.Length == 0)
{
return string.Empty;
}
return new StreamReader(new MemoryStream(data), encoding, true).ReadToEnd();
}
public static bool TryGetEncoding(byte[] data, out Encoding encoding)
{
ICharsetDetector cdet = new CharsetDetector();
cdet.Feed(data, 0, data.Length);
cdet.DataEnd();
if (cdet.Charset != null)
{
if (cdet.Charset.ToLowerInvariant() == "big-5")
{
encoding = Encoding.GetEncoding("big5");
return true;
}
else
{
try
{
encoding = Encoding.GetEncoding(cdet.Charset);
return true;
}
catch
{
encoding = Encoding.Default;
return false;
}
}
}
encoding = Encoding.Default;
return false;
}
/// <summary>
/// <para>Returns the human-readable file size for an arbitrary, 64-bit file size</para>
/// <para>The default format is "0.### XB", e.g. "4.2 KB" or "1.434 GB"</para>
/// </summary>
public static string GetFileSizeString(long i)
{
long absolute_i = (i < 0 ? -i : i);
string suffix;
double readable;
// GB is enough for a VCS I think
if (absolute_i >= 0x40000000)
{
suffix = "GB";
readable = (i >> 20);
}
else if (absolute_i >= 0x100000)
{
suffix = "MB";
readable = (i >> 10);
}
else if (absolute_i >= 0x400)
{
suffix = "kB";
readable = i;
}
else
{
return i.ToString("0 B");
}
// Divide by 1024 to get fractional value
readable = readable / 1024;
return readable.ToString("0.### ") + suffix;
}
}
}