更具if modified since更新图片,并且删除陈旧的图片:
# ---------------------------------------------
# This method will get the document identified by $url and store it in file called $filename.
# If the file already exists, then the request will contain an "If-Modified-Since" header matching the modification time of the file.
# If the document on the server has not changed since this time, then nothing happens.
# If the document has been updated, it will be downloaded again.
# The modification time of the file will be forced to match that of the server.
# The return value is the the response object.
# Modify Log
# 11/29/2010 edison create
# ---------------------------------------------
sub mirrorFile {
my($this, $url, $file) = @_;
my $browser = $this->{'browser'};
my $request = HTTP::Request->new('GET', $url);
# If the file exists, add a cache-related header
if ( -e $file ) {print "exist\n";
my ($mtime) = ( stat($file) )[9];
if ($mtime) {
$request->header( 'If-Modified-Since' => HTTP::Date::time2str($mtime) );
}
# use the current time to replace the last access time
my $last_access_time = time();
utime $last_access_time, $mtime, $file;
}
my $tmpfile = "$file-$$";
# If a $tmpfile is provided with the :content_file option, then the response content will be saved here instead of in the response object.
my $response = $browser->request($request, $tmpfile);
if ( $response->header('X-Died') ) {
die $response->header('X-Died');
}
# Only fetching a fresh copy of the would be considered success.
# If the file was not modified, "304" would returned, which
# is considered by HTTP::Status to be a "redirect", /not/ "success"
if ( $response->is_success ) {print "success\n";
my @stat = stat($tmpfile) or die "Could not stat tmpfile '$tmpfile': $!";
my $file_length = $stat[7];
my ($content_length) = $response->header('Content-length');
if ( defined $content_length and $file_length < $content_length ) {
unlink($tmpfile);
die "Transfer truncated: " . "only $file_length out of $content_length bytes received\n";
}
elsif ( defined $content_length and $file_length > $content_length ) {
unlink($tmpfile);
die "Content-length mismatch: " . "expected $content_length bytes, got $file_length\n";
}
# The file was the expected length.
else {
# Replace the stale file with a fresh copy
if ( -e $file ) {
# Some dosish systems fail to rename if the target exists
chmod 0777, $file;
unlink $file;
}
rename( $tmpfile, $file )
or die "Cannot rename '$tmpfile' to '$file': $!\n";
# make sure the file has the same last modification time
# and use the current time to replace the last access time
if ( my $lm = $response->last_modified ) {
my $last_access_time = time();
utime $last_access_time, $lm, $file;
}
}
}
# The local copy is fresh enough, so just delete the temp file
else {
unlink($tmpfile);print "unsuccess\n";
}
return $response;
}
# ---------------------------------------------
# Remove images which are not fresh enough to save disk space.
# Modify Log
# 11/30/2010 edison create
# ---------------------------------------------
sub removeStaleImages {
my $this = shift;
my $settedNotAccessDay = $this->{'config'}->{'image_not_access_day'};
if(!$settedNotAccessDay){
die "miss 'image_not_access_day' in base.conf file\n";
}
if($settedNotAccessDay =~ /[^\d]/){
die "'image_not_access_day' in base.conf file should be numeric\n";
}
my $deleteImageCount = 0;
my $data_feed_path = $this->{'config'}->{'data_feed_path'};
my @fullPathImages = glob($data_feed_path.'/??/??/*');
foreach (@fullPathImages){
my @stat = stat($_) or die "Could not stat imagefile '$_': $!";
my $atime = $stat[8];
my $timeNotAccess = time()-$atime;
if($timeNotAccess > $settedNotAccessDay*24*3600){
unlink $_;
$deleteImageCount ++;
}
}
Log::Write::setLog('A400004',{'COUNT'=>$deleteImageCount});
}