diff options
author | Chocobozzz <me@florianbigard.com> | 2020-08-05 15:35:58 +0200 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2020-08-07 08:58:29 +0200 |
commit | 6fad8e51c47b9d07bea99b777c1f55c10f6d576d (patch) | |
tree | 09b33652d565a6dbe70e413b88c64aa611f7f4b6 /server/controllers | |
parent | a75292db788d400ba84933693f5f98a83f3aaa60 (diff) | |
download | PeerTube-6fad8e51c47b9d07bea99b777c1f55c10f6d576d.tar.gz PeerTube-6fad8e51c47b9d07bea99b777c1f55c10f6d576d.tar.zst PeerTube-6fad8e51c47b9d07bea99b777c1f55c10f6d576d.zip |
Handle playlist oembed
Diffstat (limited to 'server/controllers')
-rw-r--r-- | server/controllers/services.ts | 73 |
1 files changed, 62 insertions, 11 deletions
diff --git a/server/controllers/services.ts b/server/controllers/services.ts index ec057235f..d0217c30a 100644 --- a/server/controllers/services.ts +++ b/server/controllers/services.ts | |||
@@ -1,7 +1,8 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import { EMBED_SIZE, PREVIEWS_SIZE, WEBSERVER } from '../initializers/constants' | 2 | import { EMBED_SIZE, PREVIEWS_SIZE, WEBSERVER, THUMBNAILS_SIZE } from '../initializers/constants' |
3 | import { asyncMiddleware, oembedValidator } from '../middlewares' | 3 | import { asyncMiddleware, oembedValidator } from '../middlewares' |
4 | import { accountNameWithHostGetValidator } from '../middlewares/validators' | 4 | import { accountNameWithHostGetValidator } from '../middlewares/validators' |
5 | import { MChannelSummary } from '@server/types/models' | ||
5 | 6 | ||
6 | const servicesRouter = express.Router() | 7 | const servicesRouter = express.Router() |
7 | 8 | ||
@@ -23,23 +24,73 @@ export { | |||
23 | // --------------------------------------------------------------------------- | 24 | // --------------------------------------------------------------------------- |
24 | 25 | ||
25 | function generateOEmbed (req: express.Request, res: express.Response) { | 26 | function generateOEmbed (req: express.Request, res: express.Response) { |
27 | if (res.locals.videoAll) return generateVideoOEmbed(req, res) | ||
28 | |||
29 | return generatePlaylistOEmbed(req, res) | ||
30 | } | ||
31 | |||
32 | function generatePlaylistOEmbed (req: express.Request, res: express.Response) { | ||
33 | const playlist = res.locals.videoPlaylistSummary | ||
34 | |||
35 | const json = buildOEmbed({ | ||
36 | channel: playlist.VideoChannel, | ||
37 | title: playlist.name, | ||
38 | embedPath: playlist.getEmbedStaticPath(), | ||
39 | previewPath: playlist.getThumbnailStaticPath(), | ||
40 | previewSize: THUMBNAILS_SIZE, | ||
41 | req | ||
42 | }) | ||
43 | |||
44 | return res.json(json) | ||
45 | } | ||
46 | |||
47 | function generateVideoOEmbed (req: express.Request, res: express.Response) { | ||
26 | const video = res.locals.videoAll | 48 | const video = res.locals.videoAll |
49 | |||
50 | const json = buildOEmbed({ | ||
51 | channel: video.VideoChannel, | ||
52 | title: video.name, | ||
53 | embedPath: video.getEmbedStaticPath(), | ||
54 | previewPath: video.getPreviewStaticPath(), | ||
55 | previewSize: PREVIEWS_SIZE, | ||
56 | req | ||
57 | }) | ||
58 | |||
59 | return res.json(json) | ||
60 | } | ||
61 | |||
62 | function buildOEmbed (options: { | ||
63 | req: express.Request | ||
64 | title: string | ||
65 | channel: MChannelSummary | ||
66 | previewPath: string | null | ||
67 | embedPath: string | ||
68 | previewSize: { | ||
69 | height: number | ||
70 | width: number | ||
71 | } | ||
72 | }) { | ||
73 | const { req, previewSize, previewPath, title, channel, embedPath } = options | ||
74 | |||
27 | const webserverUrl = WEBSERVER.URL | 75 | const webserverUrl = WEBSERVER.URL |
28 | const maxHeight = parseInt(req.query.maxheight, 10) | 76 | const maxHeight = parseInt(req.query.maxheight, 10) |
29 | const maxWidth = parseInt(req.query.maxwidth, 10) | 77 | const maxWidth = parseInt(req.query.maxwidth, 10) |
30 | 78 | ||
31 | const embedUrl = webserverUrl + video.getEmbedStaticPath() | 79 | const embedUrl = webserverUrl + embedPath |
32 | let thumbnailUrl = webserverUrl + video.getPreviewStaticPath() | ||
33 | let embedWidth = EMBED_SIZE.width | 80 | let embedWidth = EMBED_SIZE.width |
34 | let embedHeight = EMBED_SIZE.height | 81 | let embedHeight = EMBED_SIZE.height |
35 | 82 | ||
83 | let thumbnailUrl = previewPath | ||
84 | ? webserverUrl + previewPath | ||
85 | : undefined | ||
86 | |||
36 | if (maxHeight < embedHeight) embedHeight = maxHeight | 87 | if (maxHeight < embedHeight) embedHeight = maxHeight |
37 | if (maxWidth < embedWidth) embedWidth = maxWidth | 88 | if (maxWidth < embedWidth) embedWidth = maxWidth |
38 | 89 | ||
39 | // Our thumbnail is too big for the consumer | 90 | // Our thumbnail is too big for the consumer |
40 | if ( | 91 | if ( |
41 | (maxHeight !== undefined && maxHeight < PREVIEWS_SIZE.height) || | 92 | (maxHeight !== undefined && maxHeight < previewSize.height) || |
42 | (maxWidth !== undefined && maxWidth < PREVIEWS_SIZE.width) | 93 | (maxWidth !== undefined && maxWidth < previewSize.width) |
43 | ) { | 94 | ) { |
44 | thumbnailUrl = undefined | 95 | thumbnailUrl = undefined |
45 | } | 96 | } |
@@ -53,20 +104,20 @@ function generateOEmbed (req: express.Request, res: express.Response) { | |||
53 | html, | 104 | html, |
54 | width: embedWidth, | 105 | width: embedWidth, |
55 | height: embedHeight, | 106 | height: embedHeight, |
56 | title: video.name, | 107 | title: title, |
57 | author_name: video.VideoChannel.Account.name, | 108 | author_name: channel.name, |
58 | author_url: video.VideoChannel.Account.Actor.url, | 109 | author_url: channel.Actor.url, |
59 | provider_name: 'PeerTube', | 110 | provider_name: 'PeerTube', |
60 | provider_url: webserverUrl | 111 | provider_url: webserverUrl |
61 | } | 112 | } |
62 | 113 | ||
63 | if (thumbnailUrl !== undefined) { | 114 | if (thumbnailUrl !== undefined) { |
64 | json.thumbnail_url = thumbnailUrl | 115 | json.thumbnail_url = thumbnailUrl |
65 | json.thumbnail_width = PREVIEWS_SIZE.width | 116 | json.thumbnail_width = previewSize.width |
66 | json.thumbnail_height = PREVIEWS_SIZE.height | 117 | json.thumbnail_height = previewSize.height |
67 | } | 118 | } |
68 | 119 | ||
69 | return res.json(json) | 120 | return json |
70 | } | 121 | } |
71 | 122 | ||
72 | function redirectToAccountUrl (req: express.Request, res: express.Response, next: express.NextFunction) { | 123 | function redirectToAccountUrl (req: express.Request, res: express.Response, next: express.NextFunction) { |