1 import * as 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/core-utils/miscs/http-error-codes'
8 import { isTestInstance } from '../../helpers/core-utils'
9 import { isIdOrUUIDValid, 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 watchRegex = /([^/]+)$/
32 const isURLOptions = {
37 // We validate 'localhost', so we don't have the top level domain
38 if (isTestInstance()) {
39 isURLOptions.require_tld = false
42 const oembedValidator = [
43 query('url').isURL(isURLOptions).withMessage('Should have a valid url'),
44 query('maxwidth').optional().isInt().withMessage('Should have a valid max width'),
45 query('maxheight').optional().isInt().withMessage('Should have a valid max height'),
46 query('format').optional().isIn([ 'xml', 'json' ]).withMessage('Should have a valid format'),
48 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
49 logger.debug('Checking oembed parameters', { parameters: req.query })
51 if (areValidationErrors(req, res)) return
53 if (req.query.format !== undefined && req.query.format !== 'json') {
55 status: HttpStatusCode.NOT_IMPLEMENTED_501,
56 message: 'Requested format is not implemented on server.',
58 format: req.query.format
63 const url = req.query.url as string
65 const isPlaylist = startPlaylistURLs.some(u => url.startsWith(u))
66 const isVideo = isPlaylist ? false : startVideoURLs.some(u => url.startsWith(u))
68 const startIsOk = isVideo || isPlaylist
70 const matches = watchRegex.exec(url)
72 if (startIsOk === false || matches === null) {
74 status: HttpStatusCode.BAD_REQUEST_400,
75 message: 'Invalid url.',
82 const elementId = toCompleteUUID(matches[1])
83 if (isIdOrUUIDValid(elementId) === false) {
84 return res.fail({ message: 'Invalid video or playlist id.' })
88 const video = await loadVideo(elementId, 'all')
92 status: HttpStatusCode.NOT_FOUND_404,
93 message: 'Video not found'
97 if (video.privacy !== VideoPrivacy.PUBLIC) {
99 status: HttpStatusCode.FORBIDDEN_403,
100 message: 'Video is not public'
104 res.locals.videoAll = video
110 const videoPlaylist = await VideoPlaylistModel.loadWithAccountAndChannelSummary(elementId, undefined)
111 if (!videoPlaylist) {
113 status: HttpStatusCode.NOT_FOUND_404,
114 message: 'Video playlist not found'
118 if (videoPlaylist.privacy !== VideoPlaylistPrivacy.PUBLIC) {
120 status: HttpStatusCode.FORBIDDEN_403,
121 message: 'Playlist is not public'
125 res.locals.videoPlaylistSummary = videoPlaylist
131 // ---------------------------------------------------------------------------