]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/services.ts
(embed) sandbox the iframe
[github/Chocobozzz/PeerTube.git] / server / controllers / services.ts
CommitLineData
d8755eed 1import * as express from 'express'
a2431b7d 2import { CONFIG, EMBED_SIZE, PREVIEWS_SIZE } from '../initializers'
3fd3ab2d 3import { asyncMiddleware, oembedValidator } from '../middlewares'
e8cb4409 4import { accountsNameWithHostGetValidator } from '../middlewares/validators'
3fd3ab2d 5import { VideoModel } from '../models/video/video'
d8755eed
C
6
7const servicesRouter = express.Router()
8
a2431b7d
C
9servicesRouter.use('/oembed',
10 asyncMiddleware(oembedValidator),
11 generateOEmbed
12)
ad9e39fb 13servicesRouter.use('/redirect/accounts/:accountName',
e8cb4409
C
14 asyncMiddleware(accountsNameWithHostGetValidator),
15 redirectToAccountUrl
16)
d8755eed
C
17
18// ---------------------------------------------------------------------------
19
20export {
21 servicesRouter
22}
23
24// ---------------------------------------------------------------------------
25
26function generateOEmbed (req: express.Request, res: express.Response, next: express.NextFunction) {
3fd3ab2d 27 const video = res.locals.video as VideoModel
d8755eed
C
28 const webserverUrl = CONFIG.WEBSERVER.URL
29 const maxHeight = parseInt(req.query.maxheight, 10)
30 const maxWidth = parseInt(req.query.maxwidth, 10)
31
32 const embedUrl = webserverUrl + video.getEmbedPath()
164174a6
C
33 let thumbnailUrl = webserverUrl + video.getPreviewPath()
34 let embedWidth = EMBED_SIZE.width
35 let embedHeight = EMBED_SIZE.height
d8755eed
C
36
37 if (maxHeight < embedHeight) embedHeight = maxHeight
38 if (maxWidth < embedWidth) embedWidth = maxWidth
39
40 // Our thumbnail is too big for the consumer
41 if (
164174a6
C
42 (maxHeight !== undefined && maxHeight < PREVIEWS_SIZE.height) ||
43 (maxWidth !== undefined && maxWidth < PREVIEWS_SIZE.width)
d8755eed
C
44 ) {
45 thumbnailUrl = undefined
46 }
47
77540346 48 const html = `<iframe width="${embedWidth}" height="${embedHeight}" sandbox="allow-same-origin allow-scripts" src="${embedUrl}" frameborder="0" allowfullscreen></iframe>`
d8755eed
C
49
50 const json: any = {
51 type: 'video',
52 version: '1.0',
53 html,
54 width: embedWidth,
55 height: embedHeight,
56 title: video.name,
38fa2065 57 author_name: video.VideoChannel.Account.name,
d8755eed
C
58 provider_name: 'PeerTube',
59 provider_url: webserverUrl
60 }
61
62 if (thumbnailUrl !== undefined) {
63 json.thumbnail_url = thumbnailUrl
164174a6
C
64 json.thumbnail_width = PREVIEWS_SIZE.width
65 json.thumbnail_height = PREVIEWS_SIZE.height
d8755eed
C
66 }
67
68 return res.json(json)
69}
e8cb4409
C
70
71function redirectToAccountUrl (req: express.Request, res: express.Response, next: express.NextFunction) {
72 return res.redirect(res.locals.account.Actor.url)
73}