App-cdnget
view release on metacpan or search on metacpan
lib/App/cdnget/Downloader.pm view on Meta::CPAN
sub terminating
{
lock($terminating);
return $terminating;
}
sub terminated
{
if (@_ > 0)
{
my $self = shift;
lock($self);
return defined($self->tid)? 0: 1;
}
lock($terminated);
return $terminated;
}
sub new
{
my $class = shift;
my ($uid, $path, $url, $hook) = @_;
while (not $downloaderSemaphore->down_timed(1))
{
if (terminating())
{
return;
}
}
if (terminating())
{
$downloaderSemaphore->up();
return;
}
lock(%uids);
return if exists($uids{$uid});
my $self = $class->SUPER();
$self->uid = $uid;
$self->path = $path;
$self->url = $url;
$self->hook = $hook;
$self->tid = undef;
{
lock($self);
my $thr = threads->create(\&run, $self) or $self->throw($!);
cond_wait($self);
unless (defined($self->tid))
{
App::cdnget::Exception->throw($thr->join());
}
$thr->detach();
}
$uids{$uid} = $self;
return $self;
}
sub DESTROY
{
my $self = shift;
$self->SUPER::DESTROY;
}
sub throw
{
my $self = shift;
my ($msg) = @_;
unless (ref($msg))
{
$msg = "Unknown" unless $msg;
$msg = "Downloader ".
"uid=".$self->uid." ".
"url=\"".shellmeta($self->url)."\" ".
"hook=\"".shellmeta($self->hook)."\" ".
$msg;
}
App::cdnget::Exception->throw($msg, 1);
}
sub processHook_img
{
my $self = shift;
my ($hook, $response, @params) = @_;
my $headers = $response->{_headers};
my $img;
given ($headers->content_type)
{
when ("image/png")
{
$img = GD::Image->newFromPngData($response->decoded_content) or $self->throw($!);
}
when ("image/jpeg")
{
$img = GD::Image->newFromJpegData($response->decoded_content) or $self->throw($!);
}
default
{
$self->throw("Unsupported content type for image");
}
}
given ($hook)
{
when (/^imgresize$/i)
{
$params[0] = $img->width unless defined($params[0]) and $params[0] > 0 and $params[0] <= 10000;
$params[1] = $img->height unless defined($params[1]) and $params[1] > 0 and $params[1] <= 10000;
$params[2] = 60 unless defined($params[2]) and $params[2] >= 0 and $params[2] <= 100;
my $newimg = new GD::Image($params[0], $params[1]) or $self->throw($!);
$newimg->copyResampled($img, 0, 0, 0, 0, $params[0], $params[1], $img->width, $img->height);
my $data;
given ($headers->content_type)
{
when ("image/png")
{
$data = $newimg->png($params[2]) or $self->throw($!);
}
when ("image/jpeg")
{
$data = $newimg->jpeg($params[2]) or $self->throw($!);
}
}
( run in 1.582 second using v1.01-cache-2.11-cpan-5b529ec07f3 )