X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fcontrollers%2Fplugins.ts;h=1caee9a29fcbb10d2fdd5fbb0f09121f154b72fe;hb=d5c8932a601c1854db0a2e399ccaf26e17385f1a;hp=a6705d9c7c803550110342b5a2a908bbb60e84aa;hpb=345da516fae80f24c90c2196e96393b489af2243;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/controllers/plugins.ts b/server/controllers/plugins.ts index a6705d9c7..1caee9a29 100644 --- a/server/controllers/plugins.ts +++ b/server/controllers/plugins.ts @@ -1,25 +1,53 @@ import * as express from 'express' import { PLUGIN_GLOBAL_CSS_PATH } from '../initializers/constants' import { join } from 'path' -import { RegisteredPlugin } from '../lib/plugins/plugin-manager' +import { PluginManager, RegisteredPlugin } from '../lib/plugins/plugin-manager' import { servePluginStaticDirectoryValidator } from '../middlewares/validators/plugins' +import { serveThemeCSSValidator } from '../middlewares/validators/themes' +import { PluginType } from '../../shared/models/plugins/plugin.type' +import { isTestInstance } from '../helpers/core-utils' +import { getCompleteLocale, is18nLocale } from '../../shared/models/i18n' + +const sendFileOptions = { + maxAge: '30 days', + immutable: !isTestInstance() +} const pluginsRouter = express.Router() -pluginsRouter.get('/global.css', - express.static(PLUGIN_GLOBAL_CSS_PATH, { fallthrough: false }) +pluginsRouter.get('/plugins/global.css', + servePluginGlobalCSS ) -pluginsRouter.get('/:pluginName/:pluginVersion/statics/:staticEndpoint', - servePluginStaticDirectoryValidator, +pluginsRouter.get('/plugins/translations/:locale.json', + getPluginTranslations +) + +pluginsRouter.get('/plugins/:pluginName/:pluginVersion/static/:staticEndpoint(*)', + servePluginStaticDirectoryValidator(PluginType.PLUGIN), servePluginStaticDirectory ) -pluginsRouter.get('/:pluginName/:pluginVersion/client-scripts/:staticEndpoint', - servePluginStaticDirectoryValidator, +pluginsRouter.get('/plugins/:pluginName/:pluginVersion/client-scripts/:staticEndpoint(*)', + servePluginStaticDirectoryValidator(PluginType.PLUGIN), servePluginClientScripts ) +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 { @@ -28,21 +56,62 @@ export { // --------------------------------------------------------------------------- +function servePluginGlobalCSS (req: express.Request, res: express.Response) { + // Only cache requests that have a ?hash=... query param + const globalCSSOptions = req.query.hash + ? sendFileOptions + : {} + + return res.sendFile(PLUGIN_GLOBAL_CSS_PATH, globalCSSOptions) +} + +function getPluginTranslations (req: express.Request, res: express.Response) { + const locale = req.params.locale + + if (is18nLocale(locale)) { + const completeLocale = getCompleteLocale(locale) + const json = PluginManager.Instance.getTranslations(completeLocale) + + return res.json(json) + } + + return res.sendStatus(404) +} + 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), sendFileOptions) } 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), sendFileOptions) +} + +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) + } + + return res.sendFile(join(plugin.path, staticEndpoint), sendFileOptions) }