@@ -1525,8 +1525,16 @@ public async Task<Stream> DownloadFileAndReturn(ChatMessages message, Stream ms
15251525 model . name = filename ;
15261526 _logger . LogInformation ( "Starting document download - FileName: {FileName}, Size: {SizeMB:F2}MB" , filename , document . size / ( 1024.0 * 1024.0 ) ) ;
15271527 MemoryStream dest = new MemoryStream ( ) ;
1528- await PrepareTransferClientAsync ( document . dc_id ) ;
1529- await client . DownloadFileAsync ( document , ms ?? dest , ( PhotoSizeBase ) null , model . ProgressCallback ) ;
1528+ // File-manager download tasks land here with a FileStream target,
1529+ // which supports positional writes for multi-connection mode.
1530+ bool multiConnDone = false ;
1531+ if ( ms is FileStream fileDest && ShouldUseMultiConnection ( document ) )
1532+ multiConnDone = await TryMultiConnectionDownloadAsync ( document , fileDest , model ) ;
1533+ if ( ! multiConnDone )
1534+ {
1535+ await PrepareTransferClientAsync ( document . dc_id ) ;
1536+ await client . DownloadFileAsync ( document , ms ?? dest , ( PhotoSizeBase ) null , model . ProgressCallback ) ;
1537+ }
15301538 _logger . LogInformation ( "Document download completed - FileName: {FileName}" , filename ) ;
15311539 return ms ?? dest ;
15321540 }
@@ -1574,8 +1582,14 @@ public async Task<Stream> DownloadFileAndReturnWithOffset(ChatMessages message,
15741582 if ( offset == 0 )
15751583 {
15761584 MemoryStream dest = new MemoryStream ( ) ;
1577- await PrepareTransferClientAsync ( document . dc_id ) ;
1578- await client . DownloadFileAsync ( document , ms ?? dest , ( PhotoSizeBase ) null , model . ProgressCallback ) ;
1585+ bool multiConnDone = false ;
1586+ if ( ms is FileStream fileDest && ShouldUseMultiConnection ( document ) )
1587+ multiConnDone = await TryMultiConnectionDownloadAsync ( document , fileDest , model ) ;
1588+ if ( ! multiConnDone )
1589+ {
1590+ await PrepareTransferClientAsync ( document . dc_id ) ;
1591+ await client . DownloadFileAsync ( document , ms ?? dest , ( PhotoSizeBase ) null , model . ProgressCallback ) ;
1592+ }
15791593 _logger . LogInformation ( "Document download completed - FileName: {FileName}" , filename ) ;
15801594 return ms ?? dest ;
15811595 }
0 commit comments