]> git.donarmstrong.com Git - debbugs.git/blobdiff - Debbugs/Libravatar.pm
Libravatar: Lazy-load LWP::UserAgent
[debbugs.git] / Debbugs / Libravatar.pm
index 81668a711e881427fe6bb1bada6951f633eb23a4..4c4bcd0a66f8ae57d2e2a46649a5f292228b242f 100644 (file)
@@ -31,7 +31,7 @@ None known.
 use warnings;
 use strict;
 use vars qw($VERSION $DEBUG %EXPORT_TAGS @EXPORT_OK @EXPORT);
-use base qw(Exporter);
+use Exporter qw(import);
 
 use Debbugs::Config qw(:config);
 use Debbugs::Common qw(:lock);
@@ -39,8 +39,9 @@ use Libravatar::URL;
 use CGI::Simple;
 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 +50,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 +93,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) =
@@ -107,6 +108,8 @@ sub retrieve_libravatar{
             return $temp_location;
         }
     }
+    require LWP::UserAgent;
+
     my $dest_type;
     eval {
         my $uri = libravatar_url(email => $param{email},
@@ -157,7 +160,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 +266,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 +280,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));