File Coverage

blib/lib/Net/API/Gett/File.pm
Criterion Covered Total %
statement 18 65 27.6
branch 1 28 3.5
condition 0 7 0.0
subroutine 5 9 55.5
pod 5 5 100.0
total 29 114 25.4


line stmt bran cond sub pod time code
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     5   30 use Moo;
  5         10  
  5         35  
10 5     5   1706 use Carp qw(croak);
  5         11  
  5         332  
11 5     5   35 use MooX::Types::MooseLike::Base qw(Int Str);
  5         9  
  5         6598  
12              
13             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 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.
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             has 'filename' => (
84             is => 'ro',
85             isa => Str,
86             );
87              
88             has 'fileid' => (
89             is => 'ro',
90             isa => Str,
91             );
92              
93             has 'downloads' => (
94             is => 'ro',
95             isa => Int,
96             );
97              
98             has 'readystate' => (
99             is => 'ro',
100             isa => Str,
101             );
102              
103             has 'getturl' => (
104             is => 'ro',
105             isa => Str,
106             );
107              
108             has 'download' => (
109             is => 'ro',
110             isa => Str,
111             );
112              
113             has 'size' => (
114             is => 'ro',
115             isa => Int,
116             );
117              
118             has 'created' => (
119             is => 'ro',
120             isa => Int,
121             );
122              
123             has 'sharename' => (
124             is => 'rw',
125             isa => Str,
126             );
127              
128             has 'put_upload_url' => (
129             is => 'ro',
130             isa => Str,
131             );
132              
133             has 'post_upload_url' => (
134             is => 'ro',
135             isa => Str,
136             );
137              
138             has 'chunk_size' => (
139             is => 'rw',
140             isa => Int,
141             default => sub { 1_048_576; },
142             );
143              
144             =over
145              
146             =item user
147              
148             L object. C predicate.
149              
150             =back
151              
152             =cut
153              
154             has 'user' => (
155             is => 'rw',
156             predicate => 'has_user',
157             isa => sub { die "$_[0] is not Net::API::Gett::User" unless ref($_[0]) =~ /User/ },
158             lazy => 1,
159             );
160              
161             =over
162              
163             =item request
164              
165             L object.
166              
167             =back
168              
169             =cut
170              
171             has 'request' => (
172             is => 'rw',
173             isa => sub { die "$_[0] is not Net::API::Gett::Request" unless ref($_[0]) =~ /Request/ },
174             default => sub { Net::API::Gett::Request->new() },
175             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 method, but it might be useful in combination with C. 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 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             sub send_file {
215 0     0 1 0 my $self = shift;
216 0         0 my $url = shift;
217 0         0 my $contents = shift;
218 0   0     0 my $encoding = shift || ":raw";
219 0   0     0 my $chunk_size = shift || $self->chunk_size || 1_048_576; # 1024 * 1024 = 1 MB
220              
221 0         0 my $fh;
222             my $length;
223 0 0       0 if ( ! ref($contents) ) {
224             # $contents is scalar
225 0 0       0 if ( ! -e $contents ) {
226             # $contents doesn't point to a valid filename,
227             # assume it's a buffer
228              
229 0         0 $contents .= "";
230             # Make sure this data is stringified.
231 0 0       0 open($fh, "<", \$contents) or croak "Couldn't open a filehandle on the content buffer\n";
232 0         0 binmode($fh, $encoding);
233 0         0 $length = length($contents);
234             }
235             else {
236 0 0       0 open($fh, "<", $contents) or croak "Couldn't open a filehandle on $contents: $!";
237 0         0 binmode($fh, $encoding);
238 0         0 $length = -s $contents;
239             }
240             }
241             else {
242 0 0       0 $fh = $contents if ref($contents) =~ /IO/;
243 0         0 $length = -s $fh;
244             }
245              
246 0 0       0 return 0 unless $fh;
247              
248 0         0 my $response = $self->request->put($url, $fh, $chunk_size, $length);
249              
250 0 0       0 if ( $response ) {
251 0         0 return 1;
252             }
253             else {
254 0         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.
264              
265             =back
266              
267             =cut
268              
269             sub get_upload_url {
270 0     0 1 0 my $self = shift;
271 0 0       0 croak "Cannot get_upload_url() without a Net::API::Gett::User object." unless $self->has_user;
272              
273 0         0 my $sharename = $self->sharename;
274 0         0 my $fileid = $self->fileid;
275              
276 0 0       0 $self->user->login unless $self->user->has_access_token;
277              
278 0         0 my $endpoint = "/files/$sharename/$fileid/upload?accesstoken=".$self->user->access_token;
279              
280 0         0 my $response = $self->request->get($endpoint);
281              
282 0 0 0     0 if ( $response && exists $response->{'puturl'} ) {
283 0         0 return $response->{'puturl'};
284             }
285             else {
286 0         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             sub destroy {
301 0     0 1 0 my $self = shift;
302 0 0       0 croak "Cannot destroy() without a Net::API::Gett::User object." unless $self->has_user;
303              
304 0         0 my $sharename = $self->sharename;
305 0         0 my $fileid = $self->fileid;
306              
307 0 0       0 $self->user->login unless $self->user->has_access_token;
308              
309 0         0 my $endpoint = "/files/$sharename/$fileid/destroy?accesstoken=".$self->access_token;
310              
311 0         0 my $response = $self->request->post($endpoint);
312              
313 0 0       0 if ( $response ) {
314 0         0 return 1;
315             }
316             else {
317 0         0 return undef;
318             }
319             }
320            
321             sub _file_contents {
322 1     1   4 my $self = shift;
323 1         9 my $endpoint = $self->request->base_url . shift;
324              
325 1         36 my $response = $self->request->ua->get($endpoint);
326              
327 1 50       226669 if ( $response->is_success ) {
328 1         21 return $response->content();
329             }
330             else {
331 0         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             sub contents {
347 1     1 1 4860 my $self = shift;
348 1         39 my $sharename = $self->sharename;
349 1         947 my $fileid = $self->fileid;
350              
351 1         9 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             sub thumbnail {
366 0     0 1   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
376              
377             =cut
378              
379             1;