| 1 | package Data::All::IO::FTP; | ||
| 2 | |||
| 3 | # $Id: FTP.pm,v 1.1.1.1 2005/05/10 23:56:20 dmandelbaum Exp $ | ||
| 4 | |||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | |||
| 9 | use Data::All::IO::Plain '-base'; | ||
| 10 | use Net::FTP::Common; | ||
| 11 | use File::Temp; | ||
| 12 | use IO::All; | ||
| 13 | |||
| 14 | our $VERSION = 0.11; | ||
| 15 | |||
| 16 | internal 'FTP'; | ||
| 17 | internal 'fp'; | ||
| 18 | internal 'ft_conf' => | ||
| 19 | { | ||
| 20 | TEMPLATE => "/tmp/data-all-$Data::All::VERSION-XXXXX", | ||
| 21 | SUFFIX => '.tmp' | ||
| 22 | }; | ||
| 23 | |||
| 24 | |||
| 25 | |||
| 26 | sub open($) | ||
| 27 | { | ||
| 28 | my $self = shift; | ||
| 29 | my $filepath; | ||
| 30 | |||
| 31 | #warn " -> Opening ftp connection ", $self->ioconf()->{'perm'}; | ||
| 32 | |||
| 33 | # Download and open a filehandle to files for reading. | ||
| 34 | # Create a temporary FH for writes. | ||
| 35 | $filepath = ($self->ioconf->{'perm'} eq 'w') | ||
| 36 | ? $self->_create_temp_filepath() | ||
| 37 | : $self->_get_file(); | ||
| 38 | |||
| 39 | my $fh = FileHandle->new($filepath, $self->ioconf()->{'perm'}); | ||
| 40 | my $IO = $fh; | ||
| 41 | |||
| 42 | |||
| 43 | $self->__IO( $IO ); | ||
| 44 | $self->__fh( $fh ); | ||
| 45 | $self->__fp( $filepath ); | ||
| 46 | |||
| 47 | $self->is_open(1); | ||
| 48 | |||
| 49 | $self->_extract_fields(); # Initialize field names | ||
| 50 | return $self->is_open(); | ||
| 51 | } | ||
| 52 | |||
| 53 | sub close() | ||
| 54 | { | ||
| 55 | my $self = shift; | ||
| 56 | $self->__IO()->close(); | ||
| 57 | $self->is_open(0); | ||
| 58 | |||
| 59 | $self->_put_file() if ($self->ioconf->{'perm'} eq 'w'); | ||
| 60 | |||
| 61 | #warn "Deleting temp file: ", $self->__fp(); | ||
| 62 | unlink $self->__fp(); | ||
| 63 | } | ||
| 64 | |||
| 65 | sub _create_temp_file() | ||
| 66 | # Create a temporary file and open it | ||
| 67 | { | ||
| 68 | my $self = shift; | ||
| 69 | |||
| 70 | my $fh = File::Temp->new(%{ $self->__ft_conf() }); | ||
| 71 | |||
| 72 | #warn "created temp file ". $fh->filename; | ||
| 73 | |||
| 74 | return $fh; | ||
| 75 | } | ||
| 76 | |||
| 77 | sub _create_temp_filepath() | ||
| 78 | # Create a temporary filename without creating the file | ||
| 79 | { | ||
| 80 | my $self = shift; | ||
| 81 | return mktemp($self->__ft_conf()->{'TEMPLATE'}); | ||
| 82 | } | ||
| 83 | |||
| 84 | sub _split_path_from_file() | ||
| 85 | # IN: /some/file/path.txt | ||
| 86 | # OUT: (/some/file, path.txt); | ||
| 87 | { | ||
| 88 | my $self = shift; | ||
| 89 | my $filepath = shift; | ||
| 90 | my @elements = split('/', $filepath); | ||
| 91 | my ($file, $path) = (pop(@elements), join('/', @elements)); | ||
| 92 | #warn "\n\nf:$file,p:$path"; | ||
| 93 | return ($path, $file); | ||
| 94 | } | ||
| 95 | |||
| 96 | sub _get_file() | ||
| 97 | # Get a file via FTP and save it to a temporary file for reading | ||
| 98 | { | ||
| 99 | my $self = shift; | ||
| 100 | my $filepath_tmp = $self->_create_temp_filepath(); | ||
| 101 | my ($lpath, $lfile) = $self->_split_path_from_file($filepath_tmp); | ||
| 102 | my ($rpath, $rfile) = $self->_split_path_from_file($self->path()->[0]); | ||
| 103 | |||
| 104 | # NOTE: The first param is sent by reference. See Net::FTP::Common docs | ||
| 105 | # for a not so valid explanation. | ||
| 106 | my $ftp = Net::FTP::Common->new($self->path()->[1], %{ $self->path()->[2] }); | ||
| 107 | |||
| 108 | $ftp->get( | ||
| 109 | RemoteDir => $rpath, | ||
| 110 | RemoteFile => $rfile, | ||
| 111 | LocalDir => $lpath, | ||
| 112 | LocalFile => $lfile | ||
| 113 | ); | ||
| 114 | |||
| 115 | undef $ftp; | ||
| 116 | |||
| 117 | return $filepath_tmp; | ||
| 118 | } | ||
| 119 | |||
| 120 | sub _put_file() | ||
| 121 | { | ||
| 122 | my $self = shift; | ||
| 123 | my $filepath_tmp = $self->__fp(); | ||
| 124 | my ($lpath, $lfile) = $self->_split_path_from_file($filepath_tmp); | ||
| 125 | my ($rpath, $rfile) = $self->_split_path_from_file($self->path()->[0]); | ||
| 126 | |||
| 127 | # NOTE: The first param is sent by reference. See Net::FTP::Common docs | ||
| 128 | # for a not so valid explanation. | ||
| 129 | my $ftp = Net::FTP::Common->new($self->path()->[1], %{ $self->path()->[2] }); | ||
| 130 | |||
| 131 | # "Don't use put!" according to perldoc Net::FTP::Common | ||
| 132 | $ftp->send( | ||
| 133 | RemoteDir => $rpath, | ||
| 134 | RemoteFile => $rfile, | ||
| 135 | LocalDir => $lpath, | ||
| 136 | LocalFile => $lfile | ||
| 137 | ); | ||
| 138 | |||
| 139 | undef $ftp; | ||
| 140 | } | ||
| 141 | |||
| 142 | |||
| 143 | 1; |