1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
package SReview::Files::Access::HTTP;
use Moose;
use Carp;
use Mojo::UserAgent;
use File::Temp qw/tempfile tempdir mktemp/;
extends 'SReview::Files::Access::Base';
has '+filename' => (
predicate => 'has_download',
);
has 'workdir' => (
is => 'ro',
lazy => 1,
builder => '_get_workdir',
);
sub _get_workdir {
return tempdir(CLEANUP => 1);
}
sub _get_file {
my $self = shift;
my @parts = split('\.', $self->relname);
my $ext = pop(@parts);
my $dir = $self->workdir;
if($self->has_data) {
if($self->download_verbose) {
print "Downloading " . $self->url . "\n";
}
my ($fh, $file) = tempfile("http-XXXXXX", dir => $dir, SUFFIX => ".$ext");
my $ua = Mojo::UserAgent->new;
my $res = $ua->get($self->url)->result;
if($res->is_success) {
$res->save_to($file);
return $file;
} else {
croak "could not download file:" . $res->message;
}
} else {
croak "Can't create files with the HTTP access method";
}
}
sub _probe_basepath {
return shift->workdir;
}
sub DEMOLISH {
my $self = shift;
if($self->has_download) {
if($self->download_verbose) {
print "Deleting " . $self->filename . "\n";
}
unlink($self->filename);
}
}
1;
|