1 import * as express from 'express'
2 import { CONFIG, EMBED_SIZE, PREVIEWS_SIZE } from '../initializers'
3 import { asyncMiddleware, oembedValidator } from '../middlewares'
4 import { VideoModel } from '../models/video/video'
6 const servicesRouter = express.Router()
8 servicesRouter.use('/oembed',
9 asyncMiddleware(oembedValidator),
13 // ---------------------------------------------------------------------------
19 // ---------------------------------------------------------------------------
21 function generateOEmbed (req: express.Request, res: express.Response, next: express.NextFunction) {
22 const video = res.locals.video as VideoModel
23 const webserverUrl = CONFIG.WEBSERVER.URL
24 const maxHeight = parseInt(req.query.maxheight, 10)
25 const maxWidth = parseInt(req.query.maxwidth, 10)
27 const embedUrl = webserverUrl + video.getEmbedPath()
28 let thumbnailUrl = webserverUrl + video.getPreviewPath()
29 let embedWidth = EMBED_SIZE.width
30 let embedHeight = EMBED_SIZE.height
32 if (maxHeight < embedHeight) embedHeight = maxHeight
33 if (maxWidth < embedWidth) embedWidth = maxWidth
35 // Our thumbnail is too big for the consumer
37 (maxHeight !== undefined && maxHeight < PREVIEWS_SIZE.height) ||
38 (maxWidth !== undefined && maxWidth < PREVIEWS_SIZE.width)
40 thumbnailUrl = undefined
43 const html = `<iframe width="${embedWidth}" height="${embedHeight}" src="${embedUrl}" frameborder="0" allowfullscreen></iframe>`
52 author_name: video.VideoChannel.Account.name,
53 provider_name: 'PeerTube',
54 provider_url: webserverUrl
57 if (thumbnailUrl !== undefined) {
58 json.thumbnail_url = thumbnailUrl
59 json.thumbnail_width = PREVIEWS_SIZE.width
60 json.thumbnail_height = PREVIEWS_SIZE.height