word edit: download images from url input
authorMischa POSLAWSKY <perl@shiar.org>
Sun, 24 May 2020 01:54:24 +0000 (03:54 +0200)
committerMischa POSLAWSKY <perl@shiar.org>
Sat, 6 Jun 2020 01:49:20 +0000 (03:49 +0200)
writer.plp

index b8e5f2d6fa0e245ea47752f3e3c068260031a899..59bacacbd5eaf89f1b7586d51c70e73ec45928de 100644 (file)
@@ -21,6 +21,10 @@ dl > dt, dl > dd {
        line-height: 4ex;
        vertical-align: text-top;
 }
+dd > img {
+       max-width: 300px;
+       display: block;
+}
 </style>
 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 <q>$download</q> 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 <small>#$Request</small>" : 'new entry';
+my $title = $row ? "entry <small>#$row->{id}</small>" : 'new entry';
 :>
 <h1>Words <:= $title :></h1>
 
@@ -79,6 +112,10 @@ for my $col (pairs @wordcols) {
        printf '<dt><label for="%s">%s</label></dt>'
                . '<dd><input id="%1$s" name="%1$s" value="%s" />',
                $col->key, $col->value, $val;
+       -e and printf ' <img src="/%s" alt="%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 '</dd>';
 }
 :>