Respect cancellation token/HTTP request aborts correctly in `SymlinkFollowingPhysicalFileResultExecutor` (#13033)

pull/13051/head
Akaanksh Raj 3 months ago committed by GitHub
parent 13ae2266de
commit 293e0f5faf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -101,7 +101,7 @@ namespace Jellyfin.Server.Infrastructure
count: null); count: null);
} }
private async Task SendFileAsync(string filePath, HttpResponse response, long offset, long? count) private async Task SendFileAsync(string filePath, HttpResponse response, long offset, long? count, CancellationToken cancellationToken = default)
{ {
var fileInfo = GetFileInfo(filePath); var fileInfo = GetFileInfo(filePath);
if (offset < 0 || offset > fileInfo.Length) if (offset < 0 || offset > fileInfo.Length)
@ -118,6 +118,9 @@ namespace Jellyfin.Server.Infrastructure
// Copied from SendFileFallback.SendFileAsync // Copied from SendFileFallback.SendFileAsync
const int BufferSize = 1024 * 16; const int BufferSize = 1024 * 16;
var useRequestAborted = !cancellationToken.CanBeCanceled;
var localCancel = useRequestAborted ? response.HttpContext.RequestAborted : cancellationToken;
var fileStream = new FileStream( var fileStream = new FileStream(
filePath, filePath,
FileMode.Open, FileMode.Open,
@ -127,11 +130,18 @@ namespace Jellyfin.Server.Infrastructure
options: FileOptions.Asynchronous | FileOptions.SequentialScan); options: FileOptions.Asynchronous | FileOptions.SequentialScan);
await using (fileStream.ConfigureAwait(false)) await using (fileStream.ConfigureAwait(false))
{ {
try
{
localCancel.ThrowIfCancellationRequested();
fileStream.Seek(offset, SeekOrigin.Begin); fileStream.Seek(offset, SeekOrigin.Begin);
await StreamCopyOperation await StreamCopyOperation
.CopyToAsync(fileStream, response.Body, count, BufferSize, CancellationToken.None) .CopyToAsync(fileStream, response.Body, count, BufferSize, localCancel)
.ConfigureAwait(true); .ConfigureAwait(true);
} }
catch (OperationCanceledException) when (useRequestAborted)
{
}
}
} }
private static bool IsSymLink(string path) => (File.GetAttributes(path) & FileAttributes.ReparsePoint) == FileAttributes.ReparsePoint; private static bool IsSymLink(string path) => (File.GetAttributes(path) & FileAttributes.ReparsePoint) == FileAttributes.ReparsePoint;

Loading…
Cancel
Save