This commit is contained in:
Denis Fedoseev 2017-07-26 14:16:18 +09:00
parent 0030adabab
commit d7f8f7dcd5

View file

@ -2,7 +2,8 @@
use strict; use strict;
use warnings; use warnings;
use Mojolicious::Lite; # app, get, post is exported. use lib 'lib';
use Mojolicious::Lite; # app, get, post is exported.
use File::Basename 'basename'; use File::Basename 'basename';
use File::Path 'mkpath'; use File::Path 'mkpath';
@ -13,66 +14,67 @@ use Imager;
use DBI; use DBI;
use Digest::SHA; use Digest::SHA;
my $config = plugin 'Config'=> {file => 'application.conf'};; use FotoStore::DB;
my $predefined_user = 'alpha6'; use Data::Dumper;
my $predefined_password = $config->{'password'}; $Data::Dumper::Maxdepth = 3;
die 'No user password defined!' unless($predefined_password); my $config = plugin 'Config' => { file => 'application.conf' };
my $dbh = DBI->connect(sprintf('dbi:SQLite:dbname=%s', $config->{'db_file'}),"",""); my $db = FotoStore::DB->new( $config->{'db_file'} );
# Image base URL # Image base URL
my $IMAGE_BASE = 'images'; my $IMAGE_BASE = 'images';
my $ORIG_DIR = 'orig'; my $ORIG_DIR = 'orig';
my $thumbs_size = 200; my $thumbs_size = 200;
my @scale_width = ($thumbs_size, 640, 800, 1024); my @scale_width = ( $thumbs_size, 640, 800, 1024 );
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) # (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 );
# Create directory if not exists # Create directory if not exists
unless (-d $IMAGE_DIR) { unless ( -d $IMAGE_DIR ) {
mkpath $IMAGE_DIR or die "Cannot create directory: $IMAGE_DIR"; mkpath $IMAGE_DIR or die "Cannot create directory: $IMAGE_DIR";
} }
my $ORIG_PATH = File::Spec->catfile($IMAGE_DIR, $ORIG_DIR); my $ORIG_PATH = File::Spec->catfile( $IMAGE_DIR, $ORIG_DIR );
unless (-d $ORIG_PATH) { unless ( -d $ORIG_PATH ) {
mkpath $ORIG_PATH or die "Cannot create directory: $ORIG_PATH"; mkpath $ORIG_PATH or die "Cannot create directory: $ORIG_PATH";
} }
for my $dir (@scale_width) { for my $dir (@scale_width) {
my $scaled_dir_path = File::Spec->catfile($IMAGE_DIR, $dir); my $scaled_dir_path = File::Spec->catfile( $IMAGE_DIR, $dir );
unless (-d $scaled_dir_path) { unless ( -d $scaled_dir_path ) {
mkpath $scaled_dir_path or die "Cannot create directory: $scaled_dir_path"; mkpath $scaled_dir_path
} or die "Cannot create directory: $scaled_dir_path";
}
} }
plugin 'authentication', { plugin 'authentication', {
autoload_user => 1, autoload_user => 1,
load_user => sub { load_user => sub {
my $self = shift; my $self = shift;
my $uid = shift; my $uid = shift;
return { return $db->get_user($uid);
'username' => $predefined_user,
'password' => $predefined_password,
'name' => 'User Name'
} if ($uid eq 'userid' || $uid eq 'useridwithextradata');
return undef;
}, },
validate_user => sub { validate_user => sub {
my $self = shift; my $self = shift;
my $username = shift || ''; my $username = shift || '';
my $password = shift || ''; my $password = shift || '';
my $extradata = shift || {}; my $extradata = shift || {};
# return 'useridwithextradata' if($username eq 'alpha6' && $password eq 'qwerty' && ( $extradata->{'ohnoes'} || '' ) eq 'itsameme'); my $digest = $sha->add($password);
return 'userid' if($username eq $predefined_user && $password eq $predefined_password);
return undef; my $user_id = $db->check_user( $username, $digest->hexdigest() );
$self->app->log->debug("user id: [$user_id]");
return $user_id;
}, },
}; };
@ -80,56 +82,69 @@ post '/login' => sub {
my $self = shift; my $self = shift;
my $u = $self->req->param('username'); my $u = $self->req->param('username');
my $p = $self->req->param('password'); my $p = $self->req->param('password');
if ($self->authenticate($u, $p)) { if ( $self->authenticate( $u, $p ) ) {
$self->redirect_to('/'); $self->redirect_to('/');
} else {
$self->render(text => 'Login failed :(');
} }
else {
$self->render( text => 'Login failed :(' );
}
}; };
get '/logout' => sub { get '/logout' => sub {
my $self = shift; my $self = shift;
$self->logout(); $self->logout();
$self->render(text => 'bye'); $self->render( text => 'bye' );
}; };
# Display top page # Display top page
get '/' => sub { get '/' => sub {
my $self = shift; my $self = shift;
my $thumbs_dir = File::Spec->catfile($IMAGE_DIR, $thumbs_size); my $thumbs_dir = File::Spec->catfile( $IMAGE_DIR, $thumbs_size );
# Get file names(Only base name) # Get file names(Only base name)
my @images = map {basename($_)} glob("$thumbs_dir/*.jpg $thumbs_dir/*.gif $thumbs_dir/*.png"); my @images =
map { basename($_) }
glob("$thumbs_dir/*.jpg $thumbs_dir/*.gif $thumbs_dir/*.png");
# Sort by new order # Sort by new order
@images = sort {$b cmp $a} @images; @images = sort { $b cmp $a } @images;
# Render # Render
return $self->render(images => \@images, image_base => $IMAGE_BASE, orig => $ORIG_DIR, thumbs_size => $thumbs_size, scales => \@scale_width); return $self->render(
images => \@images,
image_base => $IMAGE_BASE,
orig => $ORIG_DIR,
thumbs_size => $thumbs_size,
scales => \@scale_width
);
} => 'index'; } => 'index';
# Upload image file # Upload image file
post '/upload' => (authenticated => 1)=> sub { post '/upload' => ( authenticated => 1 ) => sub {
my $self = shift; my $self = shift;
# 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();
$self->app->log->debug( "user:" . Dumper($user) );
# Not upload # Not upload
unless ($image) { unless ($image) {
return $self->render( return $self->render(
template => 'error', template => 'error',
message => "Upload fail. File is not specified." message => "Upload fail. File is not specified."
); );
} }
# Upload max size # Upload max size
#my $upload_max_size = 3 * 1024 * 1024; #my $upload_max_size = 3 * 1024 * 1024;
# Over max size # Over max size
#if ($image->size > $upload_max_size) { #if ($image->size > $upload_max_size) {
# return $self->render( # return $self->render(
@ -137,90 +152,86 @@ post '/upload' => (authenticated => 1)=> sub {
# message => "Upload fail. Image size is too large." # message => "Upload fail. Image size is too large."
# ); # );
#} #}
# Check file type # Check file type
my $image_type = $image->headers->content_type; my $image_type = $image->headers->content_type;
my %valid_types = map {$_ => 1} qw(image/gif image/jpeg image/png); my %valid_types = map { $_ => 1 } qw(image/gif image/jpeg image/png);
# Content type is wrong # Content type is wrong
unless ($valid_types{$image_type}) { unless ( $valid_types{$image_type} ) {
return $self->render( return $self->render(
template => 'error', template => 'error',
message => "Upload fail. Content type is wrong." message => "Upload fail. Content type is wrong."
); );
} }
# Extention # Extention
my $exts = {'image/gif' => 'gif', 'image/jpeg' => 'jpg', my $exts = {
'image/png' => 'png'}; 'image/gif' => 'gif',
'image/jpeg' => 'jpg',
'image/png' => 'png'
};
my $ext = $exts->{$image_type}; my $ext = $exts->{$image_type};
# Image file # Image file
my $filename = create_filename($ext); my $filename = sprintf( '%s.%s', create_hash( $image->slurp() ), $ext );
my $image_file = File::Spec->catfile($ORIG_PATH, $filename); my $image_file = File::Spec->catfile( $ORIG_PATH, $filename );
# If file is exists, Retry creating filename
while(-f $image_file){
$filename = create_filename();
$image_file = File::Spec->catfile($ORIG_PATH, $filename);
}
# Save to file # Save to file
$image->move_to($image_file); $image->move_to($image_file);
my $imager = Imager->new(); my $imager = Imager->new();
$imager->read(file => $image_file) or die $imager->errstr; $imager->read( file => $image_file ) or die $imager->errstr;
#http://sylvana.net/jpegcrop/exif_orientation.html #http://sylvana.net/jpegcrop/exif_orientation.html
#http://myjaphoo.de/docs/exifidentifiers.html #http://myjaphoo.de/docs/exifidentifiers.html
my $rotation_angle = $imager->tags( name => "exif_orientation") || 1; my $rotation_angle = $imager->tags( name => "exif_orientation" ) || 1;
$self->app->log->info("Rotation angle [".$rotation_angle."] [".$image->filename."]"); $self->app->log->info(
"Rotation angle [" . $rotation_angle . "] [" . $image->filename . "]" );
if ($rotation_angle == 3) { if ( $rotation_angle == 3 ) {
$imager = $imager->rotate(degrees=>180); $imager = $imager->rotate( degrees => 180 );
} }
elsif ($rotation_angle == 6) { elsif ( $rotation_angle == 6 ) {
$imager = $imager->rotate(degrees=>90); $imager = $imager->rotate( degrees => 90 );
} }
for my $scale (@scale_width) { for my $scale (@scale_width) {
my $scaled = $imager->scale(xpixels => $scale); my $scaled = $imager->scale( xpixels => $scale );
$scaled->write(file => File::Spec->catfile($IMAGE_DIR, $scale, $filename)) or die $scaled->errstr;
}
$self->render(json => {files => [ $scaled->write(
{ file => File::Spec->catfile( $IMAGE_DIR, $scale, $filename ) )
name => $image->filename, or die $scaled->errstr;
size => $image->size, }
url => sprintf('/images/orig/%s', $filename),
thumbnailUrl => sprintf('/images/200/%s', $filename), 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 ),
}
]
}
);
# Redirect to top page # Redirect to top page
# $self->redirect_to('index'); # $self->redirect_to('index');
} => 'upload'; } => 'upload';
sub create_filename { sub create_hash {
my $ext = shift || 'jpg'; my $data_to_hash = shift;
# Date and time
my ($sec, $min, $hour, $mday, $month, $year) = localtime;
$month = $month + 1;
$year = $year + 1900;
# Random number(0 ~ 999999)
my $rand_num = int(rand 1000000);
# Create file name form datatime and random number $sha->add($data_to_hash);
# (like image-20091014051023-78973) return $sha->hexdigest();
my $name = sprintf('image-%04s%02s%02s%02s%02s%02s-%06s.%s',
$year, $month, $mday, $hour, $min, $sec, $rand_num, $ext);
return $name;
} }
app->start; app->start;