App-cdnget

 view release on metacpan or  search on metacpan

lib/App/cdnget/Downloader.pm  view on Meta::CPAN

	}
	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($!);
				}
			}
			return ("Status: 200\r\nContent-Type: ".$headers->content_type."\r\nContent-Length: ".length($data)."\r\n", $data);
		}
		when (/^imgcrop$/i)
		{
			$params[0] = $img->width unless defined($params[0]) and $params[0] > 0;
			$params[1] = $img->height unless defined($params[1]) and $params[1] > 0;
			$params[2] = 0 unless defined($params[2]) and $params[2] > 0;
			$params[3] = 0 unless defined($params[3]) and $params[3] > 0;
			$params[4] = 60 unless defined($params[4]) and $params[4] >= 0 and $params[4] <= 100;
			my $newimg = new GD::Image($params[0], $params[1]) or $self->throw($!);
			$newimg->copy($img, 0, 0, $params[2], $params[3], $params[0], $params[1]);
			my $data;
			given ($headers->content_type)
			{
				when ("image/png")
				{
					$data = $newimg->png($params[4]) or $self->throw($!);
				}
				when ("image/jpeg")
				{
					$data = $newimg->jpeg($params[4]) or $self->throw($!);
				}
			}
			return ("Status: 200\r\nContent-Type: ".$headers->content_type."\r\nContent-Length: ".length($data)."\r\n", $data);
		}
		default
		{
			$self->throw("Unsupported img hook");
		}



( run in 2.362 seconds using v1.01-cache-2.11-cpan-75ffa21a3d4 )