From aa3e60d188824d1b0aad31b5bca43b12008685df Mon Sep 17 00:00:00 2001 From: Lucas Date: Tue, 21 Mar 2023 21:43:42 +0000 Subject: [PATCH] backend: make SeaweedFS::Client::url_for accept a scalar too --- lib/PoorBooru/Backend/Controller/Media.pm | 8 ++------ lib/PoorBooru/Backend/Controller/Tags.pm | 5 +---- lib/SeaweedFS/Client.pm | 14 ++++++++++++-- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/lib/PoorBooru/Backend/Controller/Media.pm b/lib/PoorBooru/Backend/Controller/Media.pm index 03335d3..6ad7c01 100644 --- a/lib/PoorBooru/Backend/Controller/Media.pm +++ b/lib/PoorBooru/Backend/Controller/Media.pm @@ -5,7 +5,6 @@ use warnings; use Mojo::Base "Mojolicious::Controller"; use Mojo::JSON qw(decode_json); -use SeaweedFS::FID; sub list ($self) { @@ -24,9 +23,7 @@ sub list ($self) $self->title("Pooru~"); my @media = map +{ media_link => $self->url_for("media_show", media_id => $_->{media_id}), - media_src => $self->seaweed->url_for( - SeaweedFS::FID->from_string($_->{seaweedfs_fid}) - ), + media_src => $self->seaweed->url_for($_->{seaweedfs_fid}), }, $res->json->{media}->@*; return $self->render( @@ -51,8 +48,7 @@ sub show ($self) return $self->render(status => $res->code) if $res->is_error; my $media = $res->json; - $media->{download} = $self->seaweed->url_for(SeaweedFS::FID - ->from_string($media->{download})); + $media->{download} = $self->seaweed->url_for($media->{download}); return $self->render( template => "media", diff --git a/lib/PoorBooru/Backend/Controller/Tags.pm b/lib/PoorBooru/Backend/Controller/Tags.pm index 381d623..e941a9d 100644 --- a/lib/PoorBooru/Backend/Controller/Tags.pm +++ b/lib/PoorBooru/Backend/Controller/Tags.pm @@ -5,7 +5,6 @@ use warnings; use Mojo::Base "Mojolicious::Controller"; use Mojo::JSON qw(decode_json); -use SeaweedFS::FID; sub list ($self) { @@ -51,9 +50,7 @@ sub show ($self) $self->title($res->json->{name}); my @media = map +{ media_link => $self->url_for("media_show", media_id => $_->{media_id}), - media_src => $self->seaweed->url_for( - SeaweedFS::FID->from_string($_->{seaweedfs_fid}) - ), + media_src => $self->seaweed->url_for($_->{seaweedfs_fid}), }, $res->json->{media}->@*; return $self->render( diff --git a/lib/SeaweedFS/Client.pm b/lib/SeaweedFS/Client.pm index 023672d..b1c745c 100644 --- a/lib/SeaweedFS/Client.pm +++ b/lib/SeaweedFS/Client.pm @@ -5,7 +5,7 @@ use warnings; use Carp; use List::Util qw(sample); -use Scalar::Util qw(openhandle); +use Scalar::Util qw(blessed); use Mojo::UserAgent; use SeaweedFS::FID; @@ -30,8 +30,18 @@ sub get sub url_for { - my ($self, $fid) = @_; + my ($self, $p) = @_; + 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); + } my $vid = $fid->volume_id; if (!defined($self->{_vurls}{$vid}) || $self->{_vurls}{$vid}->@* == 0) {