Skip to content

Commit

Permalink
revert db5845a
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelbannov committed Jan 30, 2024
1 parent 60288f6 commit c911f4f
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions products/ASC.Files/Core/HttpHandlers/FileHandler.ashx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ private async ValueTask BulkDownloadFile(HttpContext context)
readStream.Seek(offset, SeekOrigin.Begin);
}

await SendStreamByChunksAsync(context, length, filename, readStream);
await SendStreamByChunksAsync(context, length, filename, readStream, false);
}

await context.Response.Body.FlushAsync();
Expand Down Expand Up @@ -490,7 +490,7 @@ private async Task DownloadFile<T>(HttpContext context, T id, bool forView)
}
}

flushed = await SendStreamByChunksAsync(context, length, title, fileStream);
flushed = await SendStreamByChunksAsync(context, length, title, fileStream, flushed);
}
else
{
Expand All @@ -513,7 +513,7 @@ private async Task DownloadFile<T>(HttpContext context, T id, bool forView)
fileStream.Seek(offset, SeekOrigin.Begin);
}

flushed = await SendStreamByChunksAsync(context, length, title, fileStream);
flushed = await SendStreamByChunksAsync(context, length, title, fileStream, flushed);
}
}
catch (ThreadAbortException tae)
Expand Down Expand Up @@ -609,7 +609,7 @@ private long ProcessRangeHeader(HttpContext context, long fullLength, ref long o
return length;
}

private async Task<bool> SendStreamByChunksAsync(HttpContext context, long toRead, string title, Stream fileStream)
private async Task<bool> SendStreamByChunksAsync(HttpContext context, long toRead, string title, Stream fileStream, bool flushed)
{
context.Response.Headers.Add("Connection", "Keep-Alive");
context.Response.ContentLength = toRead;
Expand All @@ -618,7 +618,6 @@ private async Task<bool> SendStreamByChunksAsync(HttpContext context, long toRea

var bufferSize = Convert.ToInt32(Math.Min(32 * 1024, toRead)); // 32KB
var buffer = new byte[bufferSize];
var flushed = false;
while (toRead > 0)
{
var length = await fileStream.ReadAsync(buffer, 0, bufferSize);
Expand Down Expand Up @@ -781,13 +780,15 @@ private async Task StreamFileAsync<T>(HttpContext context, T id)
return;
}

long offset = 0;
var length = ProcessRangeHeader(context, file.ContentLength, ref offset);
context.Response.Headers.Add("Content-Disposition", ContentDispositionUtil.GetHeaderValue(file.Title));
context.Response.ContentType = MimeMapping.GetMimeMapping(file.Title);

await using var stream = await fileDao.GetFileStreamAsync(file);
stream.Seek(offset, SeekOrigin.Begin);

await SendStreamByChunksAsync(context, length, file.Title, stream);
context.Response.Headers.Add("Content-Length",
stream.CanSeek
? stream.Length.ToString(CultureInfo.InvariantCulture)
: file.ContentLength.ToString(CultureInfo.InvariantCulture));
await stream.CopyToAsync(context.Response.Body);
}
catch (Exception ex)
{
Expand Down

0 comments on commit c911f4f

Please sign in to comment.