改造/download.cgi
ダウンロードファイル名をファイルに付けられたファイル名にする
2008年7月現在配布されているFSWikiLite 0.0.11のdownload.cgiだと、ページに添付したファイルをダウンロードする際ファイル名がどれも「download.cgi」という名になってしまい不都合あるので、ちゃんとファイルに付けられたファイル名でダウンロード出来るようにする。
改造方法
- 「download.cgi」のスクリプトを改造する
- 36行付近に下のような行があるはず
print "Content-Disposition: $disposition;filename=\"".&jcode::convert($file,'sjis')."\"\n\n";
- それを下のように変更する
print "Content-Disposition: $disposition;filename=\"".$file."\"\n\n";
MIMEタイプ取得をcommon.plに移す
ファイルのMIMEタイプ取得を必要とするのはdownload.cgiだけに限らず、汎用性高いのでcommon.plのUtilに移し各種プラグインやCGIからも利用出来るようにする。
改造方法
- 「download.cgi」「common.pl」のスクリプトを改造する
- 「download.cgi」の32行付近に下のような行があるはず
my $contenttype = &get_mime_type($file);
- それを下のように変更する (common.plのユーティリティ関数を提供するパッケージから開くように)
my $contenttype = &Util::get_mime_type($file);
- 「download.cgi」に下のような行があるので、その部分を切り取り(カット)、「common.pl」にコピーして移す
#============================================================================== # MIMEタイプを取得します #============================================================================== sub get_mime_type { my $file = shift; my $type = lc(substr($file,rindex($file,"."))); my $ctype; if ($type eq ".gif" ){ $ctype = "image/gif"; } elsif($type eq ".txt" ){ $ctype = "text/plain"; } elsif($type eq ".rb" ){ $ctype = "text/plain"; } elsif($type eq ".pl" ){ $ctype = "text/plain"; } elsif($type eq ".java"){ $ctype = "text/plain"; } # elsif($type eq ".html"){ $ctype = "text/html"; } # elsif($type eq ".htm" ){ $ctype = "text/html"; } elsif($type eq ".css" ){ $ctype = "text/css"; } elsif($type eq ".jpeg"){ $ctype = "image/jpeg"; } elsif($type eq ".jpg" ){ $ctype = "image/jpeg"; } elsif($type eq ".png" ){ $ctype = "image/png"; } elsif($type eq ".bmp" ){ $ctype = "image/bmp"; } elsif($type eq ".doc" ){ $ctype = "application/msword"; } elsif($type eq ".xls" ){ $ctype = "application/vnd.ms-excel"; } else { $ctype = "application/octet-stream"; } return $ctype; }
- 「common.pl」の中に上記のカットした行をコピーする。ユーティリティ関数を提供するパッケージ(package Util;)より下ならどこでもいいが、とりあえず分かりやすく一番最後に付け足す事に
- 「common.pl」を改造してない状態で一番最後は下のようになっているはず
#=============================================================================== # インラインプラグインをパースしてコマンドと引数に分割 #=============================================================================== sub parse_plugin { 〜中略〜 return {command=>$cmd,args=>\@ret_args}; } 1;
- 一番最後に付け加えるという事で、以下のようにする
#=============================================================================== # インラインプラグインをパースしてコマンドと引数に分割 #=============================================================================== sub parse_plugin { 〜中略〜 return {command=>$cmd,args=>\@ret_args}; } #============================================================================== # MIMEタイプを取得します #============================================================================== sub get_mime_type { my $file = shift; my $type = lc(substr($file,rindex($file,"."))); my $ctype; if ($type eq ".gif" ){ $ctype = "image/gif"; } elsif($type eq ".txt" ){ $ctype = "text/plain"; } elsif($type eq ".rb" ){ $ctype = "text/plain"; } elsif($type eq ".pl" ){ $ctype = "text/plain"; } elsif($type eq ".java"){ $ctype = "text/plain"; } # elsif($type eq ".html"){ $ctype = "text/html"; } # elsif($type eq ".htm" ){ $ctype = "text/html"; } elsif($type eq ".css" ){ $ctype = "text/css"; } elsif($type eq ".jpeg"){ $ctype = "image/jpeg"; } elsif($type eq ".jpg" ){ $ctype = "image/jpeg"; } elsif($type eq ".png" ){ $ctype = "image/png"; } elsif($type eq ".bmp" ){ $ctype = "image/bmp"; } elsif($type eq ".doc" ){ $ctype = "application/msword"; } elsif($type eq ".xls" ){ $ctype = "application/vnd.ms-excel"; } else { $ctype = "application/octet-stream"; } return $ctype; } 1;