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