| 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 | } |