Add notification settings migration
[oweals/peertube.git] / server / controllers / client.ts
1 import * as express from 'express'
2 import { join } from 'path'
3 import { root } from '../helpers/core-utils'
4 import { ACCEPT_HEADERS, STATIC_MAX_AGE } from '../initializers'
5 import { asyncMiddleware, embedCSP } from '../middlewares'
6 import { buildFileLocale, getCompleteLocale, is18nLocale, LOCALE_FILES } from '../../shared/models/i18n/i18n'
7 import { ClientHtml } from '../lib/client-html'
8 import { logger } from '../helpers/logger'
9
10 const clientsRouter = express.Router()
11
12 const distPath = join(root(), 'client', 'dist')
13 const assetsImagesPath = join(root(), 'client', 'dist', 'assets', 'images')
14 const embedPath = join(distPath, 'standalone', 'videos', 'embed.html')
15 const testEmbedPath = join(distPath, 'standalone', 'videos', 'test-embed.html')
16
17 // Special route that add OpenGraph and oEmbed tags
18 // Do not use a template engine for a so little thing
19 clientsRouter.use('/videos/watch/:id', asyncMiddleware(generateWatchHtmlPage))
20
21 clientsRouter.use(
22   '/videos/embed',
23   embedCSP,
24   (req: express.Request, res: express.Response) => {
25     res.removeHeader('X-Frame-Options')
26     res.sendFile(embedPath)
27   }
28 )
29 clientsRouter.use(
30   '/videos/test-embed',
31   (req: express.Request, res: express.Response) => res.sendFile(testEmbedPath)
32 )
33
34 // Static HTML/CSS/JS client files
35
36 const staticClientFiles = [
37   'manifest.webmanifest',
38   'ngsw-worker.js',
39   'ngsw.json'
40 ]
41 for (const staticClientFile of staticClientFiles) {
42   const path = join(root(), 'client', 'dist', staticClientFile)
43   clientsRouter.use('/' + staticClientFile, express.static(path, { maxAge: STATIC_MAX_AGE }))
44 }
45
46 clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE }))
47 clientsRouter.use('/client/assets/images', express.static(assetsImagesPath, { maxAge: STATIC_MAX_AGE }))
48
49 clientsRouter.use('/client/locales/:locale/:file.json', function (req, res) {
50   const locale = req.params.locale
51   const file = req.params.file
52
53   if (is18nLocale(locale) && LOCALE_FILES.indexOf(file) !== -1) {
54     const completeLocale = getCompleteLocale(locale)
55     const completeFileLocale = buildFileLocale(completeLocale)
56     return res.sendFile(join(__dirname, `../../../client/dist/locale/${file}_${completeFileLocale}.json`))
57   }
58
59   return res.sendStatus(404)
60 })
61
62 // 404 for static files not found
63 clientsRouter.use('/client/*', (req: express.Request, res: express.Response, next: express.NextFunction) => {
64   res.sendStatus(404)
65 })
66
67 // Always serve index client page (the client is a single page application, let it handle routing)
68 // Try to provide the right language index.html
69 clientsRouter.use('/(:language)?', async function (req, res) {
70   if (req.accepts(ACCEPT_HEADERS) === 'html') {
71     try {
72       await generateHTMLPage(req, res, req.params.language)
73       return
74     } catch (err) {
75       logger.error('Cannot generate HTML page.', err)
76     }
77   }
78
79   return res.status(404).end()
80 })
81
82 // ---------------------------------------------------------------------------
83
84 export {
85   clientsRouter
86 }
87
88 // ---------------------------------------------------------------------------
89
90 async function generateHTMLPage (req: express.Request, res: express.Response, paramLang?: string) {
91   const html = await ClientHtml.getDefaultHTMLPage(req, res, paramLang)
92
93   return sendHTML(html, res)
94 }
95
96 async function generateWatchHtmlPage (req: express.Request, res: express.Response) {
97   const html = await ClientHtml.getWatchHTMLPage(req.params.id + '', req, res)
98
99   return sendHTML(html, res)
100 }
101
102 function sendHTML (html: string, res: express.Response) {
103   res.set('Content-Type', 'text/html; charset=UTF-8')
104
105   return res.send(html)
106 }