]>
Commit | Line | Data |
---|---|---|
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 | import { escapeHTML } from '@shared/core-utils/renderer' | |
7 | ||
8 | const servicesRouter = express.Router() | |
9 | ||
10 | servicesRouter.use('/oembed', | |
11 | asyncMiddleware(oembedValidator), | |
12 | generateOEmbed | |
13 | ) | |
14 | servicesRouter.use('/redirect/accounts/:accountName', | |
15 | asyncMiddleware(accountNameWithHostGetValidator), | |
16 | redirectToAccountUrl | |
17 | ) | |
18 | ||
19 | // --------------------------------------------------------------------------- | |
20 | ||
21 | export { | |
22 | servicesRouter | |
23 | } | |
24 | ||
25 | // --------------------------------------------------------------------------- | |
26 | ||
27 | function generateOEmbed (req: express.Request, res: express.Response) { | |
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, | |
39 | embedPath: playlist.getEmbedStaticPath(), | |
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) { | |
49 | const video = res.locals.videoAll | |
50 | ||
51 | const json = buildOEmbed({ | |
52 | channel: video.VideoChannel, | |
53 | title: video.name, | |
54 | embedPath: video.getEmbedStaticPath(), | |
55 | previewPath: video.getPreviewStaticPath(), | |
56 | previewSize: PREVIEWS_SIZE, | |
57 | req | |
58 | }) | |
59 | ||
60 | return res.json(json) | |
61 | } | |
62 | ||
63 | function buildOEmbed (options: { | |
64 | req: express.Request | |
65 | title: string | |
66 | channel: MChannelSummary | |
67 | previewPath: string | null | |
68 | embedPath: string | |
69 | previewSize: { | |
70 | height: number | |
71 | width: number | |
72 | } | |
73 | }) { | |
74 | const { req, previewSize, previewPath, title, channel, embedPath } = options | |
75 | ||
76 | const webserverUrl = WEBSERVER.URL | |
77 | const maxHeight = parseInt(req.query.maxheight, 10) | |
78 | const maxWidth = parseInt(req.query.maxwidth, 10) | |
79 | ||
80 | const embedUrl = webserverUrl + embedPath | |
81 | let embedWidth = EMBED_SIZE.width | |
82 | let embedHeight = EMBED_SIZE.height | |
83 | const embedTitle = escapeHTML(title) | |
84 | ||
85 | let thumbnailUrl = previewPath | |
86 | ? webserverUrl + previewPath | |
87 | : undefined | |
88 | ||
89 | if (maxHeight < embedHeight) embedHeight = maxHeight | |
90 | if (maxWidth < embedWidth) embedWidth = maxWidth | |
91 | ||
92 | // Our thumbnail is too big for the consumer | |
93 | if ( | |
94 | (maxHeight !== undefined && maxHeight < previewSize.height) || | |
95 | (maxWidth !== undefined && maxWidth < previewSize.width) | |
96 | ) { | |
97 | thumbnailUrl = undefined | |
98 | } | |
99 | ||
100 | const html = `<iframe width="${embedWidth}" height="${embedHeight}" sandbox="allow-same-origin allow-scripts" ` + | |
101 | `title="${embedTitle}" src="${embedUrl}" frameborder="0" allowfullscreen></iframe>` | |
102 | ||
103 | const json: any = { | |
104 | type: 'video', | |
105 | version: '1.0', | |
106 | html, | |
107 | width: embedWidth, | |
108 | height: embedHeight, | |
109 | title: title, | |
110 | author_name: channel.name, | |
111 | author_url: channel.Actor.url, | |
112 | provider_name: 'PeerTube', | |
113 | provider_url: webserverUrl | |
114 | } | |
115 | ||
116 | if (thumbnailUrl !== undefined) { | |
117 | json.thumbnail_url = thumbnailUrl | |
118 | json.thumbnail_width = previewSize.width | |
119 | json.thumbnail_height = previewSize.height | |
120 | } | |
121 | ||
122 | return json | |
123 | } | |
124 | ||
125 | function redirectToAccountUrl (req: express.Request, res: express.Response, next: express.NextFunction) { | |
126 | return res.redirect(res.locals.account.Actor.url) | |
127 | } |