| 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 | my $dest = ref $entry ? $entry->{dest} : $entry; | ||
| 32 | |||
| 33 | if ( !-e $src ) { | ||
| 34 | $self->get_logger->warn("$src not found, skipping..."); | ||
| 35 | next; | ||
| 36 | } | ||
| 37 | |||
| 38 | my $content = slurp($src); | ||
| 39 | my $md5 = md5_base64($content); | ||
| 40 | my $stored = ref $entry ? $entry->{md5} : undef; | ||
| 41 | |||
| 42 | if ( defined $stored && $stored eq $md5 ) { | ||
| 43 | $self->get_logger->info("$src unchanged, skipping..."); | ||
| 44 | next; | ||
| 45 | } | ||
| 46 | |||
| 47 | $self->get_s3->put_object( $self->get_bucket_name, $dest, $content, | ||
| 48 | content_type => LWP::MediaTypes::guess_media_type($src), ); | ||
| 49 | |||
| 50 | $self->get_logger->info( sprintf 'uploaded %s => %s', $src, $dest ); | ||
| 51 | |||
| 52 | $files->{$src} = { dest => $dest, md5 => $md5 }; | ||
| 53 | $config_dirty = $TRUE; | ||
| 54 | } | ||
| 55 | |||
| 56 | if ($config_dirty) { | ||
| 57 | $self->write_config($config); | ||
| 58 | } | ||
| 59 | |||
| 60 | return $SUCCESS; | ||
| 61 | } | ||
| 62 | |||
| 63 | 1; | ||
| 64 | |||
| 65 | __END__ | ||
| 66 | |||
| 67 | =head1 NAME | ||
| 68 | |||
| 69 | OrePAN2::S3::Role::UploadArtifacts - Role providing cmd_upload_artifacts for OrePAN2::S3 | ||
| 70 | |||
| 71 | =head1 DESCRIPTION | ||
| 72 | |||
| 73 | Consumed by L<OrePAN2::S3>. Provides C<cmd_upload_artifacts> which uploads | ||
| 74 | multiple files listed in the configuration file to S3. | ||
| 75 | |||
| 76 | =cut |