aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/client.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/client.ts
parent334ddfa47120ae53bc2643792ec5e1065a4d1141 (diff)
downloadPeerTube-d8755eed1e452d2efbfc983af0e9d228d152bf6b.tar.gz
PeerTube-d8755eed1e452d2efbfc983af0e9d228d152bf6b.tar.zst
PeerTube-d8755eed1e452d2efbfc983af0e9d228d152bf6b.zip
Add oembed endpoint
Diffstat (limited to 'server/controllers/client.ts')
-rw-r--r--server/controllers/client.ts32
1 files changed, 22 insertions, 10 deletions
diff --git a/server/controllers/client.ts b/server/controllers/client.ts
index b23f7e1ae..e3c962058 100644
--- a/server/controllers/client.ts
+++ b/server/controllers/client.ts
@@ -8,7 +8,7 @@ import {
8 CONFIG, 8 CONFIG,
9 STATIC_PATHS, 9 STATIC_PATHS,
10 STATIC_MAX_AGE, 10 STATIC_MAX_AGE,
11 OPENGRAPH_COMMENT 11 OPENGRAPH_AND_OEMBED_COMMENT
12} from '../initializers' 12} from '../initializers'
13import { root, readFileBufferPromise } from '../helpers' 13import { root, readFileBufferPromise } from '../helpers'
14import { VideoInstance } from '../models' 14import { VideoInstance } from '../models'
@@ -19,7 +19,7 @@ const distPath = join(root(), 'client', 'dist')
19const embedPath = join(distPath, 'standalone', 'videos', 'embed.html') 19const embedPath = join(distPath, 'standalone', 'videos', 'embed.html')
20const indexPath = join(distPath, 'index.html') 20const indexPath = join(distPath, 'index.html')
21 21
22// Special route that add OpenGraph tags 22// Special route that add OpenGraph and oEmbed tags
23// Do not use a template engine for a so little thing 23// Do not use a template engine for a so little thing
24clientsRouter.use('/videos/watch/:id', generateWatchHtmlPage) 24clientsRouter.use('/videos/watch/:id', generateWatchHtmlPage)
25 25
@@ -43,11 +43,11 @@ export {
43 43
44// --------------------------------------------------------------------------- 44// ---------------------------------------------------------------------------
45 45
46function addOpenGraphTags (htmlStringPage: string, video: VideoInstance) { 46function addOpenGraphAndOEmbedTags (htmlStringPage: string, video: VideoInstance) {
47 const previewUrl = CONFIG.WEBSERVER.URL + STATIC_PATHS.PREVIEWS + video.getPreviewName() 47 const previewUrl = CONFIG.WEBSERVER.URL + STATIC_PATHS.PREVIEWS + video.getPreviewName()
48 const videoUrl = CONFIG.WEBSERVER.URL + '/videos/watch/' + video.id 48 const videoUrl = CONFIG.WEBSERVER.URL + '/videos/watch/' + video.uuid
49 49
50 const metaTags = { 50 const openGraphMetaTags = {
51 'og:type': 'video', 51 'og:type': 'video',
52 'og:title': video.name, 52 'og:title': video.name,
53 'og:image': previewUrl, 53 'og:image': previewUrl,
@@ -65,14 +65,26 @@ function addOpenGraphTags (htmlStringPage: string, video: VideoInstance) {
65 'twitter:image': previewUrl 65 'twitter:image': previewUrl
66 } 66 }
67 67
68 const oembedLinkTags = [
69 {
70 type: 'application/json+oembed',
71 href: CONFIG.WEBSERVER.URL + '/services/oembed?url=' + encodeURIComponent(videoUrl),
72 title: video.name
73 }
74 ]
75
68 let tagsString = '' 76 let tagsString = ''
69 Object.keys(metaTags).forEach(tagName => { 77 Object.keys(openGraphMetaTags).forEach(tagName => {
70 const tagValue = metaTags[tagName] 78 const tagValue = openGraphMetaTags[tagName]
71 79
72 tagsString += '<meta property="' + tagName + '" content="' + tagValue + '" />' 80 tagsString += `<meta property="${tagName}" content="${tagValue}" />`
73 }) 81 })
74 82
75 return htmlStringPage.replace(OPENGRAPH_COMMENT, tagsString) 83 for (const oembedLinkTag of oembedLinkTags) {
84 tagsString += `<link rel="alternate" type="${oembedLinkTag.type}" href="${oembedLinkTag.href}" title="${oembedLinkTag.title}" />`
85 }
86
87 return htmlStringPage.replace(OPENGRAPH_AND_OEMBED_COMMENT, tagsString)
76} 88}
77 89
78function generateWatchHtmlPage (req: express.Request, res: express.Response, next: express.NextFunction) { 90function generateWatchHtmlPage (req: express.Request, res: express.Response, next: express.NextFunction) {
@@ -101,7 +113,7 @@ function generateWatchHtmlPage (req: express.Request, res: express.Response, nex
101 // Let Angular application handle errors 113 // Let Angular application handle errors
102 if (!video) return res.sendFile(indexPath) 114 if (!video) return res.sendFile(indexPath)
103 115
104 const htmlStringPageWithTags = addOpenGraphTags(html, video) 116 const htmlStringPageWithTags = addOpenGraphAndOEmbedTags(html, video)
105 res.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags) 117 res.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags)
106 }) 118 })
107 .catch(err => next(err)) 119 .catch(err => next(err))