]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/services.ts
Add watch messages if live has not started
[github/Chocobozzz/PeerTube.git] / server / controllers / services.ts
CommitLineData
d8755eed 1import * as express from 'express'
6fad8e51 2import { EMBED_SIZE, PREVIEWS_SIZE, WEBSERVER, THUMBNAILS_SIZE } from '../initializers/constants'
3fd3ab2d 3import { asyncMiddleware, oembedValidator } from '../middlewares'
418d092a 4import { accountNameWithHostGetValidator } from '../middlewares/validators'
6fad8e51 5import { MChannelSummary } from '@server/types/models'
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',
418d092a 14 asyncMiddleware(accountNameWithHostGetValidator),
e8cb4409
C
15 redirectToAccountUrl
16)
d8755eed
C
17
18// ---------------------------------------------------------------------------
19
20export {
21 servicesRouter
22}
23
24// ---------------------------------------------------------------------------
25
dae86118 26function generateOEmbed (req: express.Request, res: express.Response) {
6fad8e51
C
27 if (res.locals.videoAll) return generateVideoOEmbed(req, res)
28
29 return generatePlaylistOEmbed(req, res)
30}
31
32function 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
47function generateVideoOEmbed (req: express.Request, res: express.Response) {
453e83ea 48 const video = res.locals.videoAll
6fad8e51
C
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
62function 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
6dd9de95 75 const webserverUrl = WEBSERVER.URL
d8755eed
C
76 const maxHeight = parseInt(req.query.maxheight, 10)
77 const maxWidth = parseInt(req.query.maxwidth, 10)
78
6fad8e51 79 const embedUrl = webserverUrl + embedPath
164174a6
C
80 let embedWidth = EMBED_SIZE.width
81 let embedHeight = EMBED_SIZE.height
d8755eed 82
6fad8e51
C
83 let thumbnailUrl = previewPath
84 ? webserverUrl + previewPath
85 : undefined
86
d8755eed
C
87 if (maxHeight < embedHeight) embedHeight = maxHeight
88 if (maxWidth < embedWidth) embedWidth = maxWidth
89
90 // Our thumbnail is too big for the consumer
91 if (
6fad8e51
C
92 (maxHeight !== undefined && maxHeight < previewSize.height) ||
93 (maxWidth !== undefined && maxWidth < previewSize.width)
d8755eed
C
94 ) {
95 thumbnailUrl = undefined
96 }
97
6ccdf3a2
C
98 const html = `<iframe width="${embedWidth}" height="${embedHeight}" sandbox="allow-same-origin allow-scripts" ` +
99 `src="${embedUrl}" frameborder="0" allowfullscreen></iframe>`
d8755eed
C
100
101 const json: any = {
102 type: 'video',
103 version: '1.0',
104 html,
105 width: embedWidth,
106 height: embedHeight,
6fad8e51
C
107 title: title,
108 author_name: channel.name,
109 author_url: channel.Actor.url,
d8755eed
C
110 provider_name: 'PeerTube',
111 provider_url: webserverUrl
112 }
113
114 if (thumbnailUrl !== undefined) {
115 json.thumbnail_url = thumbnailUrl
6fad8e51
C
116 json.thumbnail_width = previewSize.width
117 json.thumbnail_height = previewSize.height
d8755eed
C
118 }
119
6fad8e51 120 return json
d8755eed 121}
e8cb4409
C
122
123function redirectToAccountUrl (req: express.Request, res: express.Response, next: express.NextFunction) {
124 return res.redirect(res.locals.account.Actor.url)
125}