| 1 | package OrePAN2::S3::Role::UploadArtifacts; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use CLI::Simple::Constants qw(:booleans); | ||
| 7 | use CLI::Simple::Utils qw(slurp); | ||
| 8 | use Digest::MD5 qw(md5_base64); | ||
| 9 | |||
| 10 | use Role::Tiny; | ||
| 11 | |||
| 12 | ######################################################################## | ||
| 13 | sub cmd_upload_artifacts { | ||
| 14 | ######################################################################## | ||
| 15 | my ($self) = @_; | ||
| 16 | |||
| 17 | require LWP::MediaTypes; | ||
| 18 | |||
| 19 | my $config = $self->get_config; | ||
| 20 | my $files = $config->{index}{files} // {}; | ||
| 21 | |||
| 22 | if ( !keys %{$files} ) { | ||
| 23 | $self->get_logger->info('no artifacts configured '); | ||
| 24 | return $SUCCESS; | ||
| 25 | } | ||
| 26 | |||
| 27 | my $config_dirty = $FALSE; | ||
| 28 | |||
| 29 | for my $src ( sort keys %{$files} ) { | ||
| 30 | my $entry = $files->{$src}; | ||
| 31 | |||
| 32 | my $path = $src !~ /^\//xsm ? $src : sprintf '%s/%s', $self->get_dist_dir, $src; | ||
| 33 | |||
| 34 | my $dest = ref $entry ? $entry->{dest} : $entry; | ||
| 35 | |||
| 36 | if ( !-e $path ) { | ||
| 37 | $self->get_logger->warn("$path not found, skipping..."); | ||
| 38 | next; | ||
| 39 | } | ||
| 40 | |||
| 41 | my $content = slurp($path); | ||
| 42 | my $md5 = md5_base64($content); | ||
| 43 | my $stored = ref $entry ? $entry->{md5} : undef; | ||
| 44 | |||
| 45 | if ( defined $stored && $stored eq $md5 ) { | ||
| 46 | $self->get_logger->info("$src unchanged, skipping..."); | ||
| 47 | next; | ||
| 48 | } | ||
| 49 | |||
| 50 | $self->get_s3->put_object( $self->get_bucket_name, $dest, $content, | ||
| 51 | content_type => LWP::MediaTypes::guess_media_type($path), ); | ||
| 52 | |||
| 53 | $self->get_logger->info( sprintf 'uploaded %s => %s', $path, $dest ); | ||
| 54 | |||
| 55 | $files->{$src} = { dest => $dest, md5 => $md5 }; | ||
| 56 | $config_dirty = $TRUE; | ||
| 57 | } | ||
| 58 | |||
| 59 | if ($config_dirty) { | ||
| 60 | $self->write_config($config); | ||
| 61 | } | ||
| 62 | |||
| 63 | return $SUCCESS; | ||
| 64 | } | ||
| 65 | |||
| 66 | 1; | ||
| 67 | |||
| 68 | __END__ | ||
| 69 | |||
| 70 | =head1 NAME | ||
| 71 | |||
| 72 | OrePAN2::S3::Role::UploadArtifacts - Role providing cmd_upload_artifacts for OrePAN2::S3 | ||
| 73 | |||
| 74 | =head1 DESCRIPTION | ||
| 75 | |||
| 76 | Consumed by L<OrePAN2::S3>. Provides C<cmd_upload_artifacts> which uploads | ||
| 77 | multiple files listed in the configuration file to S3. | ||
| 78 | |||
| 79 | =cut |