Browse Source

Fixed paste downloads trying to access filestream after disposal

master
Teknikode 1 month ago
parent
commit
9fbed1aed0
  1. 14
      Teknik/Areas/Paste/Controllers/PasteController.cs
  2. 4
      Utilities/ResponseHelper.cs

14
Teknik/Areas/Paste/Controllers/PasteController.cs

@ -131,10 +131,16 @@ namespace Teknik.Areas.Paste.Controllers @@ -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 @@ -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);
}

4
Utilities/ResponseHelper.cs

@ -13,13 +13,13 @@ namespace Teknik.Utilities @@ -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 @@ -36,7 +36,7 @@ namespace Teknik.Utilities
}
while (processedBytes > 0);
}
catch (Exception)
catch (Exception ex)
{
// Don't worry about it. Just leave
}

Loading…
Cancel
Save