@@ -479,14 +479,25 @@ private static bool TryGetMd5HashFromFilename([NotNull] string fileName, out byt
479479 }
480480#nullable restore
481481
482+ private static HashSet < FileInfo > _processingFiles = new ( ) ;
483+ private static HashSet < string > _processingUrls = new ( ) ;
484+
482485 public static async void TryDownloadToCompletenessAsync ( string url , FileInfo fileInfo , CancellationToken token )
483486 => await TryDownloadToCompleteness ( url , fileInfo , token ) ;
484487
485488 public static async ValueTask TryDownloadToCompleteness ( string url , FileInfo fileInfo , CancellationToken token )
486489 {
490+ if ( _processingFiles . Contains ( fileInfo ) || _processingUrls . Contains ( url ) )
491+ {
492+ Logger . LogWriteLine ( "Found duplicate download request, skipping...\r \n \t " +
493+ $ "URL : { url } ", LogType . Warning , true ) ;
494+ return ;
495+ }
487496 byte [ ] buffer = ArrayPool < byte > . Shared . Rent ( 4 << 10 ) ;
488497 try
489498 {
499+ _processingFiles . Add ( fileInfo ) ;
500+ _processingUrls . Add ( url ) ;
490501 // Initialize file temporary name
491502 FileInfo fileInfoTemp = new FileInfo ( fileInfo . FullName + "_temp" ) ;
492503 long fileLength = 0 ;
@@ -499,23 +510,27 @@ public static async ValueTask TryDownloadToCompleteness(string url, FileInfo fil
499510 // Try to get the remote stream and download the file
500511 await using ( Stream netStream = await FallbackCDNUtil . GetHttpStreamFromResponse ( url , token ) )
501512 {
502- await using ( Stream outStream = fileInfoTemp . Create ( ) )
513+ await using ( FileStream outStream = new FileStream ( fileInfoTemp . FullName , FileMode . Create ,
514+ FileAccess . ReadWrite , FileShare . ReadWrite ) )
503515 {
504516 // Get the file length
505517 fileLength = netStream . Length ;
506518
507519 // Create the prop file for download completeness checking
508520 string outputParentPath = Path . GetDirectoryName ( fileInfoTemp . FullName ) ;
509- string outputFilename = Path . GetFileName ( fileInfoTemp . FullName ) ;
521+ string outputFilename = Path . GetFileName ( fileInfoTemp . FullName ) ;
510522 if ( outputParentPath != null )
511523 {
512524 string propFilePath = Path . Combine ( outputParentPath , $ "{ outputFilename } #{ netStream . Length } ") ;
513- await File . Create ( propFilePath ) . DisposeAsync ( ) ;
525+ await using ( FileStream _ = new FileStream ( propFilePath , FileMode . Create , FileAccess . ReadWrite , FileShare . Delete ) )
526+ {
527+ // Just create the file
528+ }
514529 }
515530
516531 // Copy (and download) the remote streams to local
517532 int read ;
518- while ( ( read = await netStream . ReadAsync ( buffer , token ) ) > 0 )
533+ while ( ( read = await netStream . ReadAsync ( buffer , 0 , buffer . Length , token ) ) > 0 )
519534 await outStream . WriteAsync ( buffer , 0 , read , token ) ;
520535 }
521536 }
@@ -539,6 +554,8 @@ public static async ValueTask TryDownloadToCompleteness(string url, FileInfo fil
539554 finally
540555 {
541556 ArrayPool < byte > . Shared . Return ( buffer ) ;
557+ _processingFiles . Remove ( fileInfo ) ;
558+ _processingUrls . Remove ( url ) ;
542559 }
543560 }
544561
0 commit comments