@@ -313,6 +313,8 @@ private async Task SaveFileToFilesystemStreaming(DownloadClientValue client, Str
313313 using var scope = ServiceProvider . CreateScope ( ) ;
314314 var postgres = scope . ServiceProvider . GetRequiredService < PostgresContext > ( ) ;
315315 var hub = scope . ServiceProvider . GetRequiredService < IHubContext < ServerHub , IServerHub > > ( ) ;
316+ var cancellationToken = client . CancellationToken ;
317+ if ( cancellationToken . IsCancellationRequested ) return ;
316318
317319 try
318320 {
@@ -340,9 +342,9 @@ private async Task SaveFileToFilesystemStreaming(DownloadClientValue client, Str
340342 int bytesRead ;
341343 var stopwatch = Stopwatch . StartNew ( ) ; // Start timing like your original code
342344
343- while ( ( bytesRead = await inputStream . ReadAsync ( buffer ) ) > 0 )
345+ while ( ( bytesRead = await inputStream . ReadAsync ( buffer , cancellationToken . Token ) ) > 0 )
344346 {
345- await fileStream . WriteAsync ( buffer , 0 , bytesRead ) ;
347+ await fileStream . WriteAsync ( buffer , 0 , bytesRead , cancellationToken . Token ) ;
346348 totalBytesRead += bytesRead ;
347349
348350 // Progress tracking equivalent to WebClient_DownloadProgressChanged
@@ -353,7 +355,7 @@ private async Task SaveFileToFilesystemStreaming(DownloadClientValue client, Str
353355
354356 // Existing database save logic preserved
355357 dbUser . Files . Add ( dbFile ) ;
356- await postgres . SaveChangesAsync ( ) ;
358+ await postgres . SaveChangesAsync ( cancellationToken . Token ) ;
357359
358360 // Completion equivalent to WebClient_DownloadDataCompleted
359361 await ReportDownloadCompleted ( hub , client ) ;
0 commit comments