Browse Source

* add app->json helper

master
Alex 'AdUser' Z 9 years ago
parent
commit
e1f1a520da
  1. 6
      lib/LDV.pm
  2. 9
      lib/LDV/Imgbin.pm
  3. 5
      lib/LDV/Zerobin.pm

6
lib/LDV.pm

@ -17,6 +17,12 @@ sub startup {
$self->app->mode('production'); $self->app->mode('production');
$self->app->secrets([ $self->app->config->{secret} ]); $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 { $self->app->attr(email => sub {
require LDV::Email; require LDV::Email;
my $email = LDV::Email->new($self->app->config->{email} // {}); my $email = LDV::Email->new($self->app->config->{email} // {});

9
lib/LDV/Imgbin.pm

@ -5,7 +5,6 @@ use warnings;
use utf8; use utf8;
use Mojo::Base 'Mojolicious::Controller'; use Mojo::Base 'Mojolicious::Controller';
use JSON qw(encode_json decode_json);
use Mojo::Asset::File; use Mojo::Asset::File;
use Imager; use Imager;
@ -45,7 +44,7 @@ sub view {
eval { eval {
my $path = $self->_data_path($time); my $path = $self->_data_path($time);
my $data = Mojo::Asset::File->new(path => $path); 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}); die("image expired\n") if (time() > $json->{expire});
$json->{path} = $self->_image_path($time, $json->{format}); $json->{path} = $self->_image_path($time, $json->{format});
@ -106,7 +105,7 @@ sub save {
$upload->move_to($self->_image_path($time, $json->{format}, 'fullpath')); $upload->move_to($self->_image_path($time, $json->{format}, 'fullpath'));
my $data = Mojo::Asset::File->new(path => $self->_data_path($time)); 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)); $data->move_to($self->_data_path($time));
$self->redirect_to("/imgbin/$time"); $self->redirect_to("/imgbin/$time");
@ -134,7 +133,7 @@ sub latest {
@items = sort { $b <=> $a } @items; @items = sort { $b <=> $a } @items;
foreach my $time (splice(@items, 0, 10)) { foreach my $time (splice(@items, 0, 10)) {
my $data = Mojo::Asset::File->new(path => $self->_data_path($time)); 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->{path} = $self->_thumb_path($time);
$image->{url} = sprintf '/imgbin/%d', $time; $image->{url} = sprintf '/imgbin/%d', $time;
push @images, $image; push @images, $image;
@ -162,7 +161,7 @@ sub prune {
my $now = time(); my $now = time();
foreach my $time (@times) { foreach my $time (@times) {
my $data = Mojo::Asset::File->new(path => $self->_data_path($time)); 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; next if $image->{expire} > $now;
unlink $self->_image_path($time, $image->{format}, 1); unlink $self->_image_path($time, $image->{format}, 1);
unlink $self->_thumb_path($time); unlink $self->_thumb_path($time);

5
lib/LDV/Zerobin.pm

@ -4,7 +4,6 @@ use strict;
use warnings; use warnings;
use utf8; use utf8;
use JSON qw(encode_json decode_json);
use POSIX qw(strftime tmpnam); use POSIX qw(strftime tmpnam);
use Mojo::Base 'Mojolicious::Controller'; use Mojo::Base 'Mojolicious::Controller';
use Mojo::Asset::File; use Mojo::Asset::File;
@ -21,7 +20,7 @@ sub _metadata_save {
my $path = $self->_metadata_path($time); my $path = $self->_metadata_path($time);
my $asset = Mojo::Asset::File->new; my $asset = Mojo::Asset::File->new;
$asset->add_chunk(encode_json($data)); $asset->add_chunk($self->app->json->encode($data));
$asset->move_to($path); $asset->move_to($path);
return 1; return 1;
@ -35,7 +34,7 @@ sub _metadata_load {
my $asset = Mojo::Asset::File->new(path => $path); my $asset = Mojo::Asset::File->new(path => $path);
$asset->cleanup(0); $asset->cleanup(0);
return decode_json($asset->slurp); return $self->app->json->decode($asset->slurp);
} }
sub _content_path { sub _content_path {

Loading…
Cancel
Save