]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/client.ts
6a2ac4aabc151f779eb8882fdeaa0cbf4a5e23fb
[github/Chocobozzz/PeerTube.git] / server / controllers / client.ts
1 import * as express from 'express'
2 import { join } from 'path'
3 import * as validator from 'validator'
4 import * as Promise from 'bluebird'
5
6 import { database as db } from '../initializers/database'
7 import {
8 CONFIG,
9 STATIC_PATHS,
10 STATIC_MAX_AGE,
11 OPENGRAPH_AND_OEMBED_COMMENT
12 } from '../initializers'
13 import { root, readFileBufferPromise, escapeHTML } from '../helpers'
14 import { VideoInstance } from '../models'
15
16 const clientsRouter = express.Router()
17
18 const distPath = join(root(), 'client', 'dist')
19 const embedPath = join(distPath, 'standalone', 'videos', 'embed.html')
20 const indexPath = join(distPath, 'index.html')
21
22 // Special route that add OpenGraph and oEmbed tags
23 // Do not use a template engine for a so little thing
24 clientsRouter.use('/videos/watch/:id', generateWatchHtmlPage)
25
26 clientsRouter.use('/videos/embed', (req: express.Request, res: express.Response, next: express.NextFunction) => {
27 res.sendFile(embedPath)
28 })
29
30 // Static HTML/CSS/JS client files
31 clientsRouter.use('/client', express.static(distPath, { 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: VideoInstance) {
47 const previewUrl = CONFIG.WEBSERVER.URL + STATIC_PATHS.PREVIEWS + video.getPreviewName()
48 const videoUrl = CONFIG.WEBSERVER.URL + '/videos/watch/' + video.uuid
49
50 const videoName = escapeHTML(video.name)
51 const videoDescription = escapeHTML(video.description)
52
53 const openGraphMetaTags = {
54 'og:type': 'video',
55 'og:title': videoName,
56 'og:image': previewUrl,
57 'og:url': videoUrl,
58 'og:description': videoDescription,
59
60 'name': videoName,
61 'description': videoDescription,
62 'image': previewUrl,
63
64 'twitter:card': 'summary_large_image',
65 'twitter:site': '@Chocobozzz',
66 'twitter:title': videoName,
67 'twitter:description': videoDescription,
68 'twitter:image': previewUrl
69 }
70
71 const oembedLinkTags = [
72 {
73 type: 'application/json+oembed',
74 href: CONFIG.WEBSERVER.URL + '/services/oembed?url=' + encodeURIComponent(videoUrl),
75 title: videoName
76 }
77 ]
78
79 let tagsString = ''
80 Object.keys(openGraphMetaTags).forEach(tagName => {
81 const tagValue = openGraphMetaTags[tagName]
82
83 tagsString += `<meta property="${tagName}" content="${tagValue}" />`
84 })
85
86 for (const oembedLinkTag of oembedLinkTags) {
87 tagsString += `<link rel="alternate" type="${oembedLinkTag.type}" href="${oembedLinkTag.href}" title="${oembedLinkTag.title}" />`
88 }
89
90 return htmlStringPage.replace(OPENGRAPH_AND_OEMBED_COMMENT, tagsString)
91 }
92
93 function generateWatchHtmlPage (req: express.Request, res: express.Response, next: express.NextFunction) {
94 const videoId = '' + req.params.id
95 let videoPromise: Promise<VideoInstance>
96
97 // Let Angular application handle errors
98 if (validator.isUUID(videoId, 4)) {
99 videoPromise = db.Video.loadByUUIDAndPopulateAuthorAndPodAndTags(videoId)
100 } else if (validator.isInt(videoId)) {
101 videoPromise = db.Video.loadAndPopulateAuthorAndPodAndTags(+videoId)
102 } else {
103 return res.sendFile(indexPath)
104 }
105
106 Promise.all([
107 readFileBufferPromise(indexPath),
108 videoPromise
109 ])
110 .then(([ file, video ]) => {
111 file = file as Buffer
112 video = video as VideoInstance
113
114 const html = file.toString()
115
116 // Let Angular application handle errors
117 if (!video) return res.sendFile(indexPath)
118
119 const htmlStringPageWithTags = addOpenGraphAndOEmbedTags(html, video)
120 res.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags)
121 })
122 .catch(err => next(err))
123 }