]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/client.ts
c3d28245c329bfc5d180d0073a21389abc893b1a
[github/Chocobozzz/PeerTube.git] / server / controllers / client.ts
1 import { parallel } from 'async'
2 import * as express from 'express'
3 import * as fs from 'fs'
4 import { join } from 'path'
5 import * as validator from 'validator'
6
7 import { database as db } from '../initializers/database'
8 import {
9 CONFIG,
10 REMOTE_SCHEME,
11 STATIC_PATHS,
12 STATIC_MAX_AGE
13 } from '../initializers'
14 import { root } from '../helpers'
15
16 const clientsRouter = express.Router()
17
18 // TODO: move to constants
19 const opengraphComment = '<!-- opengraph tags -->'
20 const distPath = join(root(), 'client', 'dist')
21 const embedPath = join(distPath, 'standalone', 'videos', 'embed.html')
22 const indexPath = join(distPath, 'index.html')
23
24 // Special route that add OpenGraph tags
25 // Do not use a template engine for a so little thing
26 clientsRouter.use('/videos/watch/:id', generateWatchHtmlPage)
27
28 clientsRouter.use('/videos/embed', function (req, res, next) {
29 res.sendFile(embedPath)
30 })
31
32 // Static HTML/CSS/JS client files
33 clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE }))
34
35 // 404 for static files not found
36 clientsRouter.use('/client/*', function (req, res, next) {
37 res.sendStatus(404)
38 })
39
40 // ---------------------------------------------------------------------------
41
42 export {
43 clientsRouter
44 }
45
46 // ---------------------------------------------------------------------------
47
48 function addOpenGraphTags (htmlStringPage, video) {
49 let basePreviewUrlHttp
50
51 if (video.isOwned()) {
52 basePreviewUrlHttp = CONFIG.WEBSERVER.URL
53 } else {
54 basePreviewUrlHttp = REMOTE_SCHEME.HTTP + '://' + video.Author.Pod.host
55 }
56
57 // We fetch the remote preview (bigger than the thumbnail)
58 // This should not overhead the remote server since social websites put in a cache the OpenGraph tags
59 // We can't use the thumbnail because these social websites want bigger images (> 200x200 for Facebook for example)
60 const previewUrl = basePreviewUrlHttp + STATIC_PATHS.PREVIEWS + video.getPreviewName()
61 const videoUrl = CONFIG.WEBSERVER.URL + '/videos/watch/' + video.id
62
63 const metaTags = {
64 'og:type': 'video',
65 'og:title': video.name,
66 'og:image': previewUrl,
67 'og:url': videoUrl,
68 'og:description': video.description,
69
70 'name': video.name,
71 'description': video.description,
72 'image': previewUrl,
73
74 'twitter:card': 'summary_large_image',
75 'twitter:site': '@Chocobozzz',
76 'twitter:title': video.name,
77 'twitter:description': video.description,
78 'twitter:image': previewUrl
79 }
80
81 let tagsString = ''
82 Object.keys(metaTags).forEach(function (tagName) {
83 const tagValue = metaTags[tagName]
84
85 tagsString += '<meta property="' + tagName + '" content="' + tagValue + '" />'
86 })
87
88 return htmlStringPage.replace(opengraphComment, tagsString)
89 }
90
91 function generateWatchHtmlPage (req, res, next) {
92 const videoId = req.params.id
93
94 // Let Angular application handle errors
95 if (!validator.isUUID(videoId, 4)) return res.sendFile(indexPath)
96
97 parallel({
98 file: function (callback) {
99 fs.readFile(indexPath, callback)
100 },
101
102 video: function (callback) {
103 db.Video.loadAndPopulateAuthorAndPodAndTags(videoId, callback)
104 }
105 }, function (err, result: any) {
106 if (err) return next(err)
107
108 const html = result.file.toString()
109 const video = result.video
110
111 // Let Angular application handle errors
112 if (!video) return res.sendFile(indexPath)
113
114 const htmlStringPageWithTags = addOpenGraphTags(html, video)
115 res.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags)
116 })
117 }