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