backend: implement /media/:media_id

This commit is contained in:
Lucas 2023-03-17 19:58:12 +00:00
parent aa5e4ffe29
commit 72412a7889
3 changed files with 57 additions and 0 deletions

View File

@ -43,6 +43,8 @@ sub startup ($self)
$r->get("/")->to("gallery#index"); $r->get("/")->to("gallery#index");
$r->get("/media/:media_id")->to("media#show");
$r->get("/tags")->to("tags#list"); $r->get("/tags")->to("tags#list");
$r->get("/tag/:tag_id_or_name")->to("tags#show"); $r->get("/tag/:tag_id_or_name")->to("tags#show");
} }

View File

@ -0,0 +1,35 @@
package PoorBooru::Backend::Controller::Media;
use v5.36;
use strict;
use warnings;
use Mojo::Base "Mojolicious::Controller";
use Mojo::JSON qw(decode_json);
use SeaweedFS::FID;
sub show ($self)
{
my $v = $self->validation;
my $page = $v->optional("page")->num(1, undef)->param // 1;
return $self->render(status => 404) if $v->has_error;
my $media_id = $self->stash("media_id");
my $poorbooru_api = $self->config("poorbooru_api");
my $res = $self->ua->get("$poorbooru_api/media/$media_id?page=$page")
->result;
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}));
return $self->render(
template => "media",
media => $media,
#pager => { $self->pager_links($res->json->{pager}) },
);
};
1;

20
templates/media.html.ep Normal file
View File

@ -0,0 +1,20 @@
% layout "main";
<p class="text-center">
<%= link_to $media->{download} => begin %>
<%= image $media->{download} %>
<% end %>
</p>
<dl>
<dt>ID</dt>
<dd><%= $media->{id} %></dd>
<dt>Upload name</dt>
<dd><%= $media->{name} %></dd>
<dt>Content-Type</dt>
<dd><%= $media->{content_type} %></dd>
<dt>Tags</dt>
<% for my $tag ($media->{tags}->@*) { %>
<dd><%= $tag %></dd>
<% } %>
</dl>