PeerTube/server/controllers/plugins.ts

100 lines
3.1 KiB
TypeScript
Raw Normal View History

import * as express from 'express'
import { PLUGIN_GLOBAL_CSS_PATH } from '../initializers/constants'
2019-07-12 11:39:58 +02:00
import { join } from 'path'
import { RegisteredPlugin } from '../lib/plugins/plugin-manager'
import { servePluginStaticDirectoryValidator } from '../middlewares/validators/plugins'
2019-07-12 11:39:58 +02:00
import { serveThemeCSSValidator } from '../middlewares/validators/themes'
import { PluginType } from '../../shared/models/plugins/plugin.type'
2019-07-23 09:48:48 +02:00
import { isTestInstance } from '../helpers/core-utils'
const sendFileOptions = {
maxAge: '30 days',
immutable: !isTestInstance()
}
const pluginsRouter = express.Router()
2019-07-12 11:39:58 +02:00
pluginsRouter.get('/plugins/global.css',
2019-07-08 14:02:03 +02:00
servePluginGlobalCSS
)
2019-07-12 11:39:58 +02:00
pluginsRouter.get('/plugins/:pluginName/:pluginVersion/static/:staticEndpoint(*)',
servePluginStaticDirectoryValidator(PluginType.PLUGIN),
servePluginStaticDirectory
)
2019-07-12 11:39:58 +02:00
pluginsRouter.get('/plugins/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)',
servePluginStaticDirectoryValidator(PluginType.PLUGIN),
servePluginClientScripts
)
2019-07-12 11:39:58 +02:00
pluginsRouter.get('/themes/:pluginName/:pluginVersion/static/:staticEndpoint(*)',
servePluginStaticDirectoryValidator(PluginType.THEME),
servePluginStaticDirectory
)
pluginsRouter.get('/themes/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)',
servePluginStaticDirectoryValidator(PluginType.THEME),
servePluginClientScripts
)
pluginsRouter.get('/themes/:themeName/:themeVersion/css/:staticEndpoint(*)',
serveThemeCSSValidator,
serveThemeCSSDirectory
)
// ---------------------------------------------------------------------------
export {
pluginsRouter
}
// ---------------------------------------------------------------------------
2019-07-08 14:02:03 +02:00
function servePluginGlobalCSS (req: express.Request, res: express.Response) {
2019-07-23 09:48:48 +02:00
// Only cache requests that have a ?hash=... query param
const globalCSSOptions = req.query.hash
? sendFileOptions
: {}
return res.sendFile(PLUGIN_GLOBAL_CSS_PATH, globalCSSOptions)
2019-07-08 14:02:03 +02:00
}
function servePluginStaticDirectory (req: express.Request, res: express.Response) {
const plugin: RegisteredPlugin = res.locals.registeredPlugin
const staticEndpoint = req.params.staticEndpoint
2019-07-08 14:02:03 +02:00
const [ directory, ...file ] = staticEndpoint.split('/')
const staticPath = plugin.staticDirs[directory]
if (!staticPath) {
return res.sendStatus(404)
}
2019-07-08 14:02:03 +02:00
const filepath = file.join('/')
2019-07-23 09:48:48 +02:00
return res.sendFile(join(plugin.path, staticPath, filepath), sendFileOptions)
}
function servePluginClientScripts (req: express.Request, res: express.Response) {
const plugin: RegisteredPlugin = res.locals.registeredPlugin
const staticEndpoint = req.params.staticEndpoint
2019-07-08 14:02:03 +02:00
const file = plugin.clientScripts[staticEndpoint]
if (!file) {
return res.sendStatus(404)
}
2019-07-23 09:48:48 +02:00
return res.sendFile(join(plugin.path, staticEndpoint), sendFileOptions)
}
2019-07-12 11:39:58 +02:00
function serveThemeCSSDirectory (req: express.Request, res: express.Response) {
const plugin: RegisteredPlugin = res.locals.registeredPlugin
const staticEndpoint = req.params.staticEndpoint
if (plugin.css.includes(staticEndpoint) === false) {
return res.sendStatus(404)
}
2019-07-23 09:48:48 +02:00
return res.sendFile(join(plugin.path, staticEndpoint), sendFileOptions)
2019-07-12 11:39:58 +02:00
}