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

Fixed paste downloads trying to access filestream after disposal

This commit is contained in:
Uncled1023 2022-05-21 19:19:38 -07:00
parent 49b3899306
commit 9fbed1aed0
2 changed files with 12 additions and 6 deletions

View File

@ -131,10 +131,16 @@ namespace Teknik.Areas.Paste.Controllers
if (fileStream == null)
return new StatusCodeResult(StatusCodes.Status404NotFound);
using (AesCounterStream cs = new AesCounterStream(fileStream, false, keyBytes, ivBytes))
using (StreamReader sr = new StreamReader(cs, Encoding.Unicode))
int contentSize = (int)fileStream.Length;
// Only load the model content if we aren't downloading it.
if (type.ToLower() != "download")
{
model.Content = await sr.ReadToEndAsync();
using (AesCounterStream cs = new AesCounterStream(fileStream, false, keyBytes, ivBytes))
using (StreamReader sr = new StreamReader(cs, Encoding.Unicode))
{
model.Content = await sr.ReadToEndAsync();
}
}
switch (type.ToLower())
@ -155,7 +161,7 @@ namespace Teknik.Areas.Paste.Controllers
Response.Headers.Add("Content-Disposition", cd.ToString());
return new BufferedFileStreamResult("application/octet-stream", async (response) => await ResponseHelper.StreamToOutput(response, true, new AesCounterStream(fileStream, false, keyBytes, ivBytes), (int)fileStream.Length, _config.PasteConfig.ChunkSize), false);
return new BufferedFileStreamResult("application/octet-stream", async (response) => await ResponseHelper.StreamToOutput(response, true, new AesCounterStream(fileStream, false, keyBytes, ivBytes), contentSize, _config.PasteConfig.ChunkSize), false);
default:
return View("~/Areas/Paste/Views/Paste/Full.cshtml", model);
}

View File

@ -13,13 +13,13 @@ namespace Teknik.Utilities
{
public async static Task StreamToOutput(HttpResponse response, bool flush, Stream stream, int length, int chunkSize)
{
int processedBytes = 0;
var bufferSize = chunkSize;
if (length < chunkSize)
bufferSize = length;
Memory<byte> buffer = new byte[bufferSize];
try
{
int processedBytes;
do
{
processedBytes = await stream.ReadAsync(buffer);
@ -36,7 +36,7 @@ namespace Teknik.Utilities
}
while (processedBytes > 0);
}
catch (Exception)
catch (Exception ex)
{
// Don't worry about it. Just leave
}