]>
Commit | Line | Data |
---|---|---|
41fb13c3 | 1 | import 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' |
eebd9838 | 5 | import { logger } from '@server/helpers/logger' |
bd45d503 | 6 | import { CONFIG } from '@server/initializers/config' |
eebd9838 | 7 | import { Hooks } from '@server/lib/plugins/hooks' |
17e69744 | 8 | import { buildFileLocale, getCompleteLocale, is18nLocale, LOCALE_FILES } from '@shared/core-utils/i18n' |
42cc1887 | 9 | import { HttpStatusCode } from '@shared/models' |
06aad801 | 10 | import { root } from '@shared/core-utils' |
f2eb23cd RK |
11 | import { STATIC_MAX_AGE } from '../initializers/constants' |
12 | import { ClientHtml, sendHTML, serveIndexHTML } from '../lib/client-html' | |
527a52ac | 13 | import { asyncMiddleware, embedCSP } from '../middlewares' |
830bcd0f | 14 | |
65fcc311 | 15 | const clientsRouter = express.Router() |
830bcd0f | 16 | |
e02643f3 | 17 | const distPath = join(root(), 'client', 'dist') |
99941732 | 18 | const testEmbedPath = join(distPath, 'standalone', 'videos', 'test-embed.html') |
830bcd0f | 19 | |
d8755eed | 20 | // Special route that add OpenGraph and oEmbed tags |
830bcd0f | 21 | // Do not use a template engine for a so little thing |
a1eda903 C |
22 | clientsRouter.use([ '/w/p/:id', '/videos/watch/playlist/:id' ], asyncMiddleware(generateWatchPlaylistHtmlPage)) |
23 | clientsRouter.use([ '/w/:id', '/videos/watch/:id' ], asyncMiddleware(generateWatchHtmlPage)) | |
9a911038 K |
24 | clientsRouter.use([ '/accounts/:nameWithHost', '/a/:nameWithHost' ], asyncMiddleware(generateAccountHtmlPage)) |
25 | clientsRouter.use([ '/video-channels/:nameWithHost', '/c/:nameWithHost' ], asyncMiddleware(generateVideoChannelHtmlPage)) | |
26 | clientsRouter.use('/@:nameWithHost', asyncMiddleware(generateActorHtmlPage)) | |
830bcd0f | 27 | |
5abc96fc C |
28 | const embedMiddlewares = [ |
29 | CONFIG.CSP.ENABLED | |
30 | ? embedCSP | |
31 | : (req: express.Request, res: express.Response, next: express.NextFunction) => next(), | |
dfab4fa9 | 32 | |
eebd9838 | 33 | // Set headers |
cf649c2e | 34 | (req: express.Request, res: express.Response, next: express.NextFunction) => { |
d00e2393 | 35 | res.removeHeader('X-Frame-Options') |
cf649c2e | 36 | |
78646451 | 37 | // Don't cache HTML file since it's an index to the immutable JS/CSS files |
cf649c2e C |
38 | res.setHeader('Cache-Control', 'public, max-age=0') |
39 | ||
40 | next() | |
41 | }, | |
42 | ||
43 | asyncMiddleware(generateEmbedHtmlPage) | |
5abc96fc C |
44 | ] |
45 | ||
46 | clientsRouter.use('/videos/embed', ...embedMiddlewares) | |
47 | clientsRouter.use('/video-playlists/embed', ...embedMiddlewares) | |
9054a8b6 C |
48 | |
49 | const testEmbedController = (req: express.Request, res: express.Response) => res.sendFile(testEmbedPath) | |
50 | ||
51 | clientsRouter.use('/videos/test-embed', testEmbedController) | |
52 | clientsRouter.use('/video-playlists/test-embed', testEmbedController) | |
830bcd0f | 53 | |
caf2aaf4 | 54 | // Dynamic PWA manifest |
03aa518e | 55 | clientsRouter.get('/manifest.webmanifest', asyncMiddleware(generateManifest)) |
caf2aaf4 K |
56 | |
57 | // Static client overrides | |
8872828d | 58 | // Must be consistent with static client overrides redirections in /support/nginx/peertube |
caf2aaf4 K |
59 | const staticClientOverrides = [ |
60 | 'assets/images/logo.svg', | |
61 | 'assets/images/favicon.png', | |
62 | 'assets/images/icons/icon-36x36.png', | |
63 | 'assets/images/icons/icon-48x48.png', | |
64 | 'assets/images/icons/icon-72x72.png', | |
65 | 'assets/images/icons/icon-96x96.png', | |
66 | 'assets/images/icons/icon-144x144.png', | |
67 | 'assets/images/icons/icon-192x192.png', | |
c80e458a | 68 | 'assets/images/icons/icon-512x512.png', |
69 | 'assets/images/default-playlist.jpg', | |
70 | 'assets/images/default-avatar-account.png', | |
d0800f76 | 71 | 'assets/images/default-avatar-account-48x48.png', |
72 | 'assets/images/default-avatar-video-channel.png', | |
73 | 'assets/images/default-avatar-video-channel-48x48.png' | |
caf2aaf4 K |
74 | ] |
75 | ||
76 | for (const staticClientOverride of staticClientOverrides) { | |
77 | const overridePhysicalPath = join(CONFIG.STORAGE.CLIENT_OVERRIDES_DIR, staticClientOverride) | |
78 | clientsRouter.use(`/client/${staticClientOverride}`, asyncMiddleware(serveClientOverride(overridePhysicalPath))) | |
79 | } | |
80 | ||
552d95b1 | 81 | clientsRouter.use('/client/locales/:locale/:file.json', serveServerTranslations) |
cd4cb177 | 82 | clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE.CLIENT })) |
552d95b1 C |
83 | |
84 | // 404 for static files not found | |
85 | clientsRouter.use('/client/*', (req: express.Request, res: express.Response) => { | |
76148b27 | 86 | res.status(HttpStatusCode.NOT_FOUND_404).end() |
552d95b1 C |
87 | }) |
88 | ||
89 | // Always serve index client page (the client is a single page application, let it handle routing) | |
90 | // Try to provide the right language index.html | |
91 | clientsRouter.use('/(:language)?', asyncMiddleware(serveIndexHTML)) | |
92 | ||
93 | // --------------------------------------------------------------------------- | |
94 | ||
95 | export { | |
96 | clientsRouter | |
97 | } | |
98 | ||
99 | // --------------------------------------------------------------------------- | |
100 | ||
a1587156 | 101 | function serveServerTranslations (req: express.Request, res: express.Response) { |
7ce44a74 C |
102 | const locale = req.params.locale |
103 | const file = req.params.file | |
104 | ||
bdd428a6 | 105 | if (is18nLocale(locale) && LOCALE_FILES.includes(file)) { |
74b7c6d4 C |
106 | const completeLocale = getCompleteLocale(locale) |
107 | const completeFileLocale = buildFileLocale(completeLocale) | |
cd4cb177 | 108 | |
350131cb | 109 | const path = join(__dirname, `../../../client/dist/locale/${file}.${completeFileLocale}.json`) |
cd4cb177 | 110 | return res.sendFile(path, { maxAge: STATIC_MAX_AGE.SERVER }) |
e945b184 C |
111 | } |
112 | ||
76148b27 | 113 | return res.status(HttpStatusCode.NOT_FOUND_404).end() |
552d95b1 | 114 | } |
79530164 | 115 | |
cf649c2e | 116 | async function generateEmbedHtmlPage (req: express.Request, res: express.Response) { |
eebd9838 C |
117 | const hookName = req.originalUrl.startsWith('/video-playlists/') |
118 | ? 'filter:html.embed.video-playlist.allowed.result' | |
119 | : 'filter:html.embed.video.allowed.result' | |
120 | ||
121 | const allowParameters = { req } | |
122 | ||
123 | const allowedResult = await Hooks.wrapFun( | |
124 | isEmbedAllowed, | |
125 | allowParameters, | |
126 | hookName | |
127 | ) | |
128 | ||
129 | if (!allowedResult || allowedResult.allowed !== true) { | |
130 | logger.info('Embed is not allowed.', { allowedResult }) | |
131 | ||
132 | return sendHTML(allowedResult?.html || '', res) | |
133 | } | |
134 | ||
cf649c2e C |
135 | const html = await ClientHtml.getEmbedHTML() |
136 | ||
137 | return sendHTML(html, res) | |
138 | } | |
139 | ||
e032aec9 | 140 | async function generateWatchHtmlPage (req: express.Request, res: express.Response) { |
c0a4982e C |
141 | // Thread link is '/w/:videoId;threadId=:threadId' |
142 | // So to get the videoId we need to remove the last part | |
143 | let videoId = req.params.id + '' | |
144 | ||
145 | const threadIdIndex = videoId.indexOf(';threadId') | |
146 | if (threadIdIndex !== -1) videoId = videoId.substring(0, threadIdIndex) | |
147 | ||
148 | const html = await ClientHtml.getWatchHTMLPage(videoId, req, res) | |
830bcd0f | 149 | |
5fc44b57 | 150 | return sendHTML(html, res, true) |
830bcd0f C |
151 | } |
152 | ||
8d987ec6 K |
153 | async function generateWatchPlaylistHtmlPage (req: express.Request, res: express.Response) { |
154 | const html = await ClientHtml.getWatchPlaylistHTMLPage(req.params.id + '', req, res) | |
155 | ||
5fc44b57 | 156 | return sendHTML(html, res, true) |
8d987ec6 K |
157 | } |
158 | ||
92bf2f62 C |
159 | async function generateAccountHtmlPage (req: express.Request, res: express.Response) { |
160 | const html = await ClientHtml.getAccountHTMLPage(req.params.nameWithHost, req, res) | |
161 | ||
5fc44b57 | 162 | return sendHTML(html, res, true) |
92bf2f62 C |
163 | } |
164 | ||
165 | async function generateVideoChannelHtmlPage (req: express.Request, res: express.Response) { | |
166 | const html = await ClientHtml.getVideoChannelHTMLPage(req.params.nameWithHost, req, res) | |
9a911038 | 167 | |
5fc44b57 | 168 | return sendHTML(html, res, true) |
9a911038 K |
169 | } |
170 | ||
171 | async function generateActorHtmlPage (req: express.Request, res: express.Response) { | |
172 | const html = await ClientHtml.getActorHTMLPage(req.params.nameWithHost, req, res) | |
92bf2f62 | 173 | |
5fc44b57 | 174 | return sendHTML(html, res, true) |
92bf2f62 C |
175 | } |
176 | ||
caf2aaf4 K |
177 | async function generateManifest (req: express.Request, res: express.Response) { |
178 | const manifestPhysicalPath = join(root(), 'client', 'dist', 'manifest.webmanifest') | |
1d22d251 | 179 | const manifestJson = await readFile(manifestPhysicalPath, 'utf8') |
caf2aaf4 K |
180 | const manifest = JSON.parse(manifestJson) |
181 | ||
182 | manifest.name = CONFIG.INSTANCE.NAME | |
183 | manifest.short_name = CONFIG.INSTANCE.NAME | |
184 | manifest.description = CONFIG.INSTANCE.SHORT_DESCRIPTION | |
185 | ||
186 | res.json(manifest) | |
187 | } | |
188 | ||
189 | function serveClientOverride (path: string) { | |
190 | return async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
191 | try { | |
192 | await fs.access(path, constants.F_OK) | |
193 | // Serve override client | |
194 | res.sendFile(path, { maxAge: STATIC_MAX_AGE.SERVER }) | |
195 | } catch { | |
196 | // Serve dist client | |
197 | next() | |
198 | } | |
199 | } | |
200 | } | |
eebd9838 C |
201 | |
202 | type AllowedResult = { allowed: boolean, html?: string } | |
203 | function isEmbedAllowed (_object: { | |
204 | req: express.Request | |
205 | }): AllowedResult { | |
206 | return { allowed: true } | |
207 | } |