]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/client.ts
Fix lint
[github/Chocobozzz/PeerTube.git] / server / controllers / client.ts
1 import * as express from 'express'
2 import { constants, promises as fs } from 'fs'
3 import { readFile } from 'fs-extra'
4 import { join } from 'path'
5 import { CONFIG } from '@server/initializers/config'
6 import { HttpStatusCode } from '@shared/core-utils'
7 import { buildFileLocale, getCompleteLocale, is18nLocale, LOCALE_FILES } from '@shared/core-utils/i18n'
8 import { root } from '../helpers/core-utils'
9 import { STATIC_MAX_AGE } from '../initializers/constants'
10 import { ClientHtml, sendHTML, serveIndexHTML } from '../lib/client-html'
11 import { asyncMiddleware, embedCSP } from '../middlewares'
12
13 const clientsRouter = express.Router()
14
15 const distPath = join(root(), 'client', 'dist')
16 const testEmbedPath = join(distPath, 'standalone', 'videos', 'test-embed.html')
17
18 // Special route that add OpenGraph and oEmbed tags
19 // Do not use a template engine for a so little thing
20 clientsRouter.use('/videos/watch/playlist/:id', asyncMiddleware(generateWatchPlaylistHtmlPage))
21 clientsRouter.use('/videos/watch/:id', asyncMiddleware(generateWatchHtmlPage))
22 clientsRouter.use('/accounts/:nameWithHost', asyncMiddleware(generateAccountHtmlPage))
23 clientsRouter.use('/video-channels/:nameWithHost', asyncMiddleware(generateVideoChannelHtmlPage))
24
25 const embedMiddlewares = [
26 CONFIG.CSP.ENABLED
27 ? embedCSP
28 : (req: express.Request, res: express.Response, next: express.NextFunction) => next(),
29
30 (req: express.Request, res: express.Response, next: express.NextFunction) => {
31 res.removeHeader('X-Frame-Options')
32
33 // Don't cache HTML file since it's an index to the immutable JS/CSS files
34 res.setHeader('Cache-Control', 'public, max-age=0')
35
36 next()
37 },
38
39 asyncMiddleware(generateEmbedHtmlPage)
40 ]
41
42 clientsRouter.use('/videos/embed', ...embedMiddlewares)
43 clientsRouter.use('/video-playlists/embed', ...embedMiddlewares)
44
45 const testEmbedController = (req: express.Request, res: express.Response) => res.sendFile(testEmbedPath)
46
47 clientsRouter.use('/videos/test-embed', testEmbedController)
48 clientsRouter.use('/video-playlists/test-embed', testEmbedController)
49
50 // Dynamic PWA manifest
51 clientsRouter.get(/\/client\/[^/]+\/manifest.webmanifest/, asyncMiddleware(generateManifest))
52
53 // Static client overrides
54 // Must be consistent with static client overrides redirections in /support/nginx/peertube
55 const staticClientOverrides = [
56 'assets/images/logo.svg',
57 'assets/images/favicon.png',
58 'assets/images/icons/icon-36x36.png',
59 'assets/images/icons/icon-48x48.png',
60 'assets/images/icons/icon-72x72.png',
61 'assets/images/icons/icon-96x96.png',
62 'assets/images/icons/icon-144x144.png',
63 'assets/images/icons/icon-192x192.png',
64 'assets/images/icons/icon-512x512.png'
65 ]
66
67 for (const staticClientOverride of staticClientOverrides) {
68 const overridePhysicalPath = join(CONFIG.STORAGE.CLIENT_OVERRIDES_DIR, staticClientOverride)
69 clientsRouter.use(`/client/${staticClientOverride}`, asyncMiddleware(serveClientOverride(overridePhysicalPath)))
70 }
71
72 clientsRouter.use('/client/locales/:locale/:file.json', serveServerTranslations)
73 clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE.CLIENT }))
74
75 // 404 for static files not found
76 clientsRouter.use('/client/*', (req: express.Request, res: express.Response) => {
77 res.sendStatus(HttpStatusCode.NOT_FOUND_404)
78 })
79
80 // Always serve index client page (the client is a single page application, let it handle routing)
81 // Try to provide the right language index.html
82 clientsRouter.use('/(:language)?', asyncMiddleware(serveIndexHTML))
83
84 // ---------------------------------------------------------------------------
85
86 export {
87 clientsRouter
88 }
89
90 // ---------------------------------------------------------------------------
91
92 function serveServerTranslations (req: express.Request, res: express.Response) {
93 const locale = req.params.locale
94 const file = req.params.file
95
96 if (is18nLocale(locale) && LOCALE_FILES.includes(file)) {
97 const completeLocale = getCompleteLocale(locale)
98 const completeFileLocale = buildFileLocale(completeLocale)
99
100 const path = join(__dirname, `../../../client/dist/locale/${file}.${completeFileLocale}.json`)
101 return res.sendFile(path, { maxAge: STATIC_MAX_AGE.SERVER })
102 }
103
104 return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
105 }
106
107 async function generateEmbedHtmlPage (req: express.Request, res: express.Response) {
108 const html = await ClientHtml.getEmbedHTML()
109
110 return sendHTML(html, res)
111 }
112
113 async function generateWatchHtmlPage (req: express.Request, res: express.Response) {
114 const html = await ClientHtml.getWatchHTMLPage(req.params.id + '', req, res)
115
116 return sendHTML(html, res)
117 }
118
119 async function generateWatchPlaylistHtmlPage (req: express.Request, res: express.Response) {
120 const html = await ClientHtml.getWatchPlaylistHTMLPage(req.params.id + '', req, res)
121
122 return sendHTML(html, res)
123 }
124
125 async function generateAccountHtmlPage (req: express.Request, res: express.Response) {
126 const html = await ClientHtml.getAccountHTMLPage(req.params.nameWithHost, req, res)
127
128 return sendHTML(html, res)
129 }
130
131 async function generateVideoChannelHtmlPage (req: express.Request, res: express.Response) {
132 const html = await ClientHtml.getVideoChannelHTMLPage(req.params.nameWithHost, req, res)
133
134 return sendHTML(html, res)
135 }
136
137 async function generateManifest (req: express.Request, res: express.Response) {
138 const manifestPhysicalPath = join(root(), 'client', 'dist', 'manifest.webmanifest')
139 const manifestJson = await readFile(manifestPhysicalPath, 'utf8')
140 const manifest = JSON.parse(manifestJson)
141
142 manifest.name = CONFIG.INSTANCE.NAME
143 manifest.short_name = CONFIG.INSTANCE.NAME
144 manifest.description = CONFIG.INSTANCE.SHORT_DESCRIPTION
145
146 res.json(manifest)
147 }
148
149 function serveClientOverride (path: string) {
150 return async (req: express.Request, res: express.Response, next: express.NextFunction) => {
151 try {
152 await fs.access(path, constants.F_OK)
153 // Serve override client
154 res.sendFile(path, { maxAge: STATIC_MAX_AGE.SERVER })
155 } catch {
156 // Serve dist client
157 next()
158 }
159 }
160 }