@@ -42,6 +42,24 @@ my $workspace = {
4242 settings => {},
4343};
4444
45+ sub _check_integrity {
46+ my ($integrity , $file_or_content ) = @_ ;
47+
48+ my ($algorithm ) = $integrity =~ m {\A sha(\d +)} ;
49+ my $sha = Digest::SHA-> new($algorithm );
50+ if (ref $file_or_content ) {
51+
52+ $sha -> addfile($file_or_content );
53+ } else {
54+ $sha -> add($file_or_content );
55+ }
56+ my $digest = $sha -> b64digest;
57+ $digest .= (' =' x ((4 - (length ($digest ) % 4)) % 4));
58+ my $file_integrity = " sha256-" . $digest ;
59+
60+ return $integrity eq (" sha$algorithm -" . $digest );
61+ }
62+
4563sub handle_recipe {
4664 my $data = shift ;
4765
@@ -70,28 +88,17 @@ sub handle_recipe {
7088 my $rel_path = catfile($k , $basename );
7189 my $full_path = catfile($archive_dir , $rel_path );
7290
73- my ($algorithm ) = $integrity =~ m {\A sha(\d +)} ;
74- my $sha = Digest::SHA-> new($algorithm );
75-
76- if (-f $full_path ) {
77- $sha -> addfile(IO::File-> new($full_path , ' r' ));
78- my $digest = $sha -> b64digest;
79- $digest .= (' =' x ((4 - (length ($digest ) % 4)) % 4));
80- my $file_integrity = " sha$algorithm -" . $digest ;
81- if ($file_integrity eq $integrity ) {
82- $archives .= " $rel_path " ;
83- next ;
84- }
91+ if (-f $full_path && _check_integrity($integrity , IO::File-> new($full_path , ' r' ))) {
92+ $archives .= " $rel_path " ;
93+ next ;
8594 }
8695
8796 mkpath(dirname($full_path ));
8897 my $response = HTTP::Tiny-> new-> get($url );
8998 die " Failed: @{[Dumper($response )]}"
9099 unless $response -> {success } && length $response -> {content };
91100
92- $sha -> add($response -> {content });
93- my $digest = " sha$algorithm -" . $sha -> base64_padded_digest;
94- if ($digest ne $integrity ) {
101+ if (!_check_integrity($integrity , $response -> {content })) {
95102 die " intergrity check failed: $url " ;
96103 }
97104
0 commit comments