]> git.donarmstrong.com Git - debbugs.git/blobdiff - Debbugs/Libravatar.pm
we need to unlink the temporary file name, not the temporary file handle
[debbugs.git] / Debbugs / Libravatar.pm
index ed36b91d23f48033ca6524beedb226740b80a738..8dcbad09033055b925a631b077005c4b89163014 100644 (file)
@@ -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;
 
@@ -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;
@@ -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,12 +279,21 @@ 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));