File Coverage

blib/lib/WebService/Dropbox/Files/UploadSession.pm
Criterion Covered Total %
statement 12 57 21.0
branch 0 16 0.0
condition 0 5 0.0
subroutine 4 9 44.4
pod 0 4 0.0
total 16 91 17.5


line stmt bran cond sub pod time code
1             package WebService::Dropbox::Files::UploadSession;
2 8     8   46 use strict;
  8         18  
  8         188  
3 8     8   38 use warnings;
  8         18  
  8         182  
4 8     8   38 use parent qw(Exporter);
  8         17  
  8         44  
5              
6             our @EXPORT = do {
7 8     8   472 no strict 'refs';
  8         16  
  8         3485  
8             grep { $_ !~ qr{ \A [A-Z]+ \z }xms } keys %{ __PACKAGE__ . '::' };
9             };
10              
11             sub upload_session {
12 0     0 0   my ($self, $path, $content, $optional_params, $limit) = @_;
13              
14 0   0       $limit ||= 4 * 1024 * 1024; # A typical chunk is 4 MB
15              
16 0           my $session_id;
17 0           my $offset = 0;
18              
19             my $commit_params = {
20             path => $path,
21 0 0         %{ $optional_params || {} },
  0            
22             };
23              
24 0           my $upload;
25             $upload = sub {
26 0     0     my $buf;
27 0           my $total = 0;
28 0           my $chunk = 1024;
29 0           my $tmp = File::Temp->new;
30 0           my $is_last;
31 0           while (my $read = read($content, $buf, $chunk)) {
32 0           $tmp->print($buf);
33 0           $total += $read;
34 0           my $remaining = $limit - $total;
35 0 0         if ($chunk > $remaining) {
36 0           $chunk = $remaining;
37             }
38 0 0         unless ($chunk) {
39 0           last;
40             }
41             }
42              
43 0           $tmp->flush;
44 0           $tmp->seek(0, 0);
45              
46             # finish or small file
47 0 0         if ($total < $limit) {
    0          
48 0 0         if ($session_id) {
49 0           my $params = {
50             cursor => {
51             session_id => $session_id,
52             offset => $offset,
53             },
54             commit => $commit_params,
55             };
56 0           return $self->upload_session_finish($tmp, $params);
57             } else {
58 0           return $self->upload($path, $tmp, $commit_params);
59             }
60             }
61              
62             # append
63             elsif ($session_id) {
64 0           my $params = {
65             cursor => {
66             session_id => $session_id,
67             offset => $offset,
68             },
69             };
70 0 0         unless ($self->upload_session_append_v2($tmp, $params)) {
71             # some error
72 0           return;
73             }
74 0           $offset += $total;
75             }
76              
77             # start
78             else {
79 0           my $res = $self->upload_session_start($tmp);
80 0 0 0       if ($res && $res->{session_id}) {
81 0           $session_id = $res->{session_id};
82 0           $offset = $total;
83             } else {
84             # some error
85 0           return;
86             }
87             }
88              
89 0           $upload->();
90 0           };
91 0           $upload->();
92             }
93              
94             # https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-start
95             sub upload_session_start {
96 0     0 0   my ($self, $content, $params) = @_;
97              
98 0           $self->api({
99             url => 'https://content.dropboxapi.com/2/files/upload_session/start',
100             params => $params,
101             content => $content,
102             });
103             }
104              
105             # https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-append_v2
106             sub upload_session_append_v2 {
107 0     0 0   my ($self, $content, $params) = @_;
108              
109 0           $self->api({
110             url => 'https://content.dropboxapi.com/2/files/upload_session/append_v2',
111             params => $params,
112             content => $content,
113             });
114             }
115              
116             # https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-finish
117             sub upload_session_finish {
118 0     0 0   my ($self, $content, $params) = @_;
119              
120 0           $self->api({
121             url => 'https://content.dropboxapi.com/2/files/upload_session/finish',
122             params => $params,
123             content => $content,
124             });
125             }
126              
127             1;