]>
Commit | Line | Data |
---|---|---|
4d4e5cd4 | 1 | import * as express from 'express' |
bd45d503 | 2 | import { constants, promises as fs } from 'fs' |
65fcc311 | 3 | import { join } from 'path' |
bd45d503 C |
4 | import { CONFIG } from '@server/initializers/config' |
5 | import { buildFileLocale, getCompleteLocale, is18nLocale, LOCALE_FILES } from '@shared/core-utils/i18n' | |
e032aec9 | 6 | import { root } from '../helpers/core-utils' |
bd45d503 | 7 | import { logger } from '../helpers/logger' |
34dd7cb4 | 8 | import { ACCEPT_HEADERS, STATIC_MAX_AGE } from '../initializers/constants' |
e032aec9 | 9 | import { ClientHtml } from '../lib/client-html' |
bd45d503 | 10 | import { asyncMiddleware, embedCSP } from '../middlewares' |
830bcd0f | 11 | |
65fcc311 | 12 | const clientsRouter = express.Router() |
830bcd0f | 13 | |
e02643f3 C |
14 | const distPath = join(root(), 'client', 'dist') |
15 | const embedPath = join(distPath, 'standalone', 'videos', 'embed.html') | |
99941732 | 16 | const testEmbedPath = join(distPath, 'standalone', 'videos', 'test-embed.html') |
830bcd0f | 17 | |
d8755eed | 18 | // Special route that add OpenGraph and oEmbed tags |
830bcd0f | 19 | // Do not use a template engine for a so little thing |
8d987ec6 | 20 | clientsRouter.use('/videos/watch/playlist/:id', asyncMiddleware(generateWatchPlaylistHtmlPage)) |
9aac4423 | 21 | clientsRouter.use('/videos/watch/:id', asyncMiddleware(generateWatchHtmlPage)) |
92bf2f62 C |
22 | clientsRouter.use('/accounts/:nameWithHost', asyncMiddleware(generateAccountHtmlPage)) |
23 | clientsRouter.use('/video-channels/:nameWithHost', asyncMiddleware(generateVideoChannelHtmlPage)) | |
830bcd0f | 24 | |
5abc96fc C |
25 | const embedMiddlewares = [ |
26 | CONFIG.CSP.ENABLED | |
27 | ? embedCSP | |
28 | : (req: express.Request, res: express.Response, next: express.NextFunction) => next(), | |
dfab4fa9 | 29 | |
9aac4423 | 30 | (req: express.Request, res: express.Response) => { |
d00e2393 | 31 | res.removeHeader('X-Frame-Options') |
78646451 C |
32 | // Don't cache HTML file since it's an index to the immutable JS/CSS files |
33 | res.sendFile(embedPath, { maxAge: 0 }) | |
d00e2393 | 34 | } |
5abc96fc C |
35 | ] |
36 | ||
37 | clientsRouter.use('/videos/embed', ...embedMiddlewares) | |
38 | clientsRouter.use('/video-playlists/embed', ...embedMiddlewares) | |
9aac4423 C |
39 | clientsRouter.use( |
40 | '/videos/test-embed', | |
41 | (req: express.Request, res: express.Response) => res.sendFile(testEmbedPath) | |
42 | ) | |
830bcd0f | 43 | |
79530164 | 44 | // Static HTML/CSS/JS client files |
78967fca | 45 | const staticClientFiles = [ |
78967fca C |
46 | 'ngsw-worker.js', |
47 | 'ngsw.json' | |
48 | ] | |
caf2aaf4 | 49 | |
78967fca C |
50 | for (const staticClientFile of staticClientFiles) { |
51 | const path = join(root(), 'client', 'dist', staticClientFile) | |
78967fca | 52 | |
caf2aaf4 | 53 | clientsRouter.get(`/${staticClientFile}`, (req: express.Request, res: express.Response) => { |
cd4cb177 C |
54 | res.sendFile(path, { maxAge: STATIC_MAX_AGE.SERVER }) |
55 | }) | |
56 | } | |
79530164 | 57 | |
caf2aaf4 K |
58 | // Dynamic PWA manifest |
59 | clientsRouter.get('/manifest.webmanifest', asyncMiddleware(generateManifest)) | |
60 | ||
61 | // Static client overrides | |
62 | const staticClientOverrides = [ | |
63 | 'assets/images/logo.svg', | |
64 | 'assets/images/favicon.png', | |
65 | 'assets/images/icons/icon-36x36.png', | |
66 | 'assets/images/icons/icon-48x48.png', | |
67 | 'assets/images/icons/icon-72x72.png', | |
68 | 'assets/images/icons/icon-96x96.png', | |
69 | 'assets/images/icons/icon-144x144.png', | |
70 | 'assets/images/icons/icon-192x192.png', | |
71 | 'assets/images/icons/icon-512x512.png' | |
72 | ] | |
73 | ||
74 | for (const staticClientOverride of staticClientOverrides) { | |
75 | const overridePhysicalPath = join(CONFIG.STORAGE.CLIENT_OVERRIDES_DIR, staticClientOverride) | |
76 | clientsRouter.use(`/client/${staticClientOverride}`, asyncMiddleware(serveClientOverride(overridePhysicalPath))) | |
77 | } | |
78 | ||
552d95b1 | 79 | clientsRouter.use('/client/locales/:locale/:file.json', serveServerTranslations) |
cd4cb177 | 80 | clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE.CLIENT })) |
552d95b1 C |
81 | |
82 | // 404 for static files not found | |
83 | clientsRouter.use('/client/*', (req: express.Request, res: express.Response) => { | |
84 | res.sendStatus(404) | |
85 | }) | |
86 | ||
87 | // Always serve index client page (the client is a single page application, let it handle routing) | |
88 | // Try to provide the right language index.html | |
89 | clientsRouter.use('/(:language)?', asyncMiddleware(serveIndexHTML)) | |
90 | ||
91 | // --------------------------------------------------------------------------- | |
92 | ||
93 | export { | |
94 | clientsRouter | |
95 | } | |
96 | ||
97 | // --------------------------------------------------------------------------- | |
98 | ||
a1587156 | 99 | function serveServerTranslations (req: express.Request, res: express.Response) { |
7ce44a74 C |
100 | const locale = req.params.locale |
101 | const file = req.params.file | |
102 | ||
bdd428a6 | 103 | if (is18nLocale(locale) && LOCALE_FILES.includes(file)) { |
74b7c6d4 C |
104 | const completeLocale = getCompleteLocale(locale) |
105 | const completeFileLocale = buildFileLocale(completeLocale) | |
cd4cb177 | 106 | |
350131cb | 107 | const path = join(__dirname, `../../../client/dist/locale/${file}.${completeFileLocale}.json`) |
cd4cb177 | 108 | return res.sendFile(path, { maxAge: STATIC_MAX_AGE.SERVER }) |
e945b184 C |
109 | } |
110 | ||
111 | return res.sendStatus(404) | |
552d95b1 | 112 | } |
79530164 | 113 | |
552d95b1 | 114 | async function serveIndexHTML (req: express.Request, res: express.Response) { |
989e526a | 115 | if (req.accepts(ACCEPT_HEADERS) === 'html') { |
57c36b27 C |
116 | try { |
117 | await generateHTMLPage(req, res, req.params.language) | |
118 | return | |
119 | } catch (err) { | |
120 | logger.error('Cannot generate HTML page.', err) | |
121 | } | |
989e526a C |
122 | } |
123 | ||
124 | return res.status(404).end() | |
65fcc311 | 125 | } |
830bcd0f | 126 | |
e032aec9 | 127 | async function generateHTMLPage (req: express.Request, res: express.Response, paramLang?: string) { |
9aac4423 | 128 | const html = await ClientHtml.getDefaultHTMLPage(req, res, paramLang) |
989e526a | 129 | |
e032aec9 | 130 | return sendHTML(html, res) |
989e526a C |
131 | } |
132 | ||
e032aec9 C |
133 | async function generateWatchHtmlPage (req: express.Request, res: express.Response) { |
134 | const html = await ClientHtml.getWatchHTMLPage(req.params.id + '', req, res) | |
830bcd0f | 135 | |
e032aec9 | 136 | return sendHTML(html, res) |
830bcd0f C |
137 | } |
138 | ||
8d987ec6 K |
139 | async function generateWatchPlaylistHtmlPage (req: express.Request, res: express.Response) { |
140 | const html = await ClientHtml.getWatchPlaylistHTMLPage(req.params.id + '', req, res) | |
141 | ||
142 | return sendHTML(html, res) | |
143 | } | |
144 | ||
92bf2f62 C |
145 | async function generateAccountHtmlPage (req: express.Request, res: express.Response) { |
146 | const html = await ClientHtml.getAccountHTMLPage(req.params.nameWithHost, req, res) | |
147 | ||
148 | return sendHTML(html, res) | |
149 | } | |
150 | ||
151 | async function generateVideoChannelHtmlPage (req: express.Request, res: express.Response) { | |
152 | const html = await ClientHtml.getVideoChannelHTMLPage(req.params.nameWithHost, req, res) | |
153 | ||
154 | return sendHTML(html, res) | |
155 | } | |
156 | ||
e032aec9 C |
157 | function sendHTML (html: string, res: express.Response) { |
158 | res.set('Content-Type', 'text/html; charset=UTF-8') | |
eb080476 | 159 | |
e032aec9 | 160 | return res.send(html) |
830bcd0f | 161 | } |
caf2aaf4 K |
162 | |
163 | async function generateManifest (req: express.Request, res: express.Response) { | |
164 | const manifestPhysicalPath = join(root(), 'client', 'dist', 'manifest.webmanifest') | |
165 | const manifestJson = await fs.readFile(manifestPhysicalPath, 'utf8') | |
166 | const manifest = JSON.parse(manifestJson) | |
167 | ||
168 | manifest.name = CONFIG.INSTANCE.NAME | |
169 | manifest.short_name = CONFIG.INSTANCE.NAME | |
170 | manifest.description = CONFIG.INSTANCE.SHORT_DESCRIPTION | |
171 | ||
172 | res.json(manifest) | |
173 | } | |
174 | ||
175 | function serveClientOverride (path: string) { | |
176 | return async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
177 | try { | |
178 | await fs.access(path, constants.F_OK) | |
179 | // Serve override client | |
180 | res.sendFile(path, { maxAge: STATIC_MAX_AGE.SERVER }) | |
181 | } catch { | |
182 | // Serve dist client | |
183 | next() | |
184 | } | |
185 | } | |
186 | } |