]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/services.ts
Add oembed endpoint
[github/Chocobozzz/PeerTube.git] / server / controllers / services.ts
1 import * as express from 'express'
2
3 import { CONFIG, THUMBNAILS_SIZE } from '../initializers'
4 import { oembedValidator } from '../middlewares'
5 import { VideoInstance } from '../models'
6
7 const servicesRouter = express.Router()
8
9 servicesRouter.use('/oembed', oembedValidator, generateOEmbed)
10
11 // ---------------------------------------------------------------------------
12
13 export {
14 servicesRouter
15 }
16
17 // ---------------------------------------------------------------------------
18
19 function generateOEmbed (req: express.Request, res: express.Response, next: express.NextFunction) {
20 const video = res.locals.video as VideoInstance
21 const webserverUrl = CONFIG.WEBSERVER.URL
22 const maxHeight = parseInt(req.query.maxheight, 10)
23 const maxWidth = parseInt(req.query.maxwidth, 10)
24
25 const embedUrl = webserverUrl + video.getEmbedPath()
26 let thumbnailUrl = webserverUrl + video.getThumbnailPath()
27 let embedWidth = 560
28 let embedHeight = 315
29
30 if (maxHeight < embedHeight) embedHeight = maxHeight
31 if (maxWidth < embedWidth) embedWidth = maxWidth
32
33 // Our thumbnail is too big for the consumer
34 if (
35 (maxHeight !== undefined && maxHeight < THUMBNAILS_SIZE.height) ||
36 (maxWidth !== undefined && maxWidth < THUMBNAILS_SIZE.width)
37 ) {
38 thumbnailUrl = undefined
39 }
40
41 const html = `<iframe width="${embedWidth}" height="${embedHeight}" src="${embedUrl}" frameborder="0" allowfullscreen></iframe>`
42
43 const json: any = {
44 type: 'video',
45 version: '1.0',
46 html,
47 width: embedWidth,
48 height: embedHeight,
49 title: video.name,
50 author_name: video.Author.name,
51 provider_name: 'PeerTube',
52 provider_url: webserverUrl
53 }
54
55 if (thumbnailUrl !== undefined) {
56 json.thumbnail_url = thumbnailUrl
57 json.thumbnail_width = THUMBNAILS_SIZE.width
58 json.thumbnail_height = THUMBNAILS_SIZE.height
59 }
60
61 return res.json(json)
62 }