#!/usr/bin/perl -- use strict; use warnings; use lib '/home/dsktnk/local/lib/perl5'; use Path::Class; use CGI; use Image::Magick; my $obj = new CGI; my $path = $obj->param('d'); #sort my $sort = $obj->param('s'); $sort ||= "L"; my $view = $obj->param('i'); my $action = $obj->param('action'); #表示させる対象ファイル my $subject = '.html$|.txt$|.md$|.jpg$|.JPG$|.jpeg$|.gif$|.png$'; my $text = '.html$|.txt$|.md$'; if($path=~/\.\./o){$path='/home/dsktnk/www/memo/'}#変なディレクトリを掘られないため $path = "/home/dsktnk/www/memo/$path"; #$path ||= '/home/dsktnk/www/memo/'; $path //= '/home/dsktnk/www/memo/'; my $baseurl="http://dsktnk.sakura.ne.jp/memo/"; my $cgi ="http://dsktnk.sakura.ne.jp/memo/index.cgi"; ###ディレクトリのリストを作成 my @dir = dir($path)->children; my (@dirs,@files); for my $d (@dir){ if ($d->is_dir){ push(@dirs, $d); }else{ push(@files, $d) } } print qq|Content-type: text/html\n\n|; print &header_html; print qq|

memo|; # パンくずリスト if ($path){ my $relative; my $path_a=$path; $path_a=~ s/\/home\/dsktnk\/www\/memo\///g; my @line = split ( /\// , $path_a ); for my $i (@line){ $relative .= "$i/"; print qq|/$i|; } print qq|

|; } #----------------------------------------------------------------------- ###ディレクトリを表示 for my $d(@dirs){ my $basename = $d->basename; my $fileurl = $d->relative; print qq|$basename |; } # サブディレクトリがある場合 if (defined $dirs[0] eq 1){ print qq|
|; } if (defined $files[1] eq 1){ my $aaa = dir($path); my $bbb = $aaa->relative; #/memo/memoを参照しようとするための対策 if($path =~ /\/home\/dsktnk\/www\/memo\/$/i){$bbb=''} print qq|
NameLast modifiedSize
|; print qq|
|; } if (($path =~ /\/img/i)&&(defined $files[0] eq 1)){ my $aaa = dir($path); my $bbb = $aaa->relative; print qq|list||; print qq|thumbnail||; print qq|gene|; print qq|
|; } #----------------------------------------------------------------------- #クエリに応じてファイルリストをソートする #デフォルトはLast modified #name if($sort eq "N"){ @files = sort {$a->relative cmp $b->relative} @files; } #Last modified elsif($sort eq "L"){ @files = sort {$b->stat->mtime <=> $a->stat->mtime} @files; } #size elsif($sort eq "S"){ @files = sort {$b->stat->size <=> $a->stat->size} @files; } elsif($sort eq "all"){ my @files=&all; @files = sort {$b->stat->mtime <=> $a->stat->mtime} @files; if ((defined $files[0] eq 1)&&(! $view)){ for my $d(@files){ #ファイルネーム取得 my $basename = $d->basename; my $fileurl = $d->relative; #時間 my $mtime = localtime($d->stat->mtime); my ($sec, $min, $hour, $mday, $month, $year, $wday, $stime) = localtime($d->stat->mtime); $year += 1900; ++$month; my @wdays = ("日", "月", "火", "水", "木", "金", "土"); my $file_size = $d->stat->size; if ( $d =~ /$text/ ) { #Markdown形式だった場合の処理 if ( $d =~ (/.md$/) ){ printf "%04d/%02d/%02d %02d:%02d\n", $year, $month, $mday, $hour, $min, $sec; print qq|$basename $file_size(b)
|; } elsif($basename =~ /thumbnail_/){ next; }else{ printf "%04d/%02d/%02d %02d:%02d\n", $year, $month, $mday, $hour, $min, $sec; print qq|$basename $file_size(byte)
|; } } } } } #----------------------------------------------------------------------- #表示部 if ((defined $files[0] eq 1)&&(! $view)&&($sort ne "all")){ for my $d(@files){ #ファイルネーム取得 my $basename = $d->basename; my $fileurl = $d->relative; #時間 my $mtime = localtime($d->stat->mtime); my ($sec, $min, $hour, $mday, $month, $year, $wday, $stime) = localtime($d->stat->mtime); $year += 1900; ++$month; my @wdays = ("日", "月", "火", "水", "木", "金", "土"); my $file_size = $d->stat->size; if ( $d =~ /$subject/ ) { #Markdown形式だった場合の処理 if ( $d =~ (/.md$/) ){ printf "%04d/%02d/%02d %02d:%02d\n", $year, $month, $mday, $hour, $min, $sec; # print qq|$basename $file_size(b)
|; print qq|$basename $file_size(b)
|; } elsif($basename =~ /thumbnail_/){ next; }else{ printf "%04d/%02d/%02d %02d:%02d\n", $year, $month, $mday, $hour, $min, $sec; print qq|$basename $file_size(byte)
|; } } } } #----------------------------------------------------------------------- #imgに関する if (($path)&&($view eq "view")){ for my $d(@files){ #ファイルネーム取得 my $basename = $d->basename; my $fileurl = $d->relative; if ( $basename =~ /$subject/i ) { if ($basename =~/thumbnail/){ my $cuturl = $fileurl; $cuturl=~ s/thumbnail_//g; print qq|\n|; } } } } if (($path)&&($view eq "gene")){ for my $d(@files){ #ファイルネーム取得 my $basename = $d->basename; my $fileurl = $d->relative; if ( $basename =~ /$subject/i ) { #print qq||; #print $fileurl; if($basename !~ /thumbnail_/){ #リサイズ処理 my $newwidth = 150; my $newheight = 150; my $i = Image::Magick->new; $i->Read("./$fileurl"); #$i = $i->Transform(geometry=>$newwidth,height=>$newheight); $i->Resize(width => $newwidth,height => $newheight); binmode(STDOUT); my $jpgname = "thumbnail_$basename"; $i->Write("$path/$jpgname"); #print ("Content-type: image/jpeg\n\n"); #$i->Write('jpg:-'); #print qq||; } } } print "サムネイルを生成しました"; } print qq|\n|; print qq|\n|; print qq||; #----------------------------------------------------------------------- #form if ($action eq "edit"){ print &form_html; } #Debug用パス表示 #print $path; sub header_html{ my $printlines = <<"__EOF__";
__EOF__ return $printlines; } sub form_html{ my $printlines = <<"__EOF__";
__EOF__ return $printlines; } sub all{ my @files; Path::Class::Dir->new("/home/dsktnk/www/memo")->recurse( callback => sub { my $file = shift; return if $file->is_dir; #ディレクトリはスキップ push(@files, $file); }, ); return @files; }