diff --git a/conf/ldv.conf.sample b/conf/ldv.conf.sample index 5ea6884..62f4cf1 100644 --- a/conf/ldv.conf.sample +++ b/conf/ldv.conf.sample @@ -47,18 +47,6 @@ show_latest => 1, }, - imgbin => { - root => 'data/imgbin', - expire => 'year', - maxres => '8192x8192', - maxmem => 96 * 1024 * 1024, - maxsize => 32 * 1024 * 1024, - thumbs_ext => 'jpg', - thumbs_size => '250x250', - show_latest => 1, - css_maxwidth => 1024, - }, - zerobin => { access => 'all', syntax => ['bash', 'c', 'css', 'html', 'perl', 'php', 'python', 'other'], diff --git a/lib/LDV/Imgbin.pm b/lib/LDV/Imgbin.pm deleted file mode 100644 index abaa92d..0000000 --- a/lib/LDV/Imgbin.pm +++ /dev/null @@ -1,181 +0,0 @@ -package LDV::Imgbin; - -use strict; -use warnings; -use utf8; - -use Mojo::Base 'Mojolicious::Controller'; -use Mojo::Asset::File; -use Imager; - -sub _data_path { - my ($self, $time) = @_; - my $root = $self->app->config->{imgbin}->{root}; - - return $self->app->home->rel_file("$root/$time.json"); -} - -sub _image_path { - my ($self, $time, $ext, $full) = @_; - - return "/images/full/$time.$ext" unless $full; - return $self->app->home->rel_file("public/images/full/$time.$ext"); -} - -sub _thumb_path { - my ($self, $time, $full) = @_; - my $ext = $self->app->config->{imgbin}->{thumbs_ext}; - - return "/images/small/$time.$ext" unless $full; - return $self->app->home->rel_file("public/images/small/$time.$ext"); -} - -sub create { - my ($self) = @_; - - $self->stash(maxsize => $self->config->{imgbin}->{maxsize}); - $self->render(); -} - -sub view { - my ($self) = @_; - my $time = $self->stash('time') || 0; - - eval { - my $path = $self->_data_path($time); - my $data = Mojo::Asset::File->new(path => $path); - my $json = $self->app->json->decode($data->slurp); - die("image expired\n") if (time() > $json->{expire}); - $json->{path} = $self->_image_path($time, $json->{format}); - - $self->stash(image => $json); 1; - $self->render(); 1; - } or do { - $self->flash(error => $@); - $self->redirect_to('/imgbin'); - }; - - $self->rendered(); -} - -sub save { - my ($self) = @_; - - eval { - die ("request too large\n") if $self->req->is_limit_exceeded; - - my $time = time(); - my $conf = $self->app->config->{imgbin}; - my $upload = $self->req->upload('file'); - my $expire = $self->req->param('expire') || 7; # days - die("no file uploaded\n") unless ($upload and $upload->size > 0); - die("file too large\n") if ($upload->size > $conf->{maxsize}); - - # hack: don't use memory backend - { - my $path = POSIX::tmpnam; - $upload->move_to($path); - $upload->asset(Mojo::Asset::File->new(path => $path)); - $upload->asset->cleanup(1); # rearm self-destruction - } - - my ($im_w, $im_h) = ($conf->{maxres} =~ m/(\d+)x(\d+)/oi); - Imager->set_file_limits( - width => $im_w, height => $im_h, - bytes => $conf->{maxmem}, - ); - - my $image = Imager->new(file => $upload->asset->path) - or die(Imager->errstr() . "\n"); - - my $json = { - comment => $image->tags(name => 'i_comment') // '', - format => $image->tags(name => 'i_format'), - expire => $time + $expire * 86400, # expired *after* this time - width => $image->getwidth, - height => $image->getheight, - name => $upload->filename, - size => $upload->size, - }; - - my ($th_w, $th_h) = ($conf->{thumbs_size} =~ m/(\d+)x(\d+)/oi); - my $thumb = $image->scale(xpixels => $th_w, ypixels => $th_h, type => 'min'); - $thumb->write(file => $self->_thumb_path($time, 'fullpath')) - or die $thumb->errstr; - $upload->move_to($self->_image_path($time, $json->{format}, 'fullpath')); - - my $data = Mojo::Asset::File->new(path => $self->_data_path($time)); - $data->add_chunk($self->app->json->encode($json)); - $data->move_to($self->_data_path($time)); - - $self->redirect_to("/imgbin/$time"); - } or do { - $self->app->log->error($@); - $@ =~ s/\s+ at \s+ .+ \s+ line \s+ \d+//oxi; - $self->flash(error => $@); - $self->redirect_to('/imgbin'); - }; - - $self->rendered(); -} - -sub latest { - my ($self) = @_; - - eval { - my @images = (); - my $root = $self->app->config->{imgbin}->{root}; - opendir(my $DH, $root) - or die("opendir: $!\n"); - my @items = grep { m/\d+\.json/oi } readdir($DH); - closedir($DH); - @items = map { m/(\d+)\.json/oi } @items; - @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 = $self->app->json->decode($data->slurp); - $image->{path} = $self->_thumb_path($time); - $image->{url} = sprintf '/imgbin/%d', $time; - push @images, $image; - } - - $self->stash(images => \@images); - $self->render(); - } or do { - $self->flash(error => $@); - $self->redirect_to('/imgbin'); - }; -} - -sub prune { - my ($self) = @_; - - eval { - my $root = $self->app->config->{imgbin}->{root}; - opendir(my $DH, $root) - or die("opendir: $!\n"); - my @files = grep { m/\d+\.json/oi } readdir($DH); - my @times = map { s/\.json//oi; $_ } @files; - closedir($DH); - - my $now = time(); - foreach my $time (@times) { - my $path = $self->_data_path($time); - my $data = Mojo::Asset::File->new(path => $path); - my $image = $self->app->json->decode($data->slurp); - next if $image->{expire} > $now; - unlink $path; - unlink $self->_image_path($time, $image->{format}, 1); - unlink $self->_thumb_path($time); - } 1; - } or do { - chomp $@; - $self->app->log->error($@); - $@ =~ s/\s+ at \s+ .+ \s+ line \s+ \d+//oxi; - $self->flash(error => $@); - }; - $self->redirect_to('/imgbin'); - $self->rendered(); -} - -1; diff --git a/templates/imgbin/create.html.ep b/templates/imgbin/create.html.ep deleted file mode 100644 index 2d9aaa4..0000000 --- a/templates/imgbin/create.html.ep +++ /dev/null @@ -1,39 +0,0 @@ -% layout 'default'; -% title 'Imgbin -- Create'; -% my %times = (day => 1, week => 7, month => 30, quarter => 90, year => 365); -% my @times = map { [l($_) => $times{$_}] } sort { $times{$a} <=> $times{$b} } keys(%times); -% param expire => $times{ config->{imgbin}->{expire} || 'year' }; -
<%= l('Max') %> <%= l('resolution') %> | : | <%= tag td => config->{imgbin}->{maxres} %>
<%= l('Max') %> <%= l('size (MB)') %> | : | <%= tag td => $size %>
<%= l(ucfirst($error)) %>
-<%= l('Dimensions') %> | : | <%= "$image->{width}x$image->{height}" %> |
<%= l('Size') %> | : | <%= sprintf("%.1f kb", $image->{size} / 1024) %> |
<%= l('Name') %> | : | <%= $image->{name} // '-' %> |
<%= l('Comment') %> | : | <%= $image->{comment} // '-' %> |