/
File.pm
341 lines (222 loc) · 6.03 KB
/
File.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package Net::API::Gett::File;
=head1 NAME
Net::API::Gett::File - Gett file object
=cut
use Moo;
use Sub::Quote;
use Carp qw(croak);
use File::Slurp qw(read_file);
use MooX::Types::MooseLike qw(Int Str);
our $VERSION = '0.02';
=head1 PURPOSE
Encapsulate Gett files. You normally shouldn't instantiate this class on
its own, as the library will create and return this object as appropriate.
=head1 ATTRIBUTES
These are read only attributes.
=over
=item filename
Scalar string
=item fileid
Scalar integer
=item downloads
Scalar integer. The number of times this particular file has been downloaded
=item readystate
Scalar string. Signifies the state a particular file is in. See the
L<Gett developer docs|http://ge.tt/developers> for more information.
=item url
Scalar string. The URL to use in a browser to access a file.
=item download
Scalar string. The URL to use to get the file contents.
=item size
Scalar integer. The size in bytes of this file.
=item created
Scalar integer. The Unix epoch time when this file was created in Gett. This value is suitable
for use in C<localtime()>.
=item sharename
Scalar string. The share in which this file lives inside.
=item put_upload_url
Scalar string. The url to use to upload the contents of this file using the PUT method. (This
attribute is only populated during certain times.)
=item post_upload_url
Scalar string. This url to use to upload the contents of this file using the POST method. (This
attribute is only populated during certain times.)
=back
=cut
has 'filename' => (
is => 'ro',
isa => Str,
);
has 'fileid' => (
is => 'ro',
isa => Int,
);
has 'downloads' => (
is => 'ro',
isa => Int,
);
has 'readystate' => (
is => 'ro',
isa => Str,
);
has 'url' => (
is => 'ro',
isa => Str,
);
has 'download' => (
is => 'ro',
isa => Str,
);
has 'size' => (
is => 'ro',
isa => Int,
);
has 'created' => (
is => 'ro',
isa => Int,
);
has 'sharename' => (
is => 'rw',
isa => Str,
);
has 'put_upload_url' => (
is => 'ro',
isa => Str,
);
has 'post_upload_url' => (
is => 'ro',
isa => Str,
);
=over
=item user
L<Net::API::Gett::User> object. C<has_user()> predicate.
=back
=cut
has 'user' => (
is => 'rw',
predicate => 'has_user',
isa => sub { die "$_[0] is not Net::API::Gett::User" unless ref($_[0]) =~ /User/ },
lazy => 1,
);
=over
=item request
L<Net::API::Gett::Request> object.
=back
=cut
has 'request' => (
is => 'rw',
isa => sub { die "$_[0] is not Net::API::Gett::Request" unless ref($_[0]) =~ /Request/ },
default => sub { Net::API::Gett::Request->new() },
lazy => 1,
);
=over
=item send_file()
This method actually uploads the file to the Gett service. This method is normally invoked by the
C<upload_file()> method, but it's a public method which might be useful in combination with
C<get_upload_url()>. It takes the following parameters:
=over
=item *
a PUT based Gett upload url
=item *
a scalar representing the file contents which can be one of: a buffer, an L<IO::Handle> object, a FILEGLOB, or a
file pathname.
=item *
an encoding scheme. By default, it uses C<:raw> (see C<perldoc -f binmode> for more information.)
=back
Returns a true value on success.
=back
=cut
sub send_file {
my $self = shift;
my $url = shift;
my $contents = shift;
my $encoding = shift || ":raw";
my $data = read_file($contents, { binmode => $encoding });
return 0 unless $data;
my $response = $self->request->put($url, $data);
if ( $response ) {
return 1;
}
else {
return undef;
}
}
=over
=item get_upload_url()
This method returns a scalar PUT upload URL for the specified sharename/fileid parameters.
Potentially useful in combination with C<send_file()>.
=back
=cut
sub get_upload_url {
my $self = shift;
croak "Cannot get_upload_url() without a Net::API::Gett::User object." unless $self->has_user;
my $sharename = $self->sharename;
my $fileid = $self->fileid;
$self->user->login unless $self->user->has_access_token;
my $endpoint = "/files/$sharename/$fileid/upload?accesstoken=".$self->user->access_token;
my $response = $self->request->get($endpoint);
if ( $response && exists $response->{'puturl'} ) {
return $response->{'puturl'};
}
else {
croak "Could not get a PUT url from $endpoint";
}
}
=over
=item destroy_file()
This method destroys a file specified by the given sharename/fileid parameters. Returns a true value.
=back
=cut
sub destroy {
my $self = shift;
croak "Cannot destroy() without a Net::API::Gett::User object." unless $self->has_user;
my $sharename = $self->sharename;
my $fileid = $self->fileid;
$self->user->login unless $self->user->has_access_token;
my $endpoint = "/files/$sharename/$fileid/destroy?accesstoken=".$self->access_token;
my $response = $self->request->post($endpoint);
if ( $response ) {
return 1;
}
else {
return undef;
}
}
sub _file_contents {
my $self = shift;
my $endpoint = $self->request->base_url . shift;
my $response = $self->request->ua->get($endpoint);
if ( $response->is_success ) {
return $response->content();
}
else {
croak "$endpoint said " . $response->status_line;
}
}
=over
=item contents()
This method retrieves the contents of a this file in the Gett service. You are responsible for
outputting the file (if desired) with any appropriate encoding.
=back
=cut
sub contents {
my $self = shift;
my $sharename = $self->sharename;
my $fileid = $self->fileid;
return $self->_file_contents("/files/$sharename/$fileid/blob");
}
=over
=item thumbnail()
This method returns a thumbnail if the file in Gett is an image. Requires a
sharename and fileid.
=back
=cut
sub thumbnail {
my $self = shift;
my $sharename = $self->sharename;
my $fileid = $self->fileid;
return $self->_file_contents("/files/$sharename/$fileid/blob/thumb");
}
=head1 SEE ALSO
L<Net::API::Gett>
=cut
1;