IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 5326


Ignore:
Timestamp:
Oct 13, 2005, 4:35:42 PM (21 years ago)
Author:
jhoblitt
Message:

split get_file() into get_file() & get_file_by_record()

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/PS-IPP-PSFTP/lib/PS/IPP/PSFTP.pm

    r5300 r5326  
    11# Copyright (C) 2005  Joshua Hoblitt
    22#
    3 # $Id: PSFTP.pm,v 1.7 2005-10-12 22:45:38 jhoblitt Exp $
     3# $Id: PSFTP.pm,v 1.8 2005-10-14 02:35:42 jhoblitt Exp $
    44
    55package PS::IPP::PSFTP;
     
    1919use POSIX;
    2020use PS::IPP::PSFTP::Parser;
     21use Regexp::Common qw( number );
    2122
    2223sub new
     
    106107}
    107108
    108 sub get_file
     109sub get_file_by_record
    109110{
    110111    my $self = shift;
     
    127128    );
    128129
     130    return $self->get_file(
     131        url         => $p{record}->url,
     132        size        => $p{record}->size,
     133        md5         => $p{record}->md5,
     134        filename    => $p{filename},
     135    );
     136}
     137
     138sub get_file
     139{
     140    my $self = shift;
     141
     142    my %p = validate(@_,
     143        {
     144            url         => {
     145                type        => SCALAR,
     146                callbacks   => {
     147                    'is valid url' => sub { is_uri( $_[0] ) },
     148                },
     149            },
     150            size        => {
     151                type        => SCALAR,
     152                regex       => qr/^$RE{num}{int}$/,
     153                optional    => 1,
     154            },
     155            md5         => {
     156                type        => SCALAR,
     157                regex       => qr/^$RE{num}{real}{-base => 16}$/,
     158                optional    => 1,
     159            },
     160            filename    => {
     161                type        => SCALAR,
     162                regex       => qr/\S+/, # string with atleast 1 non WS char
     163                callbacks   => {
     164                    'write access' => sub {
     165                        POSIX::access($_[0], &POSIX::W_OK);
     166                    },
     167                },
     168            },
     169        },
     170    );
     171
    129172    my $ua = LWP::UserAgent->new;
    130     my $request = HTTP::Request->new(GET => $p{record}->url);
     173    my $request = HTTP::Request->new(GET => $p{url});
    131174    my $response = $ua->request($request, $p{filename});
    132175
     
    136179    }
    137180
    138     my $size = stat($p{filename})->size;
    139     if (! $p{record}->size == $size) {
    140         carp "id: ", $p{record}->id,
    141              " - expected size: ", $p{record}->size,
    142              " got: ", $size;
    143         return undef;
    144     }
    145 
    146     my $md5 = file_md5_hex($p{filename});
    147     if (! $p{record}->md5 eq $md5) {
    148         carp "id: ", $p{record}->id,
    149              " - expected md5: ", $p{record}->md5,
    150              " got: ", $md5;
    151         return undef;
     181    if (defined $p{size}) {
     182        my $size = stat($p{filename})->size;
     183        if (! $p{size} == $size) {
     184            carp "url: ", $p{url},
     185                 " - expected size: ", $p{size},
     186                 " got: ", $size;
     187            return undef;
     188        }
     189    }
     190
     191    if (defined $p{md5}) {
     192        my $md5 = file_md5_hex($p{filename});
     193        if (! $p{md5} eq $md5) {
     194            carp "url: ", $p{url},
     195                 " - expected md5: ", $p{md5},
     196                 " got: ", $md5;
     197            return undef;
     198        }
    152199    }
    153200
Note: See TracChangeset for help on using the changeset viewer.