1 package Koha::REST::V1::Static;
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 use Mojo::Base 'Mojolicious::Controller';
30 Mehtod that gets file contents
36 my $c = $self->openapi->valid_input or return;
38 if ( C4::Context->preference('UseKohaPlugins')
39 && C4::Context->config("enable_plugins") )
41 my $path = $c->req->url->path->leading_slash(1);
43 return $c->render(status => 400, openapi => { error => 'Endpoint inteded for plugin static files' }) unless "$path" =~ /^\/api\/v1\/contrib/;
45 my $namespace = $path->[3];
47 my $checkpath = '/api/v1/contrib/'.$namespace.'/static';
49 return $c->render(status => 400, openapi => { error => 'Endpoint inteded for plugin static files' }) unless "$path" =~ /\Q$checkpath/;
51 my @plugins = Koha::Plugins->new()->GetPlugins(
53 method => 'api_namespace',
57 @plugins = grep { $_->api_namespace eq $namespace} @plugins;
59 return $c->render({ status => 404, openapi => { error => 'File not found' } }) unless scalar(@plugins) > 0;
60 return $c->render({ status => 500, openapi => { error => 'Namespace not unique' } }) unless scalar(@plugins) == 1;
62 my $plugin = $plugins[0];
64 my $basepath = $plugin->bundle_path;
66 my $relpath = join ('/', splice (@$path, 5));
69 my $asset = Mojo::Asset::File->new(path => join('/', $basepath, $relpath));
70 return $c->render({ status => 404, openapi => { error => 'File not found' } }) unless $asset->is_file;
71 return $c->reply->asset($asset);
74 return $c->render({ status => 404, openapi => { error => 'File not found' } });
78 $c->render({ status => 500, openapi => { error => 'Plugins are not enabled' } })