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 { WEBSERVER } from '../../initializers/constants'
11 import { areValidationErrors } from './shared'
13 const playlistPaths = [
14 join('videos', 'watch', 'playlist'),
19 join('videos', 'watch'),
23 function buildUrls (paths: string[]) {
24 return paths.map(p => WEBSERVER.SCHEME + '://' + join(WEBSERVER.HOST, p) + '/')
27 const startPlaylistURLs = buildUrls(playlistPaths)
28 const startVideoURLs = buildUrls(videoPaths)
30 const isURLOptions = {
35 // We validate 'localhost', so we don't have the top level domain
36 if (isTestOrDevInstance()) {
37 isURLOptions.require_tld = false
40 const oembedValidator = [
51 .isIn([ 'xml', 'json' ]),
53 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
54 if (areValidationErrors(req, res)) return
56 if (req.query.format !== undefined && req.query.format !== 'json') {
58 status: HttpStatusCode.NOT_IMPLEMENTED_501,
59 message: 'Requested format is not implemented on server.',
61 format: req.query.format
66 const url = req.query.url as string
71 urlPath = new URL(url).pathname
74 status: HttpStatusCode.BAD_REQUEST_400,
82 const isPlaylist = startPlaylistURLs.some(u => url.startsWith(u))
83 const isVideo = isPlaylist ? false : startVideoURLs.some(u => url.startsWith(u))
85 const startIsOk = isVideo || isPlaylist
87 const parts = urlPath.split('/')
89 if (startIsOk === false || parts.length === 0) {
91 status: HttpStatusCode.BAD_REQUEST_400,
92 message: 'Invalid url.',
99 const elementId = toCompleteUUID(parts.pop())
100 if (isIdOrUUIDValid(elementId) === false) {
101 return res.fail({ message: 'Invalid video or playlist id.' })
105 const video = await loadVideo(elementId, 'all')
109 status: HttpStatusCode.NOT_FOUND_404,
110 message: 'Video not found'
115 video.privacy === VideoPrivacy.PUBLIC ||
116 (video.privacy === VideoPrivacy.UNLISTED && isUUIDValid(elementId) === true)
118 res.locals.videoAll = video
123 status: HttpStatusCode.FORBIDDEN_403,
124 message: 'Video is not publicly available'
130 const videoPlaylist = await VideoPlaylistModel.loadWithAccountAndChannelSummary(elementId, undefined)
131 if (!videoPlaylist) {
133 status: HttpStatusCode.NOT_FOUND_404,
134 message: 'Video playlist not found'
139 videoPlaylist.privacy === VideoPlaylistPrivacy.PUBLIC ||
140 (videoPlaylist.privacy === VideoPlaylistPrivacy.UNLISTED && isUUIDValid(elementId))
142 res.locals.videoPlaylistSummary = videoPlaylist
147 status: HttpStatusCode.FORBIDDEN_403,
148 message: 'Playlist is not public'
154 // ---------------------------------------------------------------------------