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