aboutsummaryrefslogtreecommitdiffhomepage
path: root/server
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-04-12 10:10:48 +0200
committerChocobozzz <me@florianbigard.com>2021-04-12 10:10:48 +0200
commit8280d0c22797c72978f698dc2deaa8ef569a9d15 (patch)
tree8136abd28c9aa0c5050bb66e2f6f7451117b57f2 /server
parent90aa0a74e95d390d62942aa72d481d5aa7b5ac23 (diff)
downloadPeerTube-8280d0c22797c72978f698dc2deaa8ef569a9d15.tar.gz
PeerTube-8280d0c22797c72978f698dc2deaa8ef569a9d15.tar.zst
PeerTube-8280d0c22797c72978f698dc2deaa8ef569a9d15.zip
Ensure to install supported plugins
Diffstat (limited to 'server')
-rw-r--r--server/controllers/api/plugins.ts2
-rw-r--r--server/lib/plugins/plugin-index.ts12
-rw-r--r--server/lib/plugins/plugin-manager.ts9
-rw-r--r--server/lib/plugins/yarn.ts13
-rw-r--r--server/middlewares/validators/plugins.ts2
5 files changed, 29 insertions, 9 deletions
diff --git a/server/controllers/api/plugins.ts b/server/controllers/api/plugins.ts
index bb69f25a1..a186de010 100644
--- a/server/controllers/api/plugins.ts
+++ b/server/controllers/api/plugins.ts
@@ -151,7 +151,7 @@ async function updatePlugin (req: express.Request, res: express.Response) {
151 const fromDisk = !!body.path 151 const fromDisk = !!body.path
152 const toUpdate = body.npmName || body.path 152 const toUpdate = body.npmName || body.path
153 try { 153 try {
154 const plugin = await PluginManager.Instance.update(toUpdate, undefined, fromDisk) 154 const plugin = await PluginManager.Instance.update(toUpdate, fromDisk)
155 155
156 return res.json(plugin.toFormattedJSON()) 156 return res.json(plugin.toFormattedJSON())
157 } catch (err) { 157 } catch (err) {
diff --git a/server/lib/plugins/plugin-index.ts b/server/lib/plugins/plugin-index.ts
index 624f5da1d..165bc91b3 100644
--- a/server/lib/plugins/plugin-index.ts
+++ b/server/lib/plugins/plugin-index.ts
@@ -67,7 +67,19 @@ async function getLatestPluginsVersion (npmNames: string[]): Promise<PeertubePlu
67 return body 67 return body
68} 68}
69 69
70async function getLatestPluginVersion (npmName: string) {
71 const results = await getLatestPluginsVersion([ npmName ])
72
73 if (Array.isArray(results) === false || results.length !== 1) {
74 logger.warn('Cannot get latest supported plugin version of %s.', npmName)
75 return undefined
76 }
77
78 return results[0].latestVersion
79}
80
70export { 81export {
71 listAvailablePluginsFromIndex, 82 listAvailablePluginsFromIndex,
83 getLatestPluginVersion,
72 getLatestPluginsVersion 84 getLatestPluginsVersion
73} 85}
diff --git a/server/lib/plugins/plugin-manager.ts b/server/lib/plugins/plugin-manager.ts
index e3226a950..03ea48416 100644
--- a/server/lib/plugins/plugin-manager.ts
+++ b/server/lib/plugins/plugin-manager.ts
@@ -328,11 +328,18 @@ export class PluginManager implements ServerHook {
328 return plugin 328 return plugin
329 } 329 }
330 330
331 async update (toUpdate: string, version?: string, fromDisk = false) { 331 async update (toUpdate: string, fromDisk = false) {
332 const npmName = fromDisk ? basename(toUpdate) : toUpdate 332 const npmName = fromDisk ? basename(toUpdate) : toUpdate
333 333
334 logger.info('Updating plugin %s.', npmName) 334 logger.info('Updating plugin %s.', npmName)
335 335
336 // Use the latest version from DB, to not upgrade to a version that does not support our PeerTube version
337 let version: string
338 if (!fromDisk) {
339 const plugin = await PluginModel.loadByNpmName(toUpdate)
340 version = plugin.latestVersion
341 }
342
336 // Unregister old hooks 343 // Unregister old hooks
337 await this.unregister(npmName) 344 await this.unregister(npmName)
338 345
diff --git a/server/lib/plugins/yarn.ts b/server/lib/plugins/yarn.ts
index e40351b6e..3f45681d3 100644
--- a/server/lib/plugins/yarn.ts
+++ b/server/lib/plugins/yarn.ts
@@ -1,14 +1,17 @@
1import { outputJSON, pathExists } from 'fs-extra'
2import { join } from 'path'
1import { execShell } from '../../helpers/core-utils' 3import { execShell } from '../../helpers/core-utils'
2import { logger } from '../../helpers/logger'
3import { isNpmPluginNameValid, isPluginVersionValid } from '../../helpers/custom-validators/plugins' 4import { isNpmPluginNameValid, isPluginVersionValid } from '../../helpers/custom-validators/plugins'
5import { logger } from '../../helpers/logger'
4import { CONFIG } from '../../initializers/config' 6import { CONFIG } from '../../initializers/config'
5import { outputJSON, pathExists } from 'fs-extra' 7import { getLatestPluginVersion } from './plugin-index'
6import { join } from 'path'
7 8
8async function installNpmPlugin (npmName: string, version?: string) { 9async function installNpmPlugin (npmName: string, versionArg?: string) {
9 // Security check 10 // Security check
10 checkNpmPluginNameOrThrow(npmName) 11 checkNpmPluginNameOrThrow(npmName)
11 if (version) checkPluginVersionOrThrow(version) 12 if (versionArg) checkPluginVersionOrThrow(versionArg)
13
14 const version = versionArg || await getLatestPluginVersion(npmName)
12 15
13 let toInstall = npmName 16 let toInstall = npmName
14 if (version) toInstall += `@${version}` 17 if (version) toInstall += `@${version}`
diff --git a/server/middlewares/validators/plugins.ts b/server/middlewares/validators/plugins.ts
index 1083e0afa..ab87fe720 100644
--- a/server/middlewares/validators/plugins.ts
+++ b/server/middlewares/validators/plugins.ts
@@ -109,7 +109,6 @@ const installOrUpdatePluginValidator = [
109 if (!body.path && !body.npmName) { 109 if (!body.path && !body.npmName) {
110 return res.status(HttpStatusCode.BAD_REQUEST_400) 110 return res.status(HttpStatusCode.BAD_REQUEST_400)
111 .json({ error: 'Should have either a npmName or a path' }) 111 .json({ error: 'Should have either a npmName or a path' })
112 .end()
113 } 112 }
114 113
115 return next() 114 return next()
@@ -140,7 +139,6 @@ const existingPluginValidator = [
140 if (!plugin) { 139 if (!plugin) {
141 return res.status(HttpStatusCode.NOT_FOUND_404) 140 return res.status(HttpStatusCode.NOT_FOUND_404)
142 .json({ error: 'Plugin not found' }) 141 .json({ error: 'Plugin not found' })
143 .end()
144 } 142 }
145 143
146 res.locals.plugin = plugin 144 res.locals.plugin = plugin