Show
Ignore:
Timestamp:
05/26/05 19:48:46 (4 years ago)
Author:
ogawa
Message:

Add image caching

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • FlickrPublicPhotos/trunk/FlickrPublicPhotos.pl

    r69 r70  
    11# A plugin for adding "FlickrPublicPhotos" container and related tags 
    22# 
    3 # Release 0.13 (May 8, 2005) 
     3# Release 0.20 (May 26, 2005) 
    44# 
    55# This software is provided as-is. You may use it for commercial or  
     
    1616    $plugin = new MT::Plugin(); 
    1717    $plugin->name("FlickrPublicPhotos Plugin"); 
    18     $plugin->description("Add FlickrPublicPhotos container and related tags. Version 0.13"); 
     18    $plugin->description("Add FlickrPublicPhotos container and related tags. Version 0.20"); 
    1919    $plugin->doc_link("http://as-is.net/hacks/2005/05/flickrpublicphotos_plugin.html"); 
    2020    MT->add_plugin($plugin); 
     
    4343    my ($user, $refresh) = @_; 
    4444    require MT::PluginData; 
    45     my $pd = MT::PluginData->load({ plugin => 'FlickrPublicPhotos', 
     45    my $pd = MT::PluginData->load({ plugin => $plugin->name, 
    4646                                    key => $user }); 
    4747    if (!$pd) { 
    4848        $pd = new MT::PluginData(); 
    49         $pd->plugin('FlickrPublicPhotos'); 
     49        $pd->plugin($plugin->name); 
    5050        $pd->key($user); 
    5151    } 
     
    105105 
    106106sub photo_img_url { 
    107     $_[0]->stash('flickr_public_photo')->img_url($_[1]->{size} || 't'); 
     107    my ($ctx, $args) = @_; 
     108    my $url = $ctx->stash('flickr_public_photo')->img_url($args->{size} || 't'); 
     109    my $cache = $args->{cache} or return $url; 
     110    $cache .= '/' unless $cache =~ m!/$!; 
     111 
     112    my ($fname) = $url =~ m!^https?://.+/(.*)$!; 
     113 
     114    my $site_url = $ctx->stash('blog')->site_url; 
     115    $site_url .= '/' unless $site_url =~ m!/$!; 
     116    $site_url .= $cache . $fname; 
     117 
     118    my $path = $ctx->stash('blog')->site_path; 
     119    $path .= '/' unless $path =~ m!/$!; 
     120    $path .= $cache; 
     121    my $fmgr = $ctx->stash('blog')->file_mgr; 
     122    unless ($fmgr->exists($path)) { 
     123        # mkpath, and if can't return original url 
     124        $fmgr->mkpath($path) or return $url; 
     125    } 
     126    $path .= $fname; 
     127 
     128    require LWP::UserAgent; 
     129    require HTTP::Request; 
     130    require HTTP::Response; 
     131    require HTTP::Date; 
     132    my $req = HTTP::Request->new(GET => $url); 
     133    my $ua = LWP::UserAgent->new; 
     134    if ($fmgr->exists($path) && (my $mtime = (stat($path))[9])) { 
     135        $req->header('If-Modified-Since', HTTP::Date::time2str($mtime)); 
     136    } 
     137    my $rsp = $ua->request($req); 
     138    if ($rsp->is_success && $rsp->content) { 
     139        # put_data, and if can't return original url 
     140        $fmgr->put_data($rsp->content, $path) or return $url; 
     141        return $site_url; 
     142    } elsif ($rsp->code == 304) { # not modified 
     143        return $site_url; 
     144    } else { 
     145        return $url; 
     146    } 
    108147} 
    109148