| 1 | package Apertur::SDK::Resource::Upload; | ||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||
| 6 | 1 | ▶ | use JSON qw(encode_json); | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 7 | 1 | ▶ | use File::Basename qw(basename); | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 8 | 1 | ▶ | use Apertur::SDK::Crypto qw(encrypt_image); | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||
| 10 | 3 | ▶ | sub new { | ||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||
| 11 | 3 | my ($class, %args) = @_; | |||||||||||||||||||||||
| 12 | 3 | return bless { http => $args{http} }, $class; | |||||||||||||||||||||||
| 13 | } | ||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||
| 15 | 0 | ▶ | sub image { | ||||||||||||||||||||||
|
Subroutine
image: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||
| 16 | 0 | my ($self, $uuid, $file, %options) = @_; | |||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||
| 18 | 0 | my ($data, $filename); | |||||||||||||||||||||||
| 19 | 0 | ▶ | if (ref $file eq 'SCALAR') { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 20 | # Raw bytes passed as scalar ref | ||||||||||||||||||||||||
| 21 | 0 | $data = $$file; | |||||||||||||||||||||||
| 22 | 0 | ▶ | $filename = $options{filename} || 'image.jpg'; | ||||||||||||||||||||||
|
Condition0%0/2
$options{'filename'} || 'image.jpg'
MC/DC0%0/1
$options{'filename'} || 'image.jpg'
$options{'filename'}
Truth table0%0/2
A
A= $options{'filename'}
| |||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||
| 24 | else { | ||||||||||||||||||||||||
| 25 | # File path string | ||||||||||||||||||||||||
| 26 | 0 | ▶ | $filename = $options{filename} || basename($file); | ||||||||||||||||||||||
|
Condition0%0/3
$options{'filename'} || basename($file)
MC/DC0%0/2
$options{'filename'} || basename($file)
$options{'filename'}
basename($file)
Truth table0%0/3
A || B
A= $options{'filename'}B= basename($file)
| |||||||||||||||||||||||||
| 27 | 0 | ▶ | open my $fh, '<:raw', $file | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 28 | or die "Cannot open file '$file': $!\n"; | ||||||||||||||||||||||||
| 29 | 0 | local $/; | |||||||||||||||||||||||
| 30 | 0 | $data = <$fh>; | |||||||||||||||||||||||
| 31 | 0 | close $fh; | |||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||
| 34 | 0 | ▶ | my $mime_type = $options{mimeType} || $options{mime_type} || 'image/jpeg'; | ||||||||||||||||||||||
|
Condition0%0/5
$options{'mimeType'} || $options{'mime_type'} || 'image/jpeg'
$options{"mimeType"} or $options{"mime_type"}
MC/DC0%0/2
$options{'mimeType'} || $options{'mime_type'} || 'image/jpeg'
$options{"mimeType"}
$options{"mime_type"}
Truth table0%0/3
A or B
A= $options{"mimeType"}B= $options{"mime_type"}
| |||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||
| 36 | 0 | my @multipart = ( | |||||||||||||||||||||||
| 37 | file => [ | ||||||||||||||||||||||||
| 38 | undef, | ||||||||||||||||||||||||
| 39 | $filename, | ||||||||||||||||||||||||
| 40 | 'Content-Type' => $mime_type, | ||||||||||||||||||||||||
| 41 | Content => $data, | ||||||||||||||||||||||||
| 42 | ], | ||||||||||||||||||||||||
| 43 | ); | ||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||
| 45 | 0 | ▶ | if ($options{source}) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 46 | 0 | push @multipart, source => $options{source}; | |||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||
| 49 | 0 | my %headers; | |||||||||||||||||||||||
| 50 | 0 | ▶ | if ($options{password}) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 51 | 0 | $headers{'x-session-password'} = $options{password}; | |||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||
| 54 | return $self->{http}->request( | ||||||||||||||||||||||||
| 55 | 0 | 'POST', "/api/v1/upload/$uuid/images", | |||||||||||||||||||||||
| 56 | multipart => \@multipart, | ||||||||||||||||||||||||
| 57 | headers => \%headers, | ||||||||||||||||||||||||
| 58 | ); | ||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||
| 61 | 0 | ▶ | sub image_encrypted { | ||||||||||||||||||||||
|
Subroutine
image_encrypted: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||
| 62 | 0 | my ($self, $uuid, $file, $public_key, %options) = @_; | |||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||
| 64 | 0 | my ($data, $filename); | |||||||||||||||||||||||
| 65 | 0 | ▶ | if (ref $file eq 'SCALAR') { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 66 | 0 | $data = $$file; | |||||||||||||||||||||||
| 67 | 0 | ▶ | $filename = $options{filename} || 'image.jpg'; | ||||||||||||||||||||||
|
Condition0%0/2
$options{'filename'} || 'image.jpg'
MC/DC0%0/1
$options{'filename'} || 'image.jpg'
$options{'filename'}
Truth table0%0/2
A
A= $options{'filename'}
| |||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||
| 69 | else { | ||||||||||||||||||||||||
| 70 | 0 | ▶ | $filename = $options{filename} || basename($file); | ||||||||||||||||||||||
|
Condition0%0/3
$options{'filename'} || basename($file)
MC/DC0%0/2
$options{'filename'} || basename($file)
$options{'filename'}
basename($file)
Truth table0%0/3
A || B
A= $options{'filename'}B= basename($file)
| |||||||||||||||||||||||||
| 71 | 0 | ▶ | open my $fh, '<:raw', $file | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 72 | or die "Cannot open file '$file': $!\n"; | ||||||||||||||||||||||||
| 73 | 0 | local $/; | |||||||||||||||||||||||
| 74 | 0 | $data = <$fh>; | |||||||||||||||||||||||
| 75 | 0 | close $fh; | |||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||
| 78 | 0 | my $encrypted = encrypt_image($data, $public_key); | |||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||
| 80 | # The server's `default` encrypted mode reads a multipart upload, parses | ||||||||||||||||||||||||
| 81 | # the file bytes as a JSON envelope (camelCase), then RSA-OAEP-SHA256 | ||||||||||||||||||||||||
| 82 | # decrypts. Send the envelope as a file part, mirroring the plain | ||||||||||||||||||||||||
| 83 | # `image` multipart transport. | ||||||||||||||||||||||||
| 84 | my $envelope = encode_json({ | ||||||||||||||||||||||||
| 85 | encryptedKey => $encrypted->{encrypted_key}, | ||||||||||||||||||||||||
| 86 | iv => $encrypted->{iv}, | ||||||||||||||||||||||||
| 87 | encryptedData => $encrypted->{encrypted_data}, | ||||||||||||||||||||||||
| 88 | algorithm => $encrypted->{algorithm}, | ||||||||||||||||||||||||
| 89 | 0 | }); | |||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||
| 91 | 0 | my @multipart = ( | |||||||||||||||||||||||
| 92 | file => [ | ||||||||||||||||||||||||
| 93 | undef, | ||||||||||||||||||||||||
| 94 | 'image.jpg.enc', | ||||||||||||||||||||||||
| 95 | 'Content-Type' => 'application/octet-stream', | ||||||||||||||||||||||||
| 96 | Content => $envelope, | ||||||||||||||||||||||||
| 97 | ], | ||||||||||||||||||||||||
| 98 | ); | ||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||
| 100 | 0 | ▶ | if ($options{source}) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 101 | 0 | push @multipart, source => $options{source}; | |||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||
| 104 | 0 | my %headers = ( | |||||||||||||||||||||||
| 105 | 'X-Aptr-Encrypted' => 'default', | ||||||||||||||||||||||||
| 106 | ); | ||||||||||||||||||||||||
| 107 | 0 | ▶ | if ($options{password}) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 108 | 0 | $headers{'x-session-password'} = $options{password}; | |||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||
| 111 | return $self->{http}->request( | ||||||||||||||||||||||||
| 112 | 0 | 'POST', "/api/v1/upload/$uuid/images", | |||||||||||||||||||||||
| 113 | multipart => \@multipart, | ||||||||||||||||||||||||
| 114 | headers => \%headers, | ||||||||||||||||||||||||
| 115 | ); | ||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||
| 118 | 1 | 1; | |||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||
| 120 | __END__ | ||||||||||||||||||||||||