blib/lib/File/Dropbox.pm

filter: Statementstmt 21.1% 80 / 379 Branchbran 6.9% 8 / 116 Conditioncond 11.6% 7 / 60 MC/DCmcdc 8.8% 3 / 34 Subroutinesub 62.8% 22 / 35 Podpod 100% 8 / 8 totaltotal 20.2% 128 / 632 SCAR 83.0
CC
83
Cov
17%
READLINE
313.3
__flush__
116.7
CLOSE
90.0
CRAP 4005.4
↑ Summary Build »
1 package File::Dropbox;
2 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use feature ':5.10';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use base qw{ Tie::Handle Exporter };
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use Errno qw{ ENOENT EISDIR EINVAL EPERM EACCES EAGAIN ECANCELED EFBIG };
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use Fcntl qw{ SEEK_CUR SEEK_SET SEEK_END };
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶use Furl;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 12 ▶use IO::Socket::SSL;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 12 ▶use Net::DNS::Lite;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 12 our $VERSION = 0.6;
15 12 our @EXPORT_OK = qw{ contents metadata putfile movefile copyfile createfolder deletefile };
16  
17 12 my $hosts = {
18 content => 'api-content.dropbox.com',
19 api => 'api.dropbox.com',
20 };
21  
22 12 my $version = 1;
23  
24 12 my $header1 = join ', ',
25 'OAuth oauth_version="1.0"',
26 'oauth_signature_method="PLAINTEXT"',
27 'oauth_consumer_key="%s"',
28 'oauth_token="%s"',
29 'oauth_signature="%s&%s"';
30  
31 12 my $header2 = 'Bearer %s';
32  
33 sub new {
34 13 ▶ my $self = Symbol::gensym;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
35 13 tie *$self, __PACKAGE__, my $this = { @_[1 .. @_ - 1] };
36  
37 12 *$self = $this;
38  
39 12 return $self;
40 } # new
41  
42 sub TIEHANDLE {
43 13 ▶ my $self = bless $_[1], ref $_[0] || $_[0];
Subroutine TIEHANDLE: called CC=6 SCAR=18.0
Condition33%1/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
0130
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table33%1/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
44  
45 13 ▶ $self->{'chunk'} //= 4 << 20;
Condition100%2/2
$self->{"chunk"} //= 4194304
l!l
211
MC/DC100%1/1
$self->{"chunk"} //= 4194304
$self->{"chunk"}
Truth table100%2/2
A
A= $self->{"chunk"}
Aresult
11
00
46 13 ▶ $self->{'root'} //= 'sandbox';
Condition100%2/2
$self->{"root"} //= "sandbox"
l!l
112
MC/DC100%1/1
$self->{"root"} //= "sandbox"
$self->{"root"}
Truth table100%2/2
A
A= $self->{"root"}
Aresult
11
00
47  
48 die 'Unexpected root value'
49 13 ▶ unless $self->{'root'} =~ m{^(?:drop|sand)box$};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"root"} =~ /^(?:drop|sand)box$/ 1 12 13
50  
51 $self->{'furl'} = Furl->new(
52 timeout => 10,
53 inet_aton => \&Net::DNS::Lite::inet_aton,
54 ssl_opts => {
55 SSL_verify_mode => SSL_VERIFY_PEER(),
56 },
57  
58 12 ▶ %{ $self->{'furlopts'} //= {} },
Condition100%2/2
$self->{"furlopts"} //= {}
l!l
111
MC/DC100%1/1
$self->{"furlopts"} //= {}
$self->{"furlopts"}
Truth table100%2/2
A
A= $self->{"furlopts"}
Aresult
11
00
59 );
60  
61 12 $self->{'closed'} = 1;
62 12 $self->{'length'} = 0;
63 12 $self->{'position'} = 0;
64 12 $self->{'mode'} = '';
65 12 $self->{'buffer'} = '';
66  
67 12 return $self;
68 } # TIEHANDLE
69  
70 sub READ {
71 1 ▶ my ($self, undef, $length, $offset) = @_;
Subroutine READ: called CC=10 SCAR=44.7
72  
73 1 undef $!;
74  
75 die 'Read is not supported on this handle'
76 1 ▶ if $self->{'mode'} ne '<';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"mode"} ne "<" 1 0 1
77  
78 0 ▶ substr($_[1] //= '', $offset // 0) = '', return 0
Branch: 1 branch
BranchTrueFalseTotal
if $self->EOF 0 0 0
Condition0%0/4
$_[1] //= ""
l!l
00
$offset // 0
l!l
00
MC/DC0%0/1
$_[1] //= ""
$_[1]
MC/DC0%0/1
$offset // 0
$offset
Truth table0%0/2
A
A= $_[1]
Aresult
11
00
Truth table0%0/2
A
A= $offset
Aresult
11
00
79 if $self->EOF();
80  
81 0 my $furl = $self->{'furl'};
82  
83 0 my $url = 'https://';
84 0 $url .= join '/', $hosts->{'content'}, $version;
85 0 $url .= join '/', '/files', $self->{'root'}, $self->{'path'};
86  
87 my $response = $furl->get($url, [
88 Range => sprintf('bytes=%i-%i', $self->{'position'}, $self->{'position'} + ($length || 1)),
89  
90 0 ▶ @{ &__headers__ },
Condition0%0/2
$length || 1
l!l
00
MC/DC0%0/1
$length || 1
$length
Truth table0%0/2
A
A= $length
Aresult
11
00
91 ]);
92  
93 0 given ($response->code()) {
94 0 when (206) { 1 }
95  
96 0 when ([401, 403]) {
97 0 $! = EACCES;
98 0 return 0;
99 }
100  
101 0 when (500) {
102 0 ▶ continue unless $response->content() =~ m{\A(?:Cannot|Failed)};
Branch: 1 branch
BranchTrueFalseTotal
unless $response->content =~ /\A(?:Cannot|Failed)/ 0 0 0
103  
104 0 $! = ECANCELED;
105 0 return 0;
106 }
107  
108 0 when (503) {
109 0 $self->{'meta'} = { retry => $response->header('Retry-After') };
110 0 $! = EAGAIN;
111 0 return 0;
112 }
113  
114 0 default {
115 0 die join ' ', $_, $response->decoded_content();
116 }
117 }
118  
119 0 my $meta = $response->header('X-Dropbox-Metadata');
120 0 my $bytes = $response->header('Content-Length');
121  
122 0 ▶ $self->{'position'} += $bytes > $length? $length : $bytes;
Branch: 1 branch
BranchTrueFalseTotal
$bytes > $length ? : 0 0 0
123  
124 0 ▶ substr($_[1] //= '', $offset // 0) = substr $response->content(), 0, $length;
Condition0%0/4
$_[1] //= ""
l!l
00
$offset // 0
l!l
00
MC/DC0%0/1
$_[1] //= ""
$_[1]
MC/DC0%0/1
$offset // 0
$offset
Truth table0%0/2
A
A= $_[1]
Aresult
11
00
Truth table0%0/2
A
A= $offset
Aresult
11
00
125  
126 0 return $bytes;
127 } # READ
128  
129 sub READLINE {
130 1 ▶ my ($self) = @_;
Subroutine READLINE: called CC=19 SCAR=57.5
131 1 my $length;
132  
133 1 undef $!;
134  
135 die 'Readline is not supported on this handle'
136 1 ▶ if $self->{'mode'} ne '<';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"mode"} ne "<" 1 0 1
137  
138 0 ▶ if ($self->EOF()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->EOF) 0 0 0
139 0 ▶ return if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
140  
141 # Special case: slurp mode + scalar context + empty file
142 # return '' for first call and undef for subsequent
143 return ''
144 0 ▶ unless $self->{'eof'} or defined $/;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"eof"} or defined $/ 0 0 0
Condition0%0/3
$self->{"eof"} or defined $/
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"eof"} or defined $/
$self->{"eof"} defined $/
Truth table0%0/3
A or B
A= $self->{"eof"}B= defined $/
ABresult
1X1
011
000
145  
146 0 $self->{'eof'} = 1;
147 0 return undef;
148 }
149  
150 {
151 0 $length = length $self->{'buffer'};
152  
153 0 ▶ if (not wantarray and $length and defined $/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not wantarray and $length and defined $/) 0 0 0
Condition0%0/6
not wantarray and $length and defined $/
!ll&&!rl&&r
000
not wantarray and $length
!ll&&!rl&&r
000
MC/DC0%0/3
not wantarray and $length and defined $/
not wantarray $length defined $/
Truth table0%0/4
not(A) and B and C
A= not wantarrayB= $lengthC= defined $/
ABCresult
0XX0
10X0
1100
1111
154 0 my $position = index $self->{'buffer'}, $/;
155  
156 0 ▶ if (~$position) {
Branch: 1 branch
BranchTrueFalseTotal
if (~$position) 0 0 0
157 0 $self->{'position'} += ($position += length $/);
158 0 return substr $self->{'buffer'}, 0, $position, '';
159 }
160 }
161  
162 0 local $self->{'position'} = $self->{'position'} + $length;
163  
164 0 my $bytes = $self->READ($self->{'buffer'}, $self->{'chunk'}, $length);
165  
166 0 ▶ return if $!;
Branch: 1 branch
BranchTrueFalseTotal
if $! 0 0 0
167 0 ▶ redo if not $length or $bytes;
Branch: 1 branch
BranchTrueFalseTotal
if not $length or $bytes 0 0 0
Condition0%0/3
not $length or $bytes
l!l&&r!l&&!r
000
MC/DC0%0/2
not $length or $bytes
not $length $bytes
Truth table0%0/3
not(A) or B
A= not $lengthB= $bytes
ABresult
1X1
011
000
168 }
169  
170 0 $length = length $self->{'buffer'};
171  
172 0 ▶ if ($length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length) 0 0 0
173 # Multiline
174 0 ▶ if (wantarray and defined $/) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray and defined $/) 0 0 0
Condition0%0/3
wantarray and defined $/
!ll&&!rl&&r
000
MC/DC0%0/2
wantarray and defined $/
wantarray defined $/
Truth table0%0/3
A and B
A= wantarrayB= defined $/
ABresult
0X0
100
111
175 0 $self->{'position'} += $length;
176  
177 0 my ($position, $length) = (0, length $/);
178 0 my @lines;
179  
180 0 foreach ($self->{'buffer'}) {
181 0 ▶ while (~(my $offset = index $_, $/, $position)) {
Branch: 1 branch
BranchTrueFalseTotal
if (~(my $offset = index($_, $/, $position))) 0 0 0
182 0 $offset += $length;
183 0 push @lines, substr $_, $position, $offset - $position;
184 0 $position = $offset;
185 }
186  
187 0 ▶ push @lines, substr $_, $position
Branch: 1 branch
BranchTrueFalseTotal
if $position < length $_ 0 0 0
188 if $position < length;
189  
190 0 $_ = '';
191 }
192  
193 0 return @lines;
194 }
195  
196 # Slurp or last chunk
197 0 $self->{'position'} += $length;
198 0 return substr $self->{'buffer'}, 0, $length, '';
199 }
200  
201 0 return undef;
202 } # READLINE
203  
204 sub SEEK {
205 1 ▶ my ($self, $position, $whence) = @_;
Subroutine SEEK: called CC=3 SCAR=20.5
206  
207 1 undef $!;
208  
209 die 'Seek is not supported on this handle'
210 1 ▶ if $self->{'mode'} ne '<';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"mode"} ne "<" 1 0 1
211  
212 0 $self->{'buffer'} = '';
213  
214 0 delete $self->{'eof'};
215  
216 0 given ($whence) {
217 0 when (SEEK_SET) {
218 0 $self->{'position'} = $position
219 }
220  
221 0 when (SEEK_CUR) {
222 0 $self->{'position'} += $position
223 }
224  
225 0 when (SEEK_END) {
226 0 $self->{'position'} = $self->{'length'} + $position
227 }
228  
229 0 default {
230 0 $! = EINVAL;
231 0 return 0;
232 }
233 }
234  
235 $self->{'position'} = 0
236 0 ▶ if $self->{'position'} < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"position"} < 0 0 0 0
237  
238 0 return 1;
239 } # SEEK
240  
241 sub TELL {
242 1 ▶ my ($self) = @_;
Subroutine TELL: called CC=2 SCAR=8.1
243  
244 die 'Tell is not supported on this handle'
245 1 ▶ if $self->{'mode'} ne '<';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"mode"} ne "<" 1 0 1
246  
247 0 return $self->{'position'};
248 } # TELL
249  
250 sub WRITE {
251 1 ▶ my ($self, $buffer, $length, $offset) = @_;
Subroutine WRITE: called CC=7 SCAR=35.4
252  
253 1 undef $!;
254  
255 die 'Write is not supported on this handle'
256 1 ▶ if $self->{'mode'} ne '>';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"mode"} ne ">" 1 0 1
257  
258 die 'Append-only writes supported'
259 0 ▶ if $offset and $offset != $self->{'offset'} + $self->{'length'};
Branch: 1 branch
BranchTrueFalseTotal
if $offset and $offset != $self->{"offset"} + $self->{"length"} 0 0 0
Condition0%0/3
$offset and $offset != $self->{"offset"} + $self->{"length"}
!ll&&!rl&&r
000
MC/DC0%0/2
$offset and $offset != $self->{"offset"} + $self->{"length"}
$offset $offset != $self->{"offset"} + $self->{"length"}
Truth table0%0/3
A and B
A= $offsetB= $offset != $self->{"offset"} + $self->{"length"}
ABresult
0X0
100
111
260  
261 0 ▶ $self->{'offset'} //= $offset;
Condition0%0/3
$self->{"offset"} //= $offset
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"offset"} //= $offset
$self->{"offset"} $offset
Truth table0%0/3
A //= B
A= $self->{"offset"}B= $offset
ABresult
1X1
011
000
262 0 $self->{'buffer'} .= $buffer;
263 0 $self->{'length'} += $length;
264  
265 $self->__flush__() or return 0
266 0 ▶ while $self->{'length'} >= $self->{'chunk'};
Branch: 2 branches
BranchTrueFalseTotal
$self->{"length"} >= $self->{"chunk"} and do { $self->__flush__ or return 0 } 0 0 0
$self->__flush__ or return 0 0 0 0
267  
268 0 return 1;
269 } # WRITE
270  
271 sub CLOSE {
272 0 ▶ my ($self) = @_;
Subroutine CLOSE: not called CC=9 SCAR=45.0
273  
274 0 undef $!;
275  
276 return 1
277 0 ▶ if $self->{'closed'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 0 0 0
278  
279 0 my $mode = $self->{'mode'};
280  
281 0 ▶ if ($mode eq '>') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq ">") 0 0 0
282 0 ▶ if ($self->{'length'} or not $self->{'upload_id'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"length"} or not $self->{"upload_id"}) 0 0 0
Condition0%0/3
$self->{"length"} or not $self->{"upload_id"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"length"} or not $self->{"upload_id"}
$self->{"length"} not $self->{"upload_id"}
Truth table0%0/3
A or not(B)
A= $self->{"length"}B= not $self->{"upload_id"}
ABresult
1X1
011
000
283 do {
284 0 ▶ @{ $self }{qw{ closed mode }} = (1, '') and return 0
Branch: 1 branch
BranchTrueFalseTotal
unless $self->__flush__ 0 0 0
Condition0%0/5
$self->__flush__ || (@{$self;}{'closed', 'mode'} = (1, '')) && (return 0)
l!l&&r!l&&!r
000
@{$self;}{"closed", "mode"} = (1, "") and return 0
!ll
00
MC/DC0%0/2
$self->__flush__ || (@{$self;}{'closed', 'mode'} = (1, '')) && (return 0)
$self->__flush__ @{$self;}{"closed", "mode"} = (1, "")
Truth table0%0/3
A || B
A= $self->__flush__B= @{$self;}{"closed", "mode"} = (1, "")
ABresult
1X1
011
000
285 unless $self->__flush__();
286 0 ▶ } while length $self->{'buffer'};
Branch: 1 branch
BranchTrueFalseTotal
length $self->{"buffer"} and do { do { @{$self;}{"closed", "mode"} = (1, "") and return 0 unless $self->__flush__ } } 0 0 0
287 }
288 }
289  
290 0 $self->{'closed'} = 1;
291 0 $self->{'mode'} = '';
292  
293 0 ▶ return $self->__flush__()
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq ">" 0 0 0
294 if $mode eq '>';
295  
296 0 return 1;
297 } # CLOSE
298  
299 sub OPEN {
300 0 ▶ my ($self, $mode, $file) = @_;
Subroutine OPEN: not called CC=7 SCAR=40.3
301  
302 0 undef $!;
303  
304 0 ▶ ($mode, $file) = $mode =~ m{^([<>]?)(.*)$}s
Branch: 1 branch
BranchTrueFalseTotal
unless $file 0 0 0
305 unless $file;
306  
307 0 ▶ given ($mode ||= '<') {
Condition0%0/2
$mode ||= "<"
l!l
00
MC/DC0%0/1
$mode ||= "<"
$mode
Truth table0%0/2
A
A= $mode
Aresult
11
00
308 0 when (['>', '<']) { 1 }
309  
310 0 when ('r') {
311 0 $mode = '<';
312 }
313  
314 0 when (['a', 'w']) {
315 0 $mode = '>';
316 }
317  
318 0 default {
319 0 die 'Unsupported mode';
320 }
321 }
322  
323 $self->CLOSE()
324 0 ▶ unless $self->{'closed'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"closed"} 0 0 0
325  
326 0 $self->{'length'} = 0;
327 0 $self->{'position'} = 0;
328 0 $self->{'buffer'} = '';
329  
330 0 delete $self->{'offset'};
331 0 delete $self->{'revision'};
332 0 delete $self->{'upload_id'};
333 0 delete $self->{'meta'};
334 0 delete $self->{'eof'};
335  
336 0 ▶ $self->{'path'} = $file
Branch: 1 branch
BranchTrueFalseTotal
$self->{"path"} = $file or die "Path required" 0 0 0
337 or die 'Path required';
338  
339 0 ▶ return 0
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "<" and not $self->__meta__ 0 0 0
Condition0%0/3
$mode eq "<" and not $self->__meta__
!ll&&!rl&&r
000
MC/DC0%0/2
$mode eq "<" and not $self->__meta__
$mode eq "<" not $self->__meta__
Truth table0%0/3
A and not(B)
A= $mode eq "<"B= not $self->__meta__
ABresult
0X0
100
111
340 if $mode eq '<' and not $self->__meta__();
341  
342 0 $self->{'mode'} = $mode;
343 0 $self->{'closed'} = 0;
344  
345 0 return 1;
346 } # OPEN
347  
348 sub EOF {
349 1 ▶ my ($self) = @_;
Subroutine EOF: called CC=2 SCAR=8.1
350  
351 die 'Eof is not supported on this handle'
352 1 ▶ if $self->{'mode'} ne '<';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"mode"} ne "<" 1 0 1
353  
354 0 return $self->{'position'} >= $self->{'length'};
355 } # EOF
356  
357 1 ▶sub BINMODE { 1 }
Subroutine BINMODE: called CC=1 SCAR=0.0
358  
359 sub __headers__ {
360 return [
361 'Authorization',
362 $_[0]->{'oauth2'}?
363 sprintf $header2, $_[0]->{'access_token'}:
364 0 ▶ sprintf $header1, @{ $_[0] }{qw{ app_key access_token app_secret access_secret }},
Subroutine __headers__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'oauth2'} ? : 0 0 0
365 ];
366 }
367  
368 sub __flush__ {
369 0 ▶ my ($self) = @_;
Subroutine __flush__: not called CC=11 SCAR=47.6
370 0 my $furl = $self->{'furl'};
371 0 my $url;
372  
373 0 $url = 'https://';
374 0 $url .= join '/', $hosts->{'content'}, $version;
375  
376 $url .= join '/', '/commit_chunked_upload', $self->{'root'}, $self->{'path'}
377 0 ▶ if $self->{'closed'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 0 0 0
378  
379 $url .= '/chunked_upload'
380 0 ▶ unless $self->{'closed'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"closed"} 0 0 0
381  
382 0 $url .= '?';
383  
384 $url .= join '=', 'upload_id', $self->{'upload_id'}
385 0 ▶ if $self->{'upload_id'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"upload_id"} 0 0 0
386  
387 $url .= '&'
388 0 ▶ if $self->{'upload_id'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"upload_id"} 0 0 0
389  
390 $url .= join '=', 'offset', $self->{'offset'} || 0
391 0 ▶ unless $self->{'closed'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"closed"} 0 0 0
Condition0%0/2
$self->{'offset'} || 0
l!l
00
MC/DC0%0/1
$self->{'offset'} || 0
$self->{'offset'}
Truth table0%0/2
A
A= $self->{'offset'}
Aresult
11
00
392  
393 0 my $response;
394  
395 0 ▶ unless ($self->{'closed'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'closed'}) { } 0 0 0
396 12 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
397  
398 0 my $buffer = substr $self->{'buffer'}, 0, $self->{'chunk'}, '';
399 0 my $length = length $buffer;
400  
401 0 $self->{'length'} -= $length;
402 0 $self->{'offset'} += $length;
403  
404 0 $response = $furl->put($url, &__headers__, $buffer);
405 } else {
406 0 $response = $furl->post($url, &__headers__);
407 }
408  
409 0 given ($response->code()) {
410 0 when (400) {
411 0 $! = EINVAL;
412 0 return 0;
413 }
414  
415 0 when ([401, 403]) {
416 0 $! = EACCES;
417 0 return 0;
418 }
419  
420 0 when (500) {
421 0 ▶ continue unless $response->content() =~ m{\A(?:Cannot|Failed)};
Branch: 1 branch
BranchTrueFalseTotal
unless $response->content =~ /\A(?:Cannot|Failed)/ 0 0 0
422  
423 0 $! = ECANCELED;
424 0 return 0;
425 }
426  
427 0 when (503) {
428 0 $self->{'meta'} = { retry => $response->header('Retry-After') };
429 0 $! = EAGAIN;
430 0 return 0;
431 }
432  
433 0 when (507) {
434 0 $! = EFBIG;
435 0 return 0;
436 }
437  
438 0 when (200) {
439 $self->{'meta'} = from_json($response->content())
440 0 ▶ if $self->{'closed'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 0 0 0
441 }
442  
443 0 default {
444 0 die join ' ', $_, $response->decoded_content();
445 }
446 }
447  
448 0 ▶ unless ($self->{'upload_id'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"upload_id"}) 0 0 0
449 0 $response = from_json($response->content());
450 0 $self->{'upload_id'} = $response->{'upload_id'};
451 }
452  
453 0 return 1;
454 } # __flush__
455  
456 sub __meta__ {
457 0 ▶ my ($self) = @_;
Subroutine __meta__: not called CC=5 SCAR=34.0
458 0 my ($url, $meta);
459  
460 0 my $furl = $self->{'furl'};
461  
462 0 $url = 'https://';
463 0 $url .= join '/', $hosts->{'api'}, $version;
464 0 $url .= join '/', '/metadata', $self->{'root'}, $self->{'path'};
465  
466 $url .= '?hash='. delete $self->{'hash'}
467 0 ▶ if $self->{'hash'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"hash"} 0 0 0
468  
469 0 my $response = $furl->get($url, &__headers__);
470  
471 0 given ($response->code()) {
472 0 when ([401, 403]) {
473 0 $! = EACCES;
474 0 return 0;
475 }
476  
477 0 when (404) {
478 0 $! = ENOENT;
479 0 return 0;
480 }
481  
482 0 when (406) {
483 0 $! = EPERM;
484 0 return 0;
485 }
486  
487 0 when (500) {
488 0 ▶ continue unless $response->content() =~ m{\A(?:Cannot|Failed)};
Branch: 1 branch
BranchTrueFalseTotal
unless $response->content =~ /\A(?:Cannot|Failed)/ 0 0 0
489  
490 0 $! = ECANCELED;
491 0 return 0;
492 }
493  
494 0 when (503) {
495 0 $self->{'meta'} = { retry => $response->header('Retry-After') };
496 0 $! = EAGAIN;
497 0 return 0;
498 }
499  
500 0 when (200) {
501 0 $meta = $self->{'meta'} = from_json($response->content());
502  
503 # XXX: Dropbox returns metadata for recently deleted files
504 0 ▶ if ($meta->{'is_deleted'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($meta->{"is_deleted"}) 0 0 0
505 0 $! = ENOENT;
506 0 return 0;
507 }
508 }
509  
510 0 when (304) { 1 }
511  
512 0 default {
513 0 die join ' ', $_, $response->decoded_content();
514 }
515 }
516  
517 $! = EISDIR, return 0
518 0 ▶ if $meta->{'is_dir'};
Branch: 1 branch
BranchTrueFalseTotal
if $meta->{"is_dir"} 0 0 0
519  
520 0 $self->{'revision'} = $meta->{'rev'};
521 0 $self->{'length'} = $meta->{'bytes'};
522  
523 0 return 1;
524 } # __meta__
525  
526 sub __fileops__ {
527 0 ▶ my ($type, $handle, $source, $target) = @_;
Subroutine __fileops__: not called CC=2 SCAR=17.9
528  
529 0 my $self = *$handle{'HASH'};
530 0 my $furl = $self->{'furl'};
531 0 my ($url, @arguments);
532  
533 0 $url = 'https://';
534 0 $url .= join '/', $hosts->{'api'}, $version;
535 0 $url .= join '/', '/fileops', $type;
536  
537 0 given ($type) {
538 0 when (['move', 'copy']) {
539 0 @arguments = (
540 from_path => $source,
541 to_path => $target,
542 );
543 }
544  
545 0 default {
546 0 @arguments = (
547 path => $source,
548 );
549 }
550 }
551  
552 0 push @arguments, root => $self->{'root'};
553  
554 0 my $response = $furl->post($url, $self->__headers__(), \@arguments);
555  
556 0 given ($response->code()) {
557 0 when (200) {
558 0 $self->{'meta'} = from_json($response->content());
559 }
560  
561 0 when ([401, 403]) {
562 0 $! = EACCES;
563 0 return 0;
564 }
565  
566 0 when (404) {
567 0 $! = ENOENT;
568 0 return 0;
569 }
570  
571 0 when (406) {
572 0 $! = EPERM;
573 0 return 0;
574 }
575  
576 0 when (500) {
577 0 ▶ continue unless $response->content() =~ m{\A(?:Cannot|Failed)};
Branch: 1 branch
BranchTrueFalseTotal
unless $response->content =~ /\A(?:Cannot|Failed)/ 0 0 0
578  
579 0 $! = ECANCELED;
580 0 return 0;
581 }
582  
583 0 when (503) {
584 0 $self->{'meta'} = { retry => $response->header('Retry-After') };
585 0 $! = EAGAIN;
586 0 return 0;
587 }
588  
589 0 default {
590 0 die join ' ', $_, $response->decoded_content();
591 }
592 }
593  
594 0 return 1;
595 } # __fileops__
596  
597 sub contents ($;$$) {
598 0 ▶ my ($handle, $path, $hash) = @_;
Subroutine contents: not called CC=6 SCAR=37.4
Pod documented
599  
600 0 ▶ die 'GLOB reference expected'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $handle eq "GLOB" 0 0 0
601 unless ref $handle eq 'GLOB';
602  
603 0 ▶ *$handle->{'hash'} = $hash
Branch: 1 branch
BranchTrueFalseTotal
if $hash 0 0 0
604 if $hash;
605  
606 0 ▶ if (open $handle, '<', $path || '/' or $! != EISDIR) {
Branch: 1 branch
BranchTrueFalseTotal
if (open $handle, "<", $path || "/" or $! != 21) 0 0 0
Condition0%0/5
open $handle, "<", $path || "/" or $! != 21
l!l&&r!l&&!r
000
$path || '/'
l!l
00
MC/DC0%0/2
open $handle, "<", $path || "/" or $! != 21
open $handle, "<", $path || "/" $! != 21
MC/DC0%0/1
$path || '/'
$path
Truth table0%0/3
A or B
A= open $handle, "<", $path || "/"B= $! != 21
ABresult
1X1
011
000
Truth table0%0/2
A
A= $path
Aresult
11
00
607 0 delete *$handle->{'meta'};
608 0 return;
609 }
610  
611 0 undef $!;
612 0 return @{ *$handle->{'meta'}{'contents'} };
613 } # contents
614  
615 sub putfile ($$$) {
616 0 ▶ my ($handle, $path, $data) = @_;
Subroutine putfile: not called CC=4 SCAR=28.3
Pod documented
617  
618 0 ▶ die 'GLOB reference expected'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $handle eq "GLOB" 0 0 0
619 unless ref $handle eq 'GLOB';
620  
621 0 ▶ close $handle or return 0;
Branch: 1 branch
BranchTrueFalseTotal
close $handle or return 0 0 0 0
622  
623 0 my $self = *$handle{'HASH'};
624 0 my $furl = $self->{'furl'};
625 0 my ($url, $length);
626  
627 0 $url = 'https://';
628 0 $url .= join '/', $hosts->{'content'}, $version;
629 0 $url .= join '/', '/files_put', $self->{'root'}, $path;
630  
631 {
632 12 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.2
633 0 $length = length $data;
634 }
635  
636 0 my $response = $furl->put($url, $self->__headers__(), $data);
637  
638 0 given ($response->code()) {
639 0 when (400) {
640 0 $! = EINVAL;
641 0 return 0;
642 }
643  
644 0 when ([401, 403]) {
645 0 $! = EACCES;
646 0 return 0;
647 }
648  
649 0 when (500) {
650 0 ▶ continue unless $response->content() =~ m{\A(?:Cannot|Failed)};
Branch: 1 branch
BranchTrueFalseTotal
unless $response->content =~ /\A(?:Cannot|Failed)/ 0 0 0
651  
652 0 $! = ECANCELED;
653 0 return 0;
654 }
655  
656 0 when (503) {
657 0 $self->{'meta'} = { retry => $response->header('Retry-After') };
658 0 $! = EAGAIN;
659 0 return 0;
660 };
661  
662 0 when (507) {
663 0 $! = EFBIG;
664 0 return 0;
665 }
666  
667 0 when (200) {
668 0 $self->{'path'} = $path;
669 0 $self->{'meta'} = from_json($response->content());
670 }
671  
672 0 default {
673 0 die join ' ', $_, $response->decoded_content();
674 }
675 }
676  
677 0 return 1;
678 } # putfile
679  
680 0 ▶sub movefile ($$$) { __fileops__('move', @_) }
Subroutine movefile: not called CC=1 SCAR=6.9
Pod documented
681 0 ▶sub copyfile ($$$) { __fileops__('copy', @_) }
Subroutine copyfile: not called CC=1 SCAR=6.9
Pod documented
682 0 ▶sub deletefile ($$) { __fileops__('delete', @_) }
Subroutine deletefile: not called CC=1 SCAR=6.9
Pod documented
683 0 ▶sub createfolder ($$) { __fileops__('create_folder', @_) }
Subroutine createfolder: not called CC=1 SCAR=6.9
Pod documented
684  
685 sub metadata ($) {
686 0 ▶ my ($handle) = @_;
Subroutine metadata: not called CC=3 SCAR=24.8
Pod documented
687  
688 0 ▶ die 'GLOB reference expected'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $handle eq "GLOB" 0 0 0
689 unless ref $handle eq 'GLOB';
690  
691 0 my $self = *$handle{'HASH'};
692  
693 die 'Meta is unavailable for incomplete upload'
694 0 ▶ if $self->{'mode'} eq '>';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"mode"} eq ">" 0 0 0
695  
696 0 return $self->{'meta'};
697 } # metadata
698  
699 =head1 NAME
700  
701 File::Dropbox - Convenient and fast Dropbox API abstraction
702  
703 =head1 SYNOPSIS
704  
705 use File::Dropbox;
706 use Fcntl;
707  
708 # Application credentials
709 my %app = (
710 app_key => 'app key',
711 app_secret => 'app secret',
712 access_token => 'access token',
713 access_secret => 'access secret',
714 );
715  
716 my $dropbox = File::Dropbox->new(%app);
717  
718 # Open file for writing
719 open $dropbox, '>', 'example' or die $!;
720  
721 while (<>) {
722 # Upload data using 4MB chunks
723 print $dropbox $_;
724 }
725  
726 # Commit upload (optional, close will be called on reopen)
727 close $dropbox or die $!;
728  
729 # Open for reading
730 open $dropbox, '<', 'example' or die $!;
731  
732 # Download and print to STDOUT
733 # Buffered, default buffer size is 4MB
734 print while <$dropbox>;
735  
736 # Reset file position
737 seek $dropbox, 0, Fcntl::SEEK_SET;
738  
739 # Get first character (unbuffered)
740 say getc $dropbox;
741  
742 close $dropbox;
743  
744 =head1 DESCRIPTION
745  
746 C<File::Dropbox> provides high-level Dropbox API abstraction based on L<Tie::Handle>. Code required to get C<access_token> and
747 C<access_secret> for signed OAuth 1.0 requests or C<access_token> for OAuth 2.0 requests is not included in this module.
748 To get C<app_key> and C<app_secret> you need to register your application with Dropbox.
749  
750 At this moment Dropbox API is not fully supported, C<File::Dropbox> covers file read/write and directory listing methods. If you need full
751 API support take look at L<WebService::Dropbox>. C<File::Dropbox> main purpose is not 100% API coverage,
752 but simple and high-performance file operations.
753  
754 Due to API limitations and design you can not do read and write operations on one file at the same time. Therefore handle can be in read-only
755 or write-only state, depending on last call to L<open|perlfunc/open>. Supported functions for read-only state are: L<open|perlfunc/open>,
756 L<close|perlfunc/close>, L<seek|perlfunc/seek>, L<tell|perlfunc/tell>, L<readline|perlfunc/readline>, L<read|perlfunc/read>,
757 L<sysread|perlfunc/sysread>, L<getc|perlfunc/getc>, L<eof|perlfunc/eof>. For write-only state: L<open|perlfunc/open>, L<close|perlfunc/close>,
758 L<syswrite|perlfunc/syswrite>, L<print|perlfunc/print>, L<printf|perlfunc/printf>, L<say|perlfunc/say>.
759  
760 All API requests are done using L<Furl> module. For more accurate timeouts L<Net::DNS::Lite> is used, as described in L<Furl::HTTP>. Furl settings
761 can be overriden using C<furlopts>.
762  
763 =head1 METHODS
764  
765 =head2 new
766  
767 my $dropbox = File::Dropbox->new(
768 access_secret => 'secret',
769 access_token => 'token',
770 app_secret => 'app secret',
771 app_key => 'app key',
772 chunk => 8 * 1024 * 1024,
773 root => 'dropbox',
774 furlopts => {
775 timeout => 20
776 }
777 );
778  
779 my $dropbox = File::Dropbox->new(
780 access_token => 'token',
781 oauth2 => 1
782 );
783  
784 Constructor, takes key-value pairs list
785  
786 =over
787  
788 =item access_secret
789  
790 OAuth 1.0 access secret
791  
792 =item access_token
793  
794 OAuth 1.0 access token or OAuth 2.0 access token
795  
796 =item app_secret
797  
798 OAuth 1.0 app secret
799  
800 =item app_key
801  
802 OAuth 1.0 app key
803  
804 =item oauth2
805  
806 OAuth 2.0 switch, defaults to false.
807  
808 =item chunk
809  
810 Upload chunk size in bytes. Also buffer size for C<readline>. Optional. Defaults to 4MB.
811  
812 =item root
813  
814 Access type, C<sandbox> for app-folder only access and C<dropbox> for full access.
815  
816 =item furlopts
817  
818 Parameter hash, passed to L<Furl> constructor directly. Default options
819  
820 timeout => 10,
821 inet_aton => \&Net::DNS::Lite::inet_aton,
822 ssl_opts => {
823 SSL_verify_mode => SSL_VERIFY_PEER(),
824 }
825  
826 =back
827  
828 =head1 FUNCTIONS
829  
830 All functions are not exported by default but can be exported on demand.
831  
832 use File::Dropbox qw{ contents metadata putfile };
833  
834 First argument for all functions should be GLOB reference, returned by L</new>.
835  
836 =head2 contents
837  
838 Arguments: $dropbox [, $path]
839  
840 Function returns list of hashrefs representing directory content. Hash fields described in L<Dropbox API
841 docs|https://www.dropbox.com/developers/core/docs#metadata>. C<$path> defaults to C</>. If there is
842 unfinished chunked upload on handle, it will be commited.
843  
844 foreach my $file (contents($dropbox, '/data')) {
845 next if $file->{'is_dir'};
846 say $file->{'path'}, ' - ', $file->{'bytes'};
847 }
848  
849 =head2 metadata
850  
851 Arguments: $dropbox
852  
853 Function returns stored metadata for read-only handle, closed write handle or after
854 call to L</contents> or L</putfile>.
855  
856 open $dropbox, '<', '/data/2013.dat' or die $!;
857  
858 my $meta = metadata($dropbox);
859  
860 if ($meta->{'bytes'} > 1024) {
861 # Do something
862 }
863  
864 =head2 putfile
865  
866 Arguments: $dropbox, $path, $data
867  
868 Function is useful for uploading small files (up to 150MB possible) in one request (at least
869 two API requests required for chunked upload, used in open-write-close sequence). If there is
870 unfinished chunked upload on handle, it will be commited.
871  
872 local $/;
873 open my $data, '<', '2012.dat' or die $!;
874  
875 putfile($dropbox, '/data/2012.dat', <$data>) or die $!;
876  
877 say 'Uploaded ', metadata($dropbox)->{'bytes'}, ' bytes';
878  
879 close $data;
880  
881 =head2 copyfile
882  
883 Arguments: $dropbox, $source, $target
884  
885 Function copies file or directory from one location to another. Metadata for copy
886 can be accessed using L</metadata> function.
887  
888 copyfile($dropbox, '/data/2012.dat', '/data/2012.dat.bak') or die $!;
889  
890 say 'Created backup with revision ', metadata($dropbox)->{'revision'};
891  
892 =head2 movefile
893  
894 Arguments: $dropbox, $source, $target
895  
896 Function moves file or directory from one location to another. Metadata for moved file
897 can be accessed using L</metadata> function.
898  
899 movefile($dropbox, '/data/2012.dat', '/data/2012.dat.bak') or die $!;
900  
901 say 'Created backup with size ', metadata($dropbox)->{'size'};
902  
903 =head2 deletefile
904  
905 Arguments: $dropbox, $path
906  
907 Function deletes file or folder at specified path. Metadata for deleted item
908 is accessible via L</metadata> function.
909  
910 deletefile($dropbox, '/data/2012.dat.bak') or die $!;
911  
912 say 'Deleted backup with last modification ', metadata($dropbox)->{'modification'};
913  
914 =head2 createfolder
915  
916 Arguments: $dropbox, $path
917  
918 Function creates folder at specified path. Metadata for created folder
919 is accessible via L</metadata> function.
920  
921 createfolder($dropbox, '/data/backups') or die $!;
922  
923 say 'Created folder at path ', metadata($dropbox)->{'path'};
924  
925 =head1 SEE ALSO
926  
927 L<Furl>, L<Furl::HTTP>, L<WebService::Dropbox>, L<Dropbox API|https://www.dropbox.com/developers/core/docs>
928  
929 =head1 AUTHOR
930  
931 Alexander Nazarov <nfokz@cpan.org>
932  
933 =head1 COPYRIGHT AND LICENSE
934  
935 Copyright 2013, 2014 Alexander Nazarov
936  
937 This program is free software; you can redistribute it and/or modify it
938 under the same terms as Perl itself.
939  
940 =cut
941  
942 12 1;
↑ Summary Build »