fotostore/fotostore.pl

362 lines
9.6 KiB
Perl
Raw Normal View History

2017-07-19 07:19:46 +03:00
#!/usr/bin/env perl
use strict;
use warnings;
2017-07-26 08:16:18 +03:00
use lib 'lib';
use Mojolicious::Lite; # app, get, post is exported.
2018-08-01 10:11:43 +03:00
use Mojo::Promise;
use Mojo::IOLoop;
2018-08-01 09:14:41 +03:00
use File::Basename qw/basename fileparse/;
use File::Path 'mkpath';
use File::Spec 'catfile';
use Cwd;
2017-08-04 09:57:17 +03:00
use POSIX;
use Imager;
2017-07-22 11:08:15 +03:00
use DBI;
use Digest::SHA;
2017-07-26 08:16:18 +03:00
use FotoStore::DB;
2017-07-19 07:49:54 +03:00
2017-07-26 08:16:18 +03:00
use Data::Dumper;
$Data::Dumper::Maxdepth = 2;
2017-07-26 08:16:18 +03:00
my $config = plugin 'Config' => { file => 'application.conf' };
2017-07-22 11:08:15 +03:00
2017-07-26 08:16:18 +03:00
my $db = FotoStore::DB->new( $config->{'db_file'} );
# Image base URL
my $IMAGE_BASE = 'images';
2017-07-26 08:16:18 +03:00
my $ORIG_DIR = 'orig';
# set allowed thumbnails scale and image scales
my $thumbs_size = $config->{'thumbnails_size'};
my $scales_map = $config->{'image_scales'};
$scales_map->{$thumbs_size} = 1;
#Sort and filter values for array of available scales
2017-08-02 17:30:44 +03:00
my @scale_width =
map { $scales_map->{$_} == 1 ? $_ : undef }
sort { $a <=> $b } keys(%$scales_map);
2017-07-26 08:16:18 +03:00
my $sha = Digest::SHA->new('sha256');
# Directory to save image files
2017-07-26 08:16:18 +03:00
my $IMAGE_DIR = File::Spec->catfile( getcwd(), 'public', $IMAGE_BASE );
2018-07-31 09:17:32 +03:00
my $log = Mojo::Log->new();
plugin 'authentication', {
autoload_user => 1,
2017-07-26 08:16:18 +03:00
load_user => sub {
my $self = shift;
my $uid = shift;
2017-07-26 08:16:18 +03:00
return $db->get_user($uid);
},
validate_user => sub {
2017-07-26 08:16:18 +03:00
my $self = shift;
my $username = shift || '';
my $password = shift || '';
my $extradata = shift || {};
2017-07-26 08:16:18 +03:00
my $digest = $sha->add($password);
my $user_id = $db->check_user( $username, $digest->hexdigest() );
return $user_id;
},
};
post '/login' => sub {
my $self = shift;
my $u = $self->req->param('username');
my $p = $self->req->param('password');
2017-07-26 08:16:18 +03:00
if ( $self->authenticate( $u, $p ) ) {
$self->redirect_to('/');
}
2017-07-26 08:16:18 +03:00
else {
$self->render( text => 'Login failed :(' );
}
};
get '/logout' => sub {
my $self = shift;
2017-07-26 08:16:18 +03:00
$self->logout();
2017-08-01 15:55:21 +03:00
$self->render( message => 'bye' );
};
2017-07-31 10:14:59 +03:00
get '/register' => ( authenticated => 0 ) => sub {
};
post '/register' => ( authenticated => 0 ) => sub {
2017-08-02 17:30:44 +03:00
my $self = shift;
2018-07-31 09:17:32 +03:00
my $username = $self->req->param('username') || "";
my $password = $self->req->param('password') || "";
my $fullname = $self->req->param('fullname') || "";
my $invite = $self->req->param('invite') || "";
2017-08-02 17:30:44 +03:00
if ( $invite eq $config->{'invite_code'} ) {
2017-07-31 10:14:59 +03:00
#chek that username is not taken
my $user = $db->get_user($username);
2017-08-02 17:30:44 +03:00
if ( $user->{'user_id'} > 0 ) {
$self->render(
template => 'error',
message => 'Username already taken!'
);
return 0;
2017-07-31 10:14:59 +03:00
}
2017-08-02 17:30:44 +03:00
if ( $fullname eq '' ) {
2017-07-31 10:14:59 +03:00
$fullname = $username;
}
my $digest = $sha->add($password);
2017-08-02 17:30:44 +03:00
$db->add_user( $username, $digest->hexdigest(), $fullname );
2017-07-31 10:14:59 +03:00
#Authenticate user after add
if ( $self->authenticate( $username, $password ) ) {
$self->redirect_to('/');
}
else {
$self->render( template => 'error', message => 'Login failed :(' );
2017-07-31 10:14:59 +03:00
}
}
2017-08-02 17:30:44 +03:00
else {
$self->render( template => 'error', message => 'invalid invite code' );
}
};
2017-07-31 10:14:59 +03:00
# Display top page
get '/' => sub {
my $self = shift;
2017-07-26 08:16:18 +03:00
2017-07-26 09:50:45 +03:00
my $current_user = $self->current_user;
2017-07-26 08:16:18 +03:00
} => 'index';
2017-08-01 15:06:08 +03:00
get '/get_images' => ( authenticated => 1 ) => sub {
my $self = shift;
2017-08-04 09:57:17 +03:00
#Getting current user
2017-08-01 15:06:08 +03:00
my $current_user = $self->current_user;
2017-08-02 17:30:44 +03:00
my $user_id = $current_user->{'user_id'};
2017-08-04 09:57:17 +03:00
#Getting images list with paging
my $page = $self->param('page') || 1;
my $items = $self->param('per-page') || 20;
if (($page !~ /^\d+$/) || ($page <= 1)) { $page = 1}
if (($items !~ /^\d+$/) || ($items <= 0)) { $items = 20}
# process images list
my $req_result = $db->get_files( $current_user->{'user_id'}, $items , $page);
my $files_list = $req_result->{'images_list'};
my $pages_count = ceil($req_result->{'total_rows'}/$items);
2017-08-02 17:30:44 +03:00
my $thumbs_dir =
File::Spec->catfile( $IMAGE_DIR, $current_user->{'user_id'},
$thumbs_size );
2017-08-01 15:06:08 +03:00
my @images = map { $_->{'file_name'} } @$files_list;
my $images = [];
for my $img_item (@$files_list) {
2017-08-02 17:30:44 +03:00
my $file = $img_item->{'file_name'};
2017-08-01 15:06:08 +03:00
my $img_hash = {};
2017-08-04 09:57:17 +03:00
$img_hash->{'id'} = $img_item->{'file_id'};
2017-08-02 07:55:54 +03:00
$img_hash->{'filename'} = $img_item->{'original_filename'};
2017-08-02 17:30:44 +03:00
$img_hash->{'original_url'} =
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 );
2017-08-01 15:06:08 +03:00
2017-08-01 15:33:29 +03:00
my @scaled = ();
2017-08-01 15:06:08 +03:00
for my $scale (@scale_width) {
2017-08-02 17:30:44 +03:00
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
)
}
);
}
2017-08-02 17:30:44 +03:00
2017-08-01 15:06:08 +03:00
}
2017-08-01 15:33:29 +03:00
$img_hash->{'scales'} = \@scaled;
2017-08-02 17:30:44 +03:00
push( @$images, $img_hash );
2017-08-04 09:57:17 +03:00
2017-08-02 17:30:44 +03:00
}
2017-08-01 15:06:08 +03:00
2017-08-04 09:57:17 +03:00
my $reply_data = { current_page => $page, items_per_page => $items, pages_count => $pages_count, images_list => $images };
2017-08-01 15:06:08 +03:00
# Render
2017-08-04 09:57:17 +03:00
return $self->render( json => $reply_data );
2017-08-01 15:06:08 +03:00
};
# Upload image file
2017-07-31 20:21:03 +03:00
# There is no restriction for file size in app because restriction is present in nginx configuration
2017-07-26 08:16:18 +03:00
post '/upload' => ( authenticated => 1 ) => sub {
my $self = shift;
# Uploaded image(Mojo::Upload object)
my $image = $self->req->upload('image');
2017-07-26 08:16:18 +03:00
2017-08-02 17:30:44 +03:00
my $user = $self->current_user();
2017-07-26 09:50:45 +03:00
my $user_id = $user->{'user_id'};
2017-07-26 08:16:18 +03:00
# Not upload
unless ($image) {
return $self->render(
2017-07-26 08:16:18 +03:00
template => 'error',
message => "Upload fail. File is not specified."
);
}
2017-07-26 08:16:18 +03:00
# Check file type
my $image_type = $image->headers->content_type;
2018-08-01 09:14:41 +03:00
my %valid_types = (
'image/gif' => 'gif',
'image/jpeg' => 'jpg',
'image/png' => 'png'
);
2017-07-26 08:16:18 +03:00
# Content type is wrong
2017-07-26 08:16:18 +03:00
unless ( $valid_types{$image_type} ) {
return $self->render(
template => 'error',
message => "Upload fail. Content type is wrong."
);
}
2017-07-26 08:16:18 +03:00
2018-08-01 09:14:41 +03:00
my $ext = $valid_types{$image_type};
# Image file
my $filename = sprintf( '%s.%s', create_hash( $image->slurp() ), $ext );
my $image_file =
File::Spec->catfile( get_path( $user_id, $ORIG_DIR ), $filename );
# Save to file
$image->move_to($image_file);
2018-08-01 10:11:43 +03:00
my $promise = store_image($image_file, $image->filename, $user_id);
2018-08-01 10:18:24 +03:00
#TODO: add errors handling
2018-08-01 10:11:43 +03:00
Mojo::Promise->all($promise)->then(sub {
$self->render(
json => {
files => [
{
name => $image->filename,
size => $image->size,
url => sprintf( '/images/orig/%s', $filename ),
thumbnailUrl => sprintf( '/images/200/%s', $filename ),
}
]
2018-07-31 09:17:32 +03:00
}
2018-08-01 10:11:43 +03:00
);
})->wait;
2018-07-31 09:17:32 +03:00
} => 'upload';
sub create_hash {
my $data_to_hash = shift;
$sha->add($data_to_hash);
return $sha->hexdigest();
}
sub get_path {
my ( $user_id, $size ) = @_;
my $path = File::Spec->catfile( $IMAGE_DIR, $user_id, $size );
unless ( -d $path ) {
mkpath $path or die "Cannot create directory: $path";
}
return $path;
}
sub store_image {
2018-08-01 09:14:41 +03:00
my $image_file = shift;
my $original_filename = shift;
2018-07-31 09:17:32 +03:00
my $user_id = shift;
2018-08-01 10:11:43 +03:00
my $promise = Mojo::Promise->new;
2018-08-01 10:18:24 +03:00
# Process and store uploaded file in a separate process
2018-08-01 10:11:43 +03:00
Mojo::IOLoop->subprocess(
sub {
my $subprocess = shift;
my $filename = fileparse($image_file);
my $imager = Imager->new();
$imager->read( file => $image_file ) or die $imager->errstr;
2017-07-26 08:16:18 +03:00
2018-08-01 10:11:43 +03:00
#http://sylvana.net/jpegcrop/exif_orientation.html
#http://myjaphoo.de/docs/exifidentifiers.html
my $rotation_angle = $imager->tags( name => "exif_orientation" ) || 1;
$log->debug(
"Rotation angle [" . $rotation_angle . "]" );
2017-07-19 10:01:12 +03:00
2018-08-01 10:11:43 +03:00
if ( $rotation_angle == 3 ) {
$imager = $imager->rotate( degrees => 180 );
}
elsif ( $rotation_angle == 6 ) {
$imager = $imager->rotate( degrees => 90 );
}
2018-08-01 10:11:43 +03:00
my $original_width = $imager->getwidth();
2018-08-01 10:11:43 +03:00
for my $scale (@scale_width) {
2018-08-01 10:11:43 +03:00
#Skip sizes which more than original image
if ( $scale >= $original_width ) {
next;
}
2017-08-02 17:30:44 +03:00
2018-08-01 10:11:43 +03:00
my $scaled = $imager->scale( xpixels => $scale );
2018-08-01 10:11:43 +03:00
$scaled->write( file =>
File::Spec->catfile( get_path( $user_id, $scale ), $filename ) )
or die $scaled->errstr;
}
2017-07-26 08:16:18 +03:00
2018-08-01 10:11:43 +03:00
if ( !$db->add_file( $user_id, $filename, $original_filename ) ) {
2018-08-01 10:11:43 +03:00
$log->error(sprintf('Can\'t save file %s', $filename));
die sprintf('Can\'t save file %s', $filename);
}
2017-07-26 08:16:18 +03:00
2018-08-01 10:11:43 +03:00
return $filename;
},
sub {
my ($subprocess, $err, @results) = @_;
2018-08-01 10:18:24 +03:00
$log->error("Subprocess error: $err") and return if $err;
$promise->reject("Subprocess error: $err @results") if $err;
$promise->resolve(1, @results);
2018-08-01 10:11:43 +03:00
}
);
2017-07-26 08:16:18 +03:00
2018-08-01 10:11:43 +03:00
return $promise;
2017-07-26 09:50:45 +03:00
}
2018-08-01 10:11:43 +03:00
Mojo::IOLoop->start;
2017-07-26 08:16:18 +03:00
app->start;