This commit is contained in:
Denis Fedoseev 2017-08-02 17:30:44 +03:00
parent bbe0ff81b0
commit bac83221aa

View file

@ -34,7 +34,9 @@ my $scales_map = $config->{'image_scales'};
$scales_map->{$thumbs_size} = 1; $scales_map->{$thumbs_size} = 1;
#Sort and filter values for array of available scales #Sort and filter values for array of available scales
my @scale_width = map { $scales_map->{$_} == 1 ? $_ : undef } sort {$a <=> $b} keys(%$scales_map); my @scale_width =
map { $scales_map->{$_} == 1 ? $_ : undef }
sort { $a <=> $b } keys(%$scales_map);
my $sha = Digest::SHA->new('sha256'); my $sha = Digest::SHA->new('sha256');
@ -96,10 +98,14 @@ post '/register' => ( authenticated => 0 ) => sub {
my $invite = $self->req->param('invite'); my $invite = $self->req->param('invite');
if ( $invite eq $config->{'invite_code'} ) { if ( $invite eq $config->{'invite_code'} ) {
#chek that username is not taken #chek that username is not taken
my $user = $db->get_user($username); my $user = $db->get_user($username);
if ( $user->{'user_id'} > 0 ) { if ( $user->{'user_id'} > 0 ) {
$self->render(template => 'error', message => 'Username already taken!'); $self->render(
template => 'error',
message => 'Username already taken!'
);
return 0; return 0;
} }
@ -118,7 +124,8 @@ post '/register' => ( authenticated => 0 ) => sub {
$self->render( template => 'error', message => 'Login failed :(' ); $self->render( template => 'error', message => 'Login failed :(' );
} }
} else { }
else {
$self->render( template => 'error', message => 'invalid invite code' ); $self->render( template => 'error', message => 'invalid invite code' );
} }
}; };
@ -139,7 +146,9 @@ get '/get_images' => ( authenticated => 1 ) => sub {
my $files_list = $db->get_files( $current_user->{'user_id'}, 20 ); my $files_list = $db->get_files( $current_user->{'user_id'}, 20 );
my $thumbs_dir = File::Spec->catfile( $IMAGE_DIR, $current_user->{'user_id'}, $thumbs_size ); my $thumbs_dir =
File::Spec->catfile( $IMAGE_DIR, $current_user->{'user_id'},
$thumbs_size );
my @images = map { $_->{'file_name'} } @$files_list; my @images = map { $_->{'file_name'} } @$files_list;
@ -149,13 +158,26 @@ get '/get_images' => ( authenticated => 1 ) => sub {
my $file = $img_item->{'file_name'}; my $file = $img_item->{'file_name'};
my $img_hash = {}; my $img_hash = {};
$img_hash->{'filename'} = $img_item->{'original_filename'}; $img_hash->{'filename'} = $img_item->{'original_filename'};
$img_hash->{'original_url'} = File::Spec->catfile( '/', $IMAGE_BASE, $current_user->{'user_id'}, $ORIG_DIR, $file ); $img_hash->{'original_url'} =
$img_hash->{'thumbnail_url'} = File::Spec->catfile( '/', $IMAGE_BASE, $current_user->{'user_id'}, $thumbs_size, $file ); File::Spec->catfile( '/', $IMAGE_BASE, $current_user->{'user_id'},
$ORIG_DIR, $file );
$img_hash->{'thumbnail_url'} =
File::Spec->catfile( '/', $IMAGE_BASE, $current_user->{'user_id'},
$thumbs_size, $file );
my @scaled = (); my @scaled = ();
for my $scale (@scale_width) { for my $scale (@scale_width) {
if (-r File::Spec->catfile( get_path($user_id, $scale), $file )) { if ( -r File::Spec->catfile( get_path( $user_id, $scale ), $file ) )
push(@scaled, {'size' => $scale, 'url' => File::Spec->catfile( '/', $IMAGE_BASE, $user_id, $scale, $file )}) ; {
push(
@scaled,
{
'size' => $scale,
'url' => File::Spec->catfile(
'/', $IMAGE_BASE, $user_id, $scale, $file
)
}
);
} }
} }
@ -211,7 +233,8 @@ post '/upload' => ( authenticated => 1 ) => sub {
# Image file # Image file
my $filename = sprintf( '%s.%s', create_hash( $image->slurp() ), $ext ); my $filename = sprintf( '%s.%s', create_hash( $image->slurp() ), $ext );
my $image_file = File::Spec->catfile( get_path($user_id, $ORIG_DIR), $filename ); my $image_file =
File::Spec->catfile( get_path( $user_id, $ORIG_DIR ), $filename );
# Save to file # Save to file
$image->move_to($image_file); $image->move_to($image_file);
@ -235,6 +258,7 @@ post '/upload' => ( authenticated => 1 ) => sub {
my $original_width = $imager->getwidth(); my $original_width = $imager->getwidth();
for my $scale (@scale_width) { for my $scale (@scale_width) {
#Skip sizes which more than original image #Skip sizes which more than original image
if ( $scale >= $original_width ) { if ( $scale >= $original_width ) {
next; next;
@ -242,8 +266,8 @@ post '/upload' => ( authenticated => 1 ) => sub {
my $scaled = $imager->scale( xpixels => $scale ); my $scaled = $imager->scale( xpixels => $scale );
$scaled->write( $scaled->write( file =>
file => File::Spec->catfile( get_path($user_id, $scale), $filename ) ) File::Spec->catfile( get_path( $user_id, $scale ), $filename ) )
or die $scaled->errstr; or die $scaled->errstr;
} }