1 import * as express from 'express'
2 import { EMBED_SIZE, PREVIEWS_SIZE, WEBSERVER } from '../initializers/constants'
3 import { asyncMiddleware, oembedValidator } from '../middlewares'
4 import { accountNameWithHostGetValidator } from '../middlewares/validators'
6 const servicesRouter = express.Router()
8 servicesRouter.use('/oembed',
9 asyncMiddleware(oembedValidator),
12 servicesRouter.use('/redirect/accounts/:accountName',
13 asyncMiddleware(accountNameWithHostGetValidator),
17 // ---------------------------------------------------------------------------
23 // ---------------------------------------------------------------------------
25 function generateOEmbed (req: express.Request, res: express.Response) {
26 const video = res.locals.video
27 const webserverUrl = WEBSERVER.URL
28 const maxHeight = parseInt(req.query.maxheight, 10)
29 const maxWidth = parseInt(req.query.maxwidth, 10)
31 const embedUrl = webserverUrl + video.getEmbedStaticPath()
32 let thumbnailUrl = webserverUrl + video.getPreviewStaticPath()
33 let embedWidth = EMBED_SIZE.width
34 let embedHeight = EMBED_SIZE.height
36 if (maxHeight < embedHeight) embedHeight = maxHeight
37 if (maxWidth < embedWidth) embedWidth = maxWidth
39 // Our thumbnail is too big for the consumer
41 (maxHeight !== undefined && maxHeight < PREVIEWS_SIZE.height) ||
42 (maxWidth !== undefined && maxWidth < PREVIEWS_SIZE.width)
44 thumbnailUrl = undefined
47 const html = `<iframe width="${embedWidth}" height="${embedHeight}" sandbox="allow-same-origin allow-scripts" ` +
48 `src="${embedUrl}" frameborder="0" allowfullscreen></iframe>`
57 author_name: video.VideoChannel.Account.name,
58 author_url: video.VideoChannel.Account.Actor.url,
59 provider_name: 'PeerTube',
60 provider_url: webserverUrl
63 if (thumbnailUrl !== undefined) {
64 json.thumbnail_url = thumbnailUrl
65 json.thumbnail_width = PREVIEWS_SIZE.width
66 json.thumbnail_height = PREVIEWS_SIZE.height
72 function redirectToAccountUrl (req: express.Request, res: express.Response, next: express.NextFunction) {
73 return res.redirect(res.locals.account.Actor.url)