Database support #1
1 changed files with 119 additions and 108 deletions
107
fotostore.pl
107
fotostore.pl
|
@ -2,6 +2,7 @@
|
|||
use strict;
|
||||
use warnings;
|
||||
|
||||
use lib 'lib';
|
||||
use Mojolicious::Lite; # app, get, post is exported.
|
||||
|
||||
use File::Basename 'basename';
|
||||
|
@ -13,14 +14,14 @@ use Imager;
|
|||
use DBI;
|
||||
use Digest::SHA;
|
||||
|
||||
my $config = plugin 'Config'=> {file => 'application.conf'};;
|
||||
use FotoStore::DB;
|
||||
|
||||
my $predefined_user = 'alpha6';
|
||||
my $predefined_password = $config->{'password'};
|
||||
use Data::Dumper;
|
||||
$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
|
||||
my $IMAGE_BASE = 'images';
|
||||
|
@ -30,6 +31,8 @@ my $thumbs_size = 200;
|
|||
|
||||
my @scale_width = ( $thumbs_size, 640, 800, 1024 );
|
||||
|
||||
my $sha = Digest::SHA->new('sha256');
|
||||
|
||||
# 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 );
|
||||
|
@ -47,7 +50,8 @@ unless (-d $ORIG_PATH) {
|
|||
for my $dir (@scale_width) {
|
||||
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";
|
||||
mkpath $scaled_dir_path
|
||||
or die "Cannot create directory: $scaled_dir_path";
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -57,12 +61,7 @@ plugin 'authentication', {
|
|||
my $self = shift;
|
||||
my $uid = shift;
|
||||
|
||||
return {
|
||||
'username' => $predefined_user,
|
||||
'password' => $predefined_password,
|
||||
'name' => 'User Name'
|
||||
} if ($uid eq 'userid' || $uid eq 'useridwithextradata');
|
||||
return undef;
|
||||
return $db->get_user($uid);
|
||||
},
|
||||
validate_user => sub {
|
||||
my $self = shift;
|
||||
|
@ -70,9 +69,12 @@ plugin 'authentication', {
|
|||
my $password = shift || '';
|
||||
my $extradata = shift || {};
|
||||
|
||||
# return 'useridwithextradata' if($username eq 'alpha6' && $password eq 'qwerty' && ( $extradata->{'ohnoes'} || '' ) eq 'itsameme');
|
||||
return 'userid' if($username eq $predefined_user && $password eq $predefined_password);
|
||||
return undef;
|
||||
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;
|
||||
},
|
||||
};
|
||||
|
||||
|
@ -83,7 +85,8 @@ post '/login' => sub {
|
|||
|
||||
if ( $self->authenticate( $u, $p ) ) {
|
||||
$self->redirect_to('/');
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
$self->render( text => 'Login failed :(' );
|
||||
}
|
||||
|
||||
|
@ -101,14 +104,23 @@ get '/' => sub {
|
|||
my $self = shift;
|
||||
|
||||
my $thumbs_dir = File::Spec->catfile( $IMAGE_DIR, $thumbs_size );
|
||||
|
||||
# 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
|
||||
@images = sort { $b cmp $a } @images;
|
||||
|
||||
# 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';
|
||||
|
||||
|
@ -119,6 +131,9 @@ post '/upload' => (authenticated => 1)=> sub {
|
|||
# Uploaded image(Mojo::Upload object)
|
||||
my $image = $self->req->upload('image');
|
||||
|
||||
my $user = $self->current_user();
|
||||
$self->app->log->debug( "user:" . Dumper($user) );
|
||||
|
||||
# Not upload
|
||||
unless ($image) {
|
||||
return $self->render(
|
||||
|
@ -151,20 +166,17 @@ post '/upload' => (authenticated => 1)=> sub {
|
|||
}
|
||||
|
||||
# Extention
|
||||
my $exts = {'image/gif' => 'gif', 'image/jpeg' => 'jpg',
|
||||
'image/png' => 'png'};
|
||||
my $exts = {
|
||||
'image/gif' => 'gif',
|
||||
'image/jpeg' => 'jpg',
|
||||
'image/png' => 'png'
|
||||
};
|
||||
my $ext = $exts->{$image_type};
|
||||
|
||||
# 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 );
|
||||
|
||||
# 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
|
||||
$image->move_to($image_file);
|
||||
|
||||
|
@ -174,7 +186,8 @@ post '/upload' => (authenticated => 1)=> sub {
|
|||
#http://sylvana.net/jpegcrop/exif_orientation.html
|
||||
#http://myjaphoo.de/docs/exifidentifiers.html
|
||||
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 ) {
|
||||
$imager = $imager->rotate( degrees => 180 );
|
||||
|
@ -186,41 +199,39 @@ post '/upload' => (authenticated => 1)=> sub {
|
|||
for my $scale (@scale_width) {
|
||||
my $scaled = $imager->scale( xpixels => $scale );
|
||||
|
||||
$scaled->write(file => File::Spec->catfile($IMAGE_DIR, $scale, $filename)) or die $scaled->errstr;
|
||||
$scaled->write(
|
||||
file => File::Spec->catfile( $IMAGE_DIR, $scale, $filename ) )
|
||||
or die $scaled->errstr;
|
||||
}
|
||||
|
||||
if ( !$db->add_file( $user->{'user_id'}, $filename ) ) {
|
||||
|
||||
$self->render(json => {files => [
|
||||
#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
|
||||
# $self->redirect_to('index');
|
||||
|
||||
} => 'upload';
|
||||
|
||||
sub create_filename {
|
||||
my $ext = shift || 'jpg';
|
||||
sub create_hash {
|
||||
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
|
||||
# (like image-20091014051023-78973)
|
||||
my $name = sprintf('image-%04s%02s%02s%02s%02s%02s-%06s.%s',
|
||||
$year, $month, $mday, $hour, $min, $sec, $rand_num, $ext);
|
||||
|
||||
return $name;
|
||||
$sha->add($data_to_hash);
|
||||
return $sha->hexdigest();
|
||||
}
|
||||
|
||||
app->start;
|
Loading…
Reference in a new issue