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 $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 );
}

View File

@ -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,
);
}

View File

@ -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;

View File

@ -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 );
}

View File

@ -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;
};