diff --git a/lib/LDV.pm b/lib/LDV.pm index 6fb5df8..48bc392 100644 --- a/lib/LDV.pm +++ b/lib/LDV.pm @@ -17,6 +17,12 @@ sub startup { $self->app->mode('production'); $self->app->secrets([ $self->app->config->{secret} ]); + $self->app->attr(json => sub { + require JSON; + my $json = JSON->new->utf8; + return $json; + }); + $self->app->attr(email => sub { require LDV::Email; my $email = LDV::Email->new($self->app->config->{email} // {}); diff --git a/lib/LDV/Imgbin.pm b/lib/LDV/Imgbin.pm index 7bccafe..a87009c 100644 --- a/lib/LDV/Imgbin.pm +++ b/lib/LDV/Imgbin.pm @@ -5,7 +5,6 @@ use warnings; use utf8; use Mojo::Base 'Mojolicious::Controller'; -use JSON qw(encode_json decode_json); use Mojo::Asset::File; use Imager; @@ -45,7 +44,7 @@ sub view { eval { my $path = $self->_data_path($time); my $data = Mojo::Asset::File->new(path => $path); - my $json = decode_json($data->slurp); + my $json = $self->app->json->decode($data->slurp); die("image expired\n") if (time() > $json->{expire}); $json->{path} = $self->_image_path($time, $json->{format}); @@ -106,7 +105,7 @@ sub save { $upload->move_to($self->_image_path($time, $json->{format}, 'fullpath')); my $data = Mojo::Asset::File->new(path => $self->_data_path($time)); - $data->add_chunk(encode_json($json)); + $data->add_chunk($self->app->json->encode($json)); $data->move_to($self->_data_path($time)); $self->redirect_to("/imgbin/$time"); @@ -134,7 +133,7 @@ sub latest { @items = sort { $b <=> $a } @items; foreach my $time (splice(@items, 0, 10)) { my $data = Mojo::Asset::File->new(path => $self->_data_path($time)); - my $image = decode_json($data->slurp); + my $image = $self->app->json->decode($data->slurp); $image->{path} = $self->_thumb_path($time); $image->{url} = sprintf '/imgbin/%d', $time; push @images, $image; @@ -162,7 +161,7 @@ sub prune { my $now = time(); foreach my $time (@times) { my $data = Mojo::Asset::File->new(path => $self->_data_path($time)); - my $image = decode_json($data->slurp); + my $image = $self->app->json->decode($data->slurp); next if $image->{expire} > $now; unlink $self->_image_path($time, $image->{format}, 1); unlink $self->_thumb_path($time); diff --git a/lib/LDV/Zerobin.pm b/lib/LDV/Zerobin.pm index f2e02c9..39a4fc8 100644 --- a/lib/LDV/Zerobin.pm +++ b/lib/LDV/Zerobin.pm @@ -4,7 +4,6 @@ use strict; use warnings; use utf8; -use JSON qw(encode_json decode_json); use POSIX qw(strftime tmpnam); use Mojo::Base 'Mojolicious::Controller'; use Mojo::Asset::File; @@ -21,7 +20,7 @@ sub _metadata_save { my $path = $self->_metadata_path($time); my $asset = Mojo::Asset::File->new; - $asset->add_chunk(encode_json($data)); + $asset->add_chunk($self->app->json->encode($data)); $asset->move_to($path); return 1; @@ -35,7 +34,7 @@ sub _metadata_load { my $asset = Mojo::Asset::File->new(path => $path); $asset->cleanup(0); - return decode_json($asset->slurp); + return $self->app->json->decode($asset->slurp); } sub _content_path {