Skip to content

Commit

Permalink
Merge pull request #176 from ONLYOFFICE/feature/filehandler-stream-range
Browse files Browse the repository at this point in the history
filehandler: add range in stream
  • Loading branch information
pavelbannov authored Jan 29, 2024
2 parents d457d6f + e1b49e2 commit 60288f6
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 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, false);
await SendStreamByChunksAsync(context, length, filename, readStream);
}

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);
flushed = await SendStreamByChunksAsync(context, length, title, fileStream);
}
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);
flushed = await SendStreamByChunksAsync(context, length, title, fileStream);
}
}
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, bool flushed)
private async Task<bool> SendStreamByChunksAsync(HttpContext context, long toRead, string title, Stream fileStream)
{
context.Response.Headers.Add("Connection", "Keep-Alive");
context.Response.ContentLength = toRead;
Expand All @@ -618,6 +618,7 @@ 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 @@ -780,15 +781,13 @@ private async Task StreamFileAsync<T>(HttpContext context, T id)
return;
}

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

await using var stream = await fileDao.GetFileStreamAsync(file);
context.Response.Headers.Add("Content-Length",
stream.CanSeek
? stream.Length.ToString(CultureInfo.InvariantCulture)
: file.ContentLength.ToString(CultureInfo.InvariantCulture));
await stream.CopyToAsync(context.Response.Body);
stream.Seek(offset, SeekOrigin.Begin);

await SendStreamByChunksAsync(context, length, file.Title, stream);
}
catch (Exception ex)
{
Expand Down

0 comments on commit 60288f6

Please sign in to comment.