line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Net::Amazon::Thumbnail;
|
2
|
2
|
|
|
2
|
|
2446
|
use strict;
|
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
72
|
|
3
|
2
|
|
|
2
|
|
9
|
use warnings;
|
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
62
|
|
4
|
|
|
|
|
|
|
|
5
|
2
|
|
|
2
|
|
12
|
use File::Spec;
|
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
54
|
|
6
|
2
|
|
|
2
|
|
3064
|
use LWP::UserAgent;
|
|
2
|
|
|
|
|
193369
|
|
|
2
|
|
|
|
|
257
|
|
7
|
2
|
|
|
2
|
|
23
|
use URI;
|
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
55
|
|
8
|
2
|
|
|
2
|
|
2310
|
use URI::QueryParam;
|
|
2
|
|
|
|
|
1903
|
|
|
2
|
|
|
|
|
543
|
|
9
|
2
|
|
|
2
|
|
7637
|
use XML::XPath;
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
use XML::XPath::XMLParser;
|
11
|
|
|
|
|
|
|
use Digest::HMAC_SHA1 qw(hmac_sha1);
|
12
|
|
|
|
|
|
|
use POSIX qw( strftime );
|
13
|
|
|
|
|
|
|
use base qw(Class::Accessor::Fast);
|
14
|
|
|
|
|
|
|
__PACKAGE__->mk_accessors(qw(aws_access_key_id secret_access_key empty_image thumb_size ua thumb_store urls method_type));
|
15
|
|
|
|
|
|
|
our $VERSION = "0.06";
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub new {
|
18
|
|
|
|
|
|
|
my($class, $parms) = @_;
|
19
|
|
|
|
|
|
|
my $self = {};
|
20
|
|
|
|
|
|
|
bless $self, $class;
|
21
|
|
|
|
|
|
|
if($parms->{debug}) {
|
22
|
|
|
|
|
|
|
my $fh = $parms->{debug};
|
23
|
|
|
|
|
|
|
$self->{debug} = do {local $/; <$fh> };
|
24
|
|
|
|
|
|
|
}
|
25
|
|
|
|
|
|
|
if($parms->{path}) {
|
26
|
|
|
|
|
|
|
die "Invalid directory:" . $parms->{path} unless -d $parms->{path};
|
27
|
|
|
|
|
|
|
}
|
28
|
|
|
|
|
|
|
my $ua = LWP::UserAgent->new;
|
29
|
|
|
|
|
|
|
$ua->timeout(30);
|
30
|
|
|
|
|
|
|
$self->ua($ua);
|
31
|
|
|
|
|
|
|
$self->aws_access_key_id($parms->{key_id});
|
32
|
|
|
|
|
|
|
$self->secret_access_key($parms->{access_key});
|
33
|
|
|
|
|
|
|
$self->thumb_size(ucfirst($parms->{size}) || 'Large');
|
34
|
|
|
|
|
|
|
$self->empty_image($parms->{no_image} || 0);
|
35
|
|
|
|
|
|
|
$self->thumb_store($parms->{path});
|
36
|
|
|
|
|
|
|
$self->method_type('GET');
|
37
|
|
|
|
|
|
|
return $self;
|
38
|
|
|
|
|
|
|
}
|
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub get_thumbnail {
|
41
|
|
|
|
|
|
|
my $self = shift;
|
42
|
|
|
|
|
|
|
my $url = shift;
|
43
|
|
|
|
|
|
|
die "No Url given\n" unless($url);
|
44
|
|
|
|
|
|
|
$self->method_type('GET') unless($self->method_type eq 'GET');
|
45
|
|
|
|
|
|
|
my $thumbs = $self->_XML2thumb($self->_request($self->_format_url($url)));
|
46
|
|
|
|
|
|
|
return $thumbs;
|
47
|
|
|
|
|
|
|
}
|
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub post_thumbnail {
|
50
|
|
|
|
|
|
|
my $self = shift;
|
51
|
|
|
|
|
|
|
my $url = shift;
|
52
|
|
|
|
|
|
|
die "No Url given\n" unless($url);
|
53
|
|
|
|
|
|
|
$self->method_type('POST') unless($self->method_type eq 'POST');
|
54
|
|
|
|
|
|
|
my $thumbs = $self->_XML2thumb($self->_request($self->_format_url($url)));
|
55
|
|
|
|
|
|
|
return $thumbs;
|
56
|
|
|
|
|
|
|
}
|
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub _store {
|
60
|
|
|
|
|
|
|
my $self = shift;
|
61
|
|
|
|
|
|
|
my $image = shift;
|
62
|
|
|
|
|
|
|
my $url = shift;
|
63
|
|
|
|
|
|
|
my $uri = URI->new($url);
|
64
|
|
|
|
|
|
|
my $name = $uri->authority;
|
65
|
|
|
|
|
|
|
if($self->{nameList} && $self->{nameList}->{$name}) {
|
66
|
|
|
|
|
|
|
$name = $self->{nameList}->{$name};
|
67
|
|
|
|
|
|
|
}
|
68
|
|
|
|
|
|
|
my $path = File::Spec->catfile($self->thumb_store, $name . '.jpg');
|
69
|
|
|
|
|
|
|
unlink($path) if -e $path;
|
70
|
|
|
|
|
|
|
my $response = $self->ua->get($image, ':content_file' => $path);
|
71
|
|
|
|
|
|
|
return $path;
|
72
|
|
|
|
|
|
|
}
|
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub _XML2thumb {
|
75
|
|
|
|
|
|
|
my $self = shift;
|
76
|
|
|
|
|
|
|
my $xp = shift;
|
77
|
|
|
|
|
|
|
my $thumbs;
|
78
|
|
|
|
|
|
|
my $image;
|
79
|
|
|
|
|
|
|
foreach my $response ($xp->find('/aws:ThumbnailResponse/aws:Response/aws:ThumbnailResult')->get_nodelist){
|
80
|
|
|
|
|
|
|
my $thumbnode = $response->find('aws:Thumbnail');
|
81
|
|
|
|
|
|
|
my $node = $thumbnode->get_node(0);
|
82
|
|
|
|
|
|
|
if($node->findvalue('@Exists') eq 'true') {
|
83
|
|
|
|
|
|
|
my $thumbnail_url = $node->string_value;
|
84
|
|
|
|
|
|
|
my $request_url = $response->find('aws:RequestUrl')->string_value;
|
85
|
|
|
|
|
|
|
$image = ($self->thumb_store) ? $self->_store($thumbnail_url, $request_url) : $thumbnail_url;
|
86
|
|
|
|
|
|
|
}
|
87
|
|
|
|
|
|
|
else {
|
88
|
|
|
|
|
|
|
$image = $self->empty_image;
|
89
|
|
|
|
|
|
|
}
|
90
|
|
|
|
|
|
|
push(@{ $thumbs }, $image);
|
91
|
|
|
|
|
|
|
}
|
92
|
|
|
|
|
|
|
return $thumbs;
|
93
|
|
|
|
|
|
|
}
|
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub _format_url {
|
96
|
|
|
|
|
|
|
my $self = shift;
|
97
|
|
|
|
|
|
|
my $url = shift;
|
98
|
|
|
|
|
|
|
my %urls_param;
|
99
|
|
|
|
|
|
|
my $scheme_reg = qr/^http/i;
|
100
|
|
|
|
|
|
|
if ( ! ref($url) ) {
|
101
|
|
|
|
|
|
|
$urls_param{'Url'} = ($url !~ $scheme_reg) ? "http://$url" : $url;
|
102
|
|
|
|
|
|
|
$urls_param{'Size'} = $self->thumb_size;
|
103
|
|
|
|
|
|
|
}
|
104
|
|
|
|
|
|
|
elsif ( UNIVERSAL::isa($url,'HASH') ) {
|
105
|
|
|
|
|
|
|
my $next = 0;
|
106
|
|
|
|
|
|
|
for my $key ( keys %$url ) {
|
107
|
|
|
|
|
|
|
$next++;
|
108
|
|
|
|
|
|
|
my $url_value = ($key !~ $scheme_reg) ? "http://$key" : $key;
|
109
|
|
|
|
|
|
|
my $name = (length(_trim($url->{$key})) > 0) ? $url->{$key} : 0;
|
110
|
|
|
|
|
|
|
my $uri = URI->new($url_value);
|
111
|
|
|
|
|
|
|
my $key_name = $uri->authority;
|
112
|
|
|
|
|
|
|
$self->{nameList}->{$key_name} = $name;
|
113
|
|
|
|
|
|
|
$urls_param{"Thumbnail.$next.Url"} = $url_value;
|
114
|
|
|
|
|
|
|
}
|
115
|
|
|
|
|
|
|
$urls_param{'Shared.Size'} = $self->thumb_size;
|
116
|
|
|
|
|
|
|
}
|
117
|
|
|
|
|
|
|
elsif ( UNIVERSAL::isa($url,'ARRAY') ) {
|
118
|
|
|
|
|
|
|
my @url_array = @{$url};
|
119
|
|
|
|
|
|
|
my $size = scalar @url_array;
|
120
|
|
|
|
|
|
|
my $next = 0;
|
121
|
|
|
|
|
|
|
for (my $i = 0; $i < $size; $i++){
|
122
|
|
|
|
|
|
|
$next = $i + 1;
|
123
|
|
|
|
|
|
|
my $url_value = ($url_array[$i] !~ $scheme_reg) ? "http://" . $url_array[$i] : $url_array[$i];
|
124
|
|
|
|
|
|
|
$urls_param{"Thumbnail.$next.Url"} = $url_value;
|
125
|
|
|
|
|
|
|
}
|
126
|
|
|
|
|
|
|
$urls_param{'Shared.Size'} = $self->thumb_size;
|
127
|
|
|
|
|
|
|
}
|
128
|
|
|
|
|
|
|
return \%urls_param;
|
129
|
|
|
|
|
|
|
}
|
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
sub _request {
|
132
|
|
|
|
|
|
|
my($self, $parms) = @_;
|
133
|
|
|
|
|
|
|
my $output;
|
134
|
|
|
|
|
|
|
my $xp;
|
135
|
|
|
|
|
|
|
my $response;
|
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
$parms->{Action} = "Thumbnail";
|
138
|
|
|
|
|
|
|
$parms->{AWSAccessKeyId} = $self->aws_access_key_id;
|
139
|
|
|
|
|
|
|
$parms->{Timestamp} = strftime("%Y-%m-%dT%H:%M:%S.000Z",gmtime);
|
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
my $hmac = Digest::HMAC_SHA1->new($self->secret_access_key);
|
142
|
|
|
|
|
|
|
$hmac->add( $parms->{Action} . $parms->{Timestamp} );
|
143
|
|
|
|
|
|
|
$parms->{Signature} = $hmac->b64digest . '=';
|
144
|
|
|
|
|
|
|
my $url = 'http://ast.amazonaws.com/xino/?';
|
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
my $uri = URI->new($url);
|
147
|
|
|
|
|
|
|
$uri->query_param($_, $parms->{$_}) foreach keys %$parms;
|
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
if($self->{debug}) {
|
150
|
|
|
|
|
|
|
$xp = XML::XPath->new(xml => $self->{debug});
|
151
|
|
|
|
|
|
|
return $xp;
|
152
|
|
|
|
|
|
|
}
|
153
|
|
|
|
|
|
|
else {
|
154
|
|
|
|
|
|
|
$response = ($self->method_type eq 'GET') ? $self->ua->get("$uri") : $self->ua->post("$uri");
|
155
|
|
|
|
|
|
|
$output = $response->content;
|
156
|
|
|
|
|
|
|
}
|
157
|
|
|
|
|
|
|
$xp = XML::XPath->new(xml => $output);
|
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
unless($response->is_success) {
|
160
|
|
|
|
|
|
|
my $error_code = $xp->findvalue('//*[name() = "Code"]') || 'N/A';
|
161
|
|
|
|
|
|
|
my $error_msg = $xp->findvalue('//*[name() = "Message"]') || 'N/A';
|
162
|
|
|
|
|
|
|
my $request_id = $xp->findvalue('//*[name() = "RequestID"]') || 'N/A';
|
163
|
|
|
|
|
|
|
my $error_format = "Error fetching response for request id: %s\nResponse Status: %s\nResponse Code: %s\nResponse Message: %s\n";
|
164
|
|
|
|
|
|
|
die sprintf($error_format, $request_id, $response->status_line, $error_code, $error_msg);
|
165
|
|
|
|
|
|
|
}
|
166
|
|
|
|
|
|
|
return $xp;
|
167
|
|
|
|
|
|
|
}
|
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
sub _trim {
|
170
|
|
|
|
|
|
|
@_ = @_ ? @_ : $_ if defined wantarray;
|
171
|
|
|
|
|
|
|
for (@_ ? @_ : $_) { s/\A\s+//; s/\s+\z// }
|
172
|
|
|
|
|
|
|
return wantarray ? @_ : "@_";
|
173
|
|
|
|
|
|
|
}
|
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
1;
|
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
__END__
|