Bug 17600: Standardize our EXPORT_OK
[srvgit] / Koha / REST / V1 / Static.pm
1 package Koha::REST::V1::Static;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Mojo::Base 'Mojolicious::Controller';
21
22 use Try::Tiny qw( catch try );
23
24 =head1 API
25
26 =head2 Class methods
27
28 =head3 get
29
30 Method that gets file contents
31
32 =cut
33
34 sub get {
35     my $self = shift;
36     my $c = $self->openapi->valid_input or return;
37
38     if ( C4::Context->config("enable_plugins") )
39     {
40         my $path = $c->req->url->path->leading_slash(1);
41
42         return $c->render(status => 400, openapi => { error => 'Endpoint inteded for plugin static files' }) unless "$path" =~ /^\/api\/v1\/contrib/;
43
44         my $namespace = $path->[3];
45
46         my $checkpath = '/api/v1/contrib/'.$namespace.'/static';
47
48         return $c->render(status => 400, openapi => { error => 'Endpoint inteded for plugin static files' }) unless "$path" =~ /\Q$checkpath/;
49
50         my @plugins = Koha::Plugins->new()->GetPlugins(
51             {
52                 method => 'api_namespace',
53             }
54         );
55
56         @plugins = grep { $_->api_namespace eq $namespace} @plugins;
57
58         return $c->render({ status => 404, openapi => { error => 'File not found' } }) unless scalar(@plugins) > 0;
59         return $c->render({ status => 500, openapi => { error => 'Namespace not unique' } }) unless scalar(@plugins) == 1;
60
61         my $plugin = $plugins[0];
62
63         my $basepath = $plugin->bundle_path;
64
65         my $relpath = join ('/', splice (@$path, 5));
66
67         return try {
68             my $asset = Mojo::Asset::File->new(path => join('/', $basepath, $relpath));
69             return $c->render({ status => 404, openapi => { error => 'File not found' } }) unless $asset->is_file;
70             return $c->reply->asset($asset);
71         }
72         catch {
73             return $c->render({ status => 404, openapi => { error => 'File not found' } });
74         }
75
76     } else {
77         $c->render({ status => 500, openapi => { error => 'Plugins are not enabled' } })
78     }
79
80
81 }
82
83 1;