This repository has been archived by the owner on Aug 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathUpload.aspx.cs
67 lines (66 loc) · 3.1 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
using System;
using System.IO;
using System.Linq;
using System.Web.UI;
namespace Mygod.Skylark
{
public partial class Upload : Page
{
protected void Page_Load(object sender, EventArgs e)
{
var upload = Request.HttpMethod == "POST";
if (upload && !Request.GetUser().OperateFiles)
{
Response.StatusCode = 401;
return;
}
var data = upload ? Request.Form : Request.QueryString;
string id = data["resumableIdentifier"], path = FileHelper.Combine
(RouteData.GetRelativePath(), data["resumableRelativePath"].ToValidPath(false)),
filePath = FileHelper.GetFilePath(path), dataPath = FileHelper.GetDataFilePath(path),
state = FileHelper.GetState(dataPath);
Directory.CreateDirectory(Path.GetDirectoryName(filePath));
Directory.CreateDirectory(Path.GetDirectoryName(dataPath));
if (state != TaskType.UploadTask || state == TaskType.UploadTask && new UploadTask(path).Identifier != id)
{
FileHelper.Delete(path);
File.WriteAllBytes(filePath, new byte[0]);
try
{
new UploadTask(path, id, int.Parse(data["resumableTotalChunks"]),
long.Parse(data["resumableTotalSize"])).Save();
}
catch (IOException) { } // another save in progress
}
var index = int.Parse(data["resumableChunkNumber"]) - 1;
if (upload)
{
string basePath = FileHelper.GetDataPath(path), partSuffix = ".part" + index,
partPath = basePath + ".incomplete" + partSuffix;
try
{
using (var output = new FileStream(partPath, FileMode.Create, FileAccess.Write, FileShare.Read))
Request.Files[Request.Files.AllKeys.Single()].InputStream.CopyTo(output);
File.Move(partPath, basePath + ".complete" + partSuffix);
}
catch
{
FileHelper.DeleteWithRetries(partPath); // delete imcomplete file
}
var task = new UploadTask(path);
if (task.FinishedParts != task.TotalParts) return;
using (var output = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.Read))
for (var i = 0; i < task.TotalParts; ++i)
{
using (var input = new FileStream(
partPath = FileHelper.GetDataPath(path + ".complete.part" + i),
FileMode.Open, FileAccess.Read, FileShare.Read)) input.CopyTo(output);
FileHelper.DeleteWithRetries(partPath);
}
task.Finish();
}
else Response.StatusCode = File.Exists(FileHelper.GetDataPath(path + ".complete.part" + index))
? 200 : 204; // test
}
}
}