Skip to content

Commit

Permalink
Thumbnail errors: do not log it as error but store it
Browse files Browse the repository at this point in the history
The global amw administrator can't do much about them, while the
site admin can see the error now in the attachment listing.
  • Loading branch information
melmothx committed Sep 26, 2023
1 parent 928a6ab commit ab7291c
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 17 deletions.
2 changes: 2 additions & 0 deletions lib/AmuseWikiFarm/Controller/Attachments.pm
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ sub orphans :Chained('root') :Args(0) {
name => $att->uri,
thumb => $c->uri_for($att->small_uri),
has_thumbnails => $att->has_thumbnails,
errors => $att->errors,
};
if ($ainfo->{has_thumbnails}) {
$ainfo->{has_thumbnails} = 0 unless $att->has_thumbnail_file('small');
Expand Down Expand Up @@ -98,6 +99,7 @@ sub list :Chained('root') :Args {
desc => $att->comment_html,
has_thumbnails => $att->has_thumbnails,
alt_text => $att->alt_text,
errors => $att->errors,
};
if ($ainfo->{has_thumbnails}) {
$ainfo->{has_thumbnails} = 0 unless $att->has_thumbnail_file('small');
Expand Down
15 changes: 14 additions & 1 deletion lib/AmuseWikiFarm/Schema/Result/Attachment.pm
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ use AmuseWikiFarm::Log::Contextual;
use Path::Tiny;
use AmuseWikiFarm::Utils::Amuse qw/build_full_uri/;
use HTML::Entities qw/encode_entities/;
use Try::Tiny;

sub can_be_inlined {
my $self = shift;
Expand Down Expand Up @@ -420,6 +421,18 @@ sub path_object {
}

sub generate_thumbnails {
my $self = shift;
my $errors;
try {
$self->_do_generate_thumbnails;
}
catch {
$errors = $_;
};
$self->update({ errors => $errors });
}

sub _do_generate_thumbnails {
my $self = shift;
my $srcfile = $self->path_object;
my $basename = $srcfile->basename;
Expand Down Expand Up @@ -456,7 +469,7 @@ sub generate_thumbnails {
});
}
else {
log_error { "Cannot extract thumbnail from $src into $out with $dimensions{$ext}"};
die "Cannot extract thumbnail from $src into $out with $dimensions{$ext}";
}
}
}
Expand Down
15 changes: 1 addition & 14 deletions lib/AmuseWikiFarm/Utils/Amuse.pm
Original file line number Diff line number Diff line change
Expand Up @@ -977,24 +977,11 @@ sub convert_pdf_to_png {
return $output;
}
else {
Dlog_error { "Execution of $_ failed" } \@exec;
die "Execution of " . join(" ", @exec) . " failed";
}
}

sub create_thumbnail {
my ($input, $output, $width) = @_;
return unless $input && $output && $width;
my ($w, $h);
try {
($w, $h) = _generate_thumbnail($input, $output, $width);
} catch {
my $err = $_;
log_error { "Failure to create thumbnail $input => $output => $width with error $err" };
};
return ($w, $h);
}

sub _generate_thumbnail {
my ($input, $output, $width) = @_;
die unless $input && $output && $width;
my $wd = Path::Tiny->tempdir;
Expand Down
4 changes: 4 additions & 0 deletions root/src/attachments/list.tt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@
<i title="[% att.name | html %]" class="fa fa-file-o fa-4x fa-border attachment-without-thumb"></i>
[% END %]
</a>

[% IF att.errors %]
<div class="attachment-error text-danger">[% att.errors | html %]</div>
[% END %]
</td>
</tr>
[% END %]
Expand Down
3 changes: 3 additions & 0 deletions root/src/attachments/orphans.tt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
<i title="[% att.name | html %]" class="fa fa-file-o fa-4x fa-border attachment-without-thumb"></i>
[% END %]
</a>
[% IF att.errors %]
<div class="attachment-error text-danger">[% att.errors | html %]</div>
[% END %]
</td>
<td>
<label>
Expand Down
5 changes: 4 additions & 1 deletion script/amusewiki-jobber
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ try {

# ignore the attachments in the staging area instead of dying
# because out of the root
my $attachments = $site->attachments->search({ f_archive_rel_path => { '!=' => '' } });
my $attachments = $site->attachments->search({
f_archive_rel_path => { '!=' => '' },
errors => undef,
});
while (my $att = $attachments->next) {
if ($att->has_thumbnails and !$att->thumbnails->count) {
log_info { "Generating thumbnails for " . $att->uri };
Expand Down
32 changes: 31 additions & 1 deletion t/controller_Uploads.t
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use strict;
use warnings;
use utf8;
use Test::More tests => 56;
use Test::More tests => 63;
BEGIN { $ENV{DBIX_CONFIG_DIR} = "t" };

use File::Spec::Functions qw/catfile catdir/;
use lib catdir(qw/t lib/);
use AmuseWiki::Tests qw/create_site/;
use AmuseWikiFarm::Schema;
use Test::WWW::Mechanize::Catalyst;
use Path::Tiny;
use Data::Dumper::Concise;

my $pdf = catfile(qw/t files shot.pdf/);
my $png = catfile(qw/t files shot.png/);
Expand Down Expand Up @@ -86,3 +88,31 @@ foreach my $attachment ($site->attachments) {
ok $site->thumbnails->min_dimensions(200, 200)->count, "Found thumbs with dimensions > 200";
$mech->get_ok('/latest');
$mech->content_contains("/uploads/$site_id/thumbnails/$png_att.small.png");

{
my $bad = Path::Tiny->tempfile;
$bad->spew_raw("%PDF-1.3\nalsdfjlaksdjflkasdflkjasdf\n");
my ($rev) = $site->create_new_text({ title => 'In error',
lang => 'hr',
textbody => '<p>ciao</p>',
}, 'text');
my $add = $rev->add_attachment("$bad");
diag Dumper($add);
my $first = $add->{attachment};
diag "Added $first";
$rev->edit("#ATTACH $first\n" . $rev->muse_body);
my $second = $rev->add_attachment("$bad")->{attachment};
diag "Added $second";
$rev->commit_version;
$rev->publish_text;
foreach my $att ($site->attachments) {
$att->generate_thumbnails;
}
is $site->attachments->search({ errors => { '<>' => '' } })->count, 2;
$mech->get_ok('/login');
ok $mech->submit_form(with_fields => {__auth_user => 'root', __auth_pass => 'root' });
$mech->get_ok('/attachments/orphans');
$mech->content_contains('attachment-error');
$mech->get_ok('/attachments/list');
$mech->content_contains('attachment-error');
}

0 comments on commit ab7291c

Please sign in to comment.