You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
1.2 KiB
44 lines
1.2 KiB
package HexGrid::Image;
|
|
|
|
use v5.30;
|
|
|
|
use Moo;
|
|
use MooX::Aliases;
|
|
use LWP::UserAgent;
|
|
use MIME::Base64;
|
|
|
|
use feature "signatures";
|
|
no warnings "experimental::signatures";
|
|
|
|
my $DEBUG = 0;
|
|
|
|
my $USER_AGENT;
|
|
|
|
has source => (is => 'rw', required => 1, alias => [qw(src url source_url)]);
|
|
has fetch => (is => 'rw', default => 0);
|
|
has id => (is => 'rw', required => 1);
|
|
has width => (is => 'rw', required => 1);
|
|
has height => (is => 'rw', required => 1);
|
|
|
|
sub render($this, $container)
|
|
{
|
|
# hard coded into external URL mode
|
|
my $image_element = $container->image(id => $this->{id});
|
|
$image_element->{href} = $this->{fetch} ? $this->_fetch_base64() : $this->{source};
|
|
$image_element->{width} = $this->{width} if defined($this->{width});
|
|
$image_element->{height} = $this->{height} if defined($this->{height});
|
|
return $image_element;
|
|
}
|
|
|
|
sub _fetch_base64($this)
|
|
{
|
|
$USER_AGENT // ($USER_AGENT = LWP::UserAgent->new(timeout => 10));
|
|
say STDERR "Fetching $this->{source}";
|
|
my $response = $USER_AGENT->get($this->{source});
|
|
my $content_type = $response->headers->content_type;
|
|
my $encoded_content = encode_base64($response->content);
|
|
return "data:$content_type;base64, $encoded_content";
|
|
}
|
|
|
|
sub DEBUG { $DEBUG = 1; }
|
|
1;
|
|
|