2 |
|
# Interface |
3 |
|
# --------- |
4 |
|
# new(cache) : A new urlhandler with a defined default cahce directory |
5 |
+ |
# download(url,[location]) : as get but always download |
6 |
|
# get(url,[location]) : download from the specified url to cache or location |
7 |
|
# return the full url path name incl. any base expansion |
8 |
|
# and the filename downloaded to |
52 |
|
sub get { |
53 |
|
my $self=shift; |
54 |
|
my $origurl=shift; |
55 |
+ |
my $file=""; |
56 |
|
|
57 |
< |
# Process the URL string |
58 |
< |
$url=URL::URLclass->new($origurl); |
57 |
> |
my $url=URL::URLclass->new($origurl); |
58 |
> |
my $type=$url->type(); |
59 |
> |
$url->expandurl($self->currentbase($type)); |
60 |
> |
my $fullurl=$url->url(); |
61 |
> |
|
62 |
> |
my $location=$self->{cache}->file($fullurl); |
63 |
> |
if ( $location ne "" ) { |
64 |
> |
$file=$self->{cache}->file($location); |
65 |
> |
#print " Already defined file --".$location." -- ".$file."\n"; |
66 |
> |
} |
67 |
> |
else { |
68 |
> |
($fullurl,$file)=$self->download($origurl, @_); |
69 |
> |
} |
70 |
> |
return ($fullurl, $file); |
71 |
> |
} |
72 |
> |
|
73 |
> |
sub download { |
74 |
> |
my $self=shift; |
75 |
> |
my $origurl=shift; |
76 |
> |
|
77 |
> |
print "downloading $origurl\n"; |
78 |
> |
# Process the URL string |
79 |
> |
my $url=URL::URLclass->new($origurl); |
80 |
|
my $type=$url->type(); |
81 |
|
$urltypehandler=$self->_typehandler($type); |
82 |
|
$url->expandurl($self->currentbase($type)); |
88 |
|
else { |
89 |
|
$location=$self->{cache}->filename($url->url()); |
90 |
|
} |
91 |
< |
# -- get the file form the appropriate handler |
91 |
> |
# -- get the file from the appropriate handler |
92 |
|
if ( defined $urltypehandler ) { |
93 |
|
# Call the download module |
94 |
|
$file=eval{$urltypehandler->get($url, $location)}; |
96 |
|
|
97 |
|
# now register it in the cache if successful |
98 |
|
if ( $file ) { |
99 |
< |
$self->{cache}->store($url->url(), $file); |
99 |
> |
$self->{cache}->store($url->url(), $location); |
100 |
> |
$self->{cache}->store($location, $file); |
101 |
|
} |
102 |
|
return ($url->url(), $file); |
103 |
|
} |