diff options
author | Rigel Kent <sendmemail@rigelk.eu> | 2020-12-08 21:16:10 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-08 21:16:10 +0100 |
commit | f2eb23cd87cf32b8fe545178143b5f49e06a58da (patch) | |
tree | af7d59945af70e28fd85047e2c688c59a908f548 /server/controllers/client.ts | |
parent | c977fd3ec931c059111ddb2b8d6ddbb20b6b99a1 (diff) | |
download | PeerTube-f2eb23cd87cf32b8fe545178143b5f49e06a58da.tar.gz PeerTube-f2eb23cd87cf32b8fe545178143b5f49e06a58da.tar.zst PeerTube-f2eb23cd87cf32b8fe545178143b5f49e06a58da.zip |
emit more specific status codes on video upload (#3423)
- reduce http status codes list to potentially useful codes
- convert more codes to typed ones
- factorize html generator for error responses
Diffstat (limited to 'server/controllers/client.ts')
-rw-r--r-- | server/controllers/client.ts | 32 |
1 files changed, 3 insertions, 29 deletions
diff --git a/server/controllers/client.ts b/server/controllers/client.ts index 49e6fd661..bd1f19f8c 100644 --- a/server/controllers/client.ts +++ b/server/controllers/client.ts | |||
@@ -3,12 +3,11 @@ import { constants, promises as fs } from 'fs' | |||
3 | import { join } from 'path' | 3 | import { join } from 'path' |
4 | import { CONFIG } from '@server/initializers/config' | 4 | import { CONFIG } from '@server/initializers/config' |
5 | import { buildFileLocale, getCompleteLocale, is18nLocale, LOCALE_FILES } from '@shared/core-utils/i18n' | 5 | import { buildFileLocale, getCompleteLocale, is18nLocale, LOCALE_FILES } from '@shared/core-utils/i18n' |
6 | import { HttpStatusCode } from '@shared/core-utils' | ||
6 | import { root } from '../helpers/core-utils' | 7 | import { root } from '../helpers/core-utils' |
7 | import { logger } from '../helpers/logger' | 8 | import { STATIC_MAX_AGE } from '../initializers/constants' |
8 | import { ACCEPT_HEADERS, STATIC_MAX_AGE } from '../initializers/constants' | 9 | import { ClientHtml, sendHTML, serveIndexHTML } from '../lib/client-html' |
9 | import { ClientHtml } from '../lib/client-html' | ||
10 | import { asyncMiddleware, embedCSP } from '../middlewares' | 10 | import { asyncMiddleware, embedCSP } from '../middlewares' |
11 | import { HttpStatusCode } from '@shared/core-utils' | ||
12 | 11 | ||
13 | const clientsRouter = express.Router() | 12 | const clientsRouter = express.Router() |
14 | 13 | ||
@@ -118,31 +117,12 @@ function serveServerTranslations (req: express.Request, res: express.Response) { | |||
118 | return res.sendStatus(HttpStatusCode.NOT_FOUND_404) | 117 | return res.sendStatus(HttpStatusCode.NOT_FOUND_404) |
119 | } | 118 | } |
120 | 119 | ||
121 | async function serveIndexHTML (req: express.Request, res: express.Response) { | ||
122 | if (req.accepts(ACCEPT_HEADERS) === 'html') { | ||
123 | try { | ||
124 | await generateHTMLPage(req, res, req.params.language) | ||
125 | return | ||
126 | } catch (err) { | ||
127 | logger.error('Cannot generate HTML page.', err) | ||
128 | } | ||
129 | } | ||
130 | |||
131 | return res.status(HttpStatusCode.INTERNAL_SERVER_ERROR_500).end() | ||
132 | } | ||
133 | |||
134 | async function generateEmbedHtmlPage (req: express.Request, res: express.Response) { | 120 | async function generateEmbedHtmlPage (req: express.Request, res: express.Response) { |
135 | const html = await ClientHtml.getEmbedHTML() | 121 | const html = await ClientHtml.getEmbedHTML() |
136 | 122 | ||
137 | return sendHTML(html, res) | 123 | return sendHTML(html, res) |
138 | } | 124 | } |
139 | 125 | ||
140 | async function generateHTMLPage (req: express.Request, res: express.Response, paramLang?: string) { | ||
141 | const html = await ClientHtml.getDefaultHTMLPage(req, res, paramLang) | ||
142 | |||
143 | return sendHTML(html, res) | ||
144 | } | ||
145 | |||
146 | async function generateWatchHtmlPage (req: express.Request, res: express.Response) { | 126 | async function generateWatchHtmlPage (req: express.Request, res: express.Response) { |
147 | const html = await ClientHtml.getWatchHTMLPage(req.params.id + '', req, res) | 127 | const html = await ClientHtml.getWatchHTMLPage(req.params.id + '', req, res) |
148 | 128 | ||
@@ -167,12 +147,6 @@ async function generateVideoChannelHtmlPage (req: express.Request, res: express. | |||
167 | return sendHTML(html, res) | 147 | return sendHTML(html, res) |
168 | } | 148 | } |
169 | 149 | ||
170 | function sendHTML (html: string, res: express.Response) { | ||
171 | res.set('Content-Type', 'text/html; charset=UTF-8') | ||
172 | |||
173 | return res.send(html) | ||
174 | } | ||
175 | |||
176 | async function generateManifest (req: express.Request, res: express.Response) { | 150 | async function generateManifest (req: express.Request, res: express.Response) { |
177 | const manifestPhysicalPath = join(root(), 'client', 'dist', 'manifest.webmanifest') | 151 | const manifestPhysicalPath = join(root(), 'client', 'dist', 'manifest.webmanifest') |
178 | const manifestJson = await fs.readFile(manifestPhysicalPath, 'utf8') | 152 | const manifestJson = await fs.readFile(manifestPhysicalPath, 'utf8') |