| 1 | |||||||||||||
| 2 | package AWS::S3::File; | ||||||||||||
| 3 | |||||||||||||
| 4 | 6 | ▶ | use Moose; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 6 | ▶ | use Carp 'confess'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 6 | ▶ | use MooseX::Types -declare => [qw/fileContents/]; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 6 | ▶ | use MooseX::Types::Moose qw/Str ScalarRef CodeRef/; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | 6 | subtype fileContents, as ScalarRef; | |||||||||||
| 11 | coerce fileContents, | ||||||||||||
| 12 | from CodeRef, | ||||||||||||
| 13 | 2 | ▶ | via { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||
| 14 | 2 | my $ref = $_[0]; | |||||||||||
| 15 | 2 | my $v = $ref->(); | |||||||||||
| 16 | 2 | ▶ | ref $v ? $v : \$v | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 17 | } | ||||||||||||
| 18 | 6 | ; | |||||||||||
| 19 | |||||||||||||
| 20 | 6 | has 'key' => ( | |||||||||||
| 21 | is => 'ro', | ||||||||||||
| 22 | isa => 'Str', | ||||||||||||
| 23 | required => 1, | ||||||||||||
| 24 | ); | ||||||||||||
| 25 | |||||||||||||
| 26 | 6 | has 'bucket' => ( | |||||||||||
| 27 | is => 'ro', | ||||||||||||
| 28 | isa => 'AWS::S3::Bucket', | ||||||||||||
| 29 | required => 1, | ||||||||||||
| 30 | weak_ref => 0, | ||||||||||||
| 31 | ); | ||||||||||||
| 32 | |||||||||||||
| 33 | has 'size' => ( | ||||||||||||
| 34 | is => 'ro', | ||||||||||||
| 35 | isa => 'Int', | ||||||||||||
| 36 | required => 0, | ||||||||||||
| 37 | 2 | ▶ | default => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 38 | 2 | my $self = shift; | |||||||||||
| 39 | 2 | return length ${$self->contents}; | |||||||||||
| 40 | } | ||||||||||||
| 41 | 6 | ); | |||||||||||
| 42 | |||||||||||||
| 43 | 6 | has 'etag' => ( | |||||||||||
| 44 | is => 'ro', | ||||||||||||
| 45 | isa => 'Str', | ||||||||||||
| 46 | required => 0, | ||||||||||||
| 47 | ); | ||||||||||||
| 48 | |||||||||||||
| 49 | 6 | has 'owner' => ( | |||||||||||
| 50 | is => 'ro', | ||||||||||||
| 51 | isa => 'AWS::S3::Owner', | ||||||||||||
| 52 | required => 0, | ||||||||||||
| 53 | weak_ref => 1, | ||||||||||||
| 54 | ); | ||||||||||||
| 55 | |||||||||||||
| 56 | 6 | has 'storage_class' => ( | |||||||||||
| 57 | is => 'ro', | ||||||||||||
| 58 | isa => 'Str', | ||||||||||||
| 59 | default => 'STANDARD', | ||||||||||||
| 60 | required => 1, | ||||||||||||
| 61 | ); | ||||||||||||
| 62 | |||||||||||||
| 63 | 6 | has 'lastmodified' => ( | |||||||||||
| 64 | is => 'ro', | ||||||||||||
| 65 | isa => 'Str', | ||||||||||||
| 66 | required => 0, | ||||||||||||
| 67 | ); | ||||||||||||
| 68 | |||||||||||||
| 69 | 6 | has 'contenttype' => ( | |||||||||||
| 70 | is => 'rw', | ||||||||||||
| 71 | isa => 'Str', | ||||||||||||
| 72 | required => 0, | ||||||||||||
| 73 | default => 'binary/octet-stream' | ||||||||||||
| 74 | ); | ||||||||||||
| 75 | |||||||||||||
| 76 | has 'is_encrypted' => ( | ||||||||||||
| 77 | is => 'rw', | ||||||||||||
| 78 | isa => 'Bool', | ||||||||||||
| 79 | required => 1, | ||||||||||||
| 80 | lazy => 1, | ||||||||||||
| 81 | 1 | ▶ | default => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||
| 82 | 1 | my $s = shift; | |||||||||||
| 83 | |||||||||||||
| 84 | 1 | my $type = 'GetFileInfo'; | |||||||||||
| 85 | 1 | my $req = $s->bucket->s3->request( | |||||||||||
| 86 | $type, | ||||||||||||
| 87 | bucket => $s->bucket->name, | ||||||||||||
| 88 | key => $s->key, | ||||||||||||
| 89 | ); | ||||||||||||
| 90 | |||||||||||||
| 91 | 1 | ▶ | return $req->request->response->header( 'x-amz-server-side-encryption' ) ? 1 : 0; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 92 | }, | ||||||||||||
| 93 | 6 | ); | |||||||||||
| 94 | |||||||||||||
| 95 | 6 | has 'contents' => ( | |||||||||||
| 96 | is => 'rw', | ||||||||||||
| 97 | isa => fileContents, | ||||||||||||
| 98 | required => 0, | ||||||||||||
| 99 | lazy => 1, | ||||||||||||
| 100 | coerce => 1, | ||||||||||||
| 101 | default => \&_get_contents, | ||||||||||||
| 102 | trigger => \&_set_contents | ||||||||||||
| 103 | ); | ||||||||||||
| 104 | |||||||||||||
| 105 | 14 | ▶ | sub BUILD { | ||||||||||
|
Subroutine
BUILD: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||
| 106 | 14 | my $s = shift; | |||||||||||
| 107 | |||||||||||||
| 108 | 14 | ▶ | return unless $s->etag; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 109 | 14 | ( my $etag = $s->etag ) =~ s{^"}{}; | |||||||||||
| 110 | 14 | $etag =~ s{"$}{}; | |||||||||||
| 111 | 14 | $s->{etag} = $etag; | |||||||||||
| 112 | } # end BUILD() | ||||||||||||
| 113 | |||||||||||||
| 114 | 4 | ▶ | sub update { | ||||||||||
|
Subroutine
update: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 115 | 4 | my $s = shift; | |||||||||||
| 116 | 4 | my %args = @_; | |||||||||||
| 117 | 4 | my @args_ok = grep { /^content(?:s|type)$/ } keys %args; | |||||||||||
| 118 | 4 | ▶ | if ( @args_ok ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 119 | 2 | $s->{$_} = $args{$_} for @args_ok; | |||||||||||
| 120 | 2 | $s->_set_contents(); | |||||||||||
| 121 | 2 | return 1; | |||||||||||
| 122 | } | ||||||||||||
| 123 | 2 | return; | |||||||||||
| 124 | } # end update() | ||||||||||||
| 125 | |||||||||||||
| 126 | 2 | ▶ | sub _get_contents { | ||||||||||
|
Subroutine
_get_contents: called CC=1 SCAR=0.0
| |||||||||||||
| 127 | 2 | my $s = shift; | |||||||||||
| 128 | |||||||||||||
| 129 | 2 | my $type = 'GetFileContents'; | |||||||||||
| 130 | 2 | my $req = $s->bucket->s3->request( | |||||||||||
| 131 | $type, | ||||||||||||
| 132 | bucket => $s->bucket->name, | ||||||||||||
| 133 | key => $s->key, | ||||||||||||
| 134 | ); | ||||||||||||
| 135 | |||||||||||||
| 136 | 2 | return \$req->request->response->decoded_content; | |||||||||||
| 137 | } # end contents() | ||||||||||||
| 138 | |||||||||||||
| 139 | 6 | ▶ | sub _set_contents { | ||||||||||
|
Subroutine
_set_contents: called CC=3 SCAR=11.1
| |||||||||||||
| 140 | 6 | my ( $s, $ref ) = @_; | |||||||||||
| 141 | |||||||||||||
| 142 | 6 | my $type = 'SetFileContents'; | |||||||||||
| 143 | 6 | my %args = (); | |||||||||||
| 144 | 6 | ▶ | my $response = $s->bucket->s3->request( | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 145 | $type, | ||||||||||||
| 146 | bucket => $s->bucket->name, | ||||||||||||
| 147 | file => $s, | ||||||||||||
| 148 | contents => $ref, | ||||||||||||
| 149 | content_type => $s->contenttype, | ||||||||||||
| 150 | server_side_encryption => $s->is_encrypted ? 'AES256' : undef, | ||||||||||||
| 151 | )->request(); | ||||||||||||
| 152 | |||||||||||||
| 153 | 6 | ( my $etag = $response->response->header( 'etag' ) ) =~ s{^"}{}; | |||||||||||
| 154 | 6 | $etag =~ s{"$}{}; | |||||||||||
| 155 | 6 | $s->{etag} = $etag; | |||||||||||
| 156 | |||||||||||||
| 157 | 6 | ▶ | if ( my $msg = $response->friendly_error() ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 158 | 0 | die $msg; | |||||||||||
| 159 | } # end if() | ||||||||||||
| 160 | } # end _set_contents() | ||||||||||||
| 161 | |||||||||||||
| 162 | 2 | ▶ | sub signed_url { | ||||||||||
|
Subroutine
signed_url: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||||
| 163 | 2 | my $s = shift; | |||||||||||
| 164 | 2 | ▶ | my $expires = shift || 3600; | ||||||||||
|
Condition50%1/2
shift() || 3600
MC/DC0%0/1
shift() || 3600
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||
| 165 | |||||||||||||
| 166 | # expiry for v4 signature is in seconds, not epoch time | ||||||||||||
| 167 | 2 | ▶ | if ( $expires > time ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 168 | 0 | $expires -= time; | |||||||||||
| 169 | } | ||||||||||||
| 170 | |||||||||||||
| 171 | 2 | my $key = $s->key; | |||||||||||
| 172 | |||||||||||||
| 173 | 2 | ▶ | if ( ! $s->bucket->s3->honor_leading_slashes ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 174 | 1 | $key =~ s!^/!!; | |||||||||||
| 175 | } | ||||||||||||
| 176 | |||||||||||||
| 177 | 2 | my $type = "GetPreSignedUrl"; | |||||||||||
| 178 | 2 | my $uri = $s->bucket->s3->request( | |||||||||||
| 179 | $type, | ||||||||||||
| 180 | bucket => $s->bucket->name, | ||||||||||||
| 181 | key => $key, | ||||||||||||
| 182 | expires => $expires, | ||||||||||||
| 183 | )->request; | ||||||||||||
| 184 | |||||||||||||
| 185 | 2 | return $uri; | |||||||||||
| 186 | } | ||||||||||||
| 187 | |||||||||||||
| 188 | 2 | ▶ | sub delete { | ||||||||||
|
Subroutine
delete: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||
| 189 | 2 | my $s = shift; | |||||||||||
| 190 | |||||||||||||
| 191 | 2 | my $type = 'DeleteFile'; | |||||||||||
| 192 | 2 | my $req = $s->bucket->s3->request( | |||||||||||
| 193 | $type, | ||||||||||||
| 194 | bucket => $s->bucket->name, | ||||||||||||
| 195 | key => $s->key, | ||||||||||||
| 196 | ); | ||||||||||||
| 197 | 2 | my $response = $req->request(); | |||||||||||
| 198 | |||||||||||||
| 199 | 2 | ▶ | if ( my $msg = $response->friendly_error() ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 200 | 0 | die $msg; | |||||||||||
| 201 | } # end if() | ||||||||||||
| 202 | |||||||||||||
| 203 | 2 | return 1; | |||||||||||
| 204 | } # end delete() | ||||||||||||
| 205 | |||||||||||||
| 206 | 6 | __PACKAGE__->meta->make_immutable; | |||||||||||
| 207 | |||||||||||||
| 208 | __END__ | ||||||||||||