IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 5300


Ignore:
Timestamp:
Oct 12, 2005, 12:45:38 PM (21 years ago)
Author:
jhoblitt
Message:

add get_file()

File:
1 edited

Legend:

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

    r5283 r5300  
    11# Copyright (C) 2005  Joshua Hoblitt
    22#
    3 # $Id: PSFTP.pm,v 1.6 2005-10-12 02:58:03 jhoblitt Exp $
     3# $Id: PSFTP.pm,v 1.7 2005-10-12 22:45:38 jhoblitt Exp $
    44
    55package PS::IPP::PSFTP;
     
    1313use Carp qw( carp );
    1414use Data::Validate::URI qw( is_uri );
     15use Digest::MD5::File qw( file_md5_hex );
     16use File::stat;
    1517use LWP::UserAgent;
    1618use Params::Validate qw( validate SCALAR );
     19use POSIX;
    1720use PS::IPP::PSFTP::Parser;
    1821
     
    7275    my %p = validate(@_,
    7376        {
    74             id => {
     77            id          => {
    7578                type        => SCALAR,
    7679                regex       => qr/\S+/, # string with atleast 1 non WS char
    7780            },
    78             discarded => {
     81            discarded   => {
    7982                type        => SCALAR,
    8083                callbacks   => {
     
    103106}
    104107
     108sub get_file
     109{
     110    my $self = shift;
     111
     112    my %p = validate(@_,
     113        {
     114            record      => {
     115                isa         => 'PS::IPP::PSFTP::Record',
     116            },
     117            filename    => {
     118                type        => SCALAR,
     119                regex       => qr/\S+/, # string with atleast 1 non WS char
     120                callbacks   => {
     121                    'write access' => sub {
     122                        POSIX::access($_[0], &POSIX::W_OK);
     123                    },
     124                },
     125            },
     126        },
     127    );
     128
     129    my $ua = LWP::UserAgent->new;
     130    my $request = HTTP::Request->new(GET => $p{record}->url);
     131    my $response = $ua->request($request, $p{filename});
     132
     133    if (! $response->is_success) {
     134        carp $response->status_line;
     135        return undef;
     136    }
     137
     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;
     152    }
     153
     154    return 1;
     155}
     156
    1051571;
    106158
Note: See TracChangeset for help on using the changeset viewer.