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