image_dl.pm: Improve error handling
This commit is contained in:
@@ -31,8 +31,28 @@ unless (-p $c->{image_dl}->{fifo}) {
|
|||||||
sub image_dl_queue ($) {
|
sub image_dl_queue ($) {
|
||||||
my $str = shift;
|
my $str = shift;
|
||||||
chomp($str);
|
chomp($str);
|
||||||
sysopen (my $h, $c->{image_dl}->{fifo}, O_WRONLY) or return ('500', 'text/plain', "Unable to write url to queue: $!\n");
|
$str .= "\n";
|
||||||
syswrite $h, "$str\n", length("$str\n");
|
|
||||||
|
sysopen (my $h, $c->{image_dl}->{fifo}, O_WRONLY) or do {
|
||||||
|
warn "Unable to open $c->{image_dl}->{fifo}: $!";
|
||||||
|
return ('500', 'text/plain', "Unable to write url to queue: $!\n");
|
||||||
|
};
|
||||||
|
|
||||||
|
use bytes;
|
||||||
|
my $len = length($str);
|
||||||
|
no bytes;
|
||||||
|
|
||||||
|
my $res = syswrite($h, $str, $len) or do {
|
||||||
|
warn "Unable to put download to queue: $!";
|
||||||
|
return('500', 'text/plain', "Unable to put download to queue\n");
|
||||||
|
};
|
||||||
|
|
||||||
|
if ($res != $len) {
|
||||||
|
close $h;
|
||||||
|
warn "Incorrect amount of bytes written to queue: $!";
|
||||||
|
return('500', 'text/plain', "Incorrect amount of bytes written to to queue\n");
|
||||||
|
}
|
||||||
|
|
||||||
close $h;
|
close $h;
|
||||||
return('200', 'text/plain', "Download queued\n");
|
return('200', 'text/plain', "Download queued\n");
|
||||||
}
|
}
|
||||||
@@ -41,7 +61,7 @@ sub __urlencode($) {
|
|||||||
my $str = shift;
|
my $str = shift;
|
||||||
my $urlobj = url $str;
|
my $urlobj = url $str;
|
||||||
$str = $urlobj->as_string;
|
$str = $urlobj->as_string;
|
||||||
undef $urlobj;
|
$urlobj = undef; undef $urlobj;
|
||||||
return $str;
|
return $str;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,7 +73,7 @@ sub __is_picture($) {
|
|||||||
eval {
|
eval {
|
||||||
my $http = HTTP::Tiny->new();
|
my $http = HTTP::Tiny->new();
|
||||||
$r = $http->request('HEAD', $url);
|
$r = $http->request('HEAD', $url);
|
||||||
undef $http;
|
$http = undef; undef $http;
|
||||||
};
|
};
|
||||||
|
|
||||||
return undef if (defined($@) && $@ ne ''); # means eval with error
|
return undef if (defined($@) && $@ ne ''); # means eval with error
|
||||||
@@ -82,7 +102,7 @@ sub __dlfunc(@) {
|
|||||||
eval {
|
eval {
|
||||||
my $http1 = HTTP::Tiny->new();
|
my $http1 = HTTP::Tiny->new();
|
||||||
$http1->mirror($url, $file);
|
$http1->mirror($url, $file);
|
||||||
undef $http1;
|
$http1 = undef; undef $http1;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -104,7 +124,14 @@ sub __image_dl_subthread {
|
|||||||
|
|
||||||
if ($extension) {
|
if ($extension) {
|
||||||
my $savepath = $c->{image_dl}->{dir};
|
my $savepath = $c->{image_dl}->{dir};
|
||||||
mkdir ($savepath) unless (-d $savepath);
|
|
||||||
|
unless (-d $savepath) {
|
||||||
|
mkdir ($savepath) or do {
|
||||||
|
warn "[FATA] Unable to create $savepath: $!";
|
||||||
|
next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
my $fname = $str;
|
my $fname = $str;
|
||||||
$fname =~ s/[^\w!., -#]/_/g;
|
$fname =~ s/[^\w!., -#]/_/g;
|
||||||
$savepath = sprintf("%s/%s.%s", $savepath, $fname, $extension);
|
$savepath = sprintf("%s/%s.%s", $savepath, $fname, $extension);
|
||||||
@@ -127,7 +154,7 @@ sub __image_dl_subthread {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sub image_dl_thread {
|
sub image_dl_thread {
|
||||||
open (my $h, "<", $c->{image_dl}->{fifo}) or die "Unable to open queue: $!\n";
|
open (my $h, "<", $c->{image_dl}->{fifo}) or die "[FATA] Unable to open queue: $!\n";
|
||||||
threads->create('__image_dl_subthread')->detach();
|
threads->create('__image_dl_subthread')->detach();
|
||||||
do {} while (! $ready);
|
do {} while (! $ready);
|
||||||
threads::yield;
|
threads::yield;
|
||||||
@@ -137,7 +164,7 @@ sub image_dl_thread {
|
|||||||
|
|
||||||
if ((! defined($str)) || ($str eq "\n")) {
|
if ((! defined($str)) || ($str eq "\n")) {
|
||||||
close $h;
|
close $h;
|
||||||
open ($h, "<", $c->{image_dl}->{fifo}) or die "Unable to open queue: $!\n";
|
open ($h, "<", $c->{image_dl}->{fifo}) or die "[FATA] Unable to open queue $c->{image_dl}->{fifo}: $!";
|
||||||
next;
|
next;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -161,7 +188,7 @@ sub image_dl_thread {
|
|||||||
}
|
}
|
||||||
|
|
||||||
close $h;
|
close $h;
|
||||||
open ($h, "<", $c->{image_dl}->{fifo}) or die "Unable to open queue: $!\n";
|
open ($h, "<", $c->{image_dl}->{fifo}) or die "[FATA] Unable to open queue $c->{image_dl}->{fifo}: $!\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
close $h;
|
close $h;
|
||||||
|
|||||||
Reference in New Issue
Block a user