forked from squeezebox-googlemusic/squeezebox-googlemusic
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Image.pm
240 lines (164 loc) · 5.94 KB
/
Image.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
package Plugins::GoogleMusic::Image;
# Inspired by the Triode's Spotify Plugin
use strict;
use warnings;
use List::Util qw[min];
use HTTP::Status qw(RC_OK RC_NOT_FOUND RC_SERVICE_UNAVAILABLE);
use Slim::Utils::Log;
use Readonly;
Readonly my $EXP_TIME => 60 * 60 * 24 * 7; # expire in one week
Readonly my $MAX_IMAGE_REQUEST => 5; # max images to fetch from Google at once
Readonly my $IMAGE_REQUEST_TIMEOUT1 => 30; # max time to queue
Readonly my $IMAGE_REQUEST_TIMEOUT2 => 35; # max time to wait for response
my $log = logger('plugin.googlemusic');
my @fetchQ; # Q of images to fetch
my %fetching; # hash of images being fetched
my $id = 0;
# Initialization of the module
sub init {
Slim::Web::Pages->addRawFunction('/googlemusicimage', \&handler);
return;
}
# TBD: We could do this, but for now we use the squeezebox image proxy
sub handler {
my ($httpClient, $response) = @_;
my $path = $response->request->uri;
$path =~ /\/googlemusicimage\/(.*?)\/image #
(?:_(X|\d+)x(X|\d+))? # width and height are given here, e.g. 300x300
(?:_([sSfFpcom]))? # resizeMode, given by a single character
(?:_([\da-fA-F]+))? # background color, optional
\.jpg$
/ix;
my $image = $1;
my $needsResize = defined $2 || defined $3 || defined $4 || defined $5 || 0;
my $resizeParams = $needsResize ? [ $2, $3, $4, $5 ] : undef;
if (!$image) {
$log->info("bad image request - sending 404, path: $path");
$response->code(RC_NOT_FOUND);
$response->content_length(0);
Slim::Web::HTTP::addHTTPResponse($httpClient, $response, '', 1, 0);
return;
}
$id = ($id + 1) % 10_000;
$log->info("queuing image id: $id request: $image (resizing: $needsResize)");
push @fetchQ, { id => $id, timeout => time() + $IMAGE_REQUEST_TIMEOUT1, path => $path,
httpClient => $httpClient, response => $response, resizeP => $resizeParams, image => $image,
};
$log->debug(sub { "fetchQ: " . (scalar @fetchQ) . " fetching: " . (scalar keys %fetching) });
if (scalar keys %fetching < $MAX_IMAGE_REQUEST) {
_fetch();
} else {
# handle case where we don't appear to get a callback for an async request and it has timed out
for my $key (keys %fetching) {
if ($fetching{$key}->{'timeout'} < time()) {
$log->debug("stale fetch entry - closing");
my $entry = delete $fetching{$key};
_sendUnavailable($entry->{'httpClient'}, $entry->{'response'});
_fetch();
}
}
}
return;
}
sub _fetch {
my $entry;
while (!$entry && @fetchQ) {
$entry = shift @fetchQ;
if (!$entry->{'httpClient'}->connected) {
$entry = undef;
next;
}
if ($entry->{'timeout'} < time()) {
_sendUnavailable($entry->{'httpClient'}, $entry->{'response'});
$entry = undef;
}
}
return unless $entry;
my $image = $entry->{'image'};
my $resizeP = $entry->{'resizeP'};
if ($resizeP) {
my $s = min($resizeP->[0], $resizeP->[1]);
$image .= "=s$s-c";
}
$log->info("fetching image: $image");
$entry->{'timeout'} = time() + $IMAGE_REQUEST_TIMEOUT2;
$fetching{ $entry->{'id'} } = $entry;
Slim::Networking::SimpleAsyncHTTP->new(
\&_gotImage, \&_gotError, $entry
)->get("https://$image");
return;
}
sub _gotImage {
my $http = shift;
my $httpClient = $http->params('httpClient');
my $response = $http->params('response');
my $resizeP = $http->params('resizeP');
my $path = $http->params('path');
my $id = $http->params('id');
my $body;
if ($httpClient->connected) {
$response->code(RC_OK);
$response->content_type('image/jpeg');
$response->header('Cache-Control' => 'max-age=' . $EXP_TIME);
$response->expires(time() + $EXP_TIME);
use bytes;
$response->content_length($body ? length($$body) : length($http->content));
Slim::Web::HTTP::addHTTPResponse($httpClient, $response, $body || $http->contentRef, 1, 0);
}
delete $fetching{ $id };
_fetch();
return;
}
sub _gotError {
my $http = shift;
my $error = shift;
my $httpClient = $http->params('httpClient');
my $response = $http->params('response');
my $id = $http->params('id');
$log->warn("error: $error");
_sendUnavailable($httpClient, $response);
delete $fetching{ $id };
_fetch();
return;
}
sub _sendUnavailable {
my $httpClient = shift;
my $response = shift;
if ($httpClient->connected) {
$response->code(RC_SERVICE_UNAVAILABLE);
$response->header('Retry-After' => 10);
$response->content_length(0);
Slim::Web::HTTP::addHTTPResponse($httpClient, $response, '', 1, 0);
}
return;
}
sub uri {
my ($client, $image) = @_;
# Check if it's an squeezebox provided image
if ($image =~ /^\/html\/images\//) {
return $image;
}
# Sometimes there is an https:// prefix. Remove it.
$image =~ s/^https?\:\/\///;
# Very often there is already a size spec from Google. Remove it also.
$image =~ s/\=(.*)$//;
return "googlemusicimage/$image/image.jpg";
}
sub best {
my ($obj, $ref, $ref_array, $fallback) = @_;
my $image = $fallback;
if (exists $obj->{$ref}) {
$image = Plugins::GoogleMusic::Image->uri($obj->{$ref});
}
# eg. artistArtRefs is an array of images, one of which might have a 1:1 aspect ratio,
# so try that and fall back to eg. artistArtRef which is an aspect ratio crapshoot
if (exists $obj->{$ref_array}) {
for my $artRef (@{$obj->{$ref_array}}) {
if ($artRef->{aspectRatio} == 1) {
$image = Plugins::GoogleMusic::Image->uri($artRef->{url});
}
}
}
return $image;
}
1;