Gett/File.pm

filter: Statementstmt 29.4% 25 / 85 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 23.0% 3 / 13 Podpod 100% 5 / 5 totaltotal 22.0% 33 / 150 CC 21 SCAR 39.6
CC
21
Cov
20%
send_file
132.0
get_upload_url
30.0
destroy
20.0
CRAP 52.4
↑ Summary Gett.pm »
1 package Net::API::Gett::File;
2  
3 =head1 NAME
4  
5 Net::API::Gett::File - Gett file object
6  
7 =cut
8  
9 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use MooX::Types::MooseLike::Base qw(Int Str);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 5 our $VERSION = '1.06';
14  
15 =head1 PURPOSE
16  
17 Encapsulate Gett files. You normally shouldn't instantiate this class on
18 its own, as the library will create and return this object as appropriate.
19  
20 =head1 ATTRIBUTES
21  
22 These are read only attributes unless otherwise noted.
23  
24 =over
25  
26 =item filename
27  
28 Scalar string.
29  
30 =item fileid
31  
32 Scalar string.
33  
34 =item downloads
35  
36 Scalar integer. The number of times this particular file has been downloaded
37  
38 =item readystate
39  
40 Scalar string. Signifies the state a particular file is in. See the
41 L<Gett developer docs|http://ge.tt/developers> for more information.
42  
43 =item getturl
44  
45 Scalar string. The URL to use in a browser to access a file.
46  
47 =item download
48  
49 Scalar string. The URL to use to get the file contents.
50  
51 =item size
52  
53 Scalar integer. The size in bytes of this file.
54  
55 =item created
56  
57 Scalar integer. The Unix epoch time when this file was created in Gett. This value is suitable
58 for use in C<localtime()>.
59  
60 =item sharename
61  
62 Scalar string. The share in which this file lives inside.
63  
64 =item put_upload_url
65  
66 Scalar string. The url to use to upload the contents of this file using the PUT method. (This
67 attribute is only populated during certain times.)
68  
69 =item post_upload_url
70  
71 Scalar string. This url to use to upload the contents of this file using the POST method. (This
72 attribute is only populated during certain times.)
73  
74 =item chunk_size
75  
76 Scalar integer. This is the chunk size to use for file uploads. It defaults to
77 1,048,576 bytes (1 MB). This attribute is read-only.
78  
79 =back
80  
81 =cut
82  
83 5 has 'filename' => (
84 is => 'ro',
85 isa => Str,
86 );
87  
88 5 has 'fileid' => (
89 is => 'ro',
90 isa => Str,
91 );
92  
93 5 has 'downloads' => (
94 is => 'ro',
95 isa => Int,
96 );
97  
98 5 has 'readystate' => (
99 is => 'ro',
100 isa => Str,
101 );
102  
103 5 has 'getturl' => (
104 is => 'ro',
105 isa => Str,
106 );
107  
108 5 has 'download' => (
109 is => 'ro',
110 isa => Str,
111 );
112  
113 5 has 'size' => (
114 is => 'ro',
115 isa => Int,
116 );
117  
118 5 has 'created' => (
119 is => 'ro',
120 isa => Int,
121 );
122  
123 5 has 'sharename' => (
124 is => 'rw',
125 isa => Str,
126 );
127  
128 5 has 'put_upload_url' => (
129 is => 'ro',
130 isa => Str,
131 );
132  
133 5 has 'post_upload_url' => (
134 is => 'ro',
135 isa => Str,
136 );
137  
138 has 'chunk_size' => (
139 is => 'rw',
140 isa => Int,
141 0 ▶ default => sub { 1_048_576; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
142 5 );
143  
144 =over
145  
146 =item user
147  
148 L<Net::API::Gett::User> object. C<has_user()> predicate.
149  
150 =back
151  
152 =cut
153  
154 has 'user' => (
155 is => 'rw',
156 predicate => 'has_user',
157 0 ▶ isa => sub { die "$_[0] is not Net::API::Gett::User" unless ref($_[0]) =~ /User/ },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless ref($_[0]) =~ /User/ 0 0 0
158 5 lazy => 1,
159 );
160  
161 =over
162  
163 =item request
164  
165 L<Net::API::Gett::Request> object.
166  
167 =back
168  
169 =cut
170  
171 has 'request' => (
172 is => 'rw',
173 0 ▶ isa => sub { die "$_[0] is not Net::API::Gett::Request" unless ref($_[0]) =~ /Request/ },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless ref($_[0]) =~ /Request/ 0 0 0
174 0 ▶ default => sub { Net::API::Gett::Request->new() },
Subroutine __ANON__: not called CC=1 SCAR=6.9
175 5 lazy => 1,
176 );
177  
178 =head1 METHODS
179  
180 =over
181  
182 =item send_file()
183  
184 This method actually uploads the file to the Gett service. This method is normally invoked by the
185 C<upload_file()> method, but it might be useful in combination with C<get_upload_url()>. It takes
186 the following parameters:
187  
188 =over
189  
190 =item * a Gett put upload url
191  
192 =item * data
193  
194 a scalar representing the file contents which can be one of: a buffer, an L<IO::Handle> object, or a
195 file pathname.
196  
197 =item * encoding
198  
199 an encoding scheme. By default, it uses C<:raw>.
200  
201 =item * chunk_size
202  
203 The maximum chunksize to load into to memory at one time. If the file to transmit is larger than
204 this size, it will be dynamically streamed.
205  
206 =back
207  
208 Returns a true value on success.
209  
210 =back
211  
212 =cut
213  
214 0 ▶sub send_file {
Subroutine send_file: not called CC=11 SCAR=48.8
Pod documented
215 0 my $self = shift;
216 0 my $url = shift;
217 0 my $contents = shift;
218 0 ▶ my $encoding = shift || ":raw";
Condition0%0/2
shift() || ':raw'
l!l
00
MC/DC0%0/1
shift() || ':raw'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
219 0 ▶ my $chunk_size = shift || $self->chunk_size || 1_048_576; # 1024 * 1024 = 1 MB
Condition0%0/5
shift() || $self->chunk_size || 1048576
l!l
00
shift() or $self->chunk_size
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->chunk_size || 1048576
shift() $self->chunk_size
Truth table0%0/3
A or B
A= shift()B= $self->chunk_size
ABresult
1X1
011
000
220  
221 0 my $fh;
222 my $length;
223 0 ▶ if ( ! ref($contents) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $contents) { } 0 0 0
224 # $contents is scalar
225 0 ▶ if ( ! -e $contents ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -e $contents) { } 0 0 0
226 # $contents doesn't point to a valid filename,
227 # assume it's a buffer
228  
229 0 $contents .= "";
230 # Make sure this data is stringified.
231 0 ▶ open($fh, "<", \$contents) or croak "Couldn't open a filehandle on the content buffer\n";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", \$contents or croak("Couldn't open a filehandle on the content buffer\n") 0 0 0
232 0 binmode($fh, $encoding);
233 0 $length = length($contents);
234 }
235 else {
236 0 ▶ open($fh, "<", $contents) or croak "Couldn't open a filehandle on $contents: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $contents or croak("Couldn't open a filehandle on ${contents}: $!") 0 0 0
237 0 binmode($fh, $encoding);
238 0 $length = -s $contents;
239 }
240 }
241 else {
242 0 ▶ $fh = $contents if ref($contents) =~ /IO/;
Branch: 1 branch
BranchTrueFalseTotal
if ref($contents) =~ /IO/ 0 0 0
243 0 $length = -s $fh;
244 }
245  
246 0 ▶ return 0 unless $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless $fh 0 0 0
247  
248 0 my $response = $self->request->put($url, $fh, $chunk_size, $length);
249  
250 0 ▶ if ( $response ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response) { } 0 0 0
251 0 return 1;
252 }
253 else {
254 0 return undef;
255 }
256 }
257  
258 =over
259  
260 =item get_upload_url()
261  
262 This method returns a scalar PUT upload URL for the specified sharename/fileid parameters.
263 Potentially useful in combination with C<send_file()>.
264  
265 =back
266  
267 =cut
268  
269 0 ▶sub get_upload_url {
Subroutine get_upload_url: not called CC=5 SCAR=34.0
Pod documented
270 0 my $self = shift;
271 0 ▶ croak "Cannot get_upload_url() without a Net::API::Gett::User object." unless $self->has_user;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_user 0 0 0
272  
273 0 my $sharename = $self->sharename;
274 0 my $fileid = $self->fileid;
275  
276 0 ▶ $self->user->login unless $self->user->has_access_token;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->user->has_access_token 0 0 0
277  
278 0 my $endpoint = "/files/$sharename/$fileid/upload?accesstoken=".$self->user->access_token;
279  
280 0 my $response = $self->request->get($endpoint);
281  
282 0 ▶ if ( $response && exists $response->{'puturl'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response and exists $response->{'puturl'}) { } 0 0 0
Condition0%0/3
$response and exists $response->{"puturl"}
!ll&&!rl&&r
000
MC/DC0%0/2
$response and exists $response->{"puturl"}
$response exists $response->{"puturl"}
Truth table0%0/3
A and B
A= $responseB= exists $response->{"puturl"}
ABresult
0X0
100
111
283 0 return $response->{'puturl'};
284 }
285 else {
286 0 croak "Could not get a PUT url from $endpoint";
287 }
288 }
289  
290 =over
291  
292 =item destroy()
293  
294 This method destroys the file represented by the object. Returns a true value on success.
295  
296 =back
297  
298 =cut
299  
300 0 ▶sub destroy {
Subroutine destroy: not called CC=4 SCAR=30.0
Pod documented
301 0 my $self = shift;
302 0 ▶ croak "Cannot destroy() without a Net::API::Gett::User object." unless $self->has_user;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_user 0 0 0
303  
304 0 my $sharename = $self->sharename;
305 0 my $fileid = $self->fileid;
306  
307 0 ▶ $self->user->login unless $self->user->has_access_token;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->user->has_access_token 0 0 0
308  
309 0 my $endpoint = "/files/$sharename/$fileid/destroy?accesstoken=".$self->access_token;
310  
311 0 my $response = $self->request->post($endpoint);
312  
313 0 ▶ if ( $response ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response) { } 0 0 0
314 0 return 1;
315 }
316 else {
317 0 return undef;
318 }
319 }
320
321 0 ▶sub _file_contents {
Subroutine _file_contents: not called CC=2 SCAR=17.9
322 0 my $self = shift;
323 0 my $endpoint = $self->request->base_url . shift;
324  
325 0 my $response = $self->request->ua->get($endpoint);
326  
327 0 ▶ if ( $response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 0 0 0
328 0 return $response->content();
329 }
330 else {
331 0 croak "$endpoint said " . $response->status_line;
332 }
333 }
334  
335 =over
336  
337 =item contents()
338  
339 This method retrieves the contents of a this file in the Gett service. You are responsible for
340 outputting the file (if desired) with any appropriate encoding. Does not require an access token.
341  
342 =back
343  
344 =cut
345  
346 0 ▶sub contents {
Subroutine contents: not called CC=1 SCAR=6.9
Pod documented
347 0 my $self = shift;
348 0 my $sharename = $self->sharename;
349 0 my $fileid = $self->fileid;
350  
351 0 return $self->_file_contents("/files/$sharename/$fileid/blob");
352 }
353  
354 =over
355  
356 =item thumbnail()
357  
358 This method returns a thumbnail if the file in Gett is an image. Does not require an access token, but
359 is really only meaningful if the data is a valid image format file.
360  
361 =back
362  
363 =cut
364  
365 0 ▶sub thumbnail {
Subroutine thumbnail: not called CC=1 SCAR=6.9
Pod documented
366 0 my $self = shift;
367 0 my $sharename = $self->sharename;
368 0 my $fileid = $self->fileid;
369  
370 0 return $self->_file_contents("/files/$sharename/$fileid/blob/thumb");
371 }
372  
373 =head1 SEE ALSO
374  
375 L<Net::API::Gett>
376  
377 =cut
378  
379 5 1;
↑ Summary Gett.pm »