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'
8 const servicesRouter = express.Router()
10 servicesRouter.use('/oembed',
11 asyncMiddleware(oembedValidator),
14 servicesRouter.use('/redirect/accounts/:accountName',
15 asyncMiddleware(accountNameWithHostGetValidator),
19 // ---------------------------------------------------------------------------
25 // ---------------------------------------------------------------------------
27 function generateOEmbed (req: express.Request, res: express.Response) {
28 if (res.locals.videoAll) return generateVideoOEmbed(req, res)
30 return generatePlaylistOEmbed(req, res)
33 function generatePlaylistOEmbed (req: express.Request, res: express.Response) {
34 const playlist = res.locals.videoPlaylistSummary
36 const json = buildOEmbed({
37 channel: playlist.VideoChannel,
39 embedPath: playlist.getEmbedStaticPath() + buildPlayerURLQuery(req.query.url),
40 previewPath: playlist.getThumbnailStaticPath(),
41 previewSize: THUMBNAILS_SIZE,
48 function generateVideoOEmbed (req: express.Request, res: express.Response) {
49 const video = res.locals.videoAll
51 const json = buildOEmbed({
52 channel: video.VideoChannel,
54 embedPath: video.getEmbedStaticPath() + buildPlayerURLQuery(req.query.url),
55 previewPath: video.getPreviewStaticPath(),
56 previewSize: PREVIEWS_SIZE,
63 function buildPlayerURLQuery (inputQueryUrl: string) {
64 const allowedParameters = new Set([
78 'bigPlayBackgroundColor',
83 const params = new URLSearchParams()
85 new URL(inputQueryUrl).searchParams.forEach((v, k) => {
86 if (allowedParameters.has(k)) {
91 const stringQuery = params.toString()
92 if (!stringQuery) return ''
94 return '?' + stringQuery
97 function buildOEmbed (options: {
100 channel: MChannelSummary
101 previewPath: string | null
108 const { req, previewSize, previewPath, title, channel, embedPath } = options
110 const webserverUrl = WEBSERVER.URL
111 const maxHeight = parseInt(req.query.maxheight, 10)
112 const maxWidth = parseInt(req.query.maxwidth, 10)
114 const embedUrl = webserverUrl + embedPath
115 const embedTitle = escapeHTML(title)
117 let thumbnailUrl = previewPath
118 ? webserverUrl + previewPath
121 let embedWidth = EMBED_SIZE.width
122 if (maxWidth < embedWidth) embedWidth = maxWidth
124 let embedHeight = EMBED_SIZE.height
125 if (maxHeight < embedHeight) embedHeight = maxHeight
127 // Our thumbnail is too big for the consumer
129 (maxHeight !== undefined && maxHeight < previewSize.height) ||
130 (maxWidth !== undefined && maxWidth < previewSize.width)
132 thumbnailUrl = undefined
135 const html = `<iframe width="${embedWidth}" height="${embedHeight}" sandbox="allow-same-origin allow-scripts allow-popups" ` +
136 `title="${embedTitle}" src="${embedUrl}" frameborder="0" allowfullscreen></iframe>`
145 author_name: channel.name,
146 author_url: channel.Actor.url,
147 provider_name: 'PeerTube',
148 provider_url: webserverUrl
151 if (thumbnailUrl !== undefined) {
152 json.thumbnail_url = thumbnailUrl
153 json.thumbnail_width = previewSize.width
154 json.thumbnail_height = previewSize.height
160 function redirectToAccountUrl (req: express.Request, res: express.Response, next: express.NextFunction) {
161 return res.redirect(res.locals.account.Actor.url)