1 import express from 'express'
2 import { query } from 'express-validator'
3 import { join } from 'path'
4 import { loadVideo } from '@server/lib/model-loaders'
5 import { VideoPlaylistModel } from '@server/models/video/video-playlist'
6 import { VideoPlaylistPrivacy, VideoPrivacy } from '@shared/models'
7 import { HttpStatusCode } from '../../../shared/models/http/http-error-codes'
8 import { isTestOrDevInstance } from '../../helpers/core-utils'
9 import { isIdOrUUIDValid, isUUIDValid, toCompleteUUID } from '../../helpers/custom-validators/misc'
10 import { logger } from '../../helpers/logger'
11 import { WEBSERVER } from '../../initializers/constants'
12 import { areValidationErrors } from './shared'
14 const playlistPaths = [
15 join('videos', 'watch', 'playlist'),
20 join('videos', 'watch'),
24 function buildUrls (paths: string[]) {
25 return paths.map(p => WEBSERVER.SCHEME + '://' + join(WEBSERVER.HOST, p) + '/')
28 const startPlaylistURLs = buildUrls(playlistPaths)
29 const startVideoURLs = buildUrls(videoPaths)
31 const isURLOptions = {
36 // We validate 'localhost', so we don't have the top level domain
37 if (isTestOrDevInstance()) {
38 isURLOptions.require_tld = false
41 const oembedValidator = [
52 .isIn([ 'xml', 'json' ]),
54 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
55 logger.debug('Checking oembed parameters', { parameters: req.query })
57 if (areValidationErrors(req, res)) return
59 if (req.query.format !== undefined && req.query.format !== 'json') {
61 status: HttpStatusCode.NOT_IMPLEMENTED_501,
62 message: 'Requested format is not implemented on server.',
64 format: req.query.format
69 const url = req.query.url as string
74 urlPath = new URL(url).pathname
77 status: HttpStatusCode.BAD_REQUEST_400,
85 const isPlaylist = startPlaylistURLs.some(u => url.startsWith(u))
86 const isVideo = isPlaylist ? false : startVideoURLs.some(u => url.startsWith(u))
88 const startIsOk = isVideo || isPlaylist
90 const parts = urlPath.split('/')
92 if (startIsOk === false || parts.length === 0) {
94 status: HttpStatusCode.BAD_REQUEST_400,
95 message: 'Invalid url.',
102 const elementId = toCompleteUUID(parts.pop())
103 if (isIdOrUUIDValid(elementId) === false) {
104 return res.fail({ message: 'Invalid video or playlist id.' })
108 const video = await loadVideo(elementId, 'all')
112 status: HttpStatusCode.NOT_FOUND_404,
113 message: 'Video not found'
118 video.privacy === VideoPrivacy.PUBLIC ||
119 (video.privacy === VideoPrivacy.UNLISTED && isUUIDValid(elementId) === true)
121 res.locals.videoAll = video
126 status: HttpStatusCode.FORBIDDEN_403,
127 message: 'Video is not publicly available'
133 const videoPlaylist = await VideoPlaylistModel.loadWithAccountAndChannelSummary(elementId, undefined)
134 if (!videoPlaylist) {
136 status: HttpStatusCode.NOT_FOUND_404,
137 message: 'Video playlist not found'
142 videoPlaylist.privacy === VideoPlaylistPrivacy.PUBLIC ||
143 (videoPlaylist.privacy === VideoPlaylistPrivacy.UNLISTED && isUUIDValid(elementId))
145 res.locals.videoPlaylistSummary = videoPlaylist
150 status: HttpStatusCode.FORBIDDEN_403,
151 message: 'Playlist is not public'
157 // ---------------------------------------------------------------------------