1 import * as express from 'express'
2 import { query } from 'express-validator'
3 import { join } from 'path'
4 import { fetchVideo } from '@server/helpers/video'
5 import { VideoPlaylistModel } from '@server/models/video/video-playlist'
6 import { VideoPlaylistPrivacy, VideoPrivacy } from '@shared/models'
7 import { isTestInstance } from '../../helpers/core-utils'
8 import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc'
9 import { logger } from '../../helpers/logger'
10 import { WEBSERVER } from '../../initializers/constants'
11 import { areValidationErrors } from './utils'
12 import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
14 const startVideoPlaylistsURL = WEBSERVER.SCHEME + '://' + join(WEBSERVER.HOST, 'videos', 'watch', 'playlist') + '/'
15 const startVideosURL = WEBSERVER.SCHEME + '://' + join(WEBSERVER.HOST, 'videos', 'watch') + '/'
17 const watchRegex = /([^/]+)$/
18 const isURLOptions = {
23 // We validate 'localhost', so we don't have the top level domain
24 if (isTestInstance()) {
25 isURLOptions.require_tld = false
28 const oembedValidator = [
29 query('url').isURL(isURLOptions).withMessage('Should have a valid url'),
30 query('maxwidth').optional().isInt().withMessage('Should have a valid max width'),
31 query('maxheight').optional().isInt().withMessage('Should have a valid max height'),
32 query('format').optional().isIn([ 'xml', 'json' ]).withMessage('Should have a valid format'),
34 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
35 logger.debug('Checking oembed parameters', { parameters: req.query })
37 if (areValidationErrors(req, res)) return
39 if (req.query.format !== undefined && req.query.format !== 'json') {
40 return res.status(HttpStatusCode.NOT_IMPLEMENTED_501)
41 .json({ error: 'Requested format is not implemented on server.' })
44 const url = req.query.url as string
46 const isPlaylist = url.startsWith(startVideoPlaylistsURL)
47 const isVideo = isPlaylist ? false : url.startsWith(startVideosURL)
49 const startIsOk = isVideo || isPlaylist
51 const matches = watchRegex.exec(url)
53 if (startIsOk === false || matches === null) {
54 return res.status(HttpStatusCode.BAD_REQUEST_400)
55 .json({ error: 'Invalid url.' })
58 const elementId = matches[1]
59 if (isIdOrUUIDValid(elementId) === false) {
60 return res.status(HttpStatusCode.BAD_REQUEST_400)
61 .json({ error: 'Invalid video or playlist id.' })
65 const video = await fetchVideo(elementId, 'all')
68 return res.status(HttpStatusCode.NOT_FOUND_404)
69 .json({ error: 'Video not found' })
72 if (video.privacy !== VideoPrivacy.PUBLIC) {
73 return res.status(HttpStatusCode.FORBIDDEN_403)
74 .json({ error: 'Video is not public' })
77 res.locals.videoAll = video
83 const videoPlaylist = await VideoPlaylistModel.loadWithAccountAndChannelSummary(elementId, undefined)
85 return res.status(HttpStatusCode.NOT_FOUND_404)
86 .json({ error: 'Video playlist not found' })
89 if (videoPlaylist.privacy !== VideoPlaylistPrivacy.PUBLIC) {
90 return res.status(HttpStatusCode.FORBIDDEN_403)
91 .json({ error: 'Playlist is not public' })
94 res.locals.videoPlaylistSummary = videoPlaylist
100 // ---------------------------------------------------------------------------