1
0
mirror of https://git.teknik.io/Teknikode/Teknik.git synced 2023-08-02 14:16:22 +02:00

Made downloads stream the file while decrypting them.

This commit is contained in:
Uncled1023 2017-02-09 00:17:36 -08:00
parent 2ac90fbbbe
commit b7a9f2a740
3 changed files with 145 additions and 143 deletions

View File

@ -176,8 +176,7 @@ namespace Teknik.Areas.Upload.Controllers
if (System.IO.File.Exists(filePath))
{
// Read in the file
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
// We accept ranges
Response.AddHeader("Accept-Ranges", "0-" + upload.ContentLength);
@ -233,11 +232,6 @@ namespace Teknik.Areas.Upload.Controllers
length = endByte - startByte + 1; // Calculate new content length
// grab the portion of the data we want
byte[] dataRange = new byte[length];
//Array.Copy(data, startByte, dataRange, 0, length);
//data = dataRange;
// Ranges are response of 206
Response.StatusCode = 206;
}
@ -258,7 +252,7 @@ namespace Teknik.Areas.Upload.Controllers
Inline = true
};
Response.AppendHeader("Content-Disposition", cd.ToString());
Response.AddHeader("Content-Disposition", cd.ToString());
string contentType = upload.ContentType;
// We need to prevent html (make cleaner later)
@ -267,10 +261,8 @@ namespace Teknik.Areas.Upload.Controllers
contentType = "text/plain";
}
// Reset file stream to starting position
fs.Seek(0, SeekOrigin.Begin);
Response.BufferOutput = true;
// Reset file stream to starting position (or start of range)
fs.Seek(startByte, SeekOrigin.Begin);
// If the IV is set, and Key is set, then decrypt it before sending
if (!string.IsNullOrEmpty(upload.Key) && !string.IsNullOrEmpty(upload.IV))
@ -278,26 +270,50 @@ namespace Teknik.Areas.Upload.Controllers
byte[] keyBytes = Encoding.UTF8.GetBytes(upload.Key);
byte[] ivBytes = Encoding.UTF8.GetBytes(upload.IV);
IBufferedCipher cipher = AES.CreateCipher(false, keyBytes, ivBytes, "CTR", "NoPadding");
return new FileDecryptResult(upload.Url, contentType, (response) => this.GenerateExportFile(response, fs, (int)length, keyBytes, ivBytes, "CTR", "NoPadding", 4 * 1024), false);
}
else
{
// Don't buffer the response
Response.Buffer = false;
// Otherwise just send it
return File(fs, contentType);
}
}
}
}
}
return Redirect(Url.SubRouteUrl("error", "Error.Http404"));
}
return Redirect(Url.SubRouteUrl("error", "Error.Http403"));
}
int chunkSize = 1024;
public void GenerateExportFile(HttpResponseBase response, System.IO.Stream fileStream, int length, byte[] key, byte[] iv, string mode, string padding, int chunkSize)
{
response.Flush();
IBufferedCipher cipher = AES.CreateCipher(false, key, iv, mode, padding);
// Make sure the input stream is at the beginning
fs.Seek(0, SeekOrigin.Begin);
int curByte = 0;
int processedBytes = 0;
byte[] buffer = new byte[chunkSize];
do
{
processedBytes = AES.ProcessCipherBlock(cipher, fs, chunkSize, buffer, 0);
if (curByte + chunkSize > length)
{
chunkSize = length - curByte;
}
processedBytes = AES.ProcessCipherBlock(cipher, fileStream, chunkSize, buffer, 0);
if (processedBytes > 0)
{
Response.OutputStream.Write(buffer, 0, processedBytes);
response.OutputStream.Write(buffer, 0, processedBytes);
response.Flush();
// Clear the buffer
Array.Clear(buffer, 0, chunkSize);
}
curByte += processedBytes;
}
while (processedBytes > 0);
while (processedBytes > 0 && curByte < length);
// Clear the buffer
Array.Clear(buffer, 0, chunkSize);
@ -307,25 +323,12 @@ namespace Teknik.Areas.Upload.Controllers
if (processedBytes > 0)
{
// We have bytes, lets write them to the output
Response.OutputStream.Write(buffer, 0, processedBytes);
response.OutputStream.Write(buffer, 0, processedBytes);
response.Flush();
}
HttpContext.ApplicationInstance.CompleteRequest();
return Content("");
}
else
{
// Otherwise just send it
return File(fs, contentType);
}
}
}
}
}
}
return Redirect(Url.SubRouteUrl("error", "Error.Http404"));
}
return Redirect(Url.SubRouteUrl("error", "Error.Http403"));
// dispose of file stream
fileStream.Dispose();
}
[HttpPost]

View File

@ -1,8 +1,11 @@
using System;
using Org.BouncyCastle.Crypto;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
namespace Teknik.Utilities
@ -10,12 +13,9 @@ namespace Teknik.Utilities
/// <summary>
/// MVC action result that generates the file content using a delegate that writes the content directly to the output stream.
/// </summary>
public class FileGeneratingResult : FileResult
public class FileDecryptResult : FileResult
{
/// <summary>
/// The delegate that will generate the file content.
/// </summary>
private readonly Action<System.IO.Stream> content;
private readonly Action<HttpResponseBase> responseDelegate;
private readonly bool bufferOutput;
@ -26,15 +26,14 @@ namespace Teknik.Utilities
/// <param name="contentType">Type of the content.</param>
/// <param name="content">Delegate with Stream parameter. This is the stream to which content should be written.</param>
/// <param name="bufferOutput">use output buffering. Set to false for large files to prevent OutOfMemoryException.</param>
public FileGeneratingResult(string fileName, string contentType, Action<System.IO.Stream> content, bool bufferOutput = true)
public FileDecryptResult(string fileName, string contentType, Action<HttpResponseBase> response, bool bufferOutput)
: base(contentType)
{
if (content == null)
if (response == null)
throw new ArgumentNullException("content");
this.content = content;
this.responseDelegate = response;
this.bufferOutput = bufferOutput;
FileDownloadName = fileName;
}
/// <summary>
@ -44,7 +43,7 @@ namespace Teknik.Utilities
protected override void WriteFile(System.Web.HttpResponseBase response)
{
response.Buffer = bufferOutput;
content(response.OutputStream);
responseDelegate(response);
}
}
}

View File

@ -105,7 +105,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="FileGeneratingResult.cs" />
<Compile Include="FileDecryptResult.cs" />
<Compile Include="HttpWebResponseResult.cs" />
<Compile Include="BundleExtensions.cs" />
<Compile Include="ByteExtensions.cs" />