aboutsummaryrefslogtreecommitdiff
path: root/Jellyfin.Api/Controllers/PluginsController.cs
blob: 565bf23119103f054df3c6acd8e2bf4123c30aaa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net.Mime;
using System.Text.Json;
using System.Threading.Tasks;
using Jellyfin.Api.Attributes;
using Jellyfin.Api.Constants;
using Jellyfin.Api.Models.PluginDtos;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Json;
using MediaBrowser.Common.Plugins;
using MediaBrowser.Common.Updates;
using MediaBrowser.Model.Configuration;
using MediaBrowser.Model.Net;
using MediaBrowser.Model.Plugins;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Jellyfin.Api.Controllers
{
    /// <summary>
    /// Plugins controller.
    /// </summary>
    [Authorize(Policy = Policies.DefaultAuthorization)]
    public class PluginsController : BaseJellyfinApiController
    {
        private readonly IInstallationManager _installationManager;
        private readonly IPluginManager _pluginManager;
        private readonly IConfigurationManager _config;
        private readonly JsonSerializerOptions _serializerOptions;

        /// <summary>
        /// Initializes a new instance of the <see cref="PluginsController"/> class.
        /// </summary>
        /// <param name="installationManager">Instance of the <see cref="IInstallationManager"/> interface.</param>
        /// <param name="pluginManager">Instance of the <see cref="IPluginManager"/> interface.</param>
        /// <param name="config">Instance of the <see cref="IConfigurationManager"/> interface.</param>
        public PluginsController(
            IInstallationManager installationManager,
            IPluginManager pluginManager,
            IConfigurationManager config)
        {
            _installationManager = installationManager;
            _pluginManager = pluginManager;
            _serializerOptions = JsonDefaults.GetCamelCaseOptions();
            _config = config;
        }

        /// <summary>
        /// Get plugin security info.
        /// </summary>
        /// <response code="200">Plugin security info returned.</response>
        /// <returns>Plugin security info.</returns>
        [Obsolete("This endpoint should not be used.")]
        [HttpGet("SecurityInfo")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        public static ActionResult<PluginSecurityInfo> GetPluginSecurityInfo()
        {
            return new PluginSecurityInfo
            {
                IsMbSupporter = true,
                SupporterKey = "IAmTotallyLegit"
            };
        }

        /// <summary>
        /// Gets registration status for a feature.
        /// </summary>
        /// <param name="name">Feature name.</param>
        /// <response code="200">Registration status returned.</response>
        /// <returns>Mb registration record.</returns>
        [Obsolete("This endpoint should not be used.")]
        [HttpPost("RegistrationRecords/{name}")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        public static ActionResult<MBRegistrationRecord> GetRegistrationStatus([FromRoute, Required] string name)
        {
            return new MBRegistrationRecord
            {
                IsRegistered = true,
                RegChecked = true,
                TrialVersion = false,
                IsValid = true,
                RegError = false
            };
        }

        /// <summary>
        /// Gets registration status for a feature.
        /// </summary>
        /// <param name="name">Feature name.</param>
        /// <response code="501">Not implemented.</response>
        /// <returns>Not Implemented.</returns>
        /// <exception cref="NotImplementedException">This endpoint is not implemented.</exception>
        [Obsolete("Paid plugins are not supported")]
        [HttpGet("Registrations/{name}")]
        [ProducesResponseType(StatusCodes.Status501NotImplemented)]
        public static ActionResult GetRegistration([FromRoute, Required] string name)
        {
            // TODO Once we have proper apps and plugins and decide to break compatibility with paid plugins,
            // delete all these registration endpoints. They are only kept for compatibility.
            throw new NotImplementedException();
        }

        /// <summary>
        /// Gets a list of currently installed plugins.
        /// </summary>
        /// <response code="200">Installed plugins returned.</response>
        /// <returns>List of currently installed plugins.</returns>
        [HttpGet]
        [ProducesResponseType(StatusCodes.Status200OK)]
        public ActionResult<IEnumerable<PluginInfo>> GetPlugins()
        {
            return Ok(_pluginManager.Plugins
                .OrderBy(p => p.Name)
                .Select(p => p.GetPluginInfo()));
        }

        /// <summary>
        /// Enables a disabled plugin.
        /// </summary>
        /// <param name="pluginId">Plugin id.</param>
        /// <param name="version">Plugin version.</param>
        /// <response code="204">Plugin enabled.</response>
        /// <response code="404">Plugin not found.</response>
        /// <returns>An <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the plugin could not be found.</returns>
        [HttpPost("{pluginId}/{version}/Enable")]
        [Authorize(Policy = Policies.RequiresElevation)]
        [ProducesResponseType(StatusCodes.Status204NoContent)]
        [ProducesResponseType(StatusCodes.Status404NotFound)]
        public ActionResult EnablePlugin([FromRoute, Required] Guid pluginId, [FromRoute, Required] Version version)
        {
            if (!_pluginManager.TryGetPlugin(pluginId, version, out var plugin))
            {
                return NotFound();
            }

            _pluginManager.EnablePlugin(plugin!);
            return NoContent();
        }

        /// <summary>
        /// Disable a plugin.
        /// </summary>
        /// <param name="pluginId">Plugin id.</param>
        /// <param name="version">Plugin version.</param>
        /// <response code="204">Plugin disabled.</response>
        /// <response code="404">Plugin not found.</response>
        /// <returns>An <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the plugin could not be found.</returns>
        [HttpPost("{pluginId}/{version}/Disable")]
        [Authorize(Policy = Policies.RequiresElevation)]
        [ProducesResponseType(StatusCodes.Status204NoContent)]
        [ProducesResponseType(StatusCodes.Status404NotFound)]
        public ActionResult DisablePlugin([FromRoute, Required] Guid pluginId, [FromRoute, Required] Version version)
        {
            if (!_pluginManager.TryGetPlugin(pluginId, version, out var plugin))
            {
                return NotFound();
            }

            _pluginManager.DisablePlugin(plugin!);
            return NoContent();
        }

        /// <summary>
        /// Uninstalls a plugin by version.
        /// </summary>
        /// <param name="pluginId">Plugin id.</param>
        /// <param name="version">Plugin version.</param>
        /// <response code="204">Plugin uninstalled.</response>
        /// <response code="404">Plugin not found.</response>
        /// <returns>An <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the plugin could not be found.</returns>
        [HttpDelete("{pluginId}/{version}")]
        [Authorize(Policy = Policies.RequiresElevation)]
        [ProducesResponseType(StatusCodes.Status204NoContent)]
        [ProducesResponseType(StatusCodes.Status404NotFound)]
        public ActionResult UninstallPluginByVersion([FromRoute, Required] Guid pluginId, [FromRoute, Required] Version version)
        {
            if (!_pluginManager.TryGetPlugin(pluginId, version, out var plugin))
            {
                return NotFound();
            }

            _installationManager.UninstallPlugin(plugin!);
            return NoContent();
        }

        /// <summary>
        /// Uninstalls a plugin.
        /// </summary>
        /// <param name="pluginId">Plugin id.</param>
        /// <response code="204">Plugin uninstalled.</response>
        /// <response code="404">Plugin not found.</response>
        /// <returns>An <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the file could not be found.</returns>
        [HttpDelete("{pluginId}")]
        [Authorize(Policy = Policies.RequiresElevation)]
        [ProducesResponseType(StatusCodes.Status204NoContent)]
        [ProducesResponseType(StatusCodes.Status404NotFound)]
        [Obsolete("Please use the UninstallPluginByVersion API.")]
        public ActionResult UninstallPlugin([FromRoute, Required] Guid pluginId)
        {
            // If no version is given, return the current instance.
            var plugins = _pluginManager.Plugins.Where(p => p.Id.Equals(pluginId));

            // Select the un-instanced one first.
            var plugin = plugins.FirstOrDefault(p => p.Instance != null);
            if (plugin == null)
            {
                // Then by the status.
                plugin = plugins.OrderBy(p => p.Manifest.Status).FirstOrDefault();
            }

            _installationManager.UninstallPlugin(plugin!);
            return NoContent();
        }

        /// <summary>
        /// Gets plugin configuration.
        /// </summary>
        /// <param name="pluginId">Plugin id.</param>
        /// <response code="200">Plugin configuration returned.</response>
        /// <response code="404">Plugin not found or plugin configuration not found.</response>
        /// <returns>Plugin configuration.</returns>
        [HttpGet("{pluginId}/Configuration")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        [ProducesResponseType(StatusCodes.Status404NotFound)]
        [ProducesFile(MediaTypeNames.Application.Json)]
        public ActionResult<BasePluginConfiguration> GetPluginConfiguration([FromRoute, Required] Guid pluginId)
        {
            if (_pluginManager.TryGetPlugin(pluginId, null, out var plugin)
                && plugin!.Instance is IHasPluginConfiguration configPlugin)
            {
                return configPlugin.Configuration;
            }

            return NotFound();
        }

        /// <summary>
        /// Updates plugin configuration.
        /// </summary>
        /// <remarks>
        /// Accepts plugin configuration as JSON body.
        /// </remarks>
        /// <param name="pluginId">Plugin id.</param>
        /// <response code="204">Plugin configuration updated.</response>
        /// <response code="404">Plugin not found or plugin does not have configuration.</response>
        /// <returns>
        /// A <see cref="Task" /> that represents the asynchronous operation to update plugin configuration.
        ///    The task result contains an <see cref="NoContentResult"/> indicating success, or <see cref="NotFoundResult"/>
        ///    when plugin not found or plugin doesn't have configuration.
        /// </returns>
        [HttpPost("{pluginId}/Configuration")]
        [ProducesResponseType(StatusCodes.Status204NoContent)]
        [ProducesResponseType(StatusCodes.Status404NotFound)]
        public async Task<ActionResult> UpdatePluginConfiguration([FromRoute, Required] Guid pluginId)
        {
            if (!_pluginManager.TryGetPlugin(pluginId, null, out var plugin)
                         || plugin?.Instance is not IHasPluginConfiguration configPlugin)
            {
                return NotFound();
            }

            var configuration = (BasePluginConfiguration?)await JsonSerializer.DeserializeAsync(Request.Body, configPlugin.ConfigurationType, _serializerOptions)
                .ConfigureAwait(false);

            if (configuration != null)
            {
                configPlugin.UpdateConfiguration(configuration);
            }

            return NoContent();
        }

        /// <summary>
        /// Gets a plugin's image.
        /// </summary>
        /// <param name="pluginId">Plugin id.</param>
        /// <param name="version">Plugin version.</param>
        /// <response code="200">Plugin image returned.</response>
        /// <returns>Plugin's image.</returns>
        [HttpGet("{pluginId}/{version}/Image")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        [ProducesResponseType(StatusCodes.Status404NotFound)]
        [ProducesImageFile]
        [AllowAnonymous]
        public ActionResult GetPluginImage([FromRoute, Required] Guid pluginId, [FromRoute, Required] Version version)
        {
            if (!_pluginManager.TryGetPlugin(pluginId, version, out var plugin))
            {
                return NotFound();
            }

            var imgPath = Path.Combine(plugin!.Path, plugin!.Manifest.ImageUrl ?? string.Empty);
            if (((ServerConfiguration)_config.CommonConfiguration).DisablePluginImages
                || plugin!.Manifest.ImageUrl == null
                || !System.IO.File.Exists(imgPath))
            {
                // Use a blank image.
                var type = GetType();
                var stream = type.Assembly.GetManifestResourceStream(type.Namespace + ".Plugins.blank.png");
                return File(stream, "image/png");
            }

            imgPath = Path.Combine(plugin.Path, plugin.Manifest.ImageUrl);
            return PhysicalFile(imgPath, MimeTypes.GetMimeType(imgPath));
        }

        /// <summary>
        /// Gets a plugin's manifest.
        /// </summary>
        /// <param name="pluginId">Plugin id.</param>
        /// <response code="204">Plugin manifest returned.</response>
        /// <response code="404">Plugin not found.</response>
        /// <returns>
        /// A <see cref="Task" /> that represents the asynchronous operation to get the plugin's manifest.
        ///    The task result contains an <see cref="NoContentResult"/> indicating success, or <see cref="NotFoundResult"/>
        ///    when plugin not found.
        /// </returns>
        [HttpPost("{pluginId}/Manifest")]
        [ProducesResponseType(StatusCodes.Status204NoContent)]
        [ProducesResponseType(StatusCodes.Status404NotFound)]
        public ActionResult<PluginManifest> GetPluginManifest([FromRoute, Required] Guid pluginId)
        {
            if (_pluginManager.TryGetPlugin(pluginId, null, out var plugin))
            {
                return Ok(plugin!.Manifest);
            }

            return NotFound();
        }

        /// <summary>
        /// Updates plugin security info.
        /// </summary>
        /// <param name="pluginSecurityInfo">Plugin security info.</param>
        /// <response code="204">Plugin security info updated.</response>
        /// <returns>An <see cref="NoContentResult"/>.</returns>
        [Obsolete("This endpoint should not be used.")]
        [HttpPost("SecurityInfo")]
        [Authorize(Policy = Policies.RequiresElevation)]
        [ProducesResponseType(StatusCodes.Status204NoContent)]
        public ActionResult UpdatePluginSecurityInfo([FromBody, Required] PluginSecurityInfo pluginSecurityInfo)
        {
            return NoContent();
        }
    }
}