1 import express from 'express'
2 import { MChannelSummary } from '@server/types/models'
3 import { escapeHTML } from '@shared/core-utils/renderer'
4 import { EMBED_SIZE, PREVIEWS_SIZE, THUMBNAILS_SIZE, WEBSERVER } from '../initializers/constants'
5 import { asyncMiddleware, oembedValidator } from '../middlewares'
6 import { accountNameWithHostGetValidator } from '../middlewares/validators'
7 import { forceNumber } from '@shared/core-utils'
9 const servicesRouter = express.Router()
11 servicesRouter.use('/oembed',
12 asyncMiddleware(oembedValidator),
15 servicesRouter.use('/redirect/accounts/:accountName',
16 asyncMiddleware(accountNameWithHostGetValidator),
20 // ---------------------------------------------------------------------------
26 // ---------------------------------------------------------------------------
28 function generateOEmbed (req: express.Request, res: express.Response) {
29 if (res.locals.videoAll) return generateVideoOEmbed(req, res)
31 return generatePlaylistOEmbed(req, res)
34 function generatePlaylistOEmbed (req: express.Request, res: express.Response) {
35 const playlist = res.locals.videoPlaylistSummary
37 const json = buildOEmbed({
38 channel: playlist.VideoChannel,
40 embedPath: playlist.getEmbedStaticPath() + buildPlayerURLQuery(req.query.url),
41 previewPath: playlist.getThumbnailStaticPath(),
42 previewSize: THUMBNAILS_SIZE,
49 function generateVideoOEmbed (req: express.Request, res: express.Response) {
50 const video = res.locals.videoAll
52 const json = buildOEmbed({
53 channel: video.VideoChannel,
55 embedPath: video.getEmbedStaticPath() + buildPlayerURLQuery(req.query.url),
56 previewPath: video.getPreviewStaticPath(),
57 previewSize: PREVIEWS_SIZE,
64 function buildPlayerURLQuery (inputQueryUrl: string) {
65 const allowedParameters = new Set([
79 'bigPlayBackgroundColor',
84 const params = new URLSearchParams()
86 new URL(inputQueryUrl).searchParams.forEach((v, k) => {
87 if (allowedParameters.has(k)) {
92 const stringQuery = params.toString()
93 if (!stringQuery) return ''
95 return '?' + stringQuery
98 function buildOEmbed (options: {
101 channel: MChannelSummary
102 previewPath: string | null
109 const { req, previewSize, previewPath, title, channel, embedPath } = options
111 const webserverUrl = WEBSERVER.URL
112 const maxHeight = forceNumber(req.query.maxheight)
113 const maxWidth = forceNumber(req.query.maxwidth)
115 const embedUrl = webserverUrl + embedPath
116 const embedTitle = escapeHTML(title)
118 let thumbnailUrl = previewPath
119 ? webserverUrl + previewPath
122 let embedWidth = EMBED_SIZE.width
123 if (maxWidth < embedWidth) embedWidth = maxWidth
125 let embedHeight = EMBED_SIZE.height
126 if (maxHeight < embedHeight) embedHeight = maxHeight
128 // Our thumbnail is too big for the consumer
130 (maxHeight !== undefined && maxHeight < previewSize.height) ||
131 (maxWidth !== undefined && maxWidth < previewSize.width)
133 thumbnailUrl = undefined
136 const html = `<iframe width="${embedWidth}" height="${embedHeight}" sandbox="allow-same-origin allow-scripts allow-popups" ` +
137 `title="${embedTitle}" src="${embedUrl}" frameborder="0" allowfullscreen></iframe>`
146 author_name: channel.name,
147 author_url: channel.Actor.url,
148 provider_name: 'PeerTube',
149 provider_url: webserverUrl
152 if (thumbnailUrl !== undefined) {
153 json.thumbnail_url = thumbnailUrl
154 json.thumbnail_width = previewSize.width
155 json.thumbnail_height = previewSize.height
161 function redirectToAccountUrl (req: express.Request, res: express.Response, next: express.NextFunction) {
162 return res.redirect(res.locals.account.Actor.url)