]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/services.ts
Translated using Weblate (German)
[github/Chocobozzz/PeerTube.git] / server / controllers / services.ts
1 import express from 'express'
2 import { MChannelSummary } from '@server/types/models'
3 import { escapeHTML } from '@shared/core-utils/renderer'
4 import { EMBED_SIZE, PREVIEWS_SIZE, THUMBNAILS_SIZE, WEBSERVER } from '../initializers/constants'
5 import { asyncMiddleware, oembedValidator } from '../middlewares'
6 import { accountNameWithHostGetValidator } from '../middlewares/validators'
7 import { forceNumber } from '@shared/core-utils'
8
9 const servicesRouter = express.Router()
10
11 servicesRouter.use('/oembed',
12 asyncMiddleware(oembedValidator),
13 generateOEmbed
14 )
15 servicesRouter.use('/redirect/accounts/:accountName',
16 asyncMiddleware(accountNameWithHostGetValidator),
17 redirectToAccountUrl
18 )
19
20 // ---------------------------------------------------------------------------
21
22 export {
23 servicesRouter
24 }
25
26 // ---------------------------------------------------------------------------
27
28 function generateOEmbed (req: express.Request, res: express.Response) {
29 if (res.locals.videoAll) return generateVideoOEmbed(req, res)
30
31 return generatePlaylistOEmbed(req, res)
32 }
33
34 function generatePlaylistOEmbed (req: express.Request, res: express.Response) {
35 const playlist = res.locals.videoPlaylistSummary
36
37 const json = buildOEmbed({
38 channel: playlist.VideoChannel,
39 title: playlist.name,
40 embedPath: playlist.getEmbedStaticPath() + buildPlayerURLQuery(req.query.url),
41 previewPath: playlist.getThumbnailStaticPath(),
42 previewSize: THUMBNAILS_SIZE,
43 req
44 })
45
46 return res.json(json)
47 }
48
49 function generateVideoOEmbed (req: express.Request, res: express.Response) {
50 const video = res.locals.videoAll
51
52 const json = buildOEmbed({
53 channel: video.VideoChannel,
54 title: video.name,
55 embedPath: video.getEmbedStaticPath() + buildPlayerURLQuery(req.query.url),
56 previewPath: video.getPreviewStaticPath(),
57 previewSize: PREVIEWS_SIZE,
58 req
59 })
60
61 return res.json(json)
62 }
63
64 function buildPlayerURLQuery (inputQueryUrl: string) {
65 const allowedParameters = new Set([
66 'start',
67 'stop',
68 'loop',
69 'autoplay',
70 'muted',
71 'controls',
72 'controlBar',
73 'title',
74 'api',
75 'warningTitle',
76 'peertubeLink',
77 'p2p',
78 'subtitle',
79 'bigPlayBackgroundColor',
80 'mode',
81 'foregroundColor'
82 ])
83
84 const params = new URLSearchParams()
85
86 new URL(inputQueryUrl).searchParams.forEach((v, k) => {
87 if (allowedParameters.has(k)) {
88 params.append(k, v)
89 }
90 })
91
92 const stringQuery = params.toString()
93 if (!stringQuery) return ''
94
95 return '?' + stringQuery
96 }
97
98 function buildOEmbed (options: {
99 req: express.Request
100 title: string
101 channel: MChannelSummary
102 previewPath: string | null
103 embedPath: string
104 previewSize: {
105 height: number
106 width: number
107 }
108 }) {
109 const { req, previewSize, previewPath, title, channel, embedPath } = options
110
111 const webserverUrl = WEBSERVER.URL
112 const maxHeight = forceNumber(req.query.maxheight)
113 const maxWidth = forceNumber(req.query.maxwidth)
114
115 const embedUrl = webserverUrl + embedPath
116 const embedTitle = escapeHTML(title)
117
118 let thumbnailUrl = previewPath
119 ? webserverUrl + previewPath
120 : undefined
121
122 let embedWidth = EMBED_SIZE.width
123 if (maxWidth < embedWidth) embedWidth = maxWidth
124
125 let embedHeight = EMBED_SIZE.height
126 if (maxHeight < embedHeight) embedHeight = maxHeight
127
128 // Our thumbnail is too big for the consumer
129 if (
130 (maxHeight !== undefined && maxHeight < previewSize.height) ||
131 (maxWidth !== undefined && maxWidth < previewSize.width)
132 ) {
133 thumbnailUrl = undefined
134 }
135
136 const html = `<iframe width="${embedWidth}" height="${embedHeight}" sandbox="allow-same-origin allow-scripts allow-popups" ` +
137 `title="${embedTitle}" src="${embedUrl}" frameborder="0" allowfullscreen></iframe>`
138
139 const json: any = {
140 type: 'video',
141 version: '1.0',
142 html,
143 width: embedWidth,
144 height: embedHeight,
145 title,
146 author_name: channel.name,
147 author_url: channel.Actor.url,
148 provider_name: 'PeerTube',
149 provider_url: webserverUrl
150 }
151
152 if (thumbnailUrl !== undefined) {
153 json.thumbnail_url = thumbnailUrl
154 json.thumbnail_width = previewSize.width
155 json.thumbnail_height = previewSize.height
156 }
157
158 return json
159 }
160
161 function redirectToAccountUrl (req: express.Request, res: express.Response, next: express.NextFunction) {
162 return res.redirect(res.locals.account.Actor.url)
163 }