Alex 'AdUser' Z
6 years ago
5 changed files with 0 additions and 277 deletions
@ -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; |
@ -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' }; |
||||
<h1>Загрузить файл</h1> |
||||
<div> |
||||
<%= form_for "/imgbin" => (method => 'POST', enctype => 'multipart/form-data') => begin %> |
||||
<div> |
||||
<span class="zerobin cblock"> |
||||
<%= submit_button l('Upload') %> |
||||
</span> |
||||
<span class="zerobin cblock"> |
||||
<%= tag 'label' => (for => 'expire') => l('Keep for') %>: |
||||
<%= select_field 'expire' => \@times %> |
||||
</span> |
||||
% if (config->{imgbin}->{show_latest}) { |
||||
<%= link_to l('Latest images') => "/imgbin/latest", class => 'zerobin latest' %> |
||||
% } |
||||
</div> |
||||
<hr/> |
||||
<div> |
||||
<%= file_field 'file', required => 1 %> |
||||
</div> |
||||
<% end %> |
||||
<br/> |
||||
% my $size = sprintf "%.1f", config->{imgbin}->{maxsize} / 1024 ** 2; |
||||
<span style="color: lightgray;"> |
||||
<table> |
||||
<tr><td><%= l('Max') %> <%= l('resolution') %></td><td> : </td><%= tag td => config->{imgbin}->{maxres} %></tr> |
||||
<tr><td><%= l('Max') %> <%= l('size (MB)') %></td><td> : </td><%= tag td => $size %></tr> |
||||
</table> |
||||
</span> |
||||
</div> |
||||
% if (my $error = flash 'error') { |
||||
<div> |
||||
<p style="color: red; font-size: 14pt;"><%= l(ucfirst($error)) %></p> |
||||
</div> |
||||
% } |
@ -1,19 +0,0 @@
|
||||
% layout 'default'; |
||||
% title 'Imgbin -- Latest'; |
||||
<h1><%= l('Latest images') %></h1> |
||||
<div> |
||||
<div> |
||||
<%= link_to l('Upload') => "/imgbin", class => 'zerobin upload' %> |
||||
</div> |
||||
<hr/> |
||||
% 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; |
||||
<div class="img-preview"> |
||||
<%= link_to $i->{url} => begin %> |
||||
<%= image $i->{path}, title => $tooltip %> |
||||
<% end %> |
||||
</div> |
||||
% } |
||||
</div> |
@ -1,26 +0,0 @@
|
||||
% layout 'default'; |
||||
% title 'Imgbin -- View'; |
||||
<h1><%= l('View image') %></h1> |
||||
<div> |
||||
<div> |
||||
<%= link_to l('Upload') => "/imgbin", class => 'zerobin upload' %> |
||||
% if (config->{imgbin}->{show_latest}) { |
||||
<%= link_to l('Latest images') => "/imgbin/latest", class => 'zerobin latest' %> |
||||
% } |
||||
</div> |
||||
<hr/> |
||||
<div> |
||||
% 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 %> |
||||
</div> |
||||
<hr/> |
||||
<table> |
||||
<tr><td><%= l('Dimensions') %></td><td>:</td><td><%= "$image->{width}x$image->{height}" %></td></tr> |
||||
<tr><td><%= l('Size') %></td><td>:</td><td><%= sprintf("%.1f kb", $image->{size} / 1024) %></td></tr> |
||||
<tr><td><%= l('Name') %></td><td>:</td><td><%= $image->{name} // '-' %></td></tr> |
||||
<tr><td><%= l('Comment') %></td><td>:</td><td><%= $image->{comment} // '-' %></td></tr> |
||||
</table> |
||||
</div> |
Loading…
Reference in new issue