From e985205d2c7b0347c44ad23f4635dae22687713d Mon Sep 17 00:00:00 2001 From: Mischa POSLAWSKY Date: Sun, 24 May 2020 03:54:24 +0200 Subject: [PATCH] word edit: download images from url input --- writer.plp | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/writer.plp b/writer.plp index b8e5f2d..59bacac 100644 --- a/writer.plp +++ b/writer.plp @@ -21,6 +21,10 @@ dl > dt, dl > dd { line-height: 4ex; vertical-align: text-top; } +dd > img { + max-width: 300px; + display: block; +} EOT }); @@ -54,18 +58,47 @@ if ($ENV{REQUEST_METHOD} eq 'POST') { $row = {%post{ pairkeys @wordcols }}; $_ = length ? $_ : undef for values %{$row}; eval { - my %res = (returning => $Request ? '*' : 'lang, cat'); + my %res = (returning => '*'); my $query = $find ? $db->update(word => $row, $find, \%res) : $db->insert(word => $row, \%res); $row = $query->hash; } or Alert("Entry could not be saved", $@); + + my $imgpath = "data/word/org/$row->{id}.jpg"; + if (my $download = $row->{source} and !-e $imgpath) { + require LWP::UserAgent; + my $ua = LWP::UserAgent->new; + $ua->agent('/'); + my $status = $ua->mirror($download, $imgpath); + $status->is_success or Alert([ + "Source image not found", + "Download from $download failed: ".$status->status_line, + ]); + } + + my $thumbpath = "data/word/eng/$row->{form}.jpg"; + if (-e $imgpath) { + my @cmds = @{ $row->{thumb} // [] }; + unshift @cmds, -gravity => @cmds ? 'northwest' : 'center'; + unshift @cmds, 'convert'; + push @cmds, -resize => '300x200^', -extent => '300x200'; + push @cmds, '-strip', -quality => '60%', -interlace => 'plane'; + push @cmds, $imgpath, $thumbpath; + my $status = system @cmds; + $status == 0 or Alert([ + "Thumbnail image not generated", + "Failed to convert source image, error code ".($status >> 8), + ], "@cmds"); + } + + $row = {map {$_ => $row->{$_}} qw(lang cat)} if not $Request; } elsif ($find) { $row = $db->select(word => '*', $find)->hash or Abort("Word not found", 404); } -my $title = $find ? "entry #$Request" : 'new entry'; +my $title = $row ? "entry #$row->{id}" : 'new entry'; :>

Words <:= $title :>

@@ -79,6 +112,10 @@ for my $col (pairs @wordcols) { printf '
' . '
', $col->key, $col->value, $val; + -e and printf ' %s', $_, $row->{form} for + $col->key eq 'source' ? "data/word/org/$row->{id}.jpg" : + $col->key eq 'thumb' ? "data/word/eng/$row->{form}.jpg" : + (); say '
'; } :> -- 2.30.0