2017-07-19 07:19:46 +03:00
|
|
|
#!/usr/bin/env perl
|
2011-06-27 02:25:09 +04:00
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
2017-07-26 08:16:18 +03:00
|
|
|
use lib 'lib';
|
|
|
|
use Mojolicious::Lite; # app, get, post is exported.
|
2011-06-27 02:25:09 +04:00
|
|
|
|
|
|
|
use File::Basename 'basename';
|
|
|
|
use File::Path 'mkpath';
|
2017-07-18 08:02:15 +03:00
|
|
|
use File::Spec 'catfile';
|
|
|
|
use Cwd;
|
|
|
|
|
|
|
|
use Imager;
|
2017-07-22 11:08:15 +03:00
|
|
|
use DBI;
|
|
|
|
use Digest::SHA;
|
2017-07-18 08:02:15 +03:00
|
|
|
|
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 = 3;
|
2017-07-18 08:02:15 +03:00
|
|
|
|
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'} );
|
2011-06-27 02:25:09 +04:00
|
|
|
|
|
|
|
# Image base URL
|
2017-07-18 08:02:15 +03:00
|
|
|
my $IMAGE_BASE = 'images';
|
2017-07-26 08:16:18 +03:00
|
|
|
my $ORIG_DIR = 'orig';
|
2017-07-18 08:02:15 +03:00
|
|
|
|
|
|
|
my $thumbs_size = 200;
|
|
|
|
|
2017-07-26 08:16:18 +03:00
|
|
|
my @scale_width = ( $thumbs_size, 640, 800, 1024 );
|
|
|
|
|
|
|
|
my $sha = Digest::SHA->new('sha256');
|
2011-06-27 02:25:09 +04:00
|
|
|
|
|
|
|
# Directory to save image files
|
|
|
|
# (app is Mojolicious object. static is MojoX::Dispatcher::Static object)
|
2017-07-26 08:16:18 +03:00
|
|
|
my $IMAGE_DIR = File::Spec->catfile( getcwd(), 'public', $IMAGE_BASE );
|
2011-06-27 02:25:09 +04:00
|
|
|
|
|
|
|
# Create directory if not exists
|
2017-07-26 08:16:18 +03:00
|
|
|
unless ( -d $IMAGE_DIR ) {
|
2011-06-27 02:25:09 +04:00
|
|
|
mkpath $IMAGE_DIR or die "Cannot create directory: $IMAGE_DIR";
|
2011-07-03 10:45:40 +04:00
|
|
|
}
|
2017-07-18 08:02:15 +03:00
|
|
|
|
2017-07-26 08:16:18 +03:00
|
|
|
my $ORIG_PATH = File::Spec->catfile( $IMAGE_DIR, $ORIG_DIR );
|
|
|
|
unless ( -d $ORIG_PATH ) {
|
2017-07-18 08:02:15 +03:00
|
|
|
mkpath $ORIG_PATH or die "Cannot create directory: $ORIG_PATH";
|
|
|
|
}
|
|
|
|
|
|
|
|
for my $dir (@scale_width) {
|
2017-07-26 08:16:18 +03:00
|
|
|
my $scaled_dir_path = File::Spec->catfile( $IMAGE_DIR, $dir );
|
|
|
|
unless ( -d $scaled_dir_path ) {
|
|
|
|
mkpath $scaled_dir_path
|
|
|
|
or die "Cannot create directory: $scaled_dir_path";
|
|
|
|
}
|
2011-06-27 02:25:09 +04:00
|
|
|
}
|
|
|
|
|
2017-07-18 08:02:15 +03:00
|
|
|
plugin 'authentication', {
|
|
|
|
autoload_user => 1,
|
2017-07-26 08:16:18 +03:00
|
|
|
load_user => sub {
|
2017-07-18 08:02:15 +03:00
|
|
|
my $self = shift;
|
|
|
|
my $uid = shift;
|
2017-07-26 08:16:18 +03:00
|
|
|
|
|
|
|
return $db->get_user($uid);
|
2017-07-18 08:02:15 +03:00
|
|
|
},
|
|
|
|
validate_user => sub {
|
2017-07-26 08:16:18 +03:00
|
|
|
my $self = shift;
|
|
|
|
my $username = shift || '';
|
|
|
|
my $password = shift || '';
|
2017-07-18 08:02:15 +03:00
|
|
|
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() );
|
|
|
|
$self->app->log->debug("user id: [$user_id]");
|
|
|
|
|
|
|
|
return $user_id;
|
2017-07-18 08:02:15 +03:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
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 ) ) {
|
2017-07-18 08:02:15 +03:00
|
|
|
$self->redirect_to('/');
|
|
|
|
}
|
2017-07-26 08:16:18 +03:00
|
|
|
else {
|
|
|
|
$self->render( text => 'Login failed :(' );
|
|
|
|
}
|
|
|
|
|
2017-07-18 08:02:15 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
get '/logout' => sub {
|
|
|
|
my $self = shift;
|
2017-07-26 08:16:18 +03:00
|
|
|
|
2017-07-18 08:02:15 +03:00
|
|
|
$self->logout();
|
2017-07-26 08:16:18 +03:00
|
|
|
$self->render( text => 'bye' );
|
2017-07-18 08:02:15 +03:00
|
|
|
};
|
|
|
|
|
2011-06-27 02:25:09 +04:00
|
|
|
# Display top page
|
|
|
|
get '/' => sub {
|
|
|
|
my $self = shift;
|
2017-07-26 08:16:18 +03:00
|
|
|
|
|
|
|
my $thumbs_dir = File::Spec->catfile( $IMAGE_DIR, $thumbs_size );
|
|
|
|
|
2011-06-27 02:25:09 +04:00
|
|
|
# Get file names(Only base name)
|
2017-07-26 08:16:18 +03:00
|
|
|
my @images =
|
|
|
|
map { basename($_) }
|
|
|
|
glob("$thumbs_dir/*.jpg $thumbs_dir/*.gif $thumbs_dir/*.png");
|
|
|
|
|
2011-06-27 02:25:09 +04:00
|
|
|
# Sort by new order
|
2017-07-26 08:16:18 +03:00
|
|
|
@images = sort { $b cmp $a } @images;
|
|
|
|
|
2011-06-27 02:25:09 +04:00
|
|
|
# Render
|
2017-07-26 08:16:18 +03:00
|
|
|
return $self->render(
|
|
|
|
images => \@images,
|
|
|
|
image_base => $IMAGE_BASE,
|
|
|
|
orig => $ORIG_DIR,
|
|
|
|
thumbs_size => $thumbs_size,
|
|
|
|
scales => \@scale_width
|
|
|
|
);
|
2011-06-27 02:25:09 +04:00
|
|
|
|
|
|
|
} => 'index';
|
|
|
|
|
|
|
|
# Upload image file
|
2017-07-26 08:16:18 +03:00
|
|
|
post '/upload' => ( authenticated => 1 ) => sub {
|
2011-06-27 02:25:09 +04:00
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Uploaded image(Mojo::Upload object)
|
|
|
|
my $image = $self->req->upload('image');
|
2017-07-26 08:16:18 +03:00
|
|
|
|
|
|
|
my $user = $self->current_user();
|
|
|
|
$self->app->log->debug( "user:" . Dumper($user) );
|
|
|
|
|
2011-06-27 02:25:09 +04:00
|
|
|
# Not upload
|
|
|
|
unless ($image) {
|
|
|
|
return $self->render(
|
2017-07-26 08:16:18 +03:00
|
|
|
template => 'error',
|
2011-06-27 02:25:09 +04:00
|
|
|
message => "Upload fail. File is not specified."
|
|
|
|
);
|
|
|
|
}
|
2017-07-26 08:16:18 +03:00
|
|
|
|
2011-06-27 02:25:09 +04:00
|
|
|
# Upload max size
|
|
|
|
#my $upload_max_size = 3 * 1024 * 1024;
|
2017-07-26 08:16:18 +03:00
|
|
|
|
2011-06-27 02:25:09 +04:00
|
|
|
# Over max size
|
|
|
|
#if ($image->size > $upload_max_size) {
|
|
|
|
# return $self->render(
|
|
|
|
# template => 'error',
|
|
|
|
# message => "Upload fail. Image size is too large."
|
|
|
|
# );
|
|
|
|
#}
|
2017-07-26 08:16:18 +03:00
|
|
|
|
2011-06-27 02:25:09 +04:00
|
|
|
# Check file type
|
|
|
|
my $image_type = $image->headers->content_type;
|
2017-07-26 08:16:18 +03:00
|
|
|
my %valid_types = map { $_ => 1 } qw(image/gif image/jpeg image/png);
|
|
|
|
|
2011-06-27 02:25:09 +04:00
|
|
|
# Content type is wrong
|
2017-07-26 08:16:18 +03:00
|
|
|
unless ( $valid_types{$image_type} ) {
|
2011-06-27 02:25:09 +04:00
|
|
|
return $self->render(
|
|
|
|
template => 'error',
|
|
|
|
message => "Upload fail. Content type is wrong."
|
|
|
|
);
|
|
|
|
}
|
2017-07-26 08:16:18 +03:00
|
|
|
|
2011-06-27 02:25:09 +04:00
|
|
|
# Extention
|
2017-07-26 08:16:18 +03:00
|
|
|
my $exts = {
|
|
|
|
'image/gif' => 'gif',
|
|
|
|
'image/jpeg' => 'jpg',
|
|
|
|
'image/png' => 'png'
|
|
|
|
};
|
2011-06-27 02:25:09 +04:00
|
|
|
my $ext = $exts->{$image_type};
|
2017-07-26 08:16:18 +03:00
|
|
|
|
2011-06-27 02:25:09 +04:00
|
|
|
# Image file
|
2017-07-26 08:16:18 +03:00
|
|
|
my $filename = sprintf( '%s.%s', create_hash( $image->slurp() ), $ext );
|
|
|
|
my $image_file = File::Spec->catfile( $ORIG_PATH, $filename );
|
|
|
|
|
2011-06-27 02:25:09 +04:00
|
|
|
# Save to file
|
|
|
|
$image->move_to($image_file);
|
2017-07-26 08:16:18 +03:00
|
|
|
|
2017-07-18 08:02:15 +03:00
|
|
|
my $imager = Imager->new();
|
2017-07-26 08:16:18 +03:00
|
|
|
$imager->read( file => $image_file ) or die $imager->errstr;
|
2017-07-19 10:01:12 +03:00
|
|
|
|
2017-07-19 09:43:05 +03:00
|
|
|
#http://sylvana.net/jpegcrop/exif_orientation.html
|
2017-07-19 10:01:12 +03:00
|
|
|
#http://myjaphoo.de/docs/exifidentifiers.html
|
2017-07-26 08:16:18 +03:00
|
|
|
my $rotation_angle = $imager->tags( name => "exif_orientation" ) || 1;
|
|
|
|
$self->app->log->info(
|
|
|
|
"Rotation angle [" . $rotation_angle . "] [" . $image->filename . "]" );
|
2011-06-27 02:25:09 +04:00
|
|
|
|
2017-07-26 08:16:18 +03:00
|
|
|
if ( $rotation_angle == 3 ) {
|
|
|
|
$imager = $imager->rotate( degrees => 180 );
|
|
|
|
}
|
|
|
|
elsif ( $rotation_angle == 6 ) {
|
|
|
|
$imager = $imager->rotate( degrees => 90 );
|
|
|
|
}
|
2017-07-19 09:43:05 +03:00
|
|
|
|
2017-07-19 10:21:20 +03:00
|
|
|
for my $scale (@scale_width) {
|
2017-07-26 08:16:18 +03:00
|
|
|
my $scaled = $imager->scale( xpixels => $scale );
|
|
|
|
|
|
|
|
$scaled->write(
|
|
|
|
file => File::Spec->catfile( $IMAGE_DIR, $scale, $filename ) )
|
|
|
|
or die $scaled->errstr;
|
2017-07-18 08:02:15 +03:00
|
|
|
}
|
2011-06-27 02:25:09 +04:00
|
|
|
|
2017-07-26 08:16:18 +03:00
|
|
|
if ( !$db->add_file( $user->{'user_id'}, $filename ) ) {
|
|
|
|
|
|
|
|
#TODO: Send error msg
|
|
|
|
}
|
|
|
|
|
|
|
|
$self->render(
|
|
|
|
json => {
|
|
|
|
files => [
|
|
|
|
{
|
|
|
|
name => $image->filename,
|
|
|
|
size => $image->size,
|
|
|
|
url => sprintf( '/images/orig/%s', $filename ),
|
|
|
|
thumbnailUrl => sprintf( '/images/200/%s', $filename ),
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
);
|
2017-07-19 09:43:05 +03:00
|
|
|
|
2011-06-27 02:25:09 +04:00
|
|
|
# Redirect to top page
|
2017-07-19 09:43:05 +03:00
|
|
|
# $self->redirect_to('index');
|
2017-07-26 08:16:18 +03:00
|
|
|
|
2011-06-27 02:25:09 +04:00
|
|
|
} => 'upload';
|
|
|
|
|
2017-07-26 08:16:18 +03:00
|
|
|
sub create_hash {
|
|
|
|
my $data_to_hash = shift;
|
|
|
|
|
|
|
|
$sha->add($data_to_hash);
|
|
|
|
return $sha->hexdigest();
|
2011-06-27 02:25:09 +04:00
|
|
|
}
|
|
|
|
|
2017-07-26 08:16:18 +03:00
|
|
|
app->start;
|