| 1 | package File::Feed::Source; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use URI; | ||
| 7 | |||
| 8 | sub new { | ||
| 9 | my $cls = shift; | ||
| 10 | my %arg; | ||
| 11 | if (@_ % 2 && ref($_[0]) eq 'HASH') { | ||
| 12 | %arg = %{ shift() }; | ||
| 13 | } | ||
| 14 | elsif (@_ % 2) { | ||
| 15 | %arg = ( 'uri', @_ ); | ||
| 16 | } | ||
| 17 | else { | ||
| 18 | %arg = @_; | ||
| 19 | } | ||
| 20 | my $uri = $arg{'#'} || $arg{'uri'} or die "Can't instantiate a source without a URI"; | ||
| 21 | $uri = 'file://' . $uri if $uri =~ m{^/}; | ||
| 22 | $uri = URI->new($uri) if !ref $uri; | ||
| 23 | my $scheme = $uri->scheme; | ||
| 24 | $cls .= '::' . lc $scheme; | ||
| 25 | eval "use $cls; 1" or die $@; | ||
| 26 | bless { | ||
| 27 | 'uri' => $uri, | ||
| 28 | }, $cls; | ||
| 29 | } | ||
| 30 | |||
| 31 | sub uri { $_[0]->{'uri'} } | ||
| 32 | sub host { $_[0]->{'host'} ||= $_[0]->{'uri'}->host } | ||
| 33 | sub root { $_[0]->{'root'} ||= $_[0]->{'uri'}->path } | ||
| 34 | sub user { $_[0]->{'user'} ||= $_[0]->{'uri'}->user } | ||
| 35 | sub password { $_[0]->{'password'} ||= $_[0]->{'uri'}->password } | ||
| 36 | |||
| 37 | sub fetch { | ||
| 38 | my ($self, %arg) = @_; | ||
| 39 | my $channel = $arg{'channel'}; | ||
| 40 | my $exclude = $arg{'exclude'} || {}; | ||
| 41 | my $dest = $arg{'destination'}; | ||
| 42 | my $root = $self->root; | ||
| 43 | my $recurse = $channel->recursive; | ||
| 44 | my $filter = $channel->file_filter; | ||
| 45 | my $path = $channel->path; | ||
| 46 | my $lpath = $channel->local_path; | ||
| 47 | my $dir = defined $path ? $path : '.'; | ||
| 48 | my $ldir = defined $lpath ? "$dest/$lpath" : $dest; | ||
| 49 | my $pfx = defined $path ? $path . '/' : ''; | ||
| 50 | my $lpfx = defined $lpath ? $lpath . '/' : ''; | ||
| 51 | my @fetched; | ||
| 52 | foreach ($self->list($dir, $recurse)) { | ||
| 53 | my $from = defined $path ? "$path/$_" : $_; | ||
| 54 | my $to = "$ldir/$_"; | ||
| 55 | next if $exclude->{$from} || !$filter->($_); | ||
| 56 | $self->fetch_file($from, $to) or die "Can't fetch $from to $to: $!"; | ||
| 57 | push @fetched, { | ||
| 58 | '#' => $pfx . $_, | ||
| 59 | 'path' => $pfx . $_, | ||
| 60 | 'local-path' => $lpfx . $_, | ||
| 61 | }; | ||
| 62 | } | ||
| 63 | return @fetched; | ||
| 64 | } | ||
| 65 | |||
| 66 | 1; |