]>
Commit | Line | Data |
---|---|---|
1 | import * as express from 'express' | |
2 | import { EMBED_SIZE, PREVIEWS_SIZE, WEBSERVER } from '../initializers/constants' | |
3 | import { asyncMiddleware, oembedValidator } from '../middlewares' | |
4 | import { accountNameWithHostGetValidator } from '../middlewares/validators' | |
5 | ||
6 | const servicesRouter = express.Router() | |
7 | ||
8 | servicesRouter.use('/oembed', | |
9 | asyncMiddleware(oembedValidator), | |
10 | generateOEmbed | |
11 | ) | |
12 | servicesRouter.use('/redirect/accounts/:accountName', | |
13 | asyncMiddleware(accountNameWithHostGetValidator), | |
14 | redirectToAccountUrl | |
15 | ) | |
16 | ||
17 | // --------------------------------------------------------------------------- | |
18 | ||
19 | export { | |
20 | servicesRouter | |
21 | } | |
22 | ||
23 | // --------------------------------------------------------------------------- | |
24 | ||
25 | function generateOEmbed (req: express.Request, res: express.Response) { | |
26 | const video = res.locals.videoAll | |
27 | const webserverUrl = WEBSERVER.URL | |
28 | const maxHeight = parseInt(req.query.maxheight, 10) | |
29 | const maxWidth = parseInt(req.query.maxwidth, 10) | |
30 | ||
31 | const embedUrl = webserverUrl + video.getEmbedStaticPath() | |
32 | let thumbnailUrl = webserverUrl + video.getPreviewStaticPath() | |
33 | let embedWidth = EMBED_SIZE.width | |
34 | let embedHeight = EMBED_SIZE.height | |
35 | ||
36 | if (maxHeight < embedHeight) embedHeight = maxHeight | |
37 | if (maxWidth < embedWidth) embedWidth = maxWidth | |
38 | ||
39 | // Our thumbnail is too big for the consumer | |
40 | if ( | |
41 | (maxHeight !== undefined && maxHeight < PREVIEWS_SIZE.height) || | |
42 | (maxWidth !== undefined && maxWidth < PREVIEWS_SIZE.width) | |
43 | ) { | |
44 | thumbnailUrl = undefined | |
45 | } | |
46 | ||
47 | const html = `<iframe width="${embedWidth}" height="${embedHeight}" sandbox="allow-same-origin allow-scripts" ` + | |
48 | `src="${embedUrl}" frameborder="0" allowfullscreen></iframe>` | |
49 | ||
50 | const json: any = { | |
51 | type: 'video', | |
52 | version: '1.0', | |
53 | html, | |
54 | width: embedWidth, | |
55 | height: embedHeight, | |
56 | title: video.name, | |
57 | author_name: video.VideoChannel.Account.name, | |
58 | author_url: video.VideoChannel.Account.Actor.url, | |
59 | provider_name: 'PeerTube', | |
60 | provider_url: webserverUrl | |
61 | } | |
62 | ||
63 | if (thumbnailUrl !== undefined) { | |
64 | json.thumbnail_url = thumbnailUrl | |
65 | json.thumbnail_width = PREVIEWS_SIZE.width | |
66 | json.thumbnail_height = PREVIEWS_SIZE.height | |
67 | } | |
68 | ||
69 | return res.json(json) | |
70 | } | |
71 | ||
72 | function redirectToAccountUrl (req: express.Request, res: express.Response, next: express.NextFunction) { | |
73 | return res.redirect(res.locals.account.Actor.url) | |
74 | } |