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