import * as express from 'express'
import { PLUGIN_GLOBAL_CSS_PATH } from '../initializers/constants'
-import { join } from 'path'
+import { basename, join } from 'path'
import { RegisteredPlugin } from '../lib/plugins/plugin-manager'
import { servePluginStaticDirectoryValidator } from '../middlewares/validators/plugins'
const pluginsRouter = express.Router()
pluginsRouter.get('/global.css',
- express.static(PLUGIN_GLOBAL_CSS_PATH, { fallthrough: false })
+ servePluginGlobalCSS
)
-pluginsRouter.get('/:pluginName/:pluginVersion/statics/:staticEndpoint',
+pluginsRouter.get('/:pluginName/:pluginVersion/static/:staticEndpoint(*)',
servePluginStaticDirectoryValidator,
servePluginStaticDirectory
)
-pluginsRouter.get('/:pluginName/:pluginVersion/client-scripts/:staticEndpoint',
+pluginsRouter.get('/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)',
servePluginStaticDirectoryValidator,
servePluginClientScripts
)
// ---------------------------------------------------------------------------
+function servePluginGlobalCSS (req: express.Request, res: express.Response) {
+ return res.sendFile(PLUGIN_GLOBAL_CSS_PATH)
+}
+
function servePluginStaticDirectory (req: express.Request, res: express.Response) {
const plugin: RegisteredPlugin = res.locals.registeredPlugin
const staticEndpoint = req.params.staticEndpoint
- const staticPath = plugin.staticDirs[staticEndpoint]
+ const [ directory, ...file ] = staticEndpoint.split('/')
+
+ const staticPath = plugin.staticDirs[directory]
if (!staticPath) {
return res.sendStatus(404)
}
- return express.static(join(plugin.path, staticPath), { fallthrough: false })
+ const filepath = file.join('/')
+ return res.sendFile(join(plugin.path, staticPath, filepath))
}
function servePluginClientScripts (req: express.Request, res: express.Response) {
const plugin: RegisteredPlugin = res.locals.registeredPlugin
const staticEndpoint = req.params.staticEndpoint
- return express.static(join(plugin.path, staticEndpoint), { fallthrough: false })
+ const file = plugin.clientScripts[staticEndpoint]
+ if (!file) {
+ return res.sendStatus(404)
+ }
+
+ return res.sendFile(join(plugin.path, staticEndpoint))
}
import { basename, join } from 'path'
import { CONFIG } from '../../initializers/config'
import { isLibraryCodeValid, isPackageJSONValid } from '../../helpers/custom-validators/plugins'
-import { PluginPackageJson } from '../../../shared/models/plugins/plugin-package-json.model'
+import { ClientScript, PluginPackageJson } from '../../../shared/models/plugins/plugin-package-json.model'
import { PluginLibrary } from '../../../shared/models/plugins/plugin-library.model'
import { createReadStream, createWriteStream } from 'fs'
import { PLUGIN_GLOBAL_CSS_PATH } from '../../initializers/constants'
import { PluginType } from '../../../shared/models/plugins/plugin.type'
import { installNpmPlugin, installNpmPluginFromDisk, removeNpmPlugin } from './yarn'
+import { outputFile } from 'fs-extra'
export interface RegisteredPlugin {
name: string
path: string
staticDirs: { [name: string]: string }
+ clientScripts: { [name: string]: ClientScript }
css: string[]
}
async registerPlugins () {
+ await this.resetCSSGlobalFile()
+
const plugins = await PluginModel.listEnabledPluginsAndThemes()
for (const plugin of plugins) {
}
await plugin.unregister()
+
+ // Remove hooks of this plugin
+ for (const key of Object.keys(this.hooks)) {
+ this.hooks[key] = this.hooks[key].filter(h => h.pluginName !== name)
+ }
+
+ delete this.registeredPlugins[plugin.name]
+
+ logger.info('Regenerating registered plugin CSS to global file.')
+ await this.regeneratePluginGlobalCSS()
}
async install (toInstall: string, version: string, fromDisk = false) {
}
async uninstall (packageName: string) {
- await PluginModel.uninstall(this.normalizePluginName(packageName))
+ logger.info('Uninstalling plugin %s.', packageName)
+
+ const pluginName = this.normalizePluginName(packageName)
+
+ try {
+ await this.unregister(pluginName)
+ } catch (err) {
+ logger.warn('Cannot unregister plugin %s.', pluginName, { err })
+ }
+
+ const plugin = await PluginModel.load(pluginName)
+ if (!plugin || plugin.uninstalled === true) {
+ logger.error('Cannot uninstall plugin %s: it does not exist or is already uninstalled.', packageName)
+ return
+ }
+
+ plugin.enabled = false
+ plugin.uninstalled = true
+
+ await plugin.save()
await removeNpmPlugin(packageName)
+
+ logger.info('Plugin %s uninstalled.', packageName)
}
private async registerPluginOrTheme (plugin: PluginModel) {
library = await this.registerPlugin(plugin, pluginPath, packageJSON)
}
+ const clientScripts: { [id: string]: ClientScript } = {}
+ for (const c of packageJSON.clientScripts) {
+ clientScripts[c.script] = c
+ }
+
this.registeredPlugins[ plugin.name ] = {
name: plugin.name,
type: plugin.type,
peertubeEngine: plugin.peertubeEngine,
path: pluginPath,
staticDirs: packageJSON.staticDirs,
+ clientScripts,
css: packageJSON.css,
unregister: library ? library.unregister : undefined
}
}
}
+ private resetCSSGlobalFile () {
+ return outputFile(PLUGIN_GLOBAL_CSS_PATH, '')
+ }
+
private async addCSSToGlobalFile (pluginPath: string, cssRelativePaths: string[]) {
for (const cssPath of cssRelativePaths) {
await this.concatFiles(join(pluginPath, cssPath), PLUGIN_GLOBAL_CSS_PATH)
private concatFiles (input: string, output: string) {
return new Promise<void>((res, rej) => {
- const outputStream = createWriteStream(input)
- const inputStream = createReadStream(output)
+ const inputStream = createReadStream(input)
+ const outputStream = createWriteStream(output, { flags: 'a' })
inputStream.pipe(outputStream)
return name.replace(/^peertube-((theme)|(plugin))-/, '')
}
+ private async regeneratePluginGlobalCSS () {
+ await this.resetCSSGlobalFile()
+
+ for (const key of Object.keys(this.registeredPlugins)) {
+ const plugin = this.registeredPlugins[key]
+
+ await this.addCSSToGlobalFile(plugin.path, plugin.css)
+ }
+ }
+
static get Instance () {
return this.instance || (this.instance = new this())
}