1 import * as express from 'express'
2 import { EMBED_SIZE, PREVIEWS_SIZE, WEBSERVER, THUMBNAILS_SIZE } from '../initializers/constants'
3 import { asyncMiddleware, oembedValidator } from '../middlewares'
4 import { accountNameWithHostGetValidator } from '../middlewares/validators'
5 import { MChannelSummary } from '@server/types/models'
7 const servicesRouter = express.Router()
9 servicesRouter.use('/oembed',
10 asyncMiddleware(oembedValidator),
13 servicesRouter.use('/redirect/accounts/:accountName',
14 asyncMiddleware(accountNameWithHostGetValidator),
18 // ---------------------------------------------------------------------------
24 // ---------------------------------------------------------------------------
26 function generateOEmbed (req: express.Request, res: express.Response) {
27 if (res.locals.videoAll) return generateVideoOEmbed(req, res)
29 return generatePlaylistOEmbed(req, res)
32 function generatePlaylistOEmbed (req: express.Request, res: express.Response) {
33 const playlist = res.locals.videoPlaylistSummary
35 const json = buildOEmbed({
36 channel: playlist.VideoChannel,
38 embedPath: playlist.getEmbedStaticPath(),
39 previewPath: playlist.getThumbnailStaticPath(),
40 previewSize: THUMBNAILS_SIZE,
47 function generateVideoOEmbed (req: express.Request, res: express.Response) {
48 const video = res.locals.videoAll
50 const json = buildOEmbed({
51 channel: video.VideoChannel,
53 embedPath: video.getEmbedStaticPath(),
54 previewPath: video.getPreviewStaticPath(),
55 previewSize: PREVIEWS_SIZE,
62 function buildOEmbed (options: {
65 channel: MChannelSummary
66 previewPath: string | null
73 const { req, previewSize, previewPath, title, channel, embedPath } = options
75 const webserverUrl = WEBSERVER.URL
76 const maxHeight = parseInt(req.query.maxheight, 10)
77 const maxWidth = parseInt(req.query.maxwidth, 10)
79 const embedUrl = webserverUrl + embedPath
80 let embedWidth = EMBED_SIZE.width
81 let embedHeight = EMBED_SIZE.height
83 let thumbnailUrl = previewPath
84 ? webserverUrl + previewPath
87 if (maxHeight < embedHeight) embedHeight = maxHeight
88 if (maxWidth < embedWidth) embedWidth = maxWidth
90 // Our thumbnail is too big for the consumer
92 (maxHeight !== undefined && maxHeight < previewSize.height) ||
93 (maxWidth !== undefined && maxWidth < previewSize.width)
95 thumbnailUrl = undefined
98 const html = `<iframe width="${embedWidth}" height="${embedHeight}" sandbox="allow-same-origin allow-scripts" ` +
99 `src="${embedUrl}" frameborder="0" allowfullscreen></iframe>`
108 author_name: channel.name,
109 author_url: channel.Actor.url,
110 provider_name: 'PeerTube',
111 provider_url: webserverUrl
114 if (thumbnailUrl !== undefined) {
115 json.thumbnail_url = thumbnailUrl
116 json.thumbnail_width = previewSize.width
117 json.thumbnail_height = previewSize.height
123 function redirectToAccountUrl (req: express.Request, res: express.Response, next: express.NextFunction) {
124 return res.redirect(res.locals.account.Actor.url)