更具if modified since更新图片,并且删除陈旧的图片

更具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});
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值