diff --git a/lib/BurguillosInfo/Categories.pm b/lib/BurguillosInfo/Categories.pm index 688f8e0..bc743d0 100644 --- a/lib/BurguillosInfo/Categories.pm +++ b/lib/BurguillosInfo/Categories.pm @@ -173,11 +173,15 @@ sub _AvoidGrandChildCategories ( $self, $categories ) { } } -sub PreviewOg ( $self, $category ) { +sub PreviewOg ( $self, $category, $isWhatsApp = 0 ) { my $title = $category->{title}; my $description = $category->{description}; my $image = $category->{image}; my $image_bottom_preview = $category->{image_bottom_preview}; + if ($isWhatsApp) { + return BurguillosInfo::Preview->WhatsappAlternativeGenerate( $title, $description, $image, + $image_bottom_preview ); + } return BurguillosInfo::Preview->Generate( $title, $description, $image, $image_bottom_preview ); } diff --git a/lib/BurguillosInfo/Controller/Attribute.pm b/lib/BurguillosInfo/Controller/Attribute.pm index 7107bf8..d0e1ef0 100644 --- a/lib/BurguillosInfo/Controller/Attribute.pm +++ b/lib/BurguillosInfo/Controller/Attribute.pm @@ -25,12 +25,23 @@ sub get_attribute_preview ($self) { return $self->reply->not_found; } - $self->render( - format => 'png', - data => BurguillosInfo::Preview->Generate( + my $is_whatsapp = $self->req->headers->user_agent =~ /whatsapp/i; + my $data; + if ($is_whatsapp) { + $data = BurguillosInfo::Preview->WhatsappAlternativeGenerate( $attribute->{title}, $attribute->{description}, $attribute->{image}, $attribute->{image_bottom_preview} - ), + ) + } else { + $data = BurguillosInfo::Preview->Generate( + $attribute->{title}, $attribute->{description}, + $attribute->{image}, $attribute->{image_bottom_preview} + ) + } + + $self->render( + format => 'png', + data => $data, ); } diff --git a/lib/BurguillosInfo/Controller/Page.pm b/lib/BurguillosInfo/Controller/Page.pm index 8a806ec..efc8384 100644 --- a/lib/BurguillosInfo/Controller/Page.pm +++ b/lib/BurguillosInfo/Controller/Page.pm @@ -167,9 +167,10 @@ sub get_category_preview { return; } my $category = $categories->{$category_slug}; + my $is_whatsapp = $self->req->headers->user_agent =~ /whatsapp/i; $self->render( format => 'png', - data => $category_model->PreviewOg($category) + data => $category_model->PreviewOg($category, $is_whatsapp) ); } @@ -182,10 +183,12 @@ sub get_post_preview { $self->render( template => '404', status => 404 ); return; } + my $is_whatsapp = $self->req->headers->user_agent =~ /whatsapp/i; + say $self->req->headers->user_agent; my $post = $posts_slug->{$slug}; $self->render( format => 'png', - data => $post_model->PreviewOg($post) + data => $post_model->PreviewOg($post, $is_whatsapp) ); } 1; diff --git a/lib/BurguillosInfo/Posts.pm b/lib/BurguillosInfo/Posts.pm index 1688695..bd39522 100644 --- a/lib/BurguillosInfo/Posts.pm +++ b/lib/BurguillosInfo/Posts.pm @@ -220,13 +220,15 @@ sub RetrieveDirectPostsForCategory ( $self, $category_name ) { return $self->shufflePostsIfRequired( $category, [@$posts] ); } -sub PreviewOg { - my $self = shift; - my $post = shift; +sub PreviewOg($self, $post, $isWhatsApp) { my $title = $post->{title}; my $content = $post->{content}; my $image_file = $post->{image}; my $image_bottom_preview = $post->{image_bottom_preview}; + if ($isWhatsApp) { + return BurguillosInfo::Preview->WhatsappAlternativeGenerate( $title, $content, $image_file, + $image_bottom_preview ); + } return BurguillosInfo::Preview->Generate( $title, $content, $image_file, $image_bottom_preview ); } diff --git a/lib/BurguillosInfo/Preview.pm b/lib/BurguillosInfo/Preview.pm index de28005..32e7b22 100644 --- a/lib/BurguillosInfo/Preview.pm +++ b/lib/BurguillosInfo/Preview.pm @@ -14,6 +14,7 @@ use Const::Fast; use Capture::Tiny qw/capture/; use MIME::Base64; use Digest::SHA qw/sha512_hex/; +use Encode; const my $CURRENT_FILE => __FILE__; const my $ROOT_PROJECT => path($CURRENT_FILE)->parent->parent->parent; @@ -30,7 +31,7 @@ sub Generate ( $image_bottom_preview = undef ) { - my $sha512 = sha512_hex($title.$content.$image_file.$image_bottom_preview); + my $sha512 = sha512_hex(Encode::encode('utf8', $title.$content.($image_file//'').($image_bottom_preview//''))); my $cached_image = path("public/img/preview.$sha512.generated.png"); if (!-f $cached_image) { my $dom = Mojo::DOM->new($content); @@ -46,6 +47,18 @@ sub Generate ( return $cached_image->slurp_raw; } +sub WhatsappAlternativeGenerate($self, $title, $content, $image_file = undef, $image_bottom_preview = undef) { + my $complete_png = $self->Generate($title, $content, $image_file, $image_bottom_preview); + my ( $stdout, $stderr ) = capture { + open my $fh, '|-', 'convert', '/dev/stdin', '-resize', "@{[$SVG_WIDTH/2]}x@{[$SVG_HEIGHT/2]}", 'png:fd:1'; + binmode $fh, ':raw'; + print $fh $complete_png; + close $fh; + }; + say STDERR $stderr; + return $stdout; +} + sub _ToPng ( $self, $image ) { if ( $image =~ /\.\w+$/ ) { my $new_image = $image =~ s/\.\w+$/.generated.png/r; @@ -137,10 +150,9 @@ sub _GenerateSVGPreview ( $self, $title, $content, $image_file, } sub _SVGToPNG ( $self, $svg ) { - path('a.svg')->spew_utf8($svg); my ( $stdout, $stderr ) = capture { open my $fh, '|-', qw{convert /dev/stdin png:fd:1}; - binmode $fh, 'utf8'; + binmode $fh, ':utf8'; print $fh $svg; close $fh; };