diff options
author | Yorhel <git@yorhel.nl> | 2015-09-17 16:51:01 +0200 |
---|---|---|
committer | Yorhel <git@yorhel.nl> | 2015-09-17 16:53:43 +0200 |
commit | e603d905e48762014bbb8fa735dd918dd83e1942 (patch) | |
tree | 024870bbd691440cc625c590777dbe9491e5c396 /lib/VNDB/Handler | |
parent | a8ece1d324bbbbb675224af97cf5dcb15580d577 (diff) |
Handler::VNEdit: Use json_validate() for the VN staff/cast data
This (only very slightly) simplified further processing of the data. It
does add more validation than was present: Previously it was possible to
send invalid roles (would give a 500) or invalid numeric IDs (would
throw some perl warnings). These issues are now handled earlier on. This
change also puts a maximum length on the notes field, but nobody has
passed the 300 characters so far.
Diffstat (limited to 'lib/VNDB/Handler')
-rw-r--r-- | lib/VNDB/Handler/VNEdit.pm | 74 |
1 files changed, 38 insertions, 36 deletions
diff --git a/lib/VNDB/Handler/VNEdit.pm b/lib/VNDB/Handler/VNEdit.pm index 1a607e73..79f439ec 100644 --- a/lib/VNDB/Handler/VNEdit.pm +++ b/lib/VNDB/Handler/VNEdit.pm @@ -117,57 +117,59 @@ sub edit { { post => 'anime', required => 0, default => '' }, { post => 'image', required => 0, default => 0, template => 'int' }, { post => 'img_nsfw', required => 0, default => 0 }, - { post => 'credits', required => 0, default => '', maxlength => 5000 }, - { post => 'seiyuu', required => 0, default => '', maxlength => 5000 }, + { post => 'credits', required => 0, default => '[]', maxlength => 5000 }, + { post => 'seiyuu', required => 0, default => '[]', maxlength => 5000 }, { post => 'vnrelations', required => 0, default => '', maxlength => 5000 }, { post => 'screenshots', required => 0, default => '', maxlength => 1000 }, { post => 'editsum', required => 0, maxlength => 5000 }, { post => 'ihid', required => 0 }, { post => 'ilock', required => 0 }, ); - push @{$frm->{_err}}, 'badeditsum' if !$nosubmit && (!$frm->{editsum} || lc($frm->{editsum}) eq lc($frm->{desc})); + my $raw_c = !$frm->{_err} && json_validate($frm, 'credits', + { field => 'aid', required => 1, template => 'int' }, + { field => 'role', required => 1, enum => $self->{staff_roles} }, + { field => 'note', required => 0, maxlength => 300, default => '' }, + ); + my $raw_s = !$frm->{_err} && json_validate($frm, 'seiyuu', + { field => 'aid', required => 1, template => 'int' }, + { field => 'cid', required => 1, template => 'int' }, + { field => 'note', required => 0, maxlength => 300, default => '' }, + ); # handle image upload $frm->{image} = _uploadimage($self, $frm) if !$nosubmit; my (@credits, @seiyuu); if(!$nosubmit && !$frm->{_err}) { - eval { # catch json decoding errors - my $raw_c = $frm->{credits} ? json_decode $frm->{credits} : []; - my $raw_s = $frm->{seiyuu} ? json_decode $frm->{seiyuu} : []; - - # ensure submitted alias IDs exist within database - my @alist = map $_->{aid}, @$raw_c, @$raw_s; - my %staff = @alist ? map +($_->{aid} => 1), @{$self->dbStaffGet(aid => \@alist, results => 200)} : (); - return unless %staff; # exit from the eval block if staff list is empty - - # check for duplicate credits - my $last_c = { aid => 0, role => '' }; - for my $c (sort { $a->{aid} <=> $b->{aid} || $a->{role} cmp $b->{role} } @$raw_c) { - next unless exists $staff{$c->{aid}}; - # discard entries with identical name & role - next if $last_c->{aid} == $c->{aid} && $last_c->{role} eq $c->{role}; - $c->{aid} += 0; - push @credits, $c; - $last_c = $c; - } + # ensure submitted alias IDs exist within database + my @alist = map $_->{aid}, @$raw_c, @$raw_s; + my %staff = @alist ? map +($_->{aid} => 1), @{$self->dbStaffGet(aid => \@alist, results => 200)} : (); + + # check for duplicate credits + my $last_c = { aid => 0, role => '' }; + for my $c (sort { $a->{aid} <=> $b->{aid} || $a->{role} cmp $b->{role} } @$raw_c) { + next unless exists $staff{$c->{aid}}; + # discard entries with identical name & role + next if $last_c->{aid} == $c->{aid} && $last_c->{role} eq $c->{role}; + $c->{aid} += 0; + push @credits, $c; + $last_c = $c; + } - # if character list is empty, any seiyuu data will be discarded - if (@$chars && @$raw_s) { - my %vn_chars = map +($_->{id} => 1), @$chars; - my $last_s = { aid => 0, cid => 0 }; - for my $s (sort { $a->{aid} <=> $b->{aid} || $a->{cid} <=> $b->{cid} } @$raw_s) { - next unless $staff{$s->{aid}} && $vn_chars{$s->{cid}}; # weed out odd credits - next if $last_s->{aid} == $s->{aid} && $last_s->{cid} == $s->{cid}; - $s->{cid} += 0; # force numeric conversion - $s->{aid} += 0; - push @seiyuu, $s; - $last_s = $s; - } + # if character list is empty, any seiyuu data will be discarded + if (@$chars && @$raw_s) { + my %vn_chars = map +($_->{id} => 1), @$chars; + my $last_s = { aid => 0, cid => 0 }; + for my $s (sort { $a->{aid} <=> $b->{aid} || $a->{cid} <=> $b->{cid} } @$raw_s) { + next unless $staff{$s->{aid}} && $vn_chars{$s->{cid}}; # weed out odd credits + next if $last_s->{aid} == $s->{aid} && $last_s->{cid} == $s->{cid}; + $s->{cid} += 0; # force numeric conversion + $s->{aid} += 0; + push @seiyuu, $s; + $last_s = $s; } - }; - push @{$frm->{_err}}, [ 'credits', 'template', 'json' ] if $@; + } } if(!$nosubmit && !$frm->{_err}) { # parse and re-sort fields that have multiple representations of the same information |