View | Details | Raw Unified | Return to bug 22835
Collapse All | Expand All

(-)a/Koha/REST/Plugin/PluginRoutes.pm (-12 / +29 lines)
Lines 50-60 sub register { Link Here
50
    {
50
    {
51
        @plugins = Koha::Plugins->new()->GetPlugins(
51
        @plugins = Koha::Plugins->new()->GetPlugins(
52
            {
52
            {
53
                method => 'api_routes',
53
                method => 'api_namespace',
54
            }
54
            }
55
        );
55
        );
56
        # plugin needs to define a namespace
56
        # plugin needs to define a namespace
57
        @plugins = grep { $_->api_namespace } @plugins;
57
        #@plugins = grep { $_->api_namespace } @plugins;
58
    }
58
    }
59
59
60
    foreach my $plugin ( @plugins ) {
60
    foreach my $plugin ( @plugins ) {
Lines 98-118 sub inject_routes { Link Here
98
sub merge_spec {
98
sub merge_spec {
99
    my ( $spec, $plugin ) = @_;
99
    my ( $spec, $plugin ) = @_;
100
100
101
    my $plugin_spec = $plugin->api_routes;
101
    if($plugin->can('api_routes')) {
102
        my $plugin_spec = $plugin->api_routes;
102
103
103
    foreach my $route ( keys %{ $plugin_spec } ) {
104
        foreach my $route ( keys %{ $plugin_spec } ) {
105
            my $THE_route = '/contrib/' . $plugin->api_namespace . $route;
106
            if ( exists $spec->{ $THE_route } ) {
107
                # Route exists, overwriting is forbidden
108
                Koha::Exceptions::Plugin::ForbiddenAction->throw(
109
                    "Attempted to overwrite $THE_route"
110
                );
111
            }
104
112
105
        my $THE_route = '/contrib/' . $plugin->api_namespace . $route;
113
            $spec->{'paths'}->{ $THE_route } = $plugin_spec->{ $route };
106
        if ( exists $spec->{ $THE_route } ) {
107
            # Route exists, overwriting is forbidden
108
            Koha::Exceptions::Plugin::ForbiddenAction->throw(
109
                "Attempted to overwrite $THE_route"
110
            );
111
        }
114
        }
112
113
        $spec->{'paths'}->{ $THE_route } = $plugin_spec->{ $route };
114
    }
115
    }
115
116
117
    if($plugin->can('static_routes')) {
118
        my $plugin_spec = $plugin->static_routes;
119
120
        foreach my $route ( keys %{ $plugin_spec } ) {
121
122
            my $THE_route = '/contrib/' . $plugin->api_namespace . '/static'.$route;
123
            if ( exists $spec->{ $THE_route } ) {
124
                # Route exists, overwriting is forbidden
125
                Koha::Exceptions::Plugin::ForbiddenAction->throw(
126
                    "Attempted to overwrite $THE_route"
127
                );
128
            }
129
130
            $spec->{'paths'}->{ $THE_route } = $plugin_spec->{ $route };
131
        }
132
    }
116
    return $spec;
133
    return $spec;
117
}
134
}
118
135
(-)a/Koha/REST/V1/Static.pm (-1 / +90 lines)
Line 0 Link Here
0
- 
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 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
8
# version.
9
#
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.
13
#
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.
17
18
use Modern::Perl;
19
20
use Mojo::Base 'Mojolicious::Controller';
21
22
use Try::Tiny;
23
24
=head1 API
25
26
=head2 Class methods
27
28
=head3 get
29
30
Mehtod 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->preference('UseKohaPlugins')
39
        && C4::Context->config("enable_plugins") )
40
    {
41
        my $path = $c->req->url->path->leading_slash(1);
42
43
        return $c->render(status => 400, openapi => { error => 'Endpoint inteded for plugin static files' }) unless "$path" =~ /^\/api\/v1\/contrib/;
44
45
        my $namespace = $path->[3];
46
47
        my $checkpath = '/api/v1/contrib/'.$namespace.'/static';
48
49
        return $c->render(status => 400, openapi => { error => 'Endpoint inteded for plugin static files' }) unless "$path" =~ /\Q$checkpath/;
50
51
        my @plugins = Koha::Plugins->new()->GetPlugins(
52
            {
53
                method => 'api_namespace',
54
            }
55
        );
56
57
        @plugins = grep { $_->api_namespace eq $namespace} @plugins;
58
        warn scalar(@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;
61
62
        my $plugin = $plugins[0];
63
64
        my $basepath = $plugin->bundle_path;
65
66
        warn $basepath;
67
68
        my $relpath = join ('/', splice (@$path, 5));
69
70
        warn $relpath;
71
72
        warn join('/', $basepath, $relpath);
73
        return try {
74
            my $asset = Mojo::Asset::File->new(path => join('/', $basepath, $relpath));
75
            return $c->render({ status => 404, openapi => { error => 'File not found' } }) unless $asset->is_file;
76
            # $c->res->headers->content_type("image/jpeg");
77
            return $c->reply->asset($asset);
78
        }
79
        catch {
80
            return $c->render({ status => 404, openapi => { error => 'File not found' } });
81
        }
82
83
    } else {
84
        $c->render({ status => 500, openapi => { error => 'Plugins are not enabled' } })
85
    }
86
87
88
}
89
90
1;

Return to bug 22835