From 072fbe8bf8a9769b0a728632f53e28a6ae18d60e Mon Sep 17 00:00:00 2001 From: Denis Fedoseev Date: Wed, 26 Jul 2017 15:50:45 +0900 Subject: [PATCH] Storing list of fotos in database --- README | 2 ++ application.conf.example | 1 + cpanfile | 5 +++- fotostore.pl | 49 ++++++++++++++++------------------------ sql/sqitch.plan | 1 + 5 files changed, 28 insertions(+), 30 deletions(-) diff --git a/README b/README index b8221dd..c5eea80 100644 --- a/README +++ b/README @@ -1,2 +1,4 @@ Small script for store images. Based on http://d.hatena.ne.jp/yukikimoto/20100212/1265989676 with small modifications. + +URL format: /images/// diff --git a/application.conf.example b/application.conf.example index 641690a..490b847 100644 --- a/application.conf.example +++ b/application.conf.example @@ -1,3 +1,4 @@ { password => '', + db_file => 'sql/fotostore.db', } \ No newline at end of file diff --git a/cpanfile b/cpanfile index c680ea1..265bb40 100644 --- a/cpanfile +++ b/cpanfile @@ -5,4 +5,7 @@ requires 'File::Basename'; requires 'File::Path'; requires 'File::Spec'; requires 'Cwd'; -requires 'Getopt::Long'; \ No newline at end of file +requires 'Getopt::Long'; +requires 'DBI'; +requires 'DBD::SQLite'; +requires 'Digest::SHA'; \ No newline at end of file diff --git a/fotostore.pl b/fotostore.pl index cfd4d64..543edb5 100644 --- a/fotostore.pl +++ b/fotostore.pl @@ -37,24 +37,6 @@ my $sha = Digest::SHA->new('sha256'); # (app is Mojolicious object. static is MojoX::Dispatcher::Static object) my $IMAGE_DIR = File::Spec->catfile( getcwd(), 'public', $IMAGE_BASE ); -# Create directory if not exists -unless ( -d $IMAGE_DIR ) { - mkpath $IMAGE_DIR or die "Cannot create directory: $IMAGE_DIR"; -} - -my $ORIG_PATH = File::Spec->catfile( $IMAGE_DIR, $ORIG_DIR ); -unless ( -d $ORIG_PATH ) { - mkpath $ORIG_PATH or die "Cannot create directory: $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"; - } -} - plugin 'authentication', { autoload_user => 1, load_user => sub { @@ -103,15 +85,13 @@ get '/logout' => sub { get '/' => sub { my $self = shift; - my $thumbs_dir = File::Spec->catfile( $IMAGE_DIR, $thumbs_size ); + my $current_user = $self->current_user; - # Get file names(Only base name) - my @images = - map { basename($_) } - glob("$thumbs_dir/*.jpg $thumbs_dir/*.gif $thumbs_dir/*.png"); - - # Sort by new order - @images = sort { $b cmp $a } @images; + 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; # Render return $self->render( @@ -119,7 +99,8 @@ get '/' => sub { image_base => $IMAGE_BASE, orig => $ORIG_DIR, thumbs_size => $thumbs_size, - scales => \@scale_width + scales => \@scale_width, + user_id => $current_user->{'user_id'}, ); } => 'index'; @@ -132,6 +113,7 @@ post '/upload' => ( authenticated => 1 ) => sub { my $image = $self->req->upload('image'); my $user = $self->current_user(); + my $user_id = $user->{'user_id'}; $self->app->log->debug( "user:" . Dumper($user) ); # Not upload @@ -175,7 +157,7 @@ post '/upload' => ( authenticated => 1 ) => sub { # Image file 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( get_path($user_id, $ORIG_DIR), $filename ); # Save to file $image->move_to($image_file); @@ -200,7 +182,7 @@ post '/upload' => ( authenticated => 1 ) => sub { my $scaled = $imager->scale( xpixels => $scale ); $scaled->write( - file => File::Spec->catfile( $IMAGE_DIR, $scale, $filename ) ) + file => File::Spec->catfile( get_path($user_id, $scale), $filename ) ) or die $scaled->errstr; } @@ -234,4 +216,13 @@ sub create_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; +} + app->start; diff --git a/sql/sqitch.plan b/sql/sqitch.plan index a490023..1f26348 100644 --- a/sql/sqitch.plan +++ b/sql/sqitch.plan @@ -7,3 +7,4 @@ images 2017-07-22T07:17:52Z Denis Fedoseev # Creates albums 2017-07-22T08:50:11Z Denis Fedoseev # Albums database init user_images [users images] 2017-07-22T09:16:20Z Denis Fedoseev # +user_images table album_images [images albums] 2017-07-22T09:21:13Z Denis Fedoseev # +images to album mapping +user_albums [users albums] 2017-07-22T09:47:48Z Denis Fedoseev # Albums to users mapping