line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::Cloudinary; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
Mojolicious::Plugin::Cloudinary - Talk with cloudinary.com |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 DESCRIPTION |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
This register the methods from the L module as helpers in |
10
|
|
|
|
|
|
|
your L web application. See L for details. |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 SYNOPSIS |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
package MyWebApp; |
15
|
|
|
|
|
|
|
use Mojo::Base 'Mojolicious'; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub startup { |
18
|
|
|
|
|
|
|
my $self = shift; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
$self->plugin('Mojolicious::Plugin::Cloudinary', { |
21
|
|
|
|
|
|
|
cloud_name => $str, |
22
|
|
|
|
|
|
|
api_key => $str, |
23
|
|
|
|
|
|
|
api_secret => $str, |
24
|
|
|
|
|
|
|
}); |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
package MyWebApp::SomeController; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub upload { |
30
|
|
|
|
|
|
|
my $self = shift; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
$self->render_later; |
33
|
|
|
|
|
|
|
Mojo::IOLoop->delay( |
34
|
|
|
|
|
|
|
sub { |
35
|
|
|
|
|
|
|
my($delay) = @_; |
36
|
|
|
|
|
|
|
$self->cloudinary_upload( |
37
|
|
|
|
|
|
|
{ |
38
|
|
|
|
|
|
|
file => $self->param('upload_param'), |
39
|
|
|
|
|
|
|
}, |
40
|
|
|
|
|
|
|
$delay->begin, |
41
|
|
|
|
|
|
|
); |
42
|
|
|
|
|
|
|
}, |
43
|
|
|
|
|
|
|
sub { |
44
|
|
|
|
|
|
|
my($delay, $res, $tx) = @_; |
45
|
|
|
|
|
|
|
return $self->render(json => $res) if $res; |
46
|
|
|
|
|
|
|
return $self->render_exception; |
47
|
|
|
|
|
|
|
}, |
48
|
|
|
|
|
|
|
); |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=cut |
52
|
|
|
|
|
|
|
|
53
|
2
|
|
|
2
|
|
304914
|
use Mojo::Base -base; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
19
|
|
54
|
2
|
|
|
2
|
|
735
|
use File::Basename; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
413
|
|
55
|
2
|
|
|
2
|
|
831
|
use Mojo::UserAgent; |
|
2
|
|
|
|
|
141371
|
|
|
2
|
|
|
|
|
27
|
|
56
|
2
|
|
|
2
|
|
72
|
use Mojo::Util qw/ sha1_sum url_escape /; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
124
|
|
57
|
2
|
|
|
2
|
|
11
|
use Scalar::Util 'weaken'; |
|
2
|
|
|
|
|
12
|
|
|
2
|
|
|
|
|
92
|
|
58
|
2
|
|
|
2
|
|
11
|
use base qw/ Cloudinary Mojolicious::Plugin /; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
1678
|
|
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
our $VERSION = 0.0402; # just need something higher than the previous version |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=head2 js_image |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
This string will be used as the image src for images constructed by |
67
|
|
|
|
|
|
|
L. The default is "/image/blank.png". |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=cut |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
has js_image => '/image/blank.png'; |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=head1 HELPERS |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=head2 cloudinary_upload |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
See L. |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=head2 cloudinary_destroy |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
See L. |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=head2 cloudinary_url_for |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
See L. |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=head2 cloudinary_image |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
$str = $c->cloudinary_image($public_id, $url_for_args, $image_args); |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
This will use L to create an image |
92
|
|
|
|
|
|
|
tag where "src" is set to a cloudinary image. C<$url_for_args> are passed |
93
|
|
|
|
|
|
|
on to L and C<$image_args> are passed on to |
94
|
|
|
|
|
|
|
L. |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=head2 cloudinary_js_image |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
$str = $c->cloudinary_js_image($public_id, $url_for_args); |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
About the same as L, except it creates an image which can |
101
|
|
|
|
|
|
|
handled by the cloudinary jQuery plugin which you can read more about here: |
102
|
|
|
|
|
|
|
L |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
Example usage: |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
$c->cloudinary_js_image(1234567890 => { |
107
|
|
|
|
|
|
|
width => 115, |
108
|
|
|
|
|
|
|
height => 115, |
109
|
|
|
|
|
|
|
crop => 'thumb', |
110
|
|
|
|
|
|
|
gravity => 'faces', |
111
|
|
|
|
|
|
|
radius => '20', |
112
|
|
|
|
|
|
|
}); |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
...will produce: |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
class="cloudinary-js-image" |
118
|
|
|
|
|
|
|
alt="1234567890" |
119
|
|
|
|
|
|
|
data-src="1234567890" |
120
|
|
|
|
|
|
|
data-width="115" |
121
|
|
|
|
|
|
|
data-height="135" |
122
|
|
|
|
|
|
|
data-crop="thumb" |
123
|
|
|
|
|
|
|
data-gravity="faces" |
124
|
|
|
|
|
|
|
data-radius="20"> |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
Note: The "class" and "alt" attributes are fixed for now. |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=head1 METHODS |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=head2 register |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
Will register the L in the L application. |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=cut |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
sub register { |
137
|
1
|
|
|
1
|
1
|
53
|
my($self, $app, $config) = @_; |
138
|
|
|
|
|
|
|
|
139
|
1
|
50
|
|
|
|
2
|
for my $k (keys %{ $config || {} }) { |
|
1
|
|
|
|
|
8
|
|
140
|
1
|
50
|
|
|
|
33
|
$self->$k($config->{$k}) if exists $config->{$k}; |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
1
|
|
|
|
|
48
|
$self->_ua($app->ua); |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
$app->helper( |
146
|
|
|
|
|
|
|
cloudinary_upload => sub { |
147
|
1
|
|
|
1
|
|
55050
|
my $c = shift; |
148
|
1
|
|
|
|
|
15
|
$self->upload(@_); |
149
|
|
|
|
|
|
|
} |
150
|
1
|
|
|
|
|
209
|
); |
151
|
|
|
|
|
|
|
$app->helper( |
152
|
|
|
|
|
|
|
cloudinary_destroy => sub { |
153
|
1
|
|
|
1
|
|
242811
|
my $c = shift; |
154
|
1
|
|
|
|
|
17
|
$self->destroy(@_); |
155
|
|
|
|
|
|
|
} |
156
|
1
|
|
|
|
|
2522
|
); |
157
|
|
|
|
|
|
|
$app->helper( |
158
|
|
|
|
|
|
|
cloudinary_url_for => sub { |
159
|
1
|
|
|
1
|
|
81788
|
my($c, $public_id, $args) = @_; |
160
|
1
|
|
50
|
|
|
6
|
my $scheme = $c->req->url->scheme || ''; |
161
|
|
|
|
|
|
|
|
162
|
1
|
50
|
33
|
|
|
118
|
if(not defined $args->{'secure'} and $scheme eq 'https') { |
163
|
0
|
|
|
|
|
0
|
$args->{'secure'} = 1; |
164
|
|
|
|
|
|
|
} |
165
|
|
|
|
|
|
|
|
166
|
1
|
|
|
|
|
15
|
return $self->url_for($public_id, $args); |
167
|
|
|
|
|
|
|
} |
168
|
1
|
|
|
|
|
116
|
); |
169
|
|
|
|
|
|
|
$app->helper( |
170
|
|
|
|
|
|
|
cloudinary_image => sub { |
171
|
1
|
|
|
1
|
|
56240
|
my($c, $public_id, $args, $image_args) = @_; |
172
|
1
|
|
50
|
|
|
7
|
my $scheme = $c->req->url->scheme || ''; |
173
|
|
|
|
|
|
|
|
174
|
1
|
50
|
33
|
|
|
158
|
if(not defined $args->{'secure'} and $scheme eq 'https') { |
175
|
0
|
|
|
|
|
0
|
$args->{'secure'} = 1; |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
|
178
|
1
|
|
|
|
|
23
|
return $c->image($self->url_for($public_id, $args), alt => $public_id, %$image_args); |
179
|
|
|
|
|
|
|
} |
180
|
1
|
|
|
|
|
5849
|
); |
181
|
|
|
|
|
|
|
$app->helper( |
182
|
|
|
|
|
|
|
cloudinary_js_image => sub { |
183
|
1
|
|
|
1
|
|
42161
|
my($c, $public_id, $args) = @_; |
184
|
1
|
|
50
|
|
|
7
|
my $scheme = $c->req->url->scheme || ''; |
185
|
|
|
|
|
|
|
|
186
|
1
|
50
|
33
|
|
|
123
|
if(not defined $args->{'secure'} and $scheme eq 'https') { |
187
|
0
|
|
|
|
|
0
|
$args->{'secure'} = 1; |
188
|
|
|
|
|
|
|
} |
189
|
|
|
|
|
|
|
|
190
|
2
|
|
66
|
|
|
21
|
return $c->image( |
191
|
|
|
|
|
|
|
$self->js_image, |
192
|
|
|
|
|
|
|
'alt' => $public_id, |
193
|
|
|
|
|
|
|
'class' => 'cloudinary-js-image', |
194
|
|
|
|
|
|
|
'data-src' => $public_id, |
195
|
|
|
|
|
|
|
map { |
196
|
1
|
|
|
|
|
26
|
my $k = $Cloudinary::LONGER{$_} || $_; |
197
|
2
|
|
|
|
|
13
|
("data-$k" => $args->{$_}) |
198
|
|
|
|
|
|
|
} keys %$args |
199
|
|
|
|
|
|
|
); |
200
|
|
|
|
|
|
|
} |
201
|
1
|
|
|
|
|
1883
|
); |
202
|
|
|
|
|
|
|
} |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
=head1 COPYRIGHT & LICENSE |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
See L. |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
=head1 AUTHOR |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
Jan Henning Thorsen - jhthorsen@cpan.org |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
=cut |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
1; |