]>
Commit | Line | Data |
---|---|---|
4d4e5cd4 | 1 | import * as express from 'express' |
bd45d503 | 2 | import { constants, promises as fs } from 'fs' |
1d22d251 | 3 | import { readFile } from 'fs-extra' |
65fcc311 | 4 | import { join } from 'path' |
bd45d503 | 5 | import { CONFIG } from '@server/initializers/config' |
f2eb23cd | 6 | import { HttpStatusCode } from '@shared/core-utils' |
17e69744 | 7 | import { buildFileLocale, getCompleteLocale, is18nLocale, LOCALE_FILES } from '@shared/core-utils/i18n' |
e032aec9 | 8 | import { root } from '../helpers/core-utils' |
f2eb23cd RK |
9 | import { STATIC_MAX_AGE } from '../initializers/constants' |
10 | import { ClientHtml, sendHTML, serveIndexHTML } from '../lib/client-html' | |
bd45d503 | 11 | import { asyncMiddleware, embedCSP } from '../middlewares' |
830bcd0f | 12 | |
65fcc311 | 13 | const clientsRouter = express.Router() |
830bcd0f | 14 | |
e02643f3 | 15 | const distPath = join(root(), 'client', 'dist') |
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 | |
cf649c2e | 30 | (req: express.Request, res: express.Response, next: express.NextFunction) => { |
d00e2393 | 31 | res.removeHeader('X-Frame-Options') |
cf649c2e | 32 | |
78646451 | 33 | // Don't cache HTML file since it's an index to the immutable JS/CSS files |
cf649c2e C |
34 | res.setHeader('Cache-Control', 'public, max-age=0') |
35 | ||
36 | next() | |
37 | }, | |
38 | ||
39 | asyncMiddleware(generateEmbedHtmlPage) | |
5abc96fc C |
40 | ] |
41 | ||
42 | clientsRouter.use('/videos/embed', ...embedMiddlewares) | |
43 | clientsRouter.use('/video-playlists/embed', ...embedMiddlewares) | |
9054a8b6 C |
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) | |
830bcd0f | 49 | |
caf2aaf4 | 50 | // Dynamic PWA manifest |
03aa518e | 51 | clientsRouter.get('/manifest.webmanifest', asyncMiddleware(generateManifest)) |
caf2aaf4 K |
52 | |
53 | // Static client overrides | |
8872828d | 54 | // Must be consistent with static client overrides redirections in /support/nginx/peertube |
caf2aaf4 K |
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 | ||
552d95b1 | 72 | clientsRouter.use('/client/locales/:locale/:file.json', serveServerTranslations) |
cd4cb177 | 73 | clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE.CLIENT })) |
552d95b1 C |
74 | |
75 | // 404 for static files not found | |
76 | clientsRouter.use('/client/*', (req: express.Request, res: express.Response) => { | |
2d53be02 | 77 | res.sendStatus(HttpStatusCode.NOT_FOUND_404) |
552d95b1 C |
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 | ||
a1587156 | 92 | function serveServerTranslations (req: express.Request, res: express.Response) { |
7ce44a74 C |
93 | const locale = req.params.locale |
94 | const file = req.params.file | |
95 | ||
bdd428a6 | 96 | if (is18nLocale(locale) && LOCALE_FILES.includes(file)) { |
74b7c6d4 C |
97 | const completeLocale = getCompleteLocale(locale) |
98 | const completeFileLocale = buildFileLocale(completeLocale) | |
cd4cb177 | 99 | |
350131cb | 100 | const path = join(__dirname, `../../../client/dist/locale/${file}.${completeFileLocale}.json`) |
cd4cb177 | 101 | return res.sendFile(path, { maxAge: STATIC_MAX_AGE.SERVER }) |
e945b184 C |
102 | } |
103 | ||
2d53be02 | 104 | return res.sendStatus(HttpStatusCode.NOT_FOUND_404) |
552d95b1 | 105 | } |
79530164 | 106 | |
cf649c2e C |
107 | async function generateEmbedHtmlPage (req: express.Request, res: express.Response) { |
108 | const html = await ClientHtml.getEmbedHTML() | |
109 | ||
110 | return sendHTML(html, res) | |
111 | } | |
112 | ||
e032aec9 C |
113 | async function generateWatchHtmlPage (req: express.Request, res: express.Response) { |
114 | const html = await ClientHtml.getWatchHTMLPage(req.params.id + '', req, res) | |
830bcd0f | 115 | |
e032aec9 | 116 | return sendHTML(html, res) |
830bcd0f C |
117 | } |
118 | ||
8d987ec6 K |
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 | ||
92bf2f62 C |
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 | ||
caf2aaf4 K |
137 | async function generateManifest (req: express.Request, res: express.Response) { |
138 | const manifestPhysicalPath = join(root(), 'client', 'dist', 'manifest.webmanifest') | |
1d22d251 | 139 | const manifestJson = await readFile(manifestPhysicalPath, 'utf8') |
caf2aaf4 K |
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 | } |