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 = [
42 query('url').isURL(isURLOptions).withMessage('Should have a valid url'),
43 query('maxwidth').optional().isInt().withMessage('Should have a valid max width'),
44 query('maxheight').optional().isInt().withMessage('Should have a valid max height'),
45 query('format').optional().isIn([ 'xml', 'json' ]).withMessage('Should have a valid format'),
47 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
48 logger.debug('Checking oembed parameters', { parameters: req.query })
50 if (areValidationErrors(req, res)) return
52 if (req.query.format !== undefined && req.query.format !== 'json') {
54 status: HttpStatusCode.NOT_IMPLEMENTED_501,
55 message: 'Requested format is not implemented on server.',
57 format: req.query.format
62 const url = req.query.url as string
67 urlPath = new URL(url).pathname
70 status: HttpStatusCode.BAD_REQUEST_400,
78 const isPlaylist = startPlaylistURLs.some(u => url.startsWith(u))
79 const isVideo = isPlaylist ? false : startVideoURLs.some(u => url.startsWith(u))
81 const startIsOk = isVideo || isPlaylist
83 const parts = urlPath.split('/')
85 if (startIsOk === false || parts.length === 0) {
87 status: HttpStatusCode.BAD_REQUEST_400,
88 message: 'Invalid url.',
95 const elementId = toCompleteUUID(parts.pop())
96 if (isIdOrUUIDValid(elementId) === false) {
97 return res.fail({ message: 'Invalid video or playlist id.' })
101 const video = await loadVideo(elementId, 'all')
105 status: HttpStatusCode.NOT_FOUND_404,
106 message: 'Video not found'
111 video.privacy === VideoPrivacy.PUBLIC ||
112 (video.privacy === VideoPrivacy.UNLISTED && isUUIDValid(elementId) === true)
114 res.locals.videoAll = video
119 status: HttpStatusCode.FORBIDDEN_403,
120 message: 'Video is not publicly available'
126 const videoPlaylist = await VideoPlaylistModel.loadWithAccountAndChannelSummary(elementId, undefined)
127 if (!videoPlaylist) {
129 status: HttpStatusCode.NOT_FOUND_404,
130 message: 'Video playlist not found'
135 videoPlaylist.privacy === VideoPlaylistPrivacy.PUBLIC ||
136 (videoPlaylist.privacy === VideoPlaylistPrivacy.UNLISTED && isUUIDValid(elementId))
138 res.locals.videoPlaylistSummary = videoPlaylist
143 status: HttpStatusCode.FORBIDDEN_403,
144 message: 'Playlist is not public'
150 // ---------------------------------------------------------------------------