X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=Debbugs%2FLibravatar.pm;h=8dcbad09033055b925a631b077005c4b89163014;hb=aae281560aaba1950b00f66175b78fd7d7128c47;hp=283db41af2e1b6bf794d067fd36eb126a8713dce;hpb=bec036b7465a66ad8fea842d01ce4cd53eed146f;p=debbugs.git diff --git a/Debbugs/Libravatar.pm b/Debbugs/Libravatar.pm index 283db41..8dcbad0 100644 --- a/Debbugs/Libravatar.pm +++ b/Debbugs/Libravatar.pm @@ -41,6 +41,8 @@ use Debbugs::CGI qw(cgi_parameters); use Digest::MD5 qw(md5_hex); use LWP::UserAgent; use File::Temp qw(tempfile); +use File::LibMagic; +use Cwd qw(abs_path); use Carp; @@ -49,7 +51,7 @@ BEGIN{ $DEBUG = 0 unless defined $DEBUG; @EXPORT = (); - %EXPORT_TAGS = (libravatar => [qw(cache_valid serve_cache retrieve_libravatar cache_location)] + %EXPORT_TAGS = (libravatar => [qw(cache_valid retrieve_libravatar cache_location)] ); @EXPORT_OK = (); Exporter::export_ok_tags(keys %EXPORT_TAGS); @@ -92,7 +94,7 @@ sub retrieve_libravatar{ ); my %param = @_; my $cache_location = $param{location}; - $cache_location =~ s/\.[^\.]+$//; + $cache_location =~ s/\.[^\.\/]+$//; # take out a lock on the cache location so that if another request # is made while we are serving this one, we don't do double work my ($fh,$lockfile,$errors) = @@ -157,7 +159,7 @@ sub retrieve_libravatar{ $temp_fn, $cache_location.'.'.$dest_type) == 0 or die "convert file failed"; - unlink($temp_fh); + unlink($temp_fn); }; if ($@) { unlink($cache_location.'.'.$dest_type) if -e $cache_location.'.'.$dest_type; @@ -244,7 +246,7 @@ sub handler { my $location = $r->location(); my ($email) = $uri =~ m/\Q$location\E\/?(.*)$/; if (not length $email) { - return Apache2::Const::NOT_FOUND; + return Apache2::Const::NOT_FOUND(); } my $q = CGI::Simple->new(); my %param = cgi_parameters(query => $q, @@ -263,7 +265,7 @@ sub handler { serve_cache_mod_perl($cache_location,$r); return Apache2::Const::DECLINED(); } - $cache_location = retreive_libravatar(location => $cache_location, + $cache_location = retrieve_libravatar(location => $cache_location, email => $email, ); if (not defined $cache_location) { @@ -277,15 +279,24 @@ sub handler { } + +our $magic; + sub serve_cache_mod_perl { my ($cache_location,$r) = @_; if (not defined $cache_location or not length $cache_location) { # serve the default image $cache_location = $config{libravatar_default_image}; } + $magic = File::LibMagic->new() if not defined $magic; + + return Apache2::Const::DECLINED() if not defined $magic; + + $r->content_type($magic->checktype_filename(abs_path($cache_location))); + $r->filename($cache_location); $r->path_info(''); - $r->finfo(APR::Finfo::stat($cache_location, APR::Const::FINFO_NORM, $r->pool)); + $r->finfo(APR::Finfo::stat($cache_location, APR::Const::FINFO_NORM(), $r->pool)); } =back