Upload files transmit refactored

main
Inga 🏳‍🌈 14 years ago
parent e360cf0dc7
commit 727173210e
  1. 2
      Builder/IISMainHandler/build.txt
  2. 20
      Common/UploadManager.cs
  3. 14
      Core/extensions/Extensions.cs
  4. 17
      IISMainHandler/handlers/response/UploadHandler.cs

@ -129,5 +129,25 @@ namespace FLocal.Common {
return upload; return upload;
} }
public static void WriteUpload(Upload upload, Stream output) {
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Config.instance.UploaderUrl + "Data/" + upload.hash + "." + upload.extension);
request.Method = "GET";
HttpWebResponse response = null;
try {
try {
response = (HttpWebResponse)request.GetResponse();
} catch(WebException e) {
response = (HttpWebResponse)e.Response;
}
using(Stream responseStream = response.GetResponseStream()) {
responseStream.WriteTo(output);
}
} finally {
if(response != null) {
response.Close();
}
}
}
} }
} }

@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.IO;
namespace FLocal.Core { namespace FLocal.Core {
@ -247,5 +248,18 @@ namespace FLocal.Core {
return enumerable.Union((IEnumerable<T>)second); return enumerable.Union((IEnumerable<T>)second);
} }
/// <summary>
/// Code taken from http://msdn.microsoft.com/en-us/library/system.io.stream.write.aspx
/// </summary>
/// <param name="input"></param>
/// <param name="output"></param>
public static void WriteTo(this Stream input, Stream output) {
const int size = 4096;
byte[] bytes = new byte[4096];
int numBytes;
while((numBytes = input.Read(bytes, 0, size)) > 0)
output.Write(bytes, 0, numBytes);
}
} }
} }

@ -24,7 +24,22 @@ namespace FLocal.IISHandler.handlers.response {
throw new AccessViolationException(); throw new AccessViolationException();
} }
return Config.instance.UploaderUrl + "Data/" + this.url.upload.hash + "." + this.url.upload.extension; string mime = Util.getMimeByExtension(this.url.upload.extension);
if(mime != null) {
context.httpresponse.ContentType = mime;
} else {
//throw new HttpException(403, "wrong file type");
throw new WrongUrlException();
}
context.httpresponse.AddHeader("content-disposition", "attachment; filename=" + this.url.upload.filename);
context.httpresponse.Cache.SetExpires(DateTime.Now.AddDays(10));
context.httpresponse.Cache.SetLastModified(this.url.upload.uploadDate);
context.httpresponse.Cache.SetCacheability(System.Web.HttpCacheability.Public);
UploadManager.WriteUpload(this.url.upload, context.httpresponse.OutputStream);
throw new response.SkipXsltTransformException();
} }
} }

Loading…
Cancel
Save