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