2023-03-05 18:23:13 +01:00
|
|
|
package SeaweedFS::Client;
|
|
|
|
use v5.16;
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
use Carp;
|
|
|
|
use List::Util qw(sample);
|
2023-03-21 22:43:42 +01:00
|
|
|
use Scalar::Util qw(blessed);
|
2023-03-05 18:23:13 +01:00
|
|
|
|
|
|
|
use Mojo::UserAgent;
|
|
|
|
use SeaweedFS::FID;
|
|
|
|
|
|
|
|
our $VERSION = v0.0.1;
|
|
|
|
|
|
|
|
sub new
|
|
|
|
{
|
|
|
|
my ($class, $murl) = @_;
|
|
|
|
return bless {
|
|
|
|
_murl => $murl,
|
|
|
|
_ua => Mojo::UserAgent->new,
|
|
|
|
_vurls => {},
|
|
|
|
}, $class;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub get
|
|
|
|
{
|
|
|
|
my ($self, $target_fid) = @_;
|
|
|
|
return $self->{_ua}->get($self->url_for($target_fid));
|
|
|
|
}
|
|
|
|
|
|
|
|
sub url_for
|
|
|
|
{
|
2023-03-21 22:43:42 +01:00
|
|
|
my ($self, $p) = @_;
|
2023-03-05 18:23:13 +01:00
|
|
|
|
2023-03-21 22:43:42 +01:00
|
|
|
my $fid;
|
|
|
|
if (defined(blessed $p) && $p->can("volume_id")) {
|
|
|
|
$fid = $p;
|
|
|
|
} elsif (!defined(blessed $p)) {
|
|
|
|
eval {
|
|
|
|
$fid = SeaweedFS::FID->from_string($p);
|
|
|
|
} or $fid = SeaweedFS::FID->from_bin($p);
|
|
|
|
} else {
|
|
|
|
croak "Unsupported FID type " . blessed($p);
|
|
|
|
}
|
2023-03-05 18:23:13 +01:00
|
|
|
my $vid = $fid->volume_id;
|
|
|
|
|
2023-03-21 22:43:36 +01:00
|
|
|
if (!defined($self->{_vurls}{$vid}) || $self->{_vurls}{$vid}->@* == 0) {
|
2023-03-05 18:23:13 +01:00
|
|
|
my $res = $self->{_ua}
|
|
|
|
->get("$self->{_murl}/dir/lookup?fileId=$fid&read=yes")
|
|
|
|
->result;
|
|
|
|
|
|
|
|
$self->{_vurls}{$vid} = [
|
2023-03-21 22:43:36 +01:00
|
|
|
map { $_->{publicUrl} } $res->json->{locations}->@*
|
2023-03-05 18:23:13 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2023-03-21 22:43:36 +01:00
|
|
|
my $url = sample(1, $self->{_vurls}{$vid}->@*);
|
2023-03-05 18:23:13 +01:00
|
|
|
return undef unless defined $url;
|
|
|
|
|
|
|
|
$url = "http://$url" if $url !~ m{^https?://};
|
|
|
|
return "$url/$fid";
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|