Trying again to fix previews for whatsapp.

This commit is contained in:
sergiotarxz 2023-09-17 10:17:50 +02:00
parent 94287532a7
commit 90b2c19022
5 changed files with 45 additions and 13 deletions

View File

@ -173,11 +173,15 @@ sub _AvoidGrandChildCategories ( $self, $categories ) {
} }
} }
sub PreviewOg ( $self, $category ) { sub PreviewOg ( $self, $category, $isWhatsApp = 0 ) {
my $title = $category->{title}; my $title = $category->{title};
my $description = $category->{description}; my $description = $category->{description};
my $image = $category->{image}; my $image = $category->{image};
my $image_bottom_preview = $category->{image_bottom_preview}; 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, return BurguillosInfo::Preview->Generate( $title, $description, $image,
$image_bottom_preview ); $image_bottom_preview );
} }

View File

@ -25,12 +25,23 @@ sub get_attribute_preview ($self) {
return $self->reply->not_found; return $self->reply->not_found;
} }
$self->render( my $is_whatsapp = $self->req->headers->user_agent =~ /whatsapp/i;
format => 'png', my $data;
data => BurguillosInfo::Preview->Generate( if ($is_whatsapp) {
$data = BurguillosInfo::Preview->WhatsappAlternativeGenerate(
$attribute->{title}, $attribute->{description}, $attribute->{title}, $attribute->{description},
$attribute->{image}, $attribute->{image_bottom_preview} $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,
); );
} }

View File

@ -167,9 +167,10 @@ sub get_category_preview {
return; return;
} }
my $category = $categories->{$category_slug}; my $category = $categories->{$category_slug};
my $is_whatsapp = $self->req->headers->user_agent =~ /whatsapp/i;
$self->render( $self->render(
format => 'png', 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 ); $self->render( template => '404', status => 404 );
return; return;
} }
my $is_whatsapp = $self->req->headers->user_agent =~ /whatsapp/i;
say $self->req->headers->user_agent;
my $post = $posts_slug->{$slug}; my $post = $posts_slug->{$slug};
$self->render( $self->render(
format => 'png', format => 'png',
data => $post_model->PreviewOg($post) data => $post_model->PreviewOg($post, $is_whatsapp)
); );
} }
1; 1;

View File

@ -220,13 +220,15 @@ sub RetrieveDirectPostsForCategory ( $self, $category_name ) {
return $self->shufflePostsIfRequired( $category, [@$posts] ); return $self->shufflePostsIfRequired( $category, [@$posts] );
} }
sub PreviewOg { sub PreviewOg($self, $post, $isWhatsApp) {
my $self = shift;
my $post = shift;
my $title = $post->{title}; my $title = $post->{title};
my $content = $post->{content}; my $content = $post->{content};
my $image_file = $post->{image}; my $image_file = $post->{image};
my $image_bottom_preview = $post->{image_bottom_preview}; 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, return BurguillosInfo::Preview->Generate( $title, $content, $image_file,
$image_bottom_preview ); $image_bottom_preview );
} }

View File

@ -14,6 +14,7 @@ use Const::Fast;
use Capture::Tiny qw/capture/; use Capture::Tiny qw/capture/;
use MIME::Base64; use MIME::Base64;
use Digest::SHA qw/sha512_hex/; use Digest::SHA qw/sha512_hex/;
use Encode;
const my $CURRENT_FILE => __FILE__; const my $CURRENT_FILE => __FILE__;
const my $ROOT_PROJECT => path($CURRENT_FILE)->parent->parent->parent; const my $ROOT_PROJECT => path($CURRENT_FILE)->parent->parent->parent;
@ -30,7 +31,7 @@ sub Generate (
$image_bottom_preview = undef $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"); my $cached_image = path("public/img/preview.$sha512.generated.png");
if (!-f $cached_image) { if (!-f $cached_image) {
my $dom = Mojo::DOM->new($content); my $dom = Mojo::DOM->new($content);
@ -46,6 +47,18 @@ sub Generate (
return $cached_image->slurp_raw; 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 ) { sub _ToPng ( $self, $image ) {
if ( $image =~ /\.\w+$/ ) { if ( $image =~ /\.\w+$/ ) {
my $new_image = $image =~ s/\.\w+$/.generated.png/r; my $new_image = $image =~ s/\.\w+$/.generated.png/r;
@ -137,10 +150,9 @@ sub _GenerateSVGPreview ( $self, $title, $content, $image_file,
} }
sub _SVGToPNG ( $self, $svg ) { sub _SVGToPNG ( $self, $svg ) {
path('a.svg')->spew_utf8($svg);
my ( $stdout, $stderr ) = capture { my ( $stdout, $stderr ) = capture {
open my $fh, '|-', qw{convert /dev/stdin png:fd:1}; open my $fh, '|-', qw{convert /dev/stdin png:fd:1};
binmode $fh, 'utf8'; binmode $fh, ':utf8';
print $fh $svg; print $fh $svg;
close $fh; close $fh;
}; };