33using System . IO ;
44using System . Linq ;
55using System . Net . Http ;
6+ using System . Security . Cryptography ;
67using System . Threading . Tasks ;
78using Newtonsoft . Json . Linq ;
89
@@ -53,8 +54,50 @@ private static Task DownloadFiles(string package, JObject entry)
5354 var files = ( JArray ) entry [ "files" ] ;
5455
5556 return Task . WhenAll ( files
56- . Select ( f => f [ "file" ] . Value < string > ( ) )
57- . Select ( f => Download . DistFile ( package , $ "{ version } /{ f } ") ) ) ;
57+ . Select ( f => DownloadFile ( package , version , ( JObject ) f ) ) ) ;
58+ }
59+
60+ private static Task DownloadFile ( string package , string version , JObject file )
61+ {
62+ var local = file [ "local" ] . Value < string > ( ) ;
63+ if ( File . Exists ( local ) )
64+ {
65+ var integrity = file [ "integrity" ] . Value < string > ( ) ;
66+ var integrityBits = integrity . Split ( '-' , 2 ) ;
67+ if ( integrityBits . Length == 2 )
68+ {
69+ var hashAlgorithm = GetAlgorithm ( integrityBits [ 0 ] ) ;
70+ if ( hashAlgorithm != null )
71+ {
72+ using ( var stream = File . OpenRead ( local ) )
73+ {
74+ var hash = hashAlgorithm . ComputeHash ( stream ) ;
75+ if ( integrityBits [ 1 ] . Equals ( Convert . ToBase64String ( hash ) ) )
76+ {
77+ Console . WriteLine ( $ "{ local } is up-to-date.") ;
78+ return Task . CompletedTask ;
79+ }
80+ }
81+ }
82+ }
83+ }
84+
85+ return Download . DistFile ( package , $ "{ version } /{ file [ "file" ] . Value < string > ( ) } ") ;
86+ }
87+
88+ private static HashAlgorithm GetAlgorithm ( string name )
89+ {
90+ switch ( name . ToLowerInvariant ( ) )
91+ {
92+ case "sha256" :
93+ return SHA256 . Create ( ) ;
94+ case "sha384" :
95+ return SHA384 . Create ( ) ;
96+ case "sha512" :
97+ return SHA512 . Create ( ) ;
98+ default :
99+ return null ;
100+ }
58101 }
59102 }
60103}
0 commit comments