1 import * as express from 'express'
2 import { CONFIG, EMBED_SIZE, PREVIEWS_SIZE } from '../initializers'
3 import { asyncMiddleware, oembedValidator } from '../middlewares'
4 import { accountsNameWithHostGetValidator } from '../middlewares/validators'
5 import { VideoModel } from '../models/video/video'
7 const servicesRouter = express.Router()
9 servicesRouter.use('/oembed',
10 asyncMiddleware(oembedValidator),
13 servicesRouter.use('/redirect/accounts/:accountName',
14 asyncMiddleware(accountsNameWithHostGetValidator),
18 // ---------------------------------------------------------------------------
24 // ---------------------------------------------------------------------------
26 function generateOEmbed (req: express.Request, res: express.Response, next: express.NextFunction) {
27 const video = res.locals.video as VideoModel
28 const webserverUrl = CONFIG.WEBSERVER.URL
29 const maxHeight = parseInt(req.query.maxheight, 10)
30 const maxWidth = parseInt(req.query.maxwidth, 10)
32 const embedUrl = webserverUrl + video.getEmbedPath()
33 let thumbnailUrl = webserverUrl + video.getPreviewPath()
34 let embedWidth = EMBED_SIZE.width
35 let embedHeight = EMBED_SIZE.height
37 if (maxHeight < embedHeight) embedHeight = maxHeight
38 if (maxWidth < embedWidth) embedWidth = maxWidth
40 // Our thumbnail is too big for the consumer
42 (maxHeight !== undefined && maxHeight < PREVIEWS_SIZE.height) ||
43 (maxWidth !== undefined && maxWidth < PREVIEWS_SIZE.width)
45 thumbnailUrl = undefined
48 const html = `<iframe width="${embedWidth}" height="${embedHeight}" src="${embedUrl}" frameborder="0" allowfullscreen></iframe>`
57 author_name: video.VideoChannel.Account.name,
58 provider_name: 'PeerTube',
59 provider_url: webserverUrl
62 if (thumbnailUrl !== undefined) {
63 json.thumbnail_url = thumbnailUrl
64 json.thumbnail_width = PREVIEWS_SIZE.width
65 json.thumbnail_height = PREVIEWS_SIZE.height
71 function redirectToAccountUrl (req: express.Request, res: express.Response, next: express.NextFunction) {
72 return res.redirect(res.locals.account.Actor.url)