| 1 | package Shell::Amazon::S3::Command::putfilewacl; | ||
| 2 | use Moose; | ||
| 3 | use Path::Class qw(file); | ||
| 4 | |||
| 5 | extends 'Shell::Amazon::S3::CommandWithBucket'; | ||
| 6 | |||
| 7 | has '+desc' => ( default => | ||
| 8 | "putfilewacl <id> <file> ['private'|'public-read'|'public-read-write'|'authenticated-read']" | ||
| 9 | ); | ||
| 10 | |||
| 11 | override 'validate_tokens', sub { | ||
| 12 | my ( $self, $tokens ) = @_; | ||
| 13 | if ( !@{$tokens} == 3 ) { | ||
| 14 | return ( 0, | ||
| 15 | "error: putfilewacl <id> <file> ['private'|'public-read'|'public-read-write'|'authenticated-read']" | ||
| 16 | ); | ||
| 17 | } | ||
| 18 | |||
| 19 | if ( @{$tokens} == 3 ) { | ||
| 20 | my $acl = $tokens->[2]; | ||
| 21 | unless ( $self->is_valid_acl($acl) ) { | ||
| 22 | return ( 0, | ||
| 23 | "acl must be ['private'|'public-read'|'public-read-write'|'authenticated-read']" | ||
| 24 | ); | ||
| 25 | } | ||
| 26 | } | ||
| 27 | |||
| 28 | return ( 1, '' ); | ||
| 29 | }; | ||
| 30 | |||
| 31 | override 'parse_tokens', sub { | ||
| 32 | my ( $self, $tokens ) = @_; | ||
| 33 | my $args = {}; | ||
| 34 | $args->{key} = $tokens->[0]; | ||
| 35 | $args->{filename} = $tokens->[1]; | ||
| 36 | $args->{acl} = $tokens->[2]; | ||
| 37 | $args; | ||
| 38 | }; | ||
| 39 | |||
| 40 | sub is_valid_acl { | ||
| 41 | my ( $self, $acl ) = @_; | ||
| 42 | unless ( $acl eq 'private' | ||
| 43 | || $acl eq 'public-read' | ||
| 44 | || $acl eq 'public-read-write' | ||
| 45 | || $acl eq 'authenticated-read' ) | ||
| 46 | { | ||
| 47 | return 0; | ||
| 48 | } | ||
| 49 | return 1; | ||
| 50 | } | ||
| 51 | |||
| 52 | sub execute { | ||
| 53 | my ( $self, $args ) = @_; | ||
| 54 | my $bucket = $self->bucket; | ||
| 55 | my $key = $args->{key}; | ||
| 56 | my $filename = $args->{filename}; | ||
| 57 | my $file = file($filename); | ||
| 58 | my $data = $file->slurp; | ||
| 59 | |||
| 60 | my $status = $bucket->add_key( $key, $data ); | ||
| 61 | |||
| 62 | my $acl = $args->{acl}; | ||
| 63 | my $is_success = $bucket->set_acl( { acl_short => $acl, key => $key, } ); | ||
| 64 | |||
| 65 | # TODO: error handling | ||
| 66 | my $result = ''; | ||
| 67 | $result .= "Uploaded: $key\n"; | ||
| 68 | |||
| 69 | $result; | ||
| 70 | |||
| 71 | } | ||
| 72 | |||
| 73 | 1; |