aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/services.ts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-10-16 10:05:49 +0200
committerChocobozzz <florian.bigard@gmail.com>2017-10-16 10:07:26 +0200
commitd8755eed1e452d2efbfc983af0e9d228d152bf6b (patch)
treedb94181e7c993f67919f4ea2bb12f08401c437c2 /server/controllers/services.ts
parent334ddfa47120ae53bc2643792ec5e1065a4d1141 (diff)
downloadPeerTube-d8755eed1e452d2efbfc983af0e9d228d152bf6b.tar.gz
PeerTube-d8755eed1e452d2efbfc983af0e9d228d152bf6b.tar.zst
PeerTube-d8755eed1e452d2efbfc983af0e9d228d152bf6b.zip
Add oembed endpoint
Diffstat (limited to 'server/controllers/services.ts')
-rw-r--r--server/controllers/services.ts62
1 files changed, 62 insertions, 0 deletions
diff --git a/server/controllers/services.ts b/server/controllers/services.ts
new file mode 100644
index 000000000..3ce6bd526
--- /dev/null
+++ b/server/controllers/services.ts
@@ -0,0 +1,62 @@
1import * as express from 'express'
2
3import { CONFIG, THUMBNAILS_SIZE } from '../initializers'
4import { oembedValidator } from '../middlewares'
5import { VideoInstance } from '../models'
6
7const servicesRouter = express.Router()
8
9servicesRouter.use('/oembed', oembedValidator, generateOEmbed)
10
11// ---------------------------------------------------------------------------
12
13export {
14 servicesRouter
15}
16
17// ---------------------------------------------------------------------------
18
19function generateOEmbed (req: express.Request, res: express.Response, next: express.NextFunction) {
20 const video = res.locals.video as VideoInstance
21 const webserverUrl = CONFIG.WEBSERVER.URL
22 const maxHeight = parseInt(req.query.maxheight, 10)
23 const maxWidth = parseInt(req.query.maxwidth, 10)
24
25 const embedUrl = webserverUrl + video.getEmbedPath()
26 let thumbnailUrl = webserverUrl + video.getThumbnailPath()
27 let embedWidth = 560
28 let embedHeight = 315
29
30 if (maxHeight < embedHeight) embedHeight = maxHeight
31 if (maxWidth < embedWidth) embedWidth = maxWidth
32
33 // Our thumbnail is too big for the consumer
34 if (
35 (maxHeight !== undefined && maxHeight < THUMBNAILS_SIZE.height) ||
36 (maxWidth !== undefined && maxWidth < THUMBNAILS_SIZE.width)
37 ) {
38 thumbnailUrl = undefined
39 }
40
41 const html = `<iframe width="${embedWidth}" height="${embedHeight}" src="${embedUrl}" frameborder="0" allowfullscreen></iframe>`
42
43 const json: any = {
44 type: 'video',
45 version: '1.0',
46 html,
47 width: embedWidth,
48 height: embedHeight,
49 title: video.name,
50 author_name: video.Author.name,
51 provider_name: 'PeerTube',
52 provider_url: webserverUrl
53 }
54
55 if (thumbnailUrl !== undefined) {
56 json.thumbnail_url = thumbnailUrl
57 json.thumbnail_width = THUMBNAILS_SIZE.width
58 json.thumbnail_height = THUMBNAILS_SIZE.height
59 }
60
61 return res.json(json)
62}