1 import * as express from 'express'
3 import { CONFIG, EMBED_SIZE, PREVIEWS_SIZE } from '../initializers'
4 import { oembedValidator } from '../middlewares'
5 import { asyncMiddleware } from '../middlewares/async'
6 import { VideoInstance } from '../models'
8 const servicesRouter = express.Router()
10 servicesRouter.use('/oembed',
11 asyncMiddleware(oembedValidator),
15 // ---------------------------------------------------------------------------
21 // ---------------------------------------------------------------------------
23 function generateOEmbed (req: express.Request, res: express.Response, next: express.NextFunction) {
24 const video = res.locals.video as VideoInstance
25 const webserverUrl = CONFIG.WEBSERVER.URL
26 const maxHeight = parseInt(req.query.maxheight, 10)
27 const maxWidth = parseInt(req.query.maxwidth, 10)
29 const embedUrl = webserverUrl + video.getEmbedPath()
30 let thumbnailUrl = webserverUrl + video.getPreviewPath()
31 let embedWidth = EMBED_SIZE.width
32 let embedHeight = EMBED_SIZE.height
34 if (maxHeight < embedHeight) embedHeight = maxHeight
35 if (maxWidth < embedWidth) embedWidth = maxWidth
37 // Our thumbnail is too big for the consumer
39 (maxHeight !== undefined && maxHeight < PREVIEWS_SIZE.height) ||
40 (maxWidth !== undefined && maxWidth < PREVIEWS_SIZE.width)
42 thumbnailUrl = undefined
45 const html = `<iframe width="${embedWidth}" height="${embedHeight}" src="${embedUrl}" frameborder="0" allowfullscreen></iframe>`
54 author_name: video.VideoChannel.Account.name,
55 provider_name: 'PeerTube',
56 provider_url: webserverUrl
59 if (thumbnailUrl !== undefined) {
60 json.thumbnail_url = thumbnailUrl
61 json.thumbnail_width = PREVIEWS_SIZE.width
62 json.thumbnail_height = PREVIEWS_SIZE.height