This repository has been archived by the owner on Dec 14, 2020. It is now read-only.
/
Download.aspx.cs
executable file
·342 lines (292 loc) · 10 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
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
* Download.aspx.cs
*
* Authors:
* Rolf Bjarne Kvinge (RKvinge@novell.com)
*
* Copyright 2009 Novell, Inc. (http://www.novell.com)
*
* See the LICENSE file included with the distribution for details.
*
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Text;
using System.Web;
using MonkeyWrench.Database;
using MonkeyWrench.DataClasses;
using MonkeyWrench.DataClasses.Logic;
namespace MonkeyWrench.WebServices
{
public partial class Download : System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
try {
int workfile_id;
int revision_id;
int work_id;
bool diff;
string md5;
string filename;
int.TryParse (Request ["revision_id"], out revision_id);
int.TryParse (Request ["workfile_id"], out workfile_id);
int.TryParse (Request ["work_id"], out work_id);
bool.TryParse (Request ["diff"], out diff);
md5 = Request ["md5"];
filename = Request ["filename"];
if (workfile_id != 0 || !string.IsNullOrEmpty (md5)) {
DownloadWorkFile (workfile_id, md5);
} else if (!string.IsNullOrEmpty (filename) && work_id != 0) {
DownloadNamedWorkFile (work_id, filename);
} else if (revision_id != 0) {
DownloadRevisionLog (revision_id, diff);
} else {
throw new HttpException (404, "Nothing to download.");
}
} catch (Exception ex) {
Logger.Log ("Download failed: {0}", ex);
throw;
}
}
private void DownloadStream (Stream str, string compressed_mime)
{
// access must be verified before calling this method (no verification is done here)
if (compressed_mime == MimeTypes.GZ) {
string AcceptEncoding = Request.Headers["Accept-Encoding"];
if (!string.IsNullOrEmpty (AcceptEncoding) && AcceptEncoding.Contains ("gzip")) {
Response.AppendHeader ("Content-Encoding", "gzip");
} else {
str = new GZipStream (str, CompressionMode.Decompress);
}
}
try {
if (!(str is GZipStream))
Response.AppendHeader ("Content-Length", str.Length.ToString ());
} catch (NotSupportedException) {
// GZipStreams don't usually know their length, just ignore
}
byte [] buffer = new byte [1024];
int read;
int total = 0;
read = str.Read (buffer, 0, buffer.Length);
total += read;
while (read > 0) {
Response.OutputStream.Write (buffer, 0, read);
read = str.Read (buffer, 0, buffer.Length);
total += read;
Response.Flush ();
}
}
private void DownloadMd5 (string md5)
{
// access must be verified before calling this method (no verification is done here)
string fullpath = DBFile_Extensions.GetFullPath (md5);
if (fullpath == null)
throw new HttpException (404, "Could not find the file.");
if (fullpath.EndsWith (".gz")) {
string AcceptEncoding = Request.Headers["Accept-Encoding"];
if (!string.IsNullOrEmpty(AcceptEncoding) && AcceptEncoding.Contains("gzip")) {
Response.AppendHeader ("Content-Encoding", "gzip");
} else {
// need to decompress this stream...
using (Stream str = File.OpenRead (fullpath)) {
DownloadStream (str, MimeTypes.GZ);
}
return;
}
}
Response.AppendHeader ("Content-Length", new System.IO.FileInfo (fullpath).Length.ToString ());
Response.TransmitFile (fullpath);
}
private void DownloadFile (DB db, int file_id)
{
// access must be verified before calling this method (no verification is done here)
DownloadFile (db, DBFile_Extensions.Create (db, file_id));
}
private void DownloadFile (DB db, DBFile file)
{
// access must be verified before calling this method (no verification is done here)
if (file.file_id == null) {
DownloadMd5 (file.md5);
} else {
using (Stream str = db.Download (file)) {
DownloadStream (str, file.compressed_mime);
}
}
}
private WebServiceLogin CreateLogin ()
{
WebServiceLogin login = new WebServiceLogin ();
login.Cookie = Request ["cookie"];
login.Password = Request ["password"];
if (string.IsNullOrEmpty (login.Cookie)) {
if (Request.Cookies ["cookie"] != null) {
login.Cookie = Request.Cookies ["cookie"].Value;
}
}
login.User = Request ["user"];
if (string.IsNullOrEmpty (login.User)) {
if (Request.Cookies ["user"] != null) {
login.User = Request.Cookies ["user"].Value;
}
}
login.Ip4 = Request ["ip4"];
if (string.IsNullOrEmpty (login.Ip4)) {
login.Ip4 = Utilities.GetExternalIP (Request);
}
return login;
}
private void DownloadRevisionLog (int revision_id, bool diff /* diff or log */)
{
DBRevision revision;
DBLane lane;
using (DB db = new DB ()) {
WebServiceLogin login = CreateLogin ();
revision = DBRevision_Extensions.Create (db, revision_id);
// no access restricion on revision logs/diffs
Authentication.VerifyAnonymousAccess (Context, db, login);
Response.ContentType = MimeTypes.TXT;
if (revision == null) {
Response.Write ("Revision not found.");
} else {
if (diff) {
lane = DBLane_Extensions.Create (db, revision.lane_id);
if (lane.source_control == "git") {
using (Process git = new Process ()) {
git.StartInfo.RedirectStandardOutput = true;
git.StartInfo.RedirectStandardError = true;
git.StartInfo.UseShellExecute = false;
git.StartInfo.FileName = "git";
git.StartInfo.Arguments = "diff --no-color --no-prefix " + revision.revision + "~ " + revision.revision;
git.StartInfo.WorkingDirectory = Configuration.GetSchedulerRepositoryCacheDirectory (lane.repository);
git.OutputDataReceived += (object sender, DataReceivedEventArgs ea) =>
{
Response.Write (ea.Data);
Response.Write ('\n');
};
git.ErrorDataReceived += (object sender, DataReceivedEventArgs ea) =>
{
Response.Write (ea.Data);
Response.Write ('\n');
};
// Logger.Log ("Executing: '{0} {1}' in {2}", git.StartInfo.FileName, git.StartInfo.Arguments, git.StartInfo.WorkingDirectory);
git.Start ();
git.BeginErrorReadLine ();
git.BeginOutputReadLine ();
if (!git.WaitForExit (1000 * 60 * 5 /* 5 minutes */)) {
git.Kill ();
Response.Write ("Error: git diff didn't finish in 5 minutes, aborting.\n");
}
}
} else {
if (revision.diff_file_id.HasValue) {
DownloadFile (db, revision.diff_file_id.Value);
} else if (!string.IsNullOrEmpty (revision.diff)) {
Response.Write (revision.diff);
} else {
Response.Write ("No diff yet.");
}
}
} else {
if (revision.log_file_id.HasValue) {
DownloadFile (db, revision.log_file_id.Value);
} else if (!string.IsNullOrEmpty (revision.log)) {
Response.Write (revision.log);
} else {
Response.Write ("No log yet.");
}
}
}
}
}
private void DownloadNamedWorkFile (int work_id, string filename)
{
DBWorkFileView view = null;
DBFile file = null;
string compressed_mime;
using (DB db = new DB ()) {
WebServiceLogin login = CreateLogin ();
view = DBWorkFileView_Extensions.Find (db, work_id, filename);
if (view == null)
throw new HttpException (404, "Could not find the file.");
if (view.@internal) // internal files need admin rights
Authentication.VerifyUserInRole (Context, db, login, Roles.Administrator, false);
else
Authentication.VerifyAnonymousAccess (Context, db, login);
file = DBWork_Extensions.GetFile (db, view.work_id, filename, false);
if (file == null)
throw new HttpException (404, string.Format ("Could not find the filename '{0}'", filename));
compressed_mime = file.compressed_mime;
Response.ContentType = file.mime;
Response.AppendHeader ("Content-Disposition", "filename=\"" + Path.GetFileName (filename) + "\"");
if (view.file_file_id == null) {
DownloadMd5 (view.md5);
} else {
using (Stream str = db.Download (view)) {
DownloadStream (str, compressed_mime);
}
}
}
}
private void DownloadWorkFile (int workfile_id, string md5)
{
DBWorkFileView view = null;
DBFile file = null;
string filename;
string mime;
string compressed_mime;
using (DB db = new DB ()) {
WebServiceLogin login = CreateLogin ();
filename = Request ["filename"];
if (!string.IsNullOrEmpty (md5)) { // md5 lookup needs admin rights
Authentication.VerifyUserInRole (Context, db, login, Roles.Administrator, false);
file = DBFile_Extensions.Find (db, md5);
if (file == null)
throw new HttpException (404, "Could not find the file.");
mime = file.mime;
filename = file.filename;
compressed_mime = file.compressed_mime;
} else {
view = DBWorkFileView_Extensions.Find (db, workfile_id);
if (view == null)
throw new HttpException (404, "Could not find the file.");
if (view.@internal) // internal files need admin rights
Authentication.VerifyUserInRole (Context, db, login, Roles.Administrator, false);
else
Authentication.VerifyAnonymousAccess (Context, db, login);
if (!string.IsNullOrEmpty (filename)) {
file = DBWork_Extensions.GetFile (db, view.work_id, filename, false);
if (file == null)
throw new HttpException (404, string.Format ("Could not find the filename '{0}'", filename));
mime = file.mime;
compressed_mime = file.compressed_mime;
md5 = file.md5;
view = null;
} else {
mime = view.mime;
filename = view.filename;
compressed_mime = view.compressed_mime;
}
}
Response.ContentType = mime;
Response.AppendHeader ("Content-Disposition", "filename=\"" + Path.GetFileName (filename) + "\"");
// any access rights verified, serve the file
if (view != null) {
if (view.file_file_id == null) {
DownloadMd5 (view.md5);
} else {
using (Stream str = db.Download (view)) {
DownloadStream (str, compressed_mime);
}
}
} else {
DownloadFile (db, file);
}
}
}
}
}