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' }; -

Загрузить файл

-
- <%= form_for "/imgbin" => (method => 'POST', enctype => 'multipart/form-data') => begin %> -
- - <%= submit_button l('Upload') %> - - - <%= tag 'label' => (for => 'expire') => l('Keep for') %>: - <%= select_field 'expire' => \@times %> - -% if (config->{imgbin}->{show_latest}) { - <%= link_to l('Latest images') => "/imgbin/latest", class => 'zerobin latest' %> -% } -
-
-
- <%= file_field 'file', required => 1 %> -
- <% end %> -
-% my $size = sprintf "%.1f", config->{imgbin}->{maxsize} / 1024 ** 2; - - - <%= tag td => config->{imgbin}->{maxres} %> - <%= tag td => $size %> -
<%= l('Max') %> <%= l('resolution') %> :
<%= l('Max') %> <%= l('size (MB)') %> :
-
-
-% if (my $error = flash 'error') { -
-

<%= l(ucfirst($error)) %>

-
-% } diff --git a/templates/imgbin/latest.html.ep b/templates/imgbin/latest.html.ep deleted file mode 100644 index 74b5da9..0000000 --- a/templates/imgbin/latest.html.ep +++ /dev/null @@ -1,19 +0,0 @@ -% layout 'default'; -% title 'Imgbin -- Latest'; -

<%= l('Latest images') %>

-
-
- <%= link_to l('Upload') => "/imgbin", class => 'zerobin upload' %> -
-
-% my $images = stash('images') || []; -% foreach my $i (@{ $images }) { -% my $tooltip = sprintf "%s / %dx%d / %.1f kb", -% $i->{name}, $i->{width}, $i->{height}, $i->{size} / 1024; -
- <%= link_to $i->{url} => begin %> - <%= image $i->{path}, title => $tooltip %> - <% end %> -
-% } -
diff --git a/templates/imgbin/view.html.ep b/templates/imgbin/view.html.ep deleted file mode 100644 index a903759..0000000 --- a/templates/imgbin/view.html.ep +++ /dev/null @@ -1,26 +0,0 @@ -% layout 'default'; -% title 'Imgbin -- View'; -

<%= l('View image') %>

-
-
- <%= link_to l('Upload') => "/imgbin", class => 'zerobin upload' %>  -% if (config->{imgbin}->{show_latest}) { - <%= link_to l('Latest images') => "/imgbin/latest", class => 'zerobin latest' %>  -% } -
-
-
- % my $image = (stash 'image') || {}; - % my $style = sprintf "max-width: %dpx;", $config->{imgbin}->{css_maxwidth}; - <%= link_to $image->{path} => begin %> - <%= image $image->{path}, style => $style; %> - <% end %> -
-
- - - - - -
<%= l('Dimensions') %>:<%= "$image->{width}x$image->{height}" %>
<%= l('Size') %>:<%= sprintf("%.1f kb", $image->{size} / 1024) %>
<%= l('Name') %>:<%= $image->{name} // '-' %>
<%= l('Comment') %>:<%= $image->{comment} // '-' %>
-