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