-1

I'm trying to create a hash of a file that i have downloaded using the module File::Fetch. I'm trying to store the downloaded file name and its hash in the database as a record. However, none of the methods of File::Fetch objects provide the downloaded file. Is there any way to download the file into a variable in order to create a hash of it?

Here is a snippet of my code,

eval{
    $uri_handle = File::Fetch->new(uri => $nurl) or die "Could'nt create fetch object\n";
    $getfile = $uri_handle ->output_file or die "There is no file to be fetched\n";
    my $dir_handle = $uri_handle->fetch( to => $dir2 ) or die "Couldn't fetch file: $uri_handle->error\n";
    # print "$getfile was successfully saved to $dir\n";
};
if ($@){
        print "\n There is no file at $url\n\n";            
}else{              
    my $file_data_handle = DBI->connect($database_connection_string,$database_user,$database_pass) or die "Couldn't open database: $DBI::errstr\n";
    my $file_statement_handle = $file_data_handle->prepare('insert into files (filename,filehash,sourceurl,originalurl) VALUES ($getfile,$filehash,$nurl,$url)') or die "Couldn't prepare statement: $DBI::errstr\n";
    $file_statement_handle->execute() or die "Couldn't execute statement: $DBI::errstr\n";
    $file_data_handle->disconnect();
}
7
  • Not entirely related nitpick: proper exception handling does not use if ($@)... for error detection but the result of eval itself. A common idiom is something like eval { ...; 1; } or do { print "Exception occured: $@\n"; };
    – user507077
    Commented Dec 3, 2012 at 8:59
  • And according to the documentation you've linked to you can get to the locally stored file's name: $ff->file The name of the remote file. For the local file name, the result of $ff->output_file will be used.
    – user507077
    Commented Dec 3, 2012 at 9:01
  • Actually i was making use of any error that code within eval probably throws to be the result of no file being available at the particular url, then just skipping the grab entirely. On the other hand, if there was a file downloaded, i wanted to grab the hash of it, but there file fetch doesnt hold the file, it sends it directly to a folder specified by my variable $dir2
    – Marcus Lim
    Commented Dec 3, 2012 at 9:04
  • But $url_handle should hold that file's name, so just use that name, open the local file and read its content.
    – user507077
    Commented Dec 3, 2012 at 9:07
  • Okay, i shall try that approach first . Thanks
    – Marcus Lim
    Commented Dec 3, 2012 at 9:12

1 Answer 1

2

It writes the result to disk directly. Use LWP instead.

use Digest::MD5    qw( md5_hex );
use LWP::UserAgent qw( );

my $ua = LWP::UserAgent->new();
my $response = $ua->get($url);
die $response->status_line if !$response->is_success;
my $file = $response->decoded_content( charset => 'none' );
my $md5_hex = md5_hex($file);
1
  • Replace die with next and you don't need an eval.
    – ikegami
    Commented Dec 3, 2012 at 9:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.