]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/services.ts
Live streaming implementation first step
[github/Chocobozzz/PeerTube.git] / server / controllers / services.ts
1 import * as express from 'express'
2 import { EMBED_SIZE, PREVIEWS_SIZE, WEBSERVER, THUMBNAILS_SIZE } from '../initializers/constants'
3 import { asyncMiddleware, oembedValidator } from '../middlewares'
4 import { accountNameWithHostGetValidator } from '../middlewares/validators'
5 import { MChannelSummary } from '@server/types/models'
6
7 const servicesRouter = express.Router()
8
9 servicesRouter.use('/oembed',
10 asyncMiddleware(oembedValidator),
11 generateOEmbed
12 )
13 servicesRouter.use('/redirect/accounts/:accountName',
14 asyncMiddleware(accountNameWithHostGetValidator),
15 redirectToAccountUrl
16 )
17
18 // ---------------------------------------------------------------------------
19
20 export {
21 servicesRouter
22 }
23
24 // ---------------------------------------------------------------------------
25
26 function generateOEmbed (req: express.Request, res: express.Response) {
27 if (res.locals.videoAll) return generateVideoOEmbed(req, res)
28
29 return generatePlaylistOEmbed(req, res)
30 }
31
32 function generatePlaylistOEmbed (req: express.Request, res: express.Response) {
33 const playlist = res.locals.videoPlaylistSummary
34
35 const json = buildOEmbed({
36 channel: playlist.VideoChannel,
37 title: playlist.name,
38 embedPath: playlist.getEmbedStaticPath(),
39 previewPath: playlist.getThumbnailStaticPath(),
40 previewSize: THUMBNAILS_SIZE,
41 req
42 })
43
44 return res.json(json)
45 }
46
47 function generateVideoOEmbed (req: express.Request, res: express.Response) {
48 const video = res.locals.videoAll
49
50 const json = buildOEmbed({
51 channel: video.VideoChannel,
52 title: video.name,
53 embedPath: video.getEmbedStaticPath(),
54 previewPath: video.getPreviewStaticPath(),
55 previewSize: PREVIEWS_SIZE,
56 req
57 })
58
59 return res.json(json)
60 }
61
62 function buildOEmbed (options: {
63 req: express.Request
64 title: string
65 channel: MChannelSummary
66 previewPath: string | null
67 embedPath: string
68 previewSize: {
69 height: number
70 width: number
71 }
72 }) {
73 const { req, previewSize, previewPath, title, channel, embedPath } = options
74
75 const webserverUrl = WEBSERVER.URL
76 const maxHeight = parseInt(req.query.maxheight, 10)
77 const maxWidth = parseInt(req.query.maxwidth, 10)
78
79 const embedUrl = webserverUrl + embedPath
80 let embedWidth = EMBED_SIZE.width
81 let embedHeight = EMBED_SIZE.height
82
83 let thumbnailUrl = previewPath
84 ? webserverUrl + previewPath
85 : undefined
86
87 if (maxHeight < embedHeight) embedHeight = maxHeight
88 if (maxWidth < embedWidth) embedWidth = maxWidth
89
90 // Our thumbnail is too big for the consumer
91 if (
92 (maxHeight !== undefined && maxHeight < previewSize.height) ||
93 (maxWidth !== undefined && maxWidth < previewSize.width)
94 ) {
95 thumbnailUrl = undefined
96 }
97
98 const html = `<iframe width="${embedWidth}" height="${embedHeight}" sandbox="allow-same-origin allow-scripts" ` +
99 `src="${embedUrl}" frameborder="0" allowfullscreen></iframe>`
100
101 const json: any = {
102 type: 'video',
103 version: '1.0',
104 html,
105 width: embedWidth,
106 height: embedHeight,
107 title: title,
108 author_name: channel.name,
109 author_url: channel.Actor.url,
110 provider_name: 'PeerTube',
111 provider_url: webserverUrl
112 }
113
114 if (thumbnailUrl !== undefined) {
115 json.thumbnail_url = thumbnailUrl
116 json.thumbnail_width = previewSize.width
117 json.thumbnail_height = previewSize.height
118 }
119
120 return json
121 }
122
123 function redirectToAccountUrl (req: express.Request, res: express.Response, next: express.NextFunction) {
124 return res.redirect(res.locals.account.Actor.url)
125 }