!mojoliciousのメモ
mojoliciousを使って見たかったので、CGI.pmベースの[album.cgi|http://www21051ue.sakura.ne.jp/album/album/album.cgi](アルバム)をmojolicious([Photo_album|http://www21051ue.sakura.ne.jp:8080/])に書き直す事にした。
[デモ画面|http://www21051ue.sakura.ne.jp:8080/]
![tree]
.[photo_album]
├── lib
│ ├── album.pm
│ ├── myconstant.pm
│ ├── PhotoAlbum
│ │ └── Example.pm
│ └── PhotoAlbum.pm
├── log
│ ├── development.log
│ └── production.log
├── public
│ ├── css
│ │ ├── demo.css
│ │ ├── reset.css
│ │ └── style.css
│ ├── images
│ │ ├── 10.jpg
│ │ ├── 11.jpg
│ │ ├── hide.png
│ │ └── show.png
│ ├── img
│ │ ├── 10.jpg
│ │ ├── 11.jpg
│ │ ├── hide.png
│ │ └── show.png
│ ├── index2.html
│ ├── index3.html
│ └── js
│ ├── jquery.montage.js
│ ├── jquery.montage.js.bak
│ ├── jquery.montage.min.js
│ └── jquery.montage.min.js.bak
├── script
│ ├── hypnotoad.pid
│ └── photo_album
├── t
│ └── basic.t
└── templates
├── example
│ ├── next.html.ep
│ ├── onepic.html.ep
│ └── welcome.html.ep
├── index.html.ep
└── layouts
└── default.html.ep
!!SOURCE
!photo_album/lib/PhotoAlbum.pm
package PhotoAlbum;
use utf8;
use Mojo::Base 'Mojolicious';
use album;
# This method will run once at server start
sub startup {
my $self = shift;
$self->attr( album => sub { album->new() } );
# Documentation browser under "/perldoc"
$self->plugin('PODRenderer');
# Router
my $r = $self->routes;
# Normal route to controller
$r->get('/')->to('example#welcome');
$r->get('/onepic')->to('example#onepic');
$r->post('/upload')->to('example#upload');
$r->post('/edit')->to('example#edit');
$r->get('/next')->to('example#next');
$r->get('/flick')->to('example#flick');
}
1;
!photo_album/lib/album.pm
package album;
use strict;
use utf8;
use Mojo::Base 'Mojolicious::Controller';
use myconstant;
my $cons = myconstant->new();
my $imagedir = $cons->{imagedir};
my $img = $cons->{img};
my $max_size = $cons->{max_size};
my $img_size = $cons->{img_size};
my $item_count = $cons->{item_count};
my $real = 'public';
sub rotate {
my ( $s, $c ) = @_;
my $filename = $c->param('_name');
if ( $filename ne '' ) {
if ( $c->param('_action') =~ /^\d{1,3}$/ ) {
system(
"/usr/bin/mogrify -rotate @{[$c->param('_action')]} $real/$img/$filename"
);
system(
"/usr/bin/mogrify -rotate @{[$c->param('_action')]} $real/$imagedir/$filename"
);
}
elsif ( $c->param('_action') eq 'touch' ) {
system("/usr/bin/touch $real/$img/$filename");
system("/usr/bin/touch $real/$imagedir/$filename");
}
}
}
sub get_images {
my $s = shift;
my $opt = shift || '';
my $st = shift || 0;
my $items = shift || $item_count;
my @files = $s->images_select( $st, $item_count, $s->get_images_sort() );
my $text = join(
"\n",
map {
qq{ }
} @files
);
return $text;
}
sub get_images_sort {
my $s = shift;
opendir my $dh, "$real/$imagedir" or die "Can't open $real/$imagedir";
my @files = map { $_->[0] }
sort { $a->[1] <=> $b->[1] }
map { [ $_, -M "$real/$imagedir/$_" ] }
grep { !/(hide\.png|show\.png|arrow.*\.png|black\.png)/ }
grep { -s "$real/$imagedir/$_" != 0 }
grep ( /^[^.]/, readdir $dh );
return @files;
}
sub images_select {
my $s = shift;
my ( $next, $item, @files ) = @_;
my $i = -1;
my $st = $next * $item;
my @select = ();
SELECT:
for (@files) {
$i++;
next SELECT if $i < $st;
last SELECT if ( $i >= ( $next + 1 ) * $item );
push @select, $_;
}
return @select;
}
sub flick{
my ($s,$r) = @_;
my $flick = {
"name" => $s->get_next_image($r,$r->param('_count')),
"preload" => $s->get_next_image($r,$r->param('_count')*3),
};
return $flick;
}
sub get_next_image{
my $s = shift;
my $r = shift;
my $count = shift;
my $file = $r->param('_name');
$file =~ s|.*?([^/]+\.[a-zA-Z0-9]*)(\?.*)*$|$1|;
my @files = $s->get_images_sort();
my $i = $s->search_index($file,@files);
$i += $count;
$i -= ($#files + 1) if($i > $#files);
my $img = "$img/$files[$i]";
return $img;
}
sub preload{
my $s = shift;
my $name = shift;
my @files = get_images_sort();
$name =~ s|.*?([^/]+\.[a-zA-Z]*)(\?.*)*$|$1|;
my $i = $s->search_index($name,@files);
my $text = "\n";
return $text;
}
sub search_index{
my ($s,$item,@arry) = @_;
my $i = 0;
for(@arry){
if($item eq $_){
return $i;
}
$i++;
}
return;
}
sub up_load {
my $s = shift;
my $upload = shift;
my $filename = $upload->filename;
$filename = $ENV{REMOTE_USER} . "_" . $upload->filename
if ( $ENV{REMOTE_USER} );
$upload->move_to("$real/$img/$filename");
if($filename =~ /(.*)\.mp4/i){
my $outfile = "$1_mp4.jpg";
system("ffmpeg -i $real/$img/$filename -vframes 1 $real/$imagedir/$outfile");
$filename = $outfile;
}else{
my $size = -s "$real/$img/$filename";
if ( $size > $max_size ) {
system(
"convert -resize @{[int($img_size / $size * 100)]}% $real/$img/$filename $real/$imagedir/$filename"
);
}
else {
system("cp $real/$img/$filename $real/$imagedir/$filename");
}
}
return $filename;
}
sub put_inner {
my $s = shift;
return $cons->{inner};
}
sub imgTag{
my $s = shift;
my $file = shift;
my $text;
if($file =~ /(.*)_mp4.jpg$/){
$text = <
fillLastRow : true, alternateHeight : true, alternateHeightRange : { min : 90, max : 240 }