diff options
author | Chocobozzz <me@florianbigard.com> | 2019-07-12 11:39:58 +0200 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2019-07-24 10:58:16 +0200 |
commit | b5f919ac8eb2a1c20e26582fdfd377d687710d8f (patch) | |
tree | 5ec28be6f750f54bba560803ddba681103c2d82e /server/controllers | |
parent | 8d2be0ed7bb87283a1ec98609df6b82d83db706a (diff) | |
download | PeerTube-b5f919ac8eb2a1c20e26582fdfd377d687710d8f.tar.gz PeerTube-b5f919ac8eb2a1c20e26582fdfd377d687710d8f.tar.zst PeerTube-b5f919ac8eb2a1c20e26582fdfd377d687710d8f.zip |
WIP plugins: update plugin
Diffstat (limited to 'server/controllers')
-rw-r--r-- | server/controllers/api/plugins.ts | 38 | ||||
-rw-r--r-- | server/controllers/index.ts | 1 | ||||
-rw-r--r-- | server/controllers/plugins.ts | 40 |
3 files changed, 63 insertions, 16 deletions
diff --git a/server/controllers/api/plugins.ts b/server/controllers/api/plugins.ts index 8e59f27cf..14675fdf3 100644 --- a/server/controllers/api/plugins.ts +++ b/server/controllers/api/plugins.ts | |||
@@ -13,13 +13,13 @@ import { PluginModel } from '../../models/server/plugin' | |||
13 | import { UserRight } from '../../../shared/models/users' | 13 | import { UserRight } from '../../../shared/models/users' |
14 | import { | 14 | import { |
15 | existingPluginValidator, | 15 | existingPluginValidator, |
16 | installPluginValidator, | 16 | installOrUpdatePluginValidator, |
17 | listPluginsValidator, | 17 | listPluginsValidator, |
18 | uninstallPluginValidator, | 18 | uninstallPluginValidator, |
19 | updatePluginSettingsValidator | 19 | updatePluginSettingsValidator |
20 | } from '../../middlewares/validators/plugins' | 20 | } from '../../middlewares/validators/plugins' |
21 | import { PluginManager } from '../../lib/plugins/plugin-manager' | 21 | import { PluginManager } from '../../lib/plugins/plugin-manager' |
22 | import { InstallPlugin } from '../../../shared/models/plugins/install-plugin.model' | 22 | import { InstallOrUpdatePlugin } from '../../../shared/models/plugins/install-plugin.model' |
23 | import { ManagePlugin } from '../../../shared/models/plugins/manage-plugin.model' | 23 | import { ManagePlugin } from '../../../shared/models/plugins/manage-plugin.model' |
24 | import { logger } from '../../helpers/logger' | 24 | import { logger } from '../../helpers/logger' |
25 | 25 | ||
@@ -61,10 +61,17 @@ pluginRouter.put('/:npmName/settings', | |||
61 | pluginRouter.post('/install', | 61 | pluginRouter.post('/install', |
62 | authenticate, | 62 | authenticate, |
63 | ensureUserHasRight(UserRight.MANAGE_PLUGINS), | 63 | ensureUserHasRight(UserRight.MANAGE_PLUGINS), |
64 | installPluginValidator, | 64 | installOrUpdatePluginValidator, |
65 | asyncMiddleware(installPlugin) | 65 | asyncMiddleware(installPlugin) |
66 | ) | 66 | ) |
67 | 67 | ||
68 | pluginRouter.post('/update', | ||
69 | authenticate, | ||
70 | ensureUserHasRight(UserRight.MANAGE_PLUGINS), | ||
71 | installOrUpdatePluginValidator, | ||
72 | asyncMiddleware(updatePlugin) | ||
73 | ) | ||
74 | |||
68 | pluginRouter.post('/uninstall', | 75 | pluginRouter.post('/uninstall', |
69 | authenticate, | 76 | authenticate, |
70 | ensureUserHasRight(UserRight.MANAGE_PLUGINS), | 77 | ensureUserHasRight(UserRight.MANAGE_PLUGINS), |
@@ -100,18 +107,33 @@ function getPlugin (req: express.Request, res: express.Response) { | |||
100 | } | 107 | } |
101 | 108 | ||
102 | async function installPlugin (req: express.Request, res: express.Response) { | 109 | async function installPlugin (req: express.Request, res: express.Response) { |
103 | const body: InstallPlugin = req.body | 110 | const body: InstallOrUpdatePlugin = req.body |
104 | 111 | ||
105 | const fromDisk = !!body.path | 112 | const fromDisk = !!body.path |
106 | const toInstall = body.npmName || body.path | 113 | const toInstall = body.npmName || body.path |
107 | try { | 114 | try { |
108 | await PluginManager.Instance.install(toInstall, undefined, fromDisk) | 115 | const plugin = await PluginManager.Instance.install(toInstall, undefined, fromDisk) |
116 | |||
117 | return res.json(plugin.toFormattedJSON()) | ||
109 | } catch (err) { | 118 | } catch (err) { |
110 | logger.warn('Cannot install plugin %s.', toInstall, { err }) | 119 | logger.warn('Cannot install plugin %s.', toInstall, { err }) |
111 | return res.sendStatus(400) | 120 | return res.sendStatus(400) |
112 | } | 121 | } |
122 | } | ||
113 | 123 | ||
114 | return res.sendStatus(204) | 124 | async function updatePlugin (req: express.Request, res: express.Response) { |
125 | const body: InstallOrUpdatePlugin = req.body | ||
126 | |||
127 | const fromDisk = !!body.path | ||
128 | const toUpdate = body.npmName || body.path | ||
129 | try { | ||
130 | const plugin = await PluginManager.Instance.update(toUpdate, undefined, fromDisk) | ||
131 | |||
132 | return res.json(plugin.toFormattedJSON()) | ||
133 | } catch (err) { | ||
134 | logger.warn('Cannot update plugin %s.', toUpdate, { err }) | ||
135 | return res.sendStatus(400) | ||
136 | } | ||
115 | } | 137 | } |
116 | 138 | ||
117 | async function uninstallPlugin (req: express.Request, res: express.Response) { | 139 | async function uninstallPlugin (req: express.Request, res: express.Response) { |
@@ -123,9 +145,7 @@ async function uninstallPlugin (req: express.Request, res: express.Response) { | |||
123 | } | 145 | } |
124 | 146 | ||
125 | function getPluginRegisteredSettings (req: express.Request, res: express.Response) { | 147 | function getPluginRegisteredSettings (req: express.Request, res: express.Response) { |
126 | const plugin = res.locals.plugin | 148 | const settings = PluginManager.Instance.getRegisteredSettings(req.params.npmName) |
127 | |||
128 | const settings = PluginManager.Instance.getSettings(plugin.name) | ||
129 | 149 | ||
130 | return res.json({ | 150 | return res.json({ |
131 | settings | 151 | settings |
diff --git a/server/controllers/index.ts b/server/controllers/index.ts index 869546dc7..8b3501712 100644 --- a/server/controllers/index.ts +++ b/server/controllers/index.ts | |||
@@ -8,4 +8,3 @@ export * from './webfinger' | |||
8 | export * from './tracker' | 8 | export * from './tracker' |
9 | export * from './bots' | 9 | export * from './bots' |
10 | export * from './plugins' | 10 | export * from './plugins' |
11 | export * from './themes' | ||
diff --git a/server/controllers/plugins.ts b/server/controllers/plugins.ts index 05f03324d..f255d13e8 100644 --- a/server/controllers/plugins.ts +++ b/server/controllers/plugins.ts | |||
@@ -1,25 +1,42 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import { PLUGIN_GLOBAL_CSS_PATH } from '../initializers/constants' | 2 | import { PLUGIN_GLOBAL_CSS_PATH } from '../initializers/constants' |
3 | import { basename, join } from 'path' | 3 | import { join } from 'path' |
4 | import { RegisteredPlugin } from '../lib/plugins/plugin-manager' | 4 | import { RegisteredPlugin } from '../lib/plugins/plugin-manager' |
5 | import { servePluginStaticDirectoryValidator } from '../middlewares/validators/plugins' | 5 | import { servePluginStaticDirectoryValidator } from '../middlewares/validators/plugins' |
6 | import { serveThemeCSSValidator } from '../middlewares/validators/themes' | ||
7 | import { PluginType } from '../../shared/models/plugins/plugin.type' | ||
6 | 8 | ||
7 | const pluginsRouter = express.Router() | 9 | const pluginsRouter = express.Router() |
8 | 10 | ||
9 | pluginsRouter.get('/global.css', | 11 | pluginsRouter.get('/plugins/global.css', |
10 | servePluginGlobalCSS | 12 | servePluginGlobalCSS |
11 | ) | 13 | ) |
12 | 14 | ||
13 | pluginsRouter.get('/:pluginName/:pluginVersion/static/:staticEndpoint(*)', | 15 | pluginsRouter.get('/plugins/:pluginName/:pluginVersion/static/:staticEndpoint(*)', |
14 | servePluginStaticDirectoryValidator, | 16 | servePluginStaticDirectoryValidator(PluginType.PLUGIN), |
15 | servePluginStaticDirectory | 17 | servePluginStaticDirectory |
16 | ) | 18 | ) |
17 | 19 | ||
18 | pluginsRouter.get('/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)', | 20 | pluginsRouter.get('/plugins/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)', |
19 | servePluginStaticDirectoryValidator, | 21 | servePluginStaticDirectoryValidator(PluginType.PLUGIN), |
20 | servePluginClientScripts | 22 | servePluginClientScripts |
21 | ) | 23 | ) |
22 | 24 | ||
25 | pluginsRouter.get('/themes/:pluginName/:pluginVersion/static/:staticEndpoint(*)', | ||
26 | servePluginStaticDirectoryValidator(PluginType.THEME), | ||
27 | servePluginStaticDirectory | ||
28 | ) | ||
29 | |||
30 | pluginsRouter.get('/themes/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)', | ||
31 | servePluginStaticDirectoryValidator(PluginType.THEME), | ||
32 | servePluginClientScripts | ||
33 | ) | ||
34 | |||
35 | pluginsRouter.get('/themes/:themeName/:themeVersion/css/:staticEndpoint(*)', | ||
36 | serveThemeCSSValidator, | ||
37 | serveThemeCSSDirectory | ||
38 | ) | ||
39 | |||
23 | // --------------------------------------------------------------------------- | 40 | // --------------------------------------------------------------------------- |
24 | 41 | ||
25 | export { | 42 | export { |
@@ -58,3 +75,14 @@ function servePluginClientScripts (req: express.Request, res: express.Response) | |||
58 | 75 | ||
59 | return res.sendFile(join(plugin.path, staticEndpoint)) | 76 | return res.sendFile(join(plugin.path, staticEndpoint)) |
60 | } | 77 | } |
78 | |||
79 | function serveThemeCSSDirectory (req: express.Request, res: express.Response) { | ||
80 | const plugin: RegisteredPlugin = res.locals.registeredPlugin | ||
81 | const staticEndpoint = req.params.staticEndpoint | ||
82 | |||
83 | if (plugin.css.includes(staticEndpoint) === false) { | ||
84 | return res.sendStatus(404) | ||
85 | } | ||
86 | |||
87 | return res.sendFile(join(plugin.path, staticEndpoint)) | ||
88 | } | ||