| 1 | package VFSsimple::Drv::Ftp; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use base qw(VFSsimple::Base); | ||
| 6 | use Net::FTP (); | ||
| 7 | use URI (); | ||
| 8 | use File::Temp qw(tempfile); | ||
| 9 | |||
| 10 | our $VERSION = '0.03'; | ||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | VFSsimple::Drv::Ftp - A VFSsimple implementation over ftp protocol | ||
| 15 | |||
| 16 | =head1 DESCRIPTION | ||
| 17 | |||
| 18 | This module provide access method for VFSsimple module to access to file on | ||
| 19 | ftp server. | ||
| 20 | |||
| 21 | Access is provided using L<Net::FTP> module. | ||
| 22 | |||
| 23 | =cut | ||
| 24 | |||
| 25 | sub drv_new { | ||
| 26 | my ($self) = @_; | ||
| 27 | my $uri = URI->new($self->{root}); | ||
| 28 | my $ftp = Net::FTP->new($uri->host()) or return; | ||
| 29 | my ($user, $pass) = ($uri->userinfo || '') =~ m/^([^:]):?(.*)?/; | ||
| 30 | if (!$user) { | ||
| 31 | $user = 'ftp'; | ||
| 32 | $pass = 'vfssimple@'; | ||
| 33 | } | ||
| 34 | $ftp->login($user, $pass) or return; | ||
| 35 | $ftp->pasv(); | ||
| 36 | |||
| 37 | $self->{uri} = $uri; | ||
| 38 | $self->{ftp} = $ftp; | ||
| 39 | return $ftp ? $self : (); | ||
| 40 | } | ||
| 41 | |||
| 42 | sub drv_copy { | ||
| 43 | my ($self, $src, $dest) = @_; | ||
| 44 | my ($dir, $file) = ($self->{uri}->path() . '/' . $src) =~ m:(.*)/+([^/]*):; | ||
| 45 | $self->{ftp}->cwd($dir) or do { | ||
| 46 | $self->set_error($self->{ftp}->message); | ||
| 47 | return; | ||
| 48 | }; | ||
| 49 | open(my $fh, '>', $dest) or return; | ||
| 50 | $self->{ftp}->get($file, $fh) or do { | ||
| 51 | $self->set_error($self->{ftp}->message); | ||
| 52 | return; | ||
| 53 | }; | ||
| 54 | close($fh); | ||
| 55 | return $dest; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub drv_exists { | ||
| 59 | my ($self, $file) = @_; | ||
| 60 | my ($dir, $filename) = ($self->{uri}->path() . '/' . $file) =~ m:(.*)/+([^/]*):; | ||
| 61 | $self->{ftp}->cwd($dir) or do { | ||
| 62 | $self->set_error($self->{ftp}->message); | ||
| 63 | return; | ||
| 64 | }; | ||
| 65 | return(defined($self->{ftp}->mdtm($filename))); | ||
| 66 | } | ||
| 67 | |||
| 68 | 1; | ||
| 69 | |||
| 70 | __END__ |