Check scale exists #6

Merged
alpha6 merged 2 commits from check_scale_exists into master 2017-08-02 17:36:44 +03:00
2 changed files with 81 additions and 37 deletions

View file

@ -1,4 +1,11 @@
{ {
db_file => 'sql/fotostore.db', db_file => 'sql/fotostore.db',
invite_code => 'very_secure_invite_code', invite_code => 'very_secure_invite_code',
thumbnails_size => 200,
image_scales => {
640 => 1,
800 => 1,
1024 => 1,
2048 => 1,
},
} }

View file

@ -17,7 +17,7 @@ use Digest::SHA;
use FotoStore::DB; use FotoStore::DB;
use Data::Dumper; use Data::Dumper;
$Data::Dumper::Maxdepth = 3; $Data::Dumper::Maxdepth = 2;
my $config = plugin 'Config' => { file => 'application.conf' }; my $config = plugin 'Config' => { file => 'application.conf' };
@ -27,14 +27,20 @@ my $db = FotoStore::DB->new( $config->{'db_file'} );
my $IMAGE_BASE = 'images'; my $IMAGE_BASE = 'images';
my $ORIG_DIR = 'orig'; my $ORIG_DIR = 'orig';
my $thumbs_size = 200; # set allowed thumbnails scale and image scales
my $thumbs_size = $config->{'thumbnails_size'};
my @scale_width = ( $thumbs_size, 640, 800, 1024 ); my $scales_map = $config->{'image_scales'};
$scales_map->{$thumbs_size} = 1;
#Sort and filter values for array of available scales
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');
# Directory to save image files # Directory to save image files
# (app is Mojolicious object. static is MojoX::Dispatcher::Static object)
my $IMAGE_DIR = File::Spec->catfile( getcwd(), 'public', $IMAGE_BASE ); my $IMAGE_DIR = File::Spec->catfile( getcwd(), 'public', $IMAGE_BASE );
plugin 'authentication', { plugin 'authentication', {
@ -54,7 +60,6 @@ plugin 'authentication', {
my $digest = $sha->add($password); my $digest = $sha->add($password);
my $user_id = $db->check_user( $username, $digest->hexdigest() ); my $user_id = $db->check_user( $username, $digest->hexdigest() );
# $self->app->log->debug("user id: [$user_id]");
return $user_id; return $user_id;
}, },
@ -86,39 +91,44 @@ get '/register' => ( authenticated => 0 ) => sub {
}; };
post '/register' => ( authenticated => 0 ) => sub { post '/register' => ( authenticated => 0 ) => sub {
my $self = shift; my $self = shift;
my $username = $self->req->param('username'); my $username = $self->req->param('username');
my $password = $self->req->param('password'); my $password = $self->req->param('password');
my $fullname = $self->req->param('fullname'); my $fullname = $self->req->param('fullname');
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(
return 0; template => 'error',
message => 'Username already taken!'
);
return 0;
} }
if ($fullname eq '') { if ( $fullname eq '' ) {
$fullname = $username; $fullname = $username;
} }
my $digest = $sha->add($password); my $digest = $sha->add($password);
$db->add_user($username, $digest->hexdigest(), $fullname); $db->add_user( $username, $digest->hexdigest(), $fullname );
#Authenticate user after add #Authenticate user after add
if ( $self->authenticate( $username, $password ) ) { if ( $self->authenticate( $username, $password ) ) {
$self->redirect_to('/'); $self->redirect_to('/');
} }
else { else {
$self->render( text => 'Login failed :(' ); $self->render( template => 'error', message => 'Login failed :(' );
} }
} else {
$self->render(template => 'error', message => 'invalid invite code');
} }
}; else {
$self->render( template => 'error', message => 'invalid invite code' );
}
};
# Display top page # Display top page
get '/' => sub { get '/' => sub {
@ -132,32 +142,50 @@ get '/get_images' => ( authenticated => 1 ) => sub {
my $self = shift; my $self = shift;
my $current_user = $self->current_user; my $current_user = $self->current_user;
my $user_id = $current_user->{'user_id'};
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 $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 @images = map { $_->{'file_name'} } @$files_list; my @images = map { $_->{'file_name'} } @$files_list;
my $images = []; my $images = [];
for my $img_item (@$files_list) { for my $img_item (@$files_list) {
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) {
push(@scaled, {'size' => $scale, 'url' => File::Spec->catfile( '/', $IMAGE_BASE, $current_user->{'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
)
}
);
}
} }
$img_hash->{'scales'} = \@scaled; $img_hash->{'scales'} = \@scaled;
push(@$images, $img_hash); push( @$images, $img_hash );
} }
# Render # Render
return $self->render( json => $images ); return $self->render( json => $images );
@ -171,7 +199,7 @@ post '/upload' => ( authenticated => 1 ) => sub {
# Uploaded image(Mojo::Upload object) # Uploaded image(Mojo::Upload object)
my $image = $self->req->upload('image'); my $image = $self->req->upload('image');
my $user = $self->current_user(); my $user = $self->current_user();
my $user_id = $user->{'user_id'}; my $user_id = $user->{'user_id'};
$self->app->log->debug( "user:" . Dumper($user) ); $self->app->log->debug( "user:" . Dumper($user) );
@ -205,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);
@ -226,15 +255,23 @@ post '/upload' => ( authenticated => 1 ) => sub {
$imager = $imager->rotate( degrees => 90 ); $imager = $imager->rotate( degrees => 90 );
} }
my $original_width = $imager->getwidth();
for my $scale (@scale_width) { for my $scale (@scale_width) {
#Skip sizes which more than original image
if ( $scale >= $original_width ) {
next;
}
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;
} }
if ( !$db->add_file( $user->{'user_id'}, $filename, $image->filename) ) { if ( !$db->add_file( $user->{'user_id'}, $filename, $image->filename ) ) {
#TODO: Send error msg #TODO: Send error msg
} }
@ -265,9 +302,9 @@ sub create_hash {
} }
sub get_path { sub get_path {
my ($user_id, $size) = @_; my ( $user_id, $size ) = @_;
my $path = File::Spec->catfile( $IMAGE_DIR, $user_id, $size ); my $path = File::Spec->catfile( $IMAGE_DIR, $user_id, $size );
unless (-d $path) { unless ( -d $path ) {
mkpath $path or die "Cannot create directory: $path"; mkpath $path or die "Cannot create directory: $path";
} }
return $path; return $path;