]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/client.ts
c85bd8a5ed79c94d880cd260837a2f5e18287169
[github/Chocobozzz/PeerTube.git] / server / controllers / client.ts
1 import * as Bluebird from 'bluebird'
2 import * as express from 'express'
3 import { join } from 'path'
4 import * as validator from 'validator'
5 import { escapeHTML, readFileBufferPromise, root } from '../helpers/core-utils'
6 import { CONFIG, EMBED_SIZE, OPENGRAPH_AND_OEMBED_COMMENT, STATIC_MAX_AGE, STATIC_PATHS } from '../initializers'
7 import { asyncMiddleware } from '../middlewares'
8 import { VideoModel } from '../models/video/video'
9
10 const clientsRouter = express.Router()
11
12 const distPath = join(root(), 'client', 'dist')
13 const assetsImagesPath = join(root(), 'client', 'dist', 'client', 'assets', 'images')
14 const embedPath = join(distPath, 'standalone', 'videos', 'embed.html')
15 const indexPath = join(distPath, 'index.html')
16
17 // Special route that add OpenGraph and oEmbed tags
18 // Do not use a template engine for a so little thing
19 clientsRouter.use('/videos/watch/:id',
20 asyncMiddleware(generateWatchHtmlPage)
21 )
22
23 clientsRouter.use('/videos/embed', (req: express.Request, res: express.Response, next: express.NextFunction) => {
24 res.sendFile(embedPath)
25 })
26
27 // Static HTML/CSS/JS client files
28 clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE }))
29 clientsRouter.use('/client/assets/images', express.static(assetsImagesPath, { maxAge: STATIC_MAX_AGE }))
30
31 // 404 for static files not found
32 clientsRouter.use('/client/*', (req: express.Request, res: express.Response, next: express.NextFunction) => {
33 res.sendStatus(404)
34 })
35
36 // ---------------------------------------------------------------------------
37
38 export {
39 clientsRouter
40 }
41
42 // ---------------------------------------------------------------------------
43
44 function addOpenGraphAndOEmbedTags (htmlStringPage: string, video: VideoModel) {
45 const previewUrl = CONFIG.WEBSERVER.URL + STATIC_PATHS.PREVIEWS + video.getPreviewName()
46 const videoUrl = CONFIG.WEBSERVER.URL + '/videos/watch/' + video.uuid
47
48 const videoNameEscaped = escapeHTML(video.name)
49 const videoDescriptionEscaped = escapeHTML(video.description)
50 const embedUrl = CONFIG.WEBSERVER.URL + video.getEmbedPath()
51
52 const openGraphMetaTags = {
53 'og:type': 'video',
54 'og:title': videoNameEscaped,
55 'og:image': previewUrl,
56 'og:url': videoUrl,
57 'og:description': videoDescriptionEscaped,
58
59 'og:video:url': embedUrl,
60 'og:video:secure_url': embedUrl,
61 'og:video:type': 'text/html',
62 'og:video:width': EMBED_SIZE.width,
63 'og:video:height': EMBED_SIZE.height,
64
65 'name': videoNameEscaped,
66 'description': videoDescriptionEscaped,
67 'image': previewUrl,
68
69 'twitter:card': 'summary_large_image',
70 'twitter:site': '@Chocobozzz',
71 'twitter:title': videoNameEscaped,
72 'twitter:description': videoDescriptionEscaped,
73 'twitter:image': previewUrl,
74 'twitter:player': embedUrl,
75 'twitter:player:width': EMBED_SIZE.width,
76 'twitter:player:height': EMBED_SIZE.height
77 }
78
79 const oembedLinkTags = [
80 {
81 type: 'application/json+oembed',
82 href: CONFIG.WEBSERVER.URL + '/services/oembed?url=' + encodeURIComponent(videoUrl),
83 title: videoNameEscaped
84 }
85 ]
86
87 const schemaTags = {
88 '@context': 'http://schema.org',
89 '@type': 'VideoObject',
90 name: videoNameEscaped,
91 description: videoDescriptionEscaped,
92 duration: video.getActivityStreamDuration(),
93 thumbnailURL: previewUrl,
94 contentURL: videoUrl,
95 embedURL: embedUrl,
96 uploadDate: video.createdAt
97 }
98
99 let tagsString = ''
100
101 // Opengraph
102 Object.keys(openGraphMetaTags).forEach(tagName => {
103 const tagValue = openGraphMetaTags[tagName]
104
105 tagsString += `<meta property="${tagName}" content="${tagValue}" />`
106 })
107
108 // OEmbed
109 for (const oembedLinkTag of oembedLinkTags) {
110 tagsString += `<link rel="alternate" type="${oembedLinkTag.type}" href="${oembedLinkTag.href}" title="${oembedLinkTag.title}" />`
111 }
112
113 // Schema.org
114 tagsString += `<script type="application/ld+json">${JSON.stringify(schemaTags)}</script>`
115
116 return htmlStringPage.replace(OPENGRAPH_AND_OEMBED_COMMENT, tagsString)
117 }
118
119 async function generateWatchHtmlPage (req: express.Request, res: express.Response, next: express.NextFunction) {
120 const videoId = '' + req.params.id
121 let videoPromise: Bluebird<VideoModel>
122
123 // Let Angular application handle errors
124 if (validator.isUUID(videoId, 4)) {
125 videoPromise = VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(videoId)
126 } else if (validator.isInt(videoId)) {
127 videoPromise = VideoModel.loadAndPopulateAccountAndServerAndTags(+videoId)
128 } else {
129 return res.sendFile(indexPath)
130 }
131
132 let [ file, video ] = await Promise.all([
133 readFileBufferPromise(indexPath),
134 videoPromise
135 ])
136
137 const html = file.toString()
138
139 // Let Angular application handle errors
140 if (!video) return res.sendFile(indexPath)
141
142 const htmlStringPageWithTags = addOpenGraphAndOEmbedTags(html, video)
143 res.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags)
144 }