-
Notifications
You must be signed in to change notification settings - Fork 0
/
Download.aspx.cs
152 lines (125 loc) · 4.83 KB
/
Download.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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using log4net;
using System.Text;
namespace PhotoGalerie
{
public partial class DownloadPage : System.Web.UI.Page
{
private ILog Logger = LogManager.GetLogger("PhotoGalerie.DownloadPage");
private const int PartialContentHttpStatus = 206;
/// <summary>
/// Set header:
/// </summary>
private void ProcessContentType()
{
string type = Request.QueryString.Get("type") ?? "d";
string contentType = "application/x-data";
switch (type)
{
case "i":
contentType = "image/jpeg";
break;
case "v":
contentType = "video/mp4";
break;
}
Response.ContentType = contentType;
}
private string GetFilePath()
{
string file = Request.QueryString.Get("file");
string folderParam = Request.QueryString.Get("folder");
if (file.IndexOf("..") > -1) throw new InvalidDataException();
string pageFolder = FolderHelper.GetFolderPath(Config.BaseFolder, folderParam).Path;
string filePath = pageFolder + "\\" + file;
return filePath;
}
private string GetFileName()
{
string file = Request.QueryString.Get("file");
return file;
}
/// <summary>
/// Show save as dialog.
/// Add header: [Content-Disposition]
/// </summary>
private void ProcessDownloadMode()
{
if(Request.QueryString.Get("download") == "true")
Response.AppendHeader("Content-Disposition", "attachment; filename=" + GetFileName());
}
private void ProcessCacheMode()
{
Response.Cache.SetExpires(DateTime.UtcNow.AddYears(1));
}
protected void Page_Load(object sender, EventArgs e)
{
LogRequest();
ProcessContentType();
ProcessCacheMode();
ProcessDownloadMode();
SendFile();
}
private void LogRequest()
{
StringBuilder sb = new StringBuilder();
sb.Append("Url: " + Request.Url);
sb.Append(", Range: " + Request.Headers["Range"]);
Logger.Debug(sb);
}
private void SendFile()
{
byte[] buff = new byte[0x2000];
long startPosition = 0;
string filePath = GetFilePath();
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
long maxLen = ProcessContentLength(ref startPosition, fs.Length);
fs.Seek(startPosition, SeekOrigin.Begin);
long curLen = 0;
int cnt = 0;
while (curLen < maxLen && (cnt = fs.Read(buff, 0, buff.Length)) > 0)
{
curLen += cnt;
if (curLen > maxLen) cnt -= (int)(curLen - maxLen);
Response.OutputStream.Write(buff, 0, cnt);
}
}
}
/// <summary>
/// Modify startPosition for Rage request. Add headers: Content-Length, [Content-Range]
/// </summary>
/// <param name="startPosition"></param>
/// <param name="actualLength"></param>
/// <returns></returns>
private long ProcessContentLength(ref long startPosition, long actualLength)
{
string rangeHeader = Request.Headers["Range"];
if (string.IsNullOrEmpty(rangeHeader))
{
Response.AppendHeader("Content-Length", actualLength.ToString());
return actualLength;
}
long[] ranges = rangeHeader.Replace("bytes=", "")
.Split(new[] { '-' }, StringSplitOptions.RemoveEmptyEntries)
.Select(s => long.Parse(s)).ToArray();
long length = 0;
if (ranges.Length > 0) startPosition = ranges[0];
if (ranges.Length > 1) length = ranges[1] - startPosition + 1;
long maxLen = length != 0 ? length : actualLength - startPosition;
Response.StatusCode = PartialContentHttpStatus; //Partial content;
//Response.AppendHeader("Content-Range", string.Format("bytes {0}-{1}/*", startPosition, startPosition + maxLen - 1));
Response.AppendHeader("Content-Range", string.Format("bytes {0}-{1}/{2}", startPosition, startPosition + maxLen - 1, actualLength));
Response.AppendHeader("Content-Length", maxLen.ToString());
return maxLen;
}
}
}