]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/client.ts
Add oembed endpoint
[github/Chocobozzz/PeerTube.git] / server / controllers / client.ts
CommitLineData
4d4e5cd4 1import * as express from 'express'
65fcc311 2import { join } from 'path'
4d4e5cd4 3import * as validator from 'validator'
6fcd19ba 4import * as Promise from 'bluebird'
830bcd0f 5
e02643f3 6import { database as db } from '../initializers/database'
65fcc311
C
7import {
8 CONFIG,
65fcc311 9 STATIC_PATHS,
709756b8 10 STATIC_MAX_AGE,
d8755eed 11 OPENGRAPH_AND_OEMBED_COMMENT
65fcc311 12} from '../initializers'
6fcd19ba 13import { root, readFileBufferPromise } from '../helpers'
69818c93 14import { VideoInstance } from '../models'
830bcd0f 15
65fcc311 16const clientsRouter = express.Router()
830bcd0f 17
e02643f3
C
18const distPath = join(root(), 'client', 'dist')
19const embedPath = join(distPath, 'standalone', 'videos', 'embed.html')
65fcc311 20const indexPath = join(distPath, 'index.html')
830bcd0f 21
d8755eed 22// Special route that add OpenGraph and oEmbed tags
830bcd0f 23// Do not use a template engine for a so little thing
65fcc311 24clientsRouter.use('/videos/watch/:id', generateWatchHtmlPage)
830bcd0f 25
075f16ca 26clientsRouter.use('/videos/embed', (req: express.Request, res: express.Response, next: express.NextFunction) => {
830bcd0f
C
27 res.sendFile(embedPath)
28})
29
79530164 30// Static HTML/CSS/JS client files
65fcc311 31clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE }))
79530164
C
32
33// 404 for static files not found
075f16ca 34clientsRouter.use('/client/*', (req: express.Request, res: express.Response, next: express.NextFunction) => {
79530164
C
35 res.sendStatus(404)
36})
37
830bcd0f
C
38// ---------------------------------------------------------------------------
39
65fcc311
C
40export {
41 clientsRouter
42}
830bcd0f
C
43
44// ---------------------------------------------------------------------------
45
d8755eed 46function addOpenGraphAndOEmbedTags (htmlStringPage: string, video: VideoInstance) {
d38309c3 47 const previewUrl = CONFIG.WEBSERVER.URL + STATIC_PATHS.PREVIEWS + video.getPreviewName()
d8755eed 48 const videoUrl = CONFIG.WEBSERVER.URL + '/videos/watch/' + video.uuid
830bcd0f 49
d8755eed 50 const openGraphMetaTags = {
830bcd0f
C
51 'og:type': 'video',
52 'og:title': video.name,
41b5da1d 53 'og:image': previewUrl,
830bcd0f
C
54 'og:url': videoUrl,
55 'og:description': video.description,
56
57 'name': video.name,
58 'description': video.description,
41b5da1d 59 'image': previewUrl,
830bcd0f
C
60
61 'twitter:card': 'summary_large_image',
62 'twitter:site': '@Chocobozzz',
63 'twitter:title': video.name,
64 'twitter:description': video.description,
41b5da1d 65 'twitter:image': previewUrl
830bcd0f
C
66 }
67
d8755eed
C
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
830bcd0f 76 let tagsString = ''
d8755eed
C
77 Object.keys(openGraphMetaTags).forEach(tagName => {
78 const tagValue = openGraphMetaTags[tagName]
830bcd0f 79
d8755eed 80 tagsString += `<meta property="${tagName}" content="${tagValue}" />`
830bcd0f
C
81 })
82
d8755eed
C
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)
830bcd0f
C
88}
89
69818c93
C
90function generateWatchHtmlPage (req: express.Request, res: express.Response, next: express.NextFunction) {
91 const videoId = '' + req.params.id
0a6658fd 92 let videoPromise: Promise<VideoInstance>
73ce7f96
C
93
94 // Let Angular application handle errors
0a6658fd
C
95 if (validator.isUUID(videoId, 4)) {
96 videoPromise = db.Video.loadByUUIDAndPopulateAuthorAndPodAndTags(videoId)
97 } else if (validator.isInt(videoId)) {
98 videoPromise = db.Video.loadAndPopulateAuthorAndPodAndTags(+videoId)
99 } else {
100 return res.sendFile(indexPath)
101 }
73ce7f96 102
6fcd19ba
C
103 Promise.all([
104 readFileBufferPromise(indexPath),
0a6658fd 105 videoPromise
6fcd19ba
C
106 ])
107 .then(([ file, video ]) => {
108 file = file as Buffer
109 video = video as VideoInstance
830bcd0f 110
6fcd19ba 111 const html = file.toString()
830bcd0f 112
73ce7f96
C
113 // Let Angular application handle errors
114 if (!video) return res.sendFile(indexPath)
115
d8755eed 116 const htmlStringPageWithTags = addOpenGraphAndOEmbedTags(html, video)
830bcd0f
C
117 res.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags)
118 })
6fcd19ba 119 .catch(err => next(err))
830bcd0f 120}