aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib
diff options
context:
space:
mode:
authorRigel Kent <sendmemail@rigelk.eu>2020-12-08 21:16:10 +0100
committerGitHub <noreply@github.com>2020-12-08 21:16:10 +0100
commitf2eb23cd87cf32b8fe545178143b5f49e06a58da (patch)
treeaf7d59945af70e28fd85047e2c688c59a908f548 /server/lib
parentc977fd3ec931c059111ddb2b8d6ddbb20b6b99a1 (diff)
downloadPeerTube-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/lib')
-rw-r--r--server/lib/activitypub/actor.ts3
-rw-r--r--server/lib/activitypub/playlist.ts3
-rw-r--r--server/lib/activitypub/videos.ts3
-rw-r--r--server/lib/client-html.ts42
4 files changed, 45 insertions, 6 deletions
diff --git a/server/lib/activitypub/actor.ts b/server/lib/activitypub/actor.ts
index fb5558ff6..52547536c 100644
--- a/server/lib/activitypub/actor.ts
+++ b/server/lib/activitypub/actor.ts
@@ -36,6 +36,7 @@ import {
36} from '../../types/models' 36} from '../../types/models'
37import { extname } from 'path' 37import { extname } from 'path'
38import { getServerActor } from '@server/models/application/application' 38import { getServerActor } from '@server/models/application/application'
39import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
39 40
40// Set account keys, this could be long so process after the account creation and do not block the client 41// Set account keys, this could be long so process after the account creation and do not block the client
41function setAsyncActorKeys <T extends MActor> (actor: T) { 42function setAsyncActorKeys <T extends MActor> (actor: T) {
@@ -277,7 +278,7 @@ async function refreshActorIfNeeded <T extends MActorFull | MActorAccountChannel
277 278
278 const { result, statusCode } = await fetchRemoteActor(actorUrl) 279 const { result, statusCode } = await fetchRemoteActor(actorUrl)
279 280
280 if (statusCode === 404) { 281 if (statusCode === HttpStatusCode.NOT_FOUND_404) {
281 logger.info('Deleting actor %s because there is a 404 in refresh actor.', actor.url) 282 logger.info('Deleting actor %s because there is a 404 in refresh actor.', actor.url)
282 actor.Account 283 actor.Account
283 ? await actor.Account.destroy() 284 ? await actor.Account.destroy()
diff --git a/server/lib/activitypub/playlist.ts b/server/lib/activitypub/playlist.ts
index bd442b223..53298e968 100644
--- a/server/lib/activitypub/playlist.ts
+++ b/server/lib/activitypub/playlist.ts
@@ -18,6 +18,7 @@ import { createPlaylistMiniatureFromUrl } from '../thumbnail'
18import { FilteredModelAttributes } from '../../types/sequelize' 18import { FilteredModelAttributes } from '../../types/sequelize'
19import { MAccountDefault, MAccountId, MVideoId } from '../../types/models' 19import { MAccountDefault, MAccountId, MVideoId } from '../../types/models'
20import { MVideoPlaylist, MVideoPlaylistId, MVideoPlaylistOwner } from '../../types/models/video/video-playlist' 20import { MVideoPlaylist, MVideoPlaylistId, MVideoPlaylistOwner } from '../../types/models/video/video-playlist'
21import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
21 22
22function playlistObjectToDBAttributes (playlistObject: PlaylistObject, byAccount: MAccountId, to: string[]) { 23function playlistObjectToDBAttributes (playlistObject: PlaylistObject, byAccount: MAccountId, to: string[]) {
23 const privacy = to.includes(ACTIVITY_PUB.PUBLIC) 24 const privacy = to.includes(ACTIVITY_PUB.PUBLIC)
@@ -120,7 +121,7 @@ async function refreshVideoPlaylistIfNeeded (videoPlaylist: MVideoPlaylistOwner)
120 121
121 try { 122 try {
122 const { statusCode, playlistObject } = await fetchRemoteVideoPlaylist(videoPlaylist.url) 123 const { statusCode, playlistObject } = await fetchRemoteVideoPlaylist(videoPlaylist.url)
123 if (statusCode === 404) { 124 if (statusCode === HttpStatusCode.NOT_FOUND_404) {
124 logger.info('Cannot refresh remote video playlist %s: it does not exist anymore. Deleting it.', videoPlaylist.url) 125 logger.info('Cannot refresh remote video playlist %s: it does not exist anymore. Deleting it.', videoPlaylist.url)
125 126
126 await videoPlaylist.destroy() 127 await videoPlaylist.destroy()
diff --git a/server/lib/activitypub/videos.ts b/server/lib/activitypub/videos.ts
index 04f0bfc23..b15d5da1c 100644
--- a/server/lib/activitypub/videos.ts
+++ b/server/lib/activitypub/videos.ts
@@ -76,6 +76,7 @@ import { sendCreateVideo, sendUpdateVideo } from './send'
76import { addVideoShares, shareVideoByServerAndChannel } from './share' 76import { addVideoShares, shareVideoByServerAndChannel } from './share'
77import { addVideoComments } from './video-comments' 77import { addVideoComments } from './video-comments'
78import { createRates } from './video-rates' 78import { createRates } from './video-rates'
79import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
79 80
80async function federateVideoIfNeeded (videoArg: MVideoAPWithoutCaption, isNewVideo: boolean, transaction?: sequelize.Transaction) { 81async function federateVideoIfNeeded (videoArg: MVideoAPWithoutCaption, isNewVideo: boolean, transaction?: sequelize.Transaction) {
81 const video = videoArg as MVideoAP 82 const video = videoArg as MVideoAP
@@ -488,7 +489,7 @@ async function refreshVideoIfNeeded (options: {
488 489
489 try { 490 try {
490 const { response, videoObject } = await fetchRemoteVideo(video.url) 491 const { response, videoObject } = await fetchRemoteVideo(video.url)
491 if (response.statusCode === 404) { 492 if (response.statusCode === HttpStatusCode.NOT_FOUND_404) {
492 logger.info('Cannot refresh remote video %s: video does not exist anymore. Deleting it.', video.url) 493 logger.info('Cannot refresh remote video %s: video does not exist anymore. Deleting it.', video.url)
493 494
494 // Video does not exist anymore 495 // Video does not exist anymore
diff --git a/server/lib/client-html.ts b/server/lib/client-html.ts
index d97d23180..32f5d29ab 100644
--- a/server/lib/client-html.ts
+++ b/server/lib/client-html.ts
@@ -1,4 +1,5 @@
1import * as express from 'express' 1import * as express from 'express'
2import * as Bluebird from 'bluebird'
2import { buildFileLocale, getDefaultLocale, is18nLocale, POSSIBLE_LOCALES } from '../../shared/core-utils/i18n/i18n' 3import { buildFileLocale, getDefaultLocale, is18nLocale, POSSIBLE_LOCALES } from '../../shared/core-utils/i18n/i18n'
3import { 4import {
4 AVATARS_SIZE, 5 AVATARS_SIZE,
@@ -6,7 +7,8 @@ import {
6 EMBED_SIZE, 7 EMBED_SIZE,
7 PLUGIN_GLOBAL_CSS_PATH, 8 PLUGIN_GLOBAL_CSS_PATH,
8 WEBSERVER, 9 WEBSERVER,
9 FILES_CONTENT_HASH 10 FILES_CONTENT_HASH,
11 ACCEPT_HEADERS
10} from '../initializers/constants' 12} from '../initializers/constants'
11import { join } from 'path' 13import { join } from 'path'
12import { escapeHTML, isTestInstance, sha256 } from '../helpers/core-utils' 14import { escapeHTML, isTestInstance, sha256 } from '../helpers/core-utils'
@@ -18,7 +20,6 @@ import { readFile } from 'fs-extra'
18import { getActivityStreamDuration } from '../models/video/video-format-utils' 20import { getActivityStreamDuration } from '../models/video/video-format-utils'
19import { AccountModel } from '../models/account/account' 21import { AccountModel } from '../models/account/account'
20import { VideoChannelModel } from '../models/video/video-channel' 22import { VideoChannelModel } from '../models/video/video-channel'
21import * as Bluebird from 'bluebird'
22import { CONFIG } from '../initializers/config' 23import { CONFIG } from '../initializers/config'
23import { logger } from '../helpers/logger' 24import { logger } from '../helpers/logger'
24import { MAccountActor, MChannelActor } from '../types/models' 25import { MAccountActor, MChannelActor } from '../types/models'
@@ -53,7 +54,7 @@ type Tags = {
53 } 54 }
54} 55}
55 56
56export class ClientHtml { 57class ClientHtml {
57 58
58 private static htmlCache: { [path: string]: string } = {} 59 private static htmlCache: { [path: string]: string } = {}
59 60
@@ -505,3 +506,38 @@ export class ClientHtml {
505 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.META_TAGS, tagsString) 506 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.META_TAGS, tagsString)
506 } 507 }
507} 508}
509
510function sendHTML (html: string, res: express.Response) {
511 res.set('Content-Type', 'text/html; charset=UTF-8')
512
513 return res.send(html)
514}
515
516async function serveIndexHTML (req: express.Request, res: express.Response) {
517 if (req.accepts(ACCEPT_HEADERS) === 'html' ||
518 !req.headers.accept) {
519 try {
520 await generateHTMLPage(req, res, req.params.language)
521 return
522 } catch (err) {
523 logger.error('Cannot generate HTML page.', err)
524 return res.sendStatus(HttpStatusCode.INTERNAL_SERVER_ERROR_500)
525 }
526 }
527
528 return res.sendStatus(HttpStatusCode.NOT_ACCEPTABLE_406)
529}
530
531// ---------------------------------------------------------------------------
532
533export {
534 ClientHtml,
535 sendHTML,
536 serveIndexHTML
537}
538
539async function generateHTMLPage (req: express.Request, res: express.Response, paramLang?: string) {
540 const html = await ClientHtml.getDefaultHTMLPage(req, res, paramLang)
541
542 return sendHTML(html, res)
543}