File Coverage

blib/lib/Apertur/SDK/Resource/Upload.pm
Criterion Covered Total %
statement 17 52 32.6
branch 0 14 0.0
condition 0 16 0.0
subroutine 6 8 75.0
pod 2 3 66.6
total 25 93 26.8


line stmt bran cond sub pod time code
1             package Apertur::SDK::Resource::Upload;
2              
3 1     1   6 use strict;
  1         2  
  1         42  
4 1     1   6 use warnings;
  1         3  
  1         74  
5              
6 1     1   7 use JSON qw(encode_json);
  1         1  
  1         9  
7 1     1   179 use File::Basename qw(basename);
  1         2  
  1         109  
8 1     1   673 use Apertur::SDK::Crypto qw(encrypt_image);
  1         3  
  1         810  
9              
10             sub new {
11 3     3 0 5 my ($class, %args) = @_;
12 3         15 return bless { http => $args{http} }, $class;
13             }
14              
15             sub image {
16 0     0 1   my ($self, $uuid, $file, %options) = @_;
17              
18 0           my ($data, $filename);
19 0 0         if (ref $file eq 'SCALAR') {
20             # Raw bytes passed as scalar ref
21 0           $data = $$file;
22 0   0       $filename = $options{filename} || 'image.jpg';
23             }
24             else {
25             # File path string
26 0   0       $filename = $options{filename} || basename($file);
27 0 0         open my $fh, '<:raw', $file
28             or die "Cannot open file '$file': $!\n";
29 0           local $/;
30 0           $data = <$fh>;
31 0           close $fh;
32             }
33              
34 0   0       my $mime_type = $options{mimeType} || $options{mime_type} || 'image/jpeg';
35              
36 0           my @multipart = (
37             file => [
38             undef,
39             $filename,
40             'Content-Type' => $mime_type,
41             Content => $data,
42             ],
43             );
44              
45 0 0         if ($options{source}) {
46 0           push @multipart, source => $options{source};
47             }
48              
49 0           my %headers;
50 0 0         if ($options{password}) {
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             sub image_encrypted {
62 0     0 1   my ($self, $uuid, $file, $public_key, %options) = @_;
63              
64 0           my ($data, $filename);
65 0 0         if (ref $file eq 'SCALAR') {
66 0           $data = $$file;
67 0   0       $filename = $options{filename} || 'image.jpg';
68             }
69             else {
70 0   0       $filename = $options{filename} || basename($file);
71 0 0         open my $fh, '<:raw', $file
72             or die "Cannot open file '$file': $!\n";
73 0           local $/;
74 0           $data = <$fh>;
75 0           close $fh;
76             }
77              
78 0   0       my $mime_type = $options{mimeType} || $options{mime_type} || 'image/jpeg';
79 0           my $encrypted = encrypt_image($data, $public_key);
80              
81             my $payload = encode_json({
82             encryptedKey => $encrypted->{encrypted_key},
83             iv => $encrypted->{iv},
84             encryptedData => $encrypted->{encrypted_data},
85             algorithm => $encrypted->{algorithm},
86             filename => $filename,
87             mimeType => $mime_type,
88 0   0       source => $options{source} || 'sdk',
89             });
90              
91 0           my %headers = (
92             'Content-Type' => 'application/json',
93             'X-Aptr-Encrypted' => 'default',
94             );
95 0 0         if ($options{password}) {
96 0           $headers{'x-session-password'} = $options{password};
97             }
98              
99             return $self->{http}->request(
100 0           'POST', "/api/v1/upload/$uuid/images",
101             body => $payload,
102             headers => \%headers,
103             );
104             }
105              
106             1;
107              
108             __END__