-
Notifications
You must be signed in to change notification settings - Fork 5
/
upload.aspx.cs
314 lines (270 loc) · 10.3 KB
/
upload.aspx.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Security.Cryptography;
using System.Web.Script.Serialization;
public class RealUploader
{
private string fileName;
private string tempFileName;
private int fileSize = 0;
private string uploadPath;
private string tempPath;
private string[] allowExtensions = new string[0];
private string[] denyExtensions;
private int maxFileSize = 10485760; //10M
private bool overrideFile = false;
private string allowCrossOrigin = "";
private string clientMd5 = "";
private bool checkMd5 = false;
private IDictionary checkSumMsg = new Dictionary<string, string>();
HttpRequest Request = HttpContext.Current.Request;
HttpResponse Response = HttpContext.Current.Response;
public RealUploader()
{
checkSumMsg.Add("success", "1");
checkSumMsg.Add("message", "disabled");
checkSumMsg.Add("serverMd5", "");
checkSumMsg.Add("clientMd5", "");
setUploadPath("d:/uploads");
if (!string.IsNullOrEmpty(Request.Params["ax-file-name"])) {
fileName = Request.Params["ax-file-name"];
}
if (!string.IsNullOrEmpty(Request.Params["ax-temp-name"])) {
tempFileName = Request.Params["ax-temp-name"];
}
if (!string.IsNullOrEmpty(Request.Params["ax-file-size"])) {
fileSize = Convert.ToInt32(Request.Params["ax-file-size"]);
}
if (!string.IsNullOrEmpty(Request.Params["ax-max-file-size"])) {
maxFileSize = Convert.ToInt32(Request.Params["ax-max-file-size"]);
}
//get the file path where to upload the file
//Hint: avoid from settings this path from JS unless in protected environment
if (!string.IsNullOrEmpty(Request.Params["ax-file-path"])) {
setUploadPath(Request.Params["ax-file-path"]);
}
if (!string.IsNullOrEmpty(Request.Params["ax-allow-ext"])) {
string allowExtString = Request.Params["ax-allow-ext"];
allowExtensions = (allowExtString.Length > 0) ? allowExtString.Split('|') : new string[0];
}
if (!string.IsNullOrEmpty(Request.Params["ax-file-md5"])) {
clientMd5 = Request.Params["ax-file-md5"];
}
overrideFile = !string.IsNullOrEmpty(Request.Params["ax-override"]);
checkMd5 = !string.IsNullOrEmpty(Request.Params["ax-md5checksum"]);
tempPath = System.IO.Path.GetTempPath();
denyExtensions = new string[] {"php", "php3", "php4", "php5", "phtml", "exe", "pl", "cgi", "html", "htm", "js", "asp", "aspx", "bat", "sh", "cmd" };
}
/**
* Set and create the upload path
*
* @param $uploadPath
*/
public void setUploadPath(string uploadPath)
{
this.uploadPath = uploadPath;
try {
if(!Directory.Exists(uploadPath)) {
DirectoryInfo di = Directory.CreateDirectory(uploadPath);
}
} catch(Exception e) {
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
/**
*
* Check if file size is allowed
* @param unknown_type $size
* @param unknown_type $max_file_size
*/
public bool checkSize()
{
if (fileSize > maxFileSize)
{
return false;
}
return true;
}
public bool checkName()
{
string[] windowsReserved = new string[] { "CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9" };
string[] badWinChars = new string[] { "<", ">", ":", @"\", "/", "|", "?", "*" };
for (int i = 0; i < badWinChars.Length; i++)
{
fileName.Replace(badWinChars[i], "");
}
//check if legal windows file name
if (Array.IndexOf(windowsReserved, fileName) >= 0)
{
return false;
}
return true;
}
public void checkFileExists()
{
string fileExt = System.IO.Path.GetExtension(fileName).Replace(".", "");
string fileBase = System.IO.Path.GetFileNameWithoutExtension(fileName);
string fullPath = uploadPath + fileName;
//avoid file override, check if file exists and generate another name
//to override file with same name just disable this while
int c = 0;
while (System.IO.File.Exists(fullPath))
{
c++;
fileName = fileBase + "(" + c.ToString() + ")." + fileExt;
fullPath = uploadPath + fileName;
}
}
public void finish()
{
}
public void doFileExists()
{
string msg = System.IO.File.Exists(uploadPath + fileName) ? "yes" : "no";
message(1, msg);
}
public void sendHeaders()
{
Response.AppendHeader("Cache-Control", "no-cache, must-revalidate"); // HTTP/1.1
Response.AppendHeader("Expires", "Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
Response.AppendHeader("X-Content-Type-Options", "nosniff");
Response.AppendHeader("Content-Type", "application/json");
//if we need to upload files across domains then enable allow origin variable with the domain value
if (allowCrossOrigin.Length > 0) {
Response.AppendHeader("Access-Control-Allow-Origin", allowCrossOrigin);
Response.AppendHeader("Access-Control-Allow-Credentials", "false");
Response.AppendHeader("Access-Control-Allow-Methods", "OPTIONS, HEAD, GET, POST, PUT, PATCH, DELETE");
Response.AppendHeader("Access-Control-Allow-Headers", "Content-Type, Content-Range, Content-Disposition");
}
}
public bool checkExt()
{
string fileExt = System.IO.Path.GetExtension(fileName).Replace(".", "");
fileExt = fileExt.ToLower();
if (Array.IndexOf(denyExtensions, fileExt) >= 0)
{
return false;
}
if (Array.IndexOf(allowExtensions, fileExt) < 0 && allowExtensions.Length > 0)
{
return false;
}
return true;
}
public bool checkFile()
{
if(!checkExt()) {
message(-1, "File extension is not allowed");
}
if (!checkName()) {
message(-1, "File name is not allowed");
}
if (!checkSize()) {
message(-1, "File size not allowed: " + maxFileSize);
}
return true;
}
public void uploadFile()
{
if(checkFile()) {
uploadAjax();
}
}
/**
* Main Upload method. Handle file uploads and checks
*/
private void uploadAjax()
{
int currByte = string.IsNullOrEmpty(Request.Params["ax-start-byte"]) ? 0 : Convert.ToInt32(Request.Params["ax-start-byte"]);
HttpPostedFile fileChunk = Request.Files[0];
byte[] bytesInStream = new byte[fileChunk.ContentLength];
fileChunk.InputStream.Read(bytesInStream, 0, (int)bytesInStream.Length);
if (currByte == 0) {
tempFileName = Path.GetFileName(Path.GetTempFileName());
}
string tempFile = tempPath + Path.DirectorySeparatorChar + tempFileName;
string finalFile = uploadPath + Path.DirectorySeparatorChar + fileName;
try
{
FileStream fileStream = new FileStream(tempFile, FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.None);
fileStream.Write(bytesInStream, 0, bytesInStream.Length);
fileStream.Close();
} catch(Exception e) {
message(-1, "Cannot write on file, please check permissions: " + tempFile);
}
long currentFileSize = new FileInfo(tempFile).Length;
if (fileSize > currentFileSize) {
message(1, "Chunk uploaded");
} else {
try {
checkFileExists();
File.Move(tempFile, finalFile);
verifyMd5(finalFile);
finish();
message(1, "File uploaded");
} catch(IOException e) {
message(-1, "File move error: " + finalFile);
}
}
}
private void verifyMd5(string filePath)
{
if (checkMd5 && clientMd5.Length > 0) {
var stream = File.OpenRead(filePath);
MD5 md5Hash = MD5.Create();
string serverMd5 = BitConverter.ToString(md5Hash.ComputeHash(stream)).Replace("-","").ToLower();
if (serverMd5.Equals(clientMd5)) {
checkSumMsg.Add("success", "1");
checkSumMsg.Add("message", "MD5 check correctly");
checkSumMsg.Add("serverMd5", serverMd5);
checkSumMsg.Add("clientMd5", clientMd5);
} else {
checkSumMsg.Add("success", "-1");
checkSumMsg.Add("message", "MD5 check failed");
checkSumMsg.Add("serverMd5", serverMd5);
checkSumMsg.Add("clientMd5", clientMd5);
}
}
}
private void message(int status, string msg)
{
sendHeaders();
IDictionary response = new Dictionary<string, string>();
string checkSumString = new JavaScriptSerializer().Serialize(checkSumMsg);
response.Add("name", fileName);
response.Add("temp_name", tempFileName);
response.Add("size", fileSize.ToString());
response.Add("status", status.ToString());
response.Add("checkSum", checkSumString);
response.Add("info", msg);
response.Add("path", uploadPath);
string responseJson = new JavaScriptSerializer().Serialize(response);
Response.Write(responseJson);
}
}
public partial class upload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
RealUploader uploader = new RealUploader();
if (!string.IsNullOrEmpty(Request.Params["ax-check-file"])) {
uploader.doFileExists();
} else {
uploader.uploadFile();
}
}
}