AnyEvent-FTP

 view release on metacpan or  search on metacpan

example/fput.pl  view on Meta::CPAN

$ftp->connect($remote->host, $remote->port)->recv;
$ftp->login($remote->user, $remote->password)->recv;
$ftp->type('I')->recv;

if(defined $remote->path)
{
  $ftp->cwd($remote->path)->recv;
}

open my $fh, '<', $local;
binmode $fh;

my $buffer;
my $count;

my $pb;

my $xfer = $ftp->stor($local->basename);

$xfer->on_open(sub {
  my $whandle = shift;

lib/AnyEvent/FTP/Client.pm  view on Meta::CPAN


 my $local;
 $client->retr('foo.txt', \$local);

=item file handle

The content of the remote file will be written into the local file handle as it is
received

 open my $fh, '>', 'foo.txt';
 binmode $fh;
 $client->retr('foo.txt', $fh);

=item the name of the local file

If C<$local> is just a regular non reference scalar, then it will be treated as the
local filename, which will be created and written to as data is received from the
server.

 $client->retr('foo.txt', 'foo.txt');

lib/AnyEvent/FTP/Client.pm  view on Meta::CPAN

=back

In order to resume a transfer, you need to include the C<restart> option after the
C<$local> argument.  Here is an example:

 # assumes foo.txt (partial download) exists in the current
 # loacal directory and foo.txt (full file) exists in the
 # current remote directory.
 my $filename = 'foo.txt';
 open my $fh, '>>', $filename;
 binmode $fh;
 $client->retr($filename, $fh, restart => tell $fh);

=head2 stor

 $client->stor($filename, $local);

Send a file to the server with the given remote filename (C<$filename>)
and using C<$local> as a source.

Returns an instance of L<AnyEvent::FTP::Client::Transfer>, which supports the

lib/AnyEvent/FTP/Client.pm  view on Meta::CPAN

The contents of the file will be retrieved from the scalar referred to by the reference.

 my $local = 'some data for foo.txt';
 $client->stor('foo.txt', \$local);

=item file handle

The contents of the file will be read from the file handle.

 open my $fh, '<', 'foo.txt';
 binmode $fh;
 $client->stor('foo.txt', $fh);

=item the name of the local file

If C<$local> is just a regular non reference scalar, then it will be treated as the
local filename, which will be opened and read from in order to create the file on
the server.

 $client->stor('foo.txt', 'foo.txt');

lib/AnyEvent/FTP/Client.pm  view on Meta::CPAN

upload to the remote FTP server would be to open the local file, determine the remote
file's size and seek to that position in the local file and use the C<appe> method
with C<$local> as that file handle, as in this example:

 # assume that foo.txt is in the current local dir
 # and the remote local dir
 my $filename = "foo.txt";
 $client->size($filename)->cb(sub {
   my $size = shift->recv;
   open my $fh, '<', $filename;
   binmode $fh;
   seek $fh, $size, 0;
   $client->appe($filename, $fh);
 });

Note that the C<SIZE> command is an extension to FTP, and may not be available on all
servers.

=head2 list

 $client->list($location)

lib/AnyEvent/FTP/Client.pm  view on Meta::CPAN

 $ftp->connect($remote->host, $remote->port)->recv;
 $ftp->login($remote->user, $remote->password)->recv;
 $ftp->type('I')->recv;
 
 if(defined $remote->path)
 {
   $ftp->cwd($remote->path)->recv;
 }
 
 open my $fh, '<', $local;
 binmode $fh;
 
 my $buffer;
 my $count;
 
 my $pb;
 
 my $xfer = $ftp->stor($local->basename);
 
 $xfer->on_open(sub {
   my $whandle = shift;

lib/AnyEvent/FTP/Server/Context/FSRW.pm  view on Meta::CPAN

    use autodie;
    local $CWD = $self->cwd;

    if(-f $fn)
    {
      # TODO: re-write so that this does not blocks
      my $type = $self->type eq 'A' ? 'ASCII' : 'Binary';
      my $size = -s $fn;
      $con->send_response(150 => "Opening $type mode data connection for $fn ($size bytes)");
      open my $fh, '<', $fn;
      binmode $fh, $self->_layer;
      seek $fh, $self->restart_offset, 0 if $self->restart_offset;
      $self->data->push_write(do { local $/; <$fh> });
      close $fh;
      $self->data->push_shutdown;
      $con->send_response(226 => 'Transfer complete');
    }
    elsif(-e $fn && !-d $fn)
    {
      $con->send_response(550 => 'Permission denied');
    }

lib/AnyEvent/FTP/Server/Context/FSRW.pm  view on Meta::CPAN

  }

  eval {
    use autodie;
    local $CWD = $self->cwd;

    my $type = $self->type eq 'A' ? 'ASCII' : 'Binary';
    $con->send_response(150 => "Opening $type mode data connection for $fn");

    open my $fh, '>', $fn;
    binmode $fh, $self->_layer;
    $self->data->on_read(sub {
      $self->data->push_read(sub {
        print $fh $_[0]{rbuf};
        $_[0]{rbuf} = '';
      });
    });
    $self->data->on_error(sub {
      close $fh;
      $self->data->push_shutdown;
      $con->send_response(226 => 'Transfer complete');

lib/AnyEvent/FTP/Server/Context/FSRW.pm  view on Meta::CPAN

  }

  eval {
    use autodie;
    local $CWD = $self->cwd;

    my $type = $self->type eq 'A' ? 'ASCII' : 'Binary';
    $con->send_response(150 => "Opening $type mode data connection for $fn");

    open my $fh, '>>', $fn;
    binmode $fh, $self->_layer;
    $self->data->on_read(sub {
      $self->data->push_read(sub {
        print $fh $_[0]{rbuf};
        $_[0]{rbuf} = '';
      });
    });
    $self->data->on_error(sub {
      close $fh;
      $self->data->push_shutdown;
      $con->send_response(226 => 'Transfer complete');

lib/AnyEvent/FTP/Server/Context/FSRW.pm  view on Meta::CPAN

      open $fh, '>', $fn;
    }
    else
    {
      ($fh,$fn) = tempfile( "aefXXXXXX", TMPDIR => 0 )
    }

    my $type = $self->type eq 'A' ? 'ASCII' : 'Binary';
    $con->send_response(150 => "FILE: $fn");

    binmode $fh, $self->_layer;
    $self->data->on_read(sub {
      $self->data->push_read(sub {
        print $fh $_[0]{rbuf};
        $_[0]{rbuf} = '';
      });
    });
    $self->data->on_error(sub {
      close $fh;
      $self->data->push_shutdown;
      $con->send_response(226 => 'Transfer complete');

t/anyevent_ftp_client.t  view on Meta::CPAN


      unlink $fn;
      ok !-e $fn, 'remote file deleted';

      do {
        my $data = 'some data';
        my $glob = do {
          my $dir = tempdir( CLEANUP => 1);
          my $fn = File::Spec->catfile($dir, 'flub.txt');
          open my $out, '>', $fn;
          binmode $out;
          print $out $data;
          close $out;
          open my $in, '<', $fn;
          binmode $in;
          $in;
        };
        my $ret = eval { $client->stor('foo.txt', $glob)->recv; };
        diag $@ if $@;
        isa_ok $ret, 'AnyEvent::FTP::Response';
        ok -e $fn, 'remote file created';
        my $remote = do {
          open my $fh, '<', $fn;
          local $/;
          <$fh>;

t/anyevent_ftp_client.t  view on Meta::CPAN


    prep_client( $client );

    $client->connect($config->{host}, $config->{port})->recv;
    $client->login($config->{user}, $config->{pass})->recv;
    $client->type('I')->recv;
    $client->cwd($config->{dir})->recv;

    do {
      open my $fh, '>', "$local/data.$passive";
      binmode $fh;
      print $fh "data$_\n" for 1..200;
      close $fh;
    };

    $client->stor("data.$passive", "$local/data.$passive")->recv;

    my $size = -s "$local/data.$passive";
    is $size && -s "$remote/data.$passive", $size, "size of remote file is $size";
    $size = $client->size("data.$passive")->recv;
    is $size, -s "$local/data.$passive", "size returned from remote file is correct";

    my $expected = do {
      open my $fh, '>>', "$local/data.$passive";
      binmode $fh;
      print $fh "xorxor$_\n" for 1..300;
      close $fh;

      open $fh, '<', "$local/data.$passive";
      binmode $fh;
      local $/;
      my $data = <$fh>;
      close $fh;
      $data;
    };

    do {
      open my $fh, '<', "$local/data.$passive";
      binmode $fh;
      seek $fh, $client->size("data.$passive")->recv, 0;
      $client->appe("data.$passive", $fh)->recv;
      close $fh;
    };

    $size = -s "$local/data.$passive";
    is $size && -s "$remote/data.$passive", $size, "size of remote file is $size";
    $size = $client->size("data.$passive")->recv;
    is $size, -s "$local/data.$passive", "size returned from remote file is correct";

    my $actual = do {
      open my $fh, '<', "$remote/data.$passive";
      binmode $fh;
      local $/;
      my $data = <$fh>;
      close $fh;
      $data;
    };

    is $actual, $expected, "files match";

    $client->quit->recv;
  }



( run in 0.291 second using v1.01-cache-2.11-cpan-3cd7ad12f66 )