private async Task WriteBlob(HttpContext httpContext, BlobInfo blobInfo) { if (await this.TryCached(httpContext, blobInfo)) { return; } FileStream currentFile = null; await this.WriteBlobHeaders(httpContext, blobInfo); try { var blocks = await this.GetBlocks(blobInfo); string?currentFilePath = null; foreach (var block in blocks) { if (currentFilePath != block.FilePath) { if (currentFile != null) { await currentFile.DisposeAsync(); } currentFile = File.Open(block.FilePath, FileMode.Open, FileAccess.Read, FileShare.Read); } if (currentFile == null) { throw new Exception("Error finding backing file for block"); } var blockBuffer = blockCopyPool.Rent((int)block.Length); currentFile.Position = block.StartOffset; // Can't use Span overload since we're renting the buffer await currentFile.ReadAsync(blockBuffer, 0, (int)block.Length); await httpContext.Response.Body.WriteAsync(blockBuffer, 0, (int)block.Length); } await httpContext.Response.Body.FlushAsync(); return; } catch (FileNotFoundException) { } catch (DirectoryNotFoundException) { } finally { currentFile?.Dispose(); } httpContext.Response.StatusCode = 500; await this.WriteText(httpContext, $"File backing blocks were not found for '{blobInfo.BlobName}'"); }